Skip to content
Myr

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

9 files+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])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
modified:tests/image_test.dsp+1 added-1 removed
59 unmodified lines
60# 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 lines
modified:tests/stb_cpp.dsp+1 added-1 removed
59 unmodified lines
60# 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 /c
33 unmodified lines
modified:tests/stb.dsp+3 added-3 removed
60 unmodified lines
61# 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 /c
27 unmodified lines
97SOURCE=..\stb_vorbis.c98# End Source File99# Begin Source File100101SOURCE=.\test_c_compilation.c101SOURCE=.\stretchy_buffer_test.c102# End Source File103# Begin Source File104105SOURCE=.\stretchy_buffer_test.c105SOURCE=.\test_c_compilation.c106# End Source File107# Begin Source File108109SOURCE=.\test_truetype.c
7 unmodified lines
modified:tests/stb.dsw+15 added
39 unmodified lines
40 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}}}4849###############################################################################50
8 unmodified lines
59}}}6061###############################################################################6263Project: "stretch_test"=.\stretch_test.dsp - Package Owner=<4>6465Package=<5>66{{{67}}}6869Package=<4>70{{{71}}}7273###############################################################################7475Global:7677Package=<5>78{{{
8 unmodified lines
added:tests/stretch_test.c+24 added
1#include "stretchy_buffer.h"2#include <assert.h>34int main(int arg, char **argv)5{6 int i;7 int *arr = NULL;89 for (i=0; i < 1000000; ++i)10 sb_push(arr, i);1112 assert(sb_count(arr) == 1000000);13 for (i=0; i < 1000000; ++i)14 assert(arr[i] == i);1516 sb_free(arr);17 arr = NULL;1819 for (i=0; i < 1000; ++i)20 sb_add(arr, 1000);21 assert(sb_count(arr) == 1000000);2223 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 **45# TARGTYPE "Win32 (x86) Console Application" 0x010367CFG=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 2324# Begin Project25# PROP AllowPerConfigDependencies 026# PROP Scc_ProjName ""27# PROP Scc_LocalPath ""28CPP=cl.exe29RSC=rc.exe3031!IF "$(CFG)" == "stretch_test - Win32 Release"3233# 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"47BSC32=bscmake.exe48# ADD BASE BSC32 /nologo49# ADD BSC32 /nologo50LINK32=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:I3865354!ELSEIF "$(CFG)" == "stretch_test - Win32 Debug"5556# 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"70BSC32=bscmake.exe71# ADD BASE BSC32 /nologo72# ADD BSC32 /nologo73LINK32=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:sept7677!ENDIF 7879# Begin Target8081# Name "stretch_test - Win32 Release"82# Name "stretch_test - Win32 Debug"83# Begin Source File8485SOURCE=.\stretch_test.c86# End Source File87# End Target88# End Project
modified:tools/make_readme.dsp+7 added-7 removed
46 unmodified lines
47BSC32=bscmake.exe48# ADD BASE BSC32 /nologo49# ADD BSC32 /nologo50LINK32=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:I3865354!ELSEIF "$(CFG)" == "make_readme - Win32 Debug"5556# PROP BASE Use_MFC 0
3 unmodified lines
60# 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"70BSC32=bscmake.exe71# ADD BASE BSC32 /nologo72# ADD BSC32 /nologo73LINK32=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:sept7677!ENDIF 7879# Begin Target
9 unmodified lines
modified:stretchy_buffer.h+1 added-1 removed
135 unmodified lines
136 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 #endif
4 unmodified lines