#include "internal.h" /* Grow to particular size */ void* mem·bufgrow(void* buf, vlong newLen, vlong eltsize) { assert(mem·bufcap(buf) <= (SIZE_MAX - 1) / 2); vlong newCap = MAX(16, MAX(1 + 2 * mem·bufcap(buf), newLen)); assert(newLen <= newCap); assert(newCap <= (SIZE_MAX - offsetof(mem·BufHead, buf)) / eltsize); vlong newSize = offsetof(mem·BufHead, buf) + newCap * eltsize; mem·BufHead* newHdr; if (buf) { newHdr = mem·bufhdr(buf); newHdr = (mem·BufHead*)realloc((void*)newHdr, newSize); } else { newHdr = (mem·BufHead*)malloc(newSize); newHdr->len = 0; } newHdr->cap = newCap; return (void*)newHdr->buf; } /* Pop out a value */ #if 0 void mem·bufdel(void *buf, int i, vlong eltsize) { int n; byte *b; byte stk[1024]; assert(eltsize < sizeof(stk)); b = (byte*)buf; if(n = mem·buflen(buf), i < n) { memcpy(stk, b+eltsize*i, eltsize); memcpy(b+eltsize*i, b+eltsize*(i+1), eltsize*(n-i-1)); memcpy(b+eltsize*(n-1), stk, eltsize); } mem·bufhdr(buf)->len--; } #endif