parent
728d435591
commit
a57dfc50fc
9 changed files with 168 additions and 13 deletions
@ -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; |
||||
} |
||||
} |
||||
|
@ -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; |
||||
} |
@ -0,0 +1,88 @@ |
||||
# Microsoft Developer Studio Project File - Name="stretch_test" - Package Owner=<4> |
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00 |
||||
# ** DO NOT EDIT ** |
||||
|
||||
# TARGTYPE "Win32 (x86) Console Application" 0x0103 |
||||
|
||||
CFG=stretch_test - Win32 Debug |
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE, |
||||
!MESSAGE use the Export Makefile command and run |
||||
!MESSAGE |
||||
!MESSAGE NMAKE /f "stretch_test.mak". |
||||
!MESSAGE |
||||
!MESSAGE You can specify a configuration when running NMAKE |
||||
!MESSAGE by defining the macro CFG on the command line. For example: |
||||
!MESSAGE |
||||
!MESSAGE NMAKE /f "stretch_test.mak" CFG="stretch_test - Win32 Debug" |
||||
!MESSAGE |
||||
!MESSAGE Possible choices for configuration are: |
||||
!MESSAGE |
||||
!MESSAGE "stretch_test - Win32 Release" (based on "Win32 (x86) Console Application") |
||||
!MESSAGE "stretch_test - Win32 Debug" (based on "Win32 (x86) Console Application") |
||||
!MESSAGE |
||||
|
||||
# Begin Project |
||||
# PROP AllowPerConfigDependencies 0 |
||||
# PROP Scc_ProjName "" |
||||
# PROP Scc_LocalPath "" |
||||
CPP=cl.exe |
||||
RSC=rc.exe |
||||
|
||||
!IF "$(CFG)" == "stretch_test - Win32 Release" |
||||
|
||||
# PROP BASE Use_MFC 0 |
||||
# PROP BASE Use_Debug_Libraries 0 |
||||
# PROP BASE Output_Dir "Release" |
||||
# PROP BASE Intermediate_Dir "Release" |
||||
# PROP BASE Target_Dir "" |
||||
# PROP Use_MFC 0 |
||||
# PROP Use_Debug_Libraries 0 |
||||
# PROP Output_Dir "Release" |
||||
# PROP Intermediate_Dir "Release" |
||||
# PROP Target_Dir "" |
||||
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c |
||||
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c |
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG" |
||||
# ADD RSC /l 0x409 /d "NDEBUG" |
||||
BSC32=bscmake.exe |
||||
# ADD BASE BSC32 /nologo |
||||
# ADD BSC32 /nologo |
||||
LINK32=link.exe |
||||
# 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:I386 |
||||
# 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:I386 |
||||
|
||||
!ELSEIF "$(CFG)" == "stretch_test - Win32 Debug" |
||||
|
||||
# PROP BASE Use_MFC 0 |
||||
# PROP BASE Use_Debug_Libraries 1 |
||||
# PROP BASE Output_Dir "stretch_test___Win32_Debug" |
||||
# PROP BASE Intermediate_Dir "stretch_test___Win32_Debug" |
||||
# PROP BASE Target_Dir "" |
||||
# PROP Use_MFC 0 |
||||
# PROP Use_Debug_Libraries 1 |
||||
# PROP Output_Dir "Debug" |
||||
# PROP Intermediate_Dir "Debug\stretch_test" |
||||
# PROP Target_Dir "" |
||||
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c |
||||
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c |
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG" |
||||
# ADD RSC /l 0x409 /d "_DEBUG" |
||||
BSC32=bscmake.exe |
||||
# ADD BASE BSC32 /nologo |
||||
# ADD BSC32 /nologo |
||||
LINK32=link.exe |
||||
# 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:sept |
||||
# 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:sept |
||||
|
||||
!ENDIF |
||||
|
||||
# Begin Target |
||||
|
||||
# Name "stretch_test - Win32 Release" |
||||
# Name "stretch_test - Win32 Debug" |
||||
# Begin Source File |
||||
|
||||
SOURCE=.\stretch_test.c |
||||
# End Source File |
||||
# End Target |
||||
# End Project |
Loading…
Reference in New Issue