Created
May 13, 2022 02:47
-
-
Save christianscott/ceca1aaaf1b54e56df1a31cf99796e4d to your computer and use it in GitHub Desktop.
small change to https://github.com/nothings/stb/blob/master/deprecated/stretchy_buffer.txt so I can read it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vec | |
#define vec_free(a) ((a) ? free(vec__raw(a)),0 : 0) | |
#define vec_push(a,v) (vec__maybegrow(a,1), (a)[vec__n(a)++] = (v)) | |
#define vec_count(a) ((a) ? vec__n(a) : 0) | |
#define vec_add(a,n) (vec__maybegrow(a,n), vec__n(a)+=(n), &(a)[vec__n(a)-(n)]) | |
#define vec_last(a) ((a)[vec__n(a)-1]) | |
#include <stdlib.h> | |
#define vec__raw(a) ((int *) (a) - 2) | |
#define vec__m(a) vec__raw(a)[0] | |
#define vec__n(a) vec__raw(a)[1] | |
#define vec__needgrow(a,n) ((a)==0 || vec__n(a)+n >= vec__m(a)) | |
#define vec__maybegrow(a,n) (vec__needgrow(a,(n)) ? vec__grow(a,n) : 0) | |
#define vec__grow(a,n) vec__growf((void **) &(a), (n), sizeof(*(a))) | |
static void vec__growf(void **arr, int increment, int itemsize) | |
{ | |
int m = *arr ? 2*vec__m(*arr)+increment : increment+1; | |
void *p = realloc(*arr ? vec__raw(*arr) : 0, itemsize * m + sizeof(int)*2); | |
assert(p); | |
if (p) { | |
if (!*arr) ((int *) p)[1] = 0; | |
*arr = (void *) ((int *) p + 2); | |
vec__m(*arr) = m; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment