Add libString.
This commit is contained in:
parent
57ccfb9e8f
commit
7f11104a57
20 changed files with 1315 additions and 0 deletions
34
src/libString/s_grow.c
Normal file
34
src/libString/s_grow.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <u.h>
|
||||
#include <libc.h>
|
||||
#include "libString.h"
|
||||
|
||||
/* grow a String's allocation by at least `incr' bytes */
|
||||
extern String*
|
||||
s_grow(String *s, int incr)
|
||||
{
|
||||
char *cp;
|
||||
int size;
|
||||
|
||||
if(s->fixed)
|
||||
sysfatal("s_grow of constant string");
|
||||
s = s_unique(s);
|
||||
|
||||
/*
|
||||
* take a larger increment to avoid mallocing too often
|
||||
*/
|
||||
size = s->end-s->base;
|
||||
if(size/2 < incr)
|
||||
size += incr;
|
||||
else
|
||||
size += size/2;
|
||||
|
||||
cp = realloc(s->base, size);
|
||||
if (cp == 0)
|
||||
sysfatal("s_grow: %r");
|
||||
s->ptr = (s->ptr - s->base) + cp;
|
||||
s->end = cp + size;
|
||||
s->base = cp;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue