Skip to content

Commit ce87018

Browse files
Add -GCTEST commandline option which invokes the garbage collector on every allocation, for testing purposes.
1 parent 69cdb41 commit ce87018

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ $(TESTS): cognac
3535
@rm -f $@.log $@.c $@
3636
./cognac $@.cog > $@.log
3737
./$@ >> $@.log
38-
./cognac $@.cog -debug >> $@.log
39-
./$@ >> $@.log
38+
./cognac $@.cog -debug > $@-debug.log
39+
./$@ >> $@-debug.log
40+
./cognac $@.cog -GCTEST > $@-GCTEST.log
41+
./$@ >> $@-GCTEST.log
4042
@! grep "^FAIL" $@.log --color
43+
@! grep "^FAIL" $@-debug.log --color
44+
@! grep "^FAIL" $@-GCTEST.log --color

src/cognac.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int usleep (unsigned int);
3232
char* strdup (const char*);
3333

3434
bool debug = false;
35+
bool gc_test = false;
3536

3637
static bool is_prelude(module_t* mod)
3738
{
@@ -820,13 +821,15 @@ void to_exe(module_t* mod)
820821
char* debug_args[] = {
821822
STR(CC), c_source_path, "-o", exe_path,
822823
"-O0", "-ggdb3", "-g", "-rdynamic", "-DDEBUG",
823-
"-lm", "-Wall", "-Wpedantic", "-Wno-unused", NULL
824+
"-lm", "-Wall", "-Wpedantic", "-Wno-unused",
825+
gc_test ? "-DGCTEST" : NULL, NULL
824826
} ;
825827

826828
char* normal_args[] = {
827829
STR(CC), c_source_path, "-o", exe_path,
828830
"-O3", "-flto", "-s", "-w",
829-
"-lm", "-Wall", "-Wpedantic", "-Wno-unused", NULL
831+
"-lm", "-Wall", "-Wpedantic", "-Wno-unused",
832+
gc_test ? "-DGCTEST" : NULL, NULL
830833
};
831834

832835
char** args = debug ? debug_args : normal_args;
@@ -3283,6 +3286,7 @@ int main(int argc, char** argv)
32833286
for (int i = 1 ; i < argc ; ++i)
32843287
{
32853288
if (!strcmp(argv[i], "-debug")) debug = true;
3289+
else if (!strcmp(argv[i], "-GCTEST")) gc_test = true;
32863290
else
32873291
{
32883292
char* ext = strrchr(argv[i], '.');

src/runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,9 @@ static void* gc_malloc(size_t sz)
10991099
{
11001100
static ptrdiff_t interval = 1024l*1024l*10;
11011101
interval -= sz;
1102+
#ifndef GCTEST
11021103
if unlikely(interval < 0)
1104+
#endif
11031105
{
11041106
gc_collect();
11051107
interval = 1024l*1024l*10l + alloc[z] * 6;
@@ -1118,7 +1120,9 @@ static void* gc_flatmalloc(size_t sz)
11181120
{
11191121
static ptrdiff_t interval = 1024l*1024l*10;
11201122
interval -= sz;
1123+
#ifndef GCTEST
11211124
if unlikely(interval < 0)
1125+
#endif
11221126
{
11231127
gc_collect();
11241128
interval = 1024l*1024l*10l + alloc[z] * 6;

0 commit comments

Comments
 (0)