fix bugs in new stretchy buffer code, add stretchy buffer tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 728d435 · Browse files · 9 files·+168 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+168 added−13 removed
- tests:+132 added−5 removed
- other:+36 added−8 removed
Provenance
- Commit
a57dfc5- Tree
8da1adf- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
728d435
9 files changed+168 added−13 removed
added:deprecated/stretchy_buffer.txt+28 added
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])7+8+#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]12+13+#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)))16+17+static 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+modified:tests/image_test.dsp+1 added-1 removed
59 unmodified lines60 # PROP BASE Target_Dir ""61 # PROP Use_MFC 062 # PROP Use_Debug_Libraries 163 # PROP Output_Dir "Debug"64-# PROP Intermediate_Dir "Debug"64+# PROP Intermediate_Dir "Debug\image_test"65 # PROP Target_Dir ""66 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67 # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68 # ADD BASE RSC /l 0x409 /d "_DEBUG"28 unmodified linesmodified:tests/stb_cpp.dsp+1 added-1 removed
59 unmodified lines60 # PROP BASE Target_Dir ""61 # PROP Use_MFC 062 # PROP Use_Debug_Libraries 163 # PROP Output_Dir "Debug"64-# PROP Intermediate_Dir "Debug"64+# PROP Intermediate_Dir "Debug\stb_cpp"65 # PROP Ignore_Export_Lib 066 # PROP Target_Dir ""67 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68 # ADD CPP /nologo /MTd /W3 /GX /Zd /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c33 unmodified linesmodified:tests/stb.dsp+3 added-3 removed
60 unmodified lines61 # PROP BASE Target_Dir ""62 # PROP Use_MFC 063 # PROP Use_Debug_Libraries 164 # PROP Output_Dir "Debug"65-# PROP Intermediate_Dir "Debug"65+# PROP Intermediate_Dir "Debug\stb"66 # PROP Ignore_Export_Lib 067 # PROP Target_Dir ""68 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c69 # ADD CPP /nologo /MTd /W3 /GX /Zd /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "MAIN_TEST" /FR /FD /GZ /c27 unmodified lines97 SOURCE=..\stb_vorbis.c98 # End Source File99 # Begin Source File100 101-SOURCE=.\test_c_compilation.c101+SOURCE=.\stretchy_buffer_test.c102 # End Source File103 # Begin Source File104 105-SOURCE=.\stretchy_buffer_test.c105+SOURCE=.\test_c_compilation.c106 # End Source File107 # Begin Source File108 109 SOURCE=.\test_truetype.c7 unmodified linesmodified:tests/stb.dsw+15 added
39 unmodified lines40 End Project Dependency41 Begin Project Dependency42 Project_Dep_Name image_test43 End Project Dependency44+ Begin Project Dependency45+ Project_Dep_Name stretch_test46+ End Project Dependency47 }}}48 49 ###############################################################################50 8 unmodified lines59 }}}60 61 ###############################################################################62 63+Project: "stretch_test"=.\stretch_test.dsp - Package Owner=<4>64+65+Package=<5>66+{{{67+}}}68+69+Package=<4>70+{{{71+}}}72+73+###############################################################################74+75 Global:76 77 Package=<5>78 {{{8 unmodified linesadded:tests/stretch_test.c+24 added
1+#include "stretchy_buffer.h"2+#include <assert.h>3+4+int main(int arg, char **argv)5+{6+ int i;7+ int *arr = NULL;8+9+ for (i=0; i < 1000000; ++i)10+ sb_push(arr, i);11+12+ assert(sb_count(arr) == 1000000);13+ for (i=0; i < 1000000; ++i)14+ assert(arr[i] == i);15+16+ sb_free(arr);17+ arr = NULL;18+19+ for (i=0; i < 1000; ++i)20+ sb_add(arr, 1000);21+ assert(sb_count(arr) == 1000000);22+23+ return 0;24+}added:tests/stretch_test.dsp+88 added
1+# Microsoft Developer Studio Project File - Name="stretch_test" - Package Owner=<4>2+# Microsoft Developer Studio Generated Build File, Format Version 6.003+# ** DO NOT EDIT **4+5+# TARGTYPE "Win32 (x86) Console Application" 0x01036+7+CFG=stretch_test - Win32 Debug8+!MESSAGE This is not a valid makefile. To build this project using NMAKE,9+!MESSAGE use the Export Makefile command and run10+!MESSAGE 11+!MESSAGE NMAKE /f "stretch_test.mak".12+!MESSAGE 13+!MESSAGE You can specify a configuration when running NMAKE14+!MESSAGE by defining the macro CFG on the command line. For example:15+!MESSAGE 16+!MESSAGE NMAKE /f "stretch_test.mak" CFG="stretch_test - Win32 Debug"17+!MESSAGE 18+!MESSAGE Possible choices for configuration are:19+!MESSAGE 20+!MESSAGE "stretch_test - Win32 Release" (based on "Win32 (x86) Console Application")21+!MESSAGE "stretch_test - Win32 Debug" (based on "Win32 (x86) Console Application")22+!MESSAGE 23+24+# Begin Project25+# PROP AllowPerConfigDependencies 026+# PROP Scc_ProjName ""27+# PROP Scc_LocalPath ""28+CPP=cl.exe29+RSC=rc.exe30+31+!IF "$(CFG)" == "stretch_test - Win32 Release"32+33+# PROP BASE Use_MFC 034+# PROP BASE Use_Debug_Libraries 035+# PROP BASE Output_Dir "Release"36+# PROP BASE Intermediate_Dir "Release"37+# PROP BASE Target_Dir ""38+# PROP Use_MFC 039+# PROP Use_Debug_Libraries 040+# PROP Output_Dir "Release"41+# PROP Intermediate_Dir "Release"42+# PROP Target_Dir ""43+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45+# ADD BASE RSC /l 0x409 /d "NDEBUG"46+# ADD RSC /l 0x409 /d "NDEBUG"47+BSC32=bscmake.exe48+# ADD BASE BSC32 /nologo49+# ADD BSC32 /nologo50+LINK32=link.exe51+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38652+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38653+54+!ELSEIF "$(CFG)" == "stretch_test - Win32 Debug"55+56+# PROP BASE Use_MFC 057+# PROP BASE Use_Debug_Libraries 158+# PROP BASE Output_Dir "stretch_test___Win32_Debug"59+# PROP BASE Intermediate_Dir "stretch_test___Win32_Debug"60+# PROP BASE Target_Dir ""61+# PROP Use_MFC 062+# PROP Use_Debug_Libraries 163+# PROP Output_Dir "Debug"64+# PROP Intermediate_Dir "Debug\stretch_test"65+# PROP Target_Dir ""66+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68+# ADD BASE RSC /l 0x409 /d "_DEBUG"69+# ADD RSC /l 0x409 /d "_DEBUG"70+BSC32=bscmake.exe71+# ADD BASE BSC32 /nologo72+# ADD BSC32 /nologo73+LINK32=link.exe74+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept75+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept76+77+!ENDIF 78+79+# Begin Target80+81+# Name "stretch_test - Win32 Release"82+# Name "stretch_test - Win32 Debug"83+# Begin Source File84+85+SOURCE=.\stretch_test.c86+# End Source File87+# End Target88+# End Projectmodified:tools/make_readme.dsp+7 added-7 removed
46 unmodified lines47 BSC32=bscmake.exe48 # ADD BASE BSC32 /nologo49 # ADD BSC32 /nologo50 LINK32=link.exe51-# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38652-# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38651+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38652+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38653 54 !ELSEIF "$(CFG)" == "make_readme - Win32 Debug"55 56 # PROP BASE Use_MFC 03 unmodified lines60 # PROP BASE Target_Dir ""61 # PROP Use_MFC 062 # PROP Use_Debug_Libraries 163 # PROP Output_Dir "Debug"64-# PROP Intermediate_Dir "Debug"64+# PROP Intermediate_Dir "Debug\make_readme"65 # PROP Target_Dir ""66-# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67-# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c66+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68 # ADD BASE RSC /l 0x409 /d "_DEBUG"69 # ADD RSC /l 0x409 /d "_DEBUG"70 BSC32=bscmake.exe71 # ADD BASE BSC32 /nologo72 # ADD BSC32 /nologo73 LINK32=link.exe74-# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept75-# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept74+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept75+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept76 77 !ENDIF 78 79 # Begin Target9 unmodified linesmodified:stretchy_buffer.h+1 added-1 removed
135 unmodified lines136 if (p) {137 if (!arr)138 p[1] = 0;139 p[0] = m;140- return p;140+ return p+2;141 } else {142 #ifdef STRETCHY_BUFFER_OUT_OF_MEMORY143 STRETCHY_BUFFER_OUT_OF_MEMORY ;144 #endif4 unmodified lines