plan9port

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

Vecadd.c (681B)


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