plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

s_grow.c (563B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include "libString.h"
      4 
      5 /* grow a String's allocation by at least `incr' bytes */
      6 extern String*
      7 s_grow(String *s, int incr)
      8 {
      9 	char *cp;
     10 	int size;
     11 
     12 	if(s->fixed)
     13 		sysfatal("s_grow of constant string");
     14 	s = s_unique(s);
     15 
     16 	/*
     17 	 *  take a larger increment to avoid mallocing too often
     18 	 */
     19 	size = s->end-s->base;
     20 	if(size/2 < incr)
     21 		size += incr;
     22 	else
     23 		size += size/2;
     24 
     25 	cp = realloc(s->base, size);
     26 	if (cp == 0)
     27 		sysfatal("s_grow: %r");
     28 	s->ptr = (s->ptr - s->base) + cp;
     29 	s->end = cp + size;
     30 	s->base = cp;
     31 
     32 	return s;
     33 }