[PD-cvs] pd/src desire.c, 1.1.2.217.2.116, 1.1.2.217.2.117 kernel.c, 1.1.2.32, 1.1.2.33

Mathieu Bouchard matju at users.sourceforge.net
Tue Jan 9 18:06:30 CET 2007


Update of /cvsroot/pure-data/pd/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3718

Modified Files:
      Tag: desiredata
	desire.c kernel.c 
Log Message:
resizebytes -> realloc


Index: kernel.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/Attic/kernel.c,v
retrieving revision 1.1.2.32
retrieving revision 1.1.2.33
diff -C2 -d -r1.1.2.32 -r1.1.2.33
*** kernel.c	9 Jan 2007 15:03:38 -0000	1.1.2.32
--- kernel.c	9 Jan 2007 17:06:28 -0000	1.1.2.33
***************
*** 54,61 ****
  
  void *resizebytes(void *old, size_t oldsize, size_t newsize) {
-     void *ret;
      if (newsize < 1) newsize = 1;
      if (oldsize < 1) oldsize = 1;
!     ret = (void *)realloc((char *)old, newsize);
      if (newsize > oldsize && ret) memset(((char *)ret) + oldsize, 0, newsize - oldsize);
      if (!ret) post("pd: resizebytes() failed -- out of memory");
--- 54,60 ----
  
  void *resizebytes(void *old, size_t oldsize, size_t newsize) {
      if (newsize < 1) newsize = 1;
      if (oldsize < 1) oldsize = 1;
!     void *ret = (void *)realloc((char *)old, newsize);
      if (newsize > oldsize && ret) memset(((char *)ret) + oldsize, 0, newsize - oldsize);
      if (!ret) post("pd: resizebytes() failed -- out of memory");
***************
*** 69,106 ****
  /* T.Grill - get aligned memory */
  void *getalignedbytes(size_t nbytes) {
! 	/* to align the region we also need some extra memory to save the original pointer location
! 		it is saved immediately before the aligned vector memory */
!    	void *vec = getbytes(nbytes+ (VECTORALIGNMENT/8-1)+sizeof(void *));
! 	if (vec != NULL) {
! 		/* get alignment of first possible signal vector byte */
! 		t_int alignment = ((t_int)vec+sizeof(void *))&(VECTORALIGNMENT/8-1);  
! 		/* calculate aligned pointer */
! 		void *ret = (unsigned char *)vec+sizeof(void *)+(alignment == 0?0:VECTORALIGNMENT/8-alignment);
! 		/* save original memory location */
! 		*(void **)((unsigned char *)ret-sizeof(void *)) = vec; 
! 		return ret;
! 	}
! 	return 0;
! 	
  }
  
  /* T.Grill - free aligned vector memory */
  void freealignedbytes(void *ptr,size_t nbytes) {
! 	/* get original memory location */
! 	void *ori = *(void **)((unsigned char *)ptr-sizeof(void *)); 
! 	free(ori);
  }
  
  /* T.Grill - resize aligned vector memory */
  void *resizealignedbytes(void *ptr,size_t oldsize, size_t newsize) {
! 	 /* get original memory location */
  	void *ori = *(void **)((unsigned char *)ptr-sizeof(void *));
! 	void *vec = resizebytes(ori,oldsize+(VECTORALIGNMENT/8-1)+sizeof(void *),
! 		newsize+(VECTORALIGNMENT/8-1)+sizeof(void *));
! 	/* get alignment of first possible signal vector byte */
  	t_int alignment = ((t_int)vec+sizeof(void *))&(VECTORALIGNMENT/8-1);
- 	/* calculate aligned pointer */
  	void *ret = (unsigned char *)vec+sizeof(void *)+(alignment == 0?0:VECTORALIGNMENT/8-alignment);
- 	/* save original memory location */
  	*(void **)((unsigned char *)ret-sizeof(void *)) = vec;
  	return ret;
--- 68,93 ----
  /* T.Grill - get aligned memory */
  void *getalignedbytes(size_t nbytes) {
!     /* to align the region we also need some extra memory to save the original pointer location
!        it is saved immediately before the aligned vector memory */
!     void *vec = getbytes(nbytes+(VECTORALIGNMENT/8-1)+sizeof(void *));
!     if (!vec) return 0;
!     t_int alignment = ((t_int)vec+sizeof(void *))&(VECTORALIGNMENT/8-1);
!     void *ret = (unsigned char *)vec+sizeof(void *)+(alignment == 0?0:VECTORALIGNMENT/8-alignment);
!     *(void **)((unsigned char *)ret-sizeof(void *)) = vec;
!     return ret;
  }
  
  /* T.Grill - free aligned vector memory */
  void freealignedbytes(void *ptr,size_t nbytes) {
! 	free(*(void **)((unsigned char *)ptr-sizeof(void *)));
  }
  
  /* T.Grill - resize aligned vector memory */
  void *resizealignedbytes(void *ptr,size_t oldsize, size_t newsize) {
! 	if (newsize<1) newsize=1;
  	void *ori = *(void **)((unsigned char *)ptr-sizeof(void *));
! 	void *vec = realloc(ori,newsize+(VECTORALIGNMENT/8-1)+sizeof(void *));
  	t_int alignment = ((t_int)vec+sizeof(void *))&(VECTORALIGNMENT/8-1);
  	void *ret = (unsigned char *)vec+sizeof(void *)+(alignment == 0?0:VECTORALIGNMENT/8-alignment);
  	*(void **)((unsigned char *)ret-sizeof(void *)) = vec;
  	return ret;
***************
*** 310,314 ****
  t_pd *pd_new(t_class *c) {
      if (!c) bug("pd_new: apparently called before setup routine");
!     t_pd *x = (t_pd *)t_getbytes(c->size);
      x->_class = c;
      hash_set(object_table,x,(void*)0);
--- 297,301 ----
  t_pd *pd_new(t_class *c) {
      if (!c) bug("pd_new: apparently called before setup routine");
!     t_pd *x = (t_pd *)getbytes(c->size);
      x->_class = c;
      hash_set(object_table,x,(void*)0);
***************
*** 371,375 ****
  
  static t_bindelem *bindelem_new(t_pd *who, t_bindelem *next) {
! 	t_bindelem *self = (t_bindelem *)getbytes(sizeof(t_bindelem));
  	self->who = who;
  	self->next = next;
--- 358,362 ----
  
  static t_bindelem *bindelem_new(t_pd *who, t_bindelem *next) {
! 	t_bindelem *self = (t_bindelem *)malloc(sizeof(t_bindelem));
  	self->who = who;
  	self->next = next;
***************
*** 451,455 ****
  
  void pd_pushsym(t_pd *x) {
!     t_gstack *y = (t_gstack *)t_getbytes(sizeof(*y));
      y->g_what = s__X.thing;
      y->g_next = gstack_head;
--- 438,442 ----
  
  void pd_pushsym(t_pd *x) {
!     t_gstack *y = (t_gstack *)malloc(sizeof(*y));
      y->g_what = s__X.thing;
      y->g_next = gstack_head;
***************
*** 718,722 ****
  
  t_outlet *outlet_new(t_object *owner, t_symbol *s) {
!     t_outlet *x = (t_outlet *)getbytes(sizeof(*x)), *y, *y2;
      x->o_owner = owner;
      x->o_next = 0;
--- 705,709 ----
  
  t_outlet *outlet_new(t_object *owner, t_symbol *s) {
!     t_outlet *x = (t_outlet *)malloc(sizeof(*x)), *y, *y2;
      x->o_owner = owner;
      x->o_next = 0;
***************
*** 1062,1069 ****
          }
      }
!     t_class *c = (t_class *)t_getbytes(sizeof(*c));
      c->name = c->helpname = s;
      c->size = size;
!     c->methods = (t_methodentry *)t_getbytes(0);
      c->nmethod = 0;
      c->freemethod    = (t_method)freemethod;
--- 1049,1056 ----
          }
      }
!     t_class *c = (t_class *)malloc(sizeof(*c));
      c->name = c->helpname = s;
      c->size = size;
!     c->methods = (t_methodentry *)malloc(1);
      c->nmethod = 0;
      c->freemethod    = (t_method)freemethod;
***************
*** 1127,1132 ****
      else if (sel==&s_anything) {if (argtype!='*')       goto phooey; class_addanything(c,fn);}
      else {
!         c->methods = (t_methodentry *)t_resizebytes(c->methods,
!             c->nmethod * sizeof(*c->methods), (c->nmethod+1) * sizeof(*c->methods));
          m = c->methods + c->nmethod;
          c->nmethod++;
--- 1114,1119 ----
      else if (sel==&s_anything) {if (argtype!='*')       goto phooey; class_addanything(c,fn);}
      else {
! 	/* SLOW, especially for [objectmaker] */
!         c->methods = (t_methodentry *)realloc(c->methods, (c->nmethod+1) * sizeof(*c->methods));
          m = c->methods + c->nmethod;
          c->nmethod++;
***************
*** 1304,1309 ****
      if (oldsym) sym2 = oldsym;
      else {
!         sym2 = (t_symbol *)t_getbytes(sizeof(*sym2));
!         sym2->name = (char *)t_getbytes(n+1);
          sym2->next = 0;
          sym2->thing = 0;
--- 1291,1296 ----
      if (oldsym) sym2 = oldsym;
      else {
!         sym2 = (t_symbol *)malloc(sizeof(*sym2));
!         sym2->name = (char *)malloc(n+1);
          sym2->next = 0;
          sym2->thing = 0;
***************
*** 1332,1337 ****
  
  /* replace everything but [a-zA-Z0-9_] by "0x%x" */
! static char *alternative_classname(char*classname) {
!   char *altname=(char*)getbytes(sizeof(char)*MAXPDSTRING);
    int count=0;
    int i=0;
--- 1319,1324 ----
  
  /* replace everything but [a-zA-Z0-9_] by "0x%x" */
! static char *alternative_classname(char *classname) {
!   char *altname=(char*)malloc(sizeof(char)*MAXPDSTRING);
    int count=0;
    int i=0;
***************
*** 1643,1656 ****
  
  t_binbuf *binbuf_new(void) {
!     t_binbuf *x = (t_binbuf *)t_getbytes(sizeof(*x));
      x->n = 0;
!     x->capa = 0;
!     x->vec = (t_atom *)t_getbytes(0);
      return x;
  }
  
! /* caution: capa >= x->n */
  static void binbuf_capa(t_binbuf *x, int capa) {
!     x->vec = (t_atom *)t_resizebytes(x->vec, x->capa*sizeof(*x->vec), capa*sizeof(*x->vec));
      x->capa = capa;
  }
--- 1630,1643 ----
  
  t_binbuf *binbuf_new(void) {
!     t_binbuf *x = (t_binbuf *)malloc(sizeof(*x));
      x->n = 0;
!     x->capa = 1;
!     x->vec = (t_atom *)malloc(1*sizeof(t_atom));
      return x;
  }
  
! /* caution: capa >= x->n and capa >= 1 too */
  static void binbuf_capa(t_binbuf *x, int capa) {
!     x->vec = (t_atom *)realloc(x->vec, capa*sizeof(*x->vec));
      x->capa = capa;
  }
***************
*** 1659,1665 ****
  
  t_binbuf *binbuf_duplicate(t_binbuf *y) {
!     t_binbuf *x = (t_binbuf *)t_getbytes(sizeof(*x));
      x->capa = x->n = y->n;
!     x->vec = (t_atom *)t_getbytes(x->n * sizeof(*x->vec));
      memcpy(x->vec, y->vec, x->n * sizeof(*x->vec));
      return x;
--- 1646,1652 ----
  
  t_binbuf *binbuf_duplicate(t_binbuf *y) {
!     t_binbuf *x = (t_binbuf *)malloc(sizeof(*x));
      x->capa = x->n = y->n;
!     x->vec = (t_atom *)malloc(x->n * sizeof(*x->vec));
      memcpy(x->vec, y->vec, x->n * sizeof(*x->vec));
      return x;
***************
*** 1668,1673 ****
  void binbuf_clear(t_binbuf *x) {
      x->n = 0;
!     x->vec = (t_atom *)t_resizebytes(x->vec, x->n * sizeof(*x->vec), 0);
!     x->capa = 0;
  }
  
--- 1655,1660 ----
  void binbuf_clear(t_binbuf *x) {
      x->n = 0;
!     x->vec = (t_atom *)realloc(x->vec, 4);
!     x->capa = 4;
  }
  
***************
*** 1797,1819 ****
  /* convert a binbuf to text; no null termination. */
  void binbuf_gettext(t_binbuf *x, char **bufp, int *lengthp) {
!     char *buf = (char *)getbytes(0), *newbuf;
      int length = 0;
      char string[MAXPDSTRING];
!     t_atom *ap;
      int indx;
!     for (ap = x->vec, indx = x->n; indx--; ap++) {
!         int newlength;
          if ((ap->a_type == A_SEMI || ap->a_type == A_COMMA) && length && buf[length-1] == ' ') length--;
          atom_string(ap, string, MAXPDSTRING);
!         newlength = length + strlen(string) + 1;
!         if (!(newbuf = (char *)resizebytes(buf, length, newlength))) break;
          buf = newbuf;
          strcpy(buf + length, string);
          length = newlength;
!         if (ap->a_type == A_SEMI) buf[length-1] = '\n';
!         else buf[length-1] = ' ';
      }
      if (length && buf[length-1] == ' ') {
!         newbuf = (char *)t_resizebytes(buf, length, length-1);
          if (newbuf) {
              buf = newbuf;
--- 1784,1804 ----
  /* convert a binbuf to text; no null termination. */
  void binbuf_gettext(t_binbuf *x, char **bufp, int *lengthp) {
!     char *buf = (char *)malloc(1), *newbuf;
      int length = 0;
      char string[MAXPDSTRING];
!     t_atom *ap = x->vec;
      int indx;
!     for (indx = x->n; indx--; ap++) {
          if ((ap->a_type == A_SEMI || ap->a_type == A_COMMA) && length && buf[length-1] == ' ') length--;
          atom_string(ap, string, MAXPDSTRING);
!         int newlength = length + strlen(string) + 1;
!         if (!(newbuf = (char *)realloc(buf,newlength))) break;
          buf = newbuf;
          strcpy(buf + length, string);
          length = newlength;
!         buf[length-1] = ap->a_type == A_SEMI ? '\n' : ' ';
      }
      if (length && buf[length-1] == ' ') {
!         newbuf = (char *)realloc(buf,length+1);
          if (newbuf) {
              buf = newbuf;
***************
*** 1828,1835 ****
  /* matju said: make this use vector size doubling as it used to be in binbuf_text */
  void binbuf_add(t_binbuf *x, int argc, t_atom *argv) {
!     int newsize = x->n + argc, i;
!     t_atom *ap = (t_atom *)t_resizebytes(x->vec, x->n * sizeof(*x->vec), newsize * sizeof(*x->vec));
      x->vec = ap;
!     for (ap = x->vec + x->n, i = argc; i--; ap++) *ap = *(argv++);
      x->capa = x->n = newsize;
  }
--- 1813,1821 ----
  /* matju said: make this use vector size doubling as it used to be in binbuf_text */
  void binbuf_add(t_binbuf *x, int argc, t_atom *argv) {
!     int newsize = x->n + argc;
!     t_atom *ap = (t_atom *)realloc(x->vec, newsize * sizeof(*x->vec));
      x->vec = ap;
!     ap += x->n;
!     for (int i = argc; i--; ap++) *ap = *(argv++);
      x->capa = x->n = newsize;
  }
***************
*** 1907,1911 ****
      int newsize = x->n + argc, i;
      t_atom *ap;
!     ap = (t_atom *)t_resizebytes(x->vec, x->n * sizeof(*x->vec), newsize * sizeof(*x->vec));
      if (!ap) {
          error("binbuf_addmessage: out of space");
--- 1893,1897 ----
      int newsize = x->n + argc, i;
      t_atom *ap;
!     ap = (t_atom *)realloc(x->vec, newsize * sizeof(*x->vec));
      if (!ap) {
          error("binbuf_addmessage: out of space");
***************
*** 2196,2200 ****
      if (fd < 0) {error("open: %s: %s",namebuf,strerror(errno)); return 1;}
      if ((length = lseek(fd, 0, SEEK_END)) < 0 || lseek(fd, 0, SEEK_SET) < 0 
!         || !(buf = (char *)t_getbytes(length))) {
          error("lseek: %s: %s",namebuf,strerror(errno));
          close(fd);
--- 2182,2186 ----
      if (fd < 0) {error("open: %s: %s",namebuf,strerror(errno)); return 1;}
      if ((length = lseek(fd, 0, SEEK_END)) < 0 || lseek(fd, 0, SEEK_SET) < 0 
!         || !(buf = (char *)malloc(length))) {
          error("lseek: %s: %s",namebuf,strerror(errno));
          close(fd);

Index: desire.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/Attic/desire.c,v
retrieving revision 1.1.2.217.2.116
retrieving revision 1.1.2.217.2.117
diff -C2 -d -r1.1.2.217.2.116 -r1.1.2.217.2.117
*** desire.c	9 Jan 2007 15:07:40 -0000	1.1.2.217.2.116
--- desire.c	9 Jan 2007 17:06:26 -0000	1.1.2.217.2.117
***************
*** 2402,2406 ****
      if (argc < 1) {pd_error(x,"graph_xlabel: no y value given"); return;}
      x->xlabely = atom_getfloatarg(0,argc--,argv++);
!     x->xlabel = (t_symbol **)t_resizebytes(x->xlabel, x->nxlabels*sizeof(void*), argc*sizeof(void*));
      x->nxlabels = argc;
      for (int i=0; i < argc; i++) x->xlabel[i] = atom_gensym(&argv[i]);
--- 2402,2406 ----
      if (argc < 1) {pd_error(x,"graph_xlabel: no y value given"); return;}
      x->xlabely = atom_getfloatarg(0,argc--,argv++);
!     x->xlabel = (t_symbol **)realloc(x->xlabel,argc*sizeof(void*));
      x->nxlabels = argc;
      for (int i=0; i < argc; i++) x->xlabel[i] = atom_gensym(&argv[i]);
***************
*** 2410,2414 ****
      if (argc < 1) {pd_error(x,"graph_ylabel: no x value given"); return;}
      x->ylabelx = atom_getfloatarg(0,argc--,argv++);
!     x->ylabel = (t_symbol **)t_resizebytes(x->ylabel, x->nylabels*sizeof(void*), argc*sizeof(void*));
      x->nylabels = argc;
      for (int i=0; i < argc; i++) x->ylabel[i] = atom_gensym(&argv[i]);
--- 2410,2414 ----
      if (argc < 1) {pd_error(x,"graph_ylabel: no x value given"); return;}
      x->ylabelx = atom_getfloatarg(0,argc--,argv++);
!     x->ylabel = (t_symbol **)realloc(x->ylabel,argc*sizeof(void*));
      x->nylabels = argc;
      for (int i=0; i < argc; i++) x->ylabel[i] = atom_gensym(&argv[i]);
***************
*** 2725,2731 ****
              if (nline != 2 && nline != 3) break;
              int newnargs = ntemplateargs + nline;
!             templateargs = (t_atom *)t_resizebytes(templateargs,
!                 sizeof(*templateargs) * ntemplateargs,
!                 sizeof(*templateargs) * newnargs);
              templateargs[ntemplateargs] = vec[message];
              templateargs[ntemplateargs + 1] = vec[message + 1];
--- 2725,2729 ----
              if (nline != 2 && nline != 3) break;
              int newnargs = ntemplateargs + nline;
!             templateargs = (t_atom *)realloc(templateargs, sizeof(*templateargs) * newnargs);
              templateargs[ntemplateargs] = vec[message];
              templateargs[ntemplateargs + 1] = vec[message + 1];
***************
*** 2828,2833 ****
      t_symbol **templatevec = *p_templatevec;
      for (int i=0; i < n; i++) if (templatevec[i] == templatesym) return;
!     templatevec = (t_symbol **)t_resizebytes(templatevec,
!         n * sizeof(*templatevec), (n+1) * sizeof(*templatevec));
      templatevec[n] = templatesym;
      *p_templatevec = templatevec;
--- 2826,2830 ----
      t_symbol **templatevec = *p_templatevec;
      for (int i=0; i < n; i++) if (templatevec[i] == templatesym) return;
!     templatevec = (t_symbol **)realloc(templatevec, (n+1)*sizeof(*templatevec));
      templatevec[n] = templatesym;
      *p_templatevec = templatevec;
***************
*** 2851,2855 ****
  	int ty = t->vec[i].type;
          if (ty==DT_FLOAT || ty==DT_SYMBOL) {
!             a = (t_atom *)t_resizebytes(a, natom*sizeof(*a), (natom+1)*sizeof(*a));
              if (t->vec[i].type == DT_FLOAT) SETFLOAT( a + natom, w[i].w_float);
              else                            SETSYMBOL(a + natom, w[i].w_symbol);
--- 2848,2852 ----
  	int ty = t->vec[i].type;
          if (ty==DT_FLOAT || ty==DT_SYMBOL) {
!             a = (t_atom *)realloc(a, (natom+1)*sizeof(*a));
              if (t->vec[i].type == DT_FLOAT) SETFLOAT( a + natom, w[i].w_float);
              else                            SETSYMBOL(a + natom, w[i].w_symbol);
***************
*** 3759,3763 ****
      binbuf_gettext(b, &buf, &bufsize);
      binbuf_free(b);
!     buf = (char *)t_resizebytes(buf, bufsize, bufsize+1);
      buf[bufsize] = 0;
      sprintf(buf2, "pdtk_data_dialog %%s {");
--- 3756,3760 ----
      binbuf_gettext(b, &buf, &bufsize);
      binbuf_free(b);
!     buf = (char *)realloc(buf, bufsize+1);
      buf[bufsize] = 0;
      sprintf(buf2, "pdtk_data_dialog %%s {");
***************
*** 3853,3857 ****
          }
          newn = (oldn = x->n) + 1;
!         x->vec = (t_dataslot *)t_resizebytes(x->vec, oldn*sizeof(*x->vec), newn*sizeof(*x->vec));
          x->n = newn;
          x->vec[oldn].type = newtype;
--- 3850,3854 ----
          }
          newn = (oldn = x->n) + 1;
!         x->vec = (t_dataslot *)realloc(x->vec, newn*sizeof(*x->vec));
          x->n = newn;
          x->vec[oldn].type = newtype;





More information about the Pd-cvs mailing list