Skip to content
Myr

stretchy_buffer.txt

This file

Path

deprecated/stretchy_buffer.txt

28 lines · 1.1 KiB

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than aeec66c point at it.

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1–28why, one step back
1// stretchy buffer // init: NULL // free: sbfree() // push_back: sbpush() // size: sbcount() //
2#define sbfree(a) ((a) ? free(stb__sbraw(a)),0 : 0)3#define sbpush(a,v) (stb__sbmaybegrow(a,1), (a)[stb__sbn(a)++] = (v))4#define sbcount(a) ((a) ? stb__sbn(a) : 0)5#define sbadd(a,n) (stb__sbmaybegrow(a,n), stb__sbn(a)+=(n), &(a)[stb__sbn(a)-(n)])6#define sblast(a) ((a)[stb__sbn(a)-1])78#include <stdlib.h>9#define stb__sbraw(a) ((int *) (a) - 2)10#define stb__sbm(a) stb__sbraw(a)[0]11#define stb__sbn(a) stb__sbraw(a)[1]1213#define stb__sbneedgrow(a,n) ((a)==0 || stb__sbn(a)+n >= stb__sbm(a))14#define stb__sbmaybegrow(a,n) (stb__sbneedgrow(a,(n)) ? stb__sbgrow(a,n) : 0)15#define stb__sbgrow(a,n) stb__sbgrowf((void **) &(a), (n), sizeof(*(a)))1617static void stb__sbgrowf(void **arr, int increment, int itemsize)18{19 int m = *arr ? 2*stb__sbm(*arr)+increment : increment+1;20 void *p = realloc(*arr ? stb__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2);21 assert(p);22 if (p) {23 if (!*arr) ((int *) p)[1] = 0;24 *arr = (void *) ((int *) p + 2);25 stb__sbm(*arr) = m;26 }27}28