plan9port

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

Vecadd.c (654B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <vec.h>
      4 
      5 Type
      6 Vecadd_(Type *p, ulong ms)
      7 {
      8 	Vector *v;
      9 
     10 	if (!p)
     11 		sysfatal("Vecadd: nil pointer");
     12 	Vecinsure_(p, ms, Vecsiz(*p) + 1);
     13 	v = Vec(*p);
     14 	if (v->init)
     15 		v->init((char*)*p + v->n * ms);
     16 	else
     17 		memset((char*)*p + v->n * ms, 0, ms);
     18 	return (char*)*p + v->n++ * ms;
     19 }
     20 
     21 void
     22 Vecinsure_(Type *p, ulong ms, ulong n)
     23 {
     24 	Vector *v;
     25 
     26 	if (!p)
     27 		sysfatal("Vecinsure: nil pointer");
     28 	v = Vec(*p);
     29 	if (v->size >= n)
     30 		return;
     31 	if (!v->size)
     32 		v->size = n;
     33 	else
     34 		for (; n > v->size; v->size *= 2);
     35 	v = realloc(v, sizeof(*v) + v->size * ms);
     36 	if (!v)
     37 		sysfatal("libvec vec.c:realloc(): %r");
     38 	*p = v + 1;
     39 }