Skip to content

Commit

Permalink
fix bugs in new stretchy buffer code, add stretchy buffer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nothings committed Jun 1, 2014
1 parent 728d435 commit a57dfc5
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 13 deletions.
28 changes: 28 additions & 0 deletions deprecated/stretchy_buffer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// stretchy buffer // init: NULL // free: sbfree() // push_back: sbpush() // size: sbcount() //
#define sbfree(a) ((a) ? free(stb__sbraw(a)),0 : 0)
#define sbpush(a,v) (stb__sbmaybegrow(a,1), (a)[stb__sbn(a)++] = (v))
#define sbcount(a) ((a) ? stb__sbn(a) : 0)
#define sbadd(a,n) (stb__sbmaybegrow(a,n), stb__sbn(a)+=(n), &(a)[stb__sbn(a)-(n)])
#define sblast(a) ((a)[stb__sbn(a)-1])

#include <stdlib.h>
#define stb__sbraw(a) ((int *) (a) - 2)
#define stb__sbm(a) stb__sbraw(a)[0]
#define stb__sbn(a) stb__sbraw(a)[1]

#define stb__sbneedgrow(a,n) ((a)==0 || stb__sbn(a)+n >= stb__sbm(a))
#define stb__sbmaybegrow(a,n) (stb__sbneedgrow(a,(n)) ? stb__sbgrow(a,n) : 0)
#define stb__sbgrow(a,n) stb__sbgrowf((void **) &(a), (n), sizeof(*(a)))

static void stb__sbgrowf(void **arr, int increment, int itemsize)
{
int m = *arr ? 2*stb__sbm(*arr)+increment : increment+1;
void *p = realloc(*arr ? stb__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2);
assert(p);
if (p) {
if (!*arr) ((int *) p)[1] = 0;
*arr = (void *) ((int *) p + 2);
stb__sbm(*arr) = m;
}
}

2 changes: 1 addition & 1 deletion stretchy_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static void * stb__sbgrowf(void *arr, int increment, int itemsize)
if (!arr)
p[1] = 0;
p[0] = m;
return p;
return p+2;
} else {
#ifdef STRETCHY_BUFFER_OUT_OF_MEMORY
STRETCHY_BUFFER_OUT_OF_MEMORY ;
Expand Down
2 changes: 1 addition & 1 deletion tests/image_test.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/stb.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/stb.dsw
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Package=<4>
Begin Project Dependency
Project_Dep_Name image_test
End Project Dependency
Begin Project Dependency
Project_Dep_Name stretch_test
End Project Dependency
}}}

###############################################################################
Expand All @@ -57,6 +60,18 @@ Package=<4>

###############################################################################

Project: "stretch_test"=.\stretch_test.dsp - Package Owner=<4>

Package=<5>
{{{
}}}

Package=<4>
{{{
}}}

###############################################################################

Global:

Package=<5>
Expand Down
2 changes: 1 addition & 1 deletion tests/stb_cpp.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/stretch_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "stretchy_buffer.h"
#include <assert.h>

int main(int arg, char **argv)
{
int i;
int *arr = NULL;

for (i=0; i < 1000000; ++i)
sb_push(arr, i);

assert(sb_count(arr) == 1000000);
for (i=0; i < 1000000; ++i)
assert(arr[i] == i);

sb_free(arr);
arr = NULL;

for (i=0; i < 1000; ++i)
sb_add(arr, 1000);
assert(sb_count(arr) == 1000000);

return 0;
}
88 changes: 88 additions & 0 deletions tests/stretch_test.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions tools/make_readme.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a57dfc5

Please sign in to comment.