Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/gc #372

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ endif()

add_library(libhl SHARED
${pcre_srcs}
src/alloc.c
src/gc.c
${std_srcs}
)
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ INSTALL_DIR ?= $(PREFIX)

LIBS=fmt sdl ssl openal ui uv mysql

CFLAGS = -Wall -O3 -I src -msse2 -mfpmath=sse -std=c11 -I include/pcre -I include/mikktspace -I include/minimp3 -D LIBHL_EXPORTS
#CFLAGS = -D GC_ENABLE_DEBUG -Wall -O0 -I src -msse2 -mfpmath=sse -std=c11 -I include/pcre -I include/mikktspace -I include/minimp3 -D LIBHL_EXPORTS -D HL_NO_THREADS
#CFLAGS = -Wall -O0 -I src -msse2 -mfpmath=sse -std=c11 -I include/pcre -I include/mikktspace -I include/minimp3 -D LIBHL_EXPORTS -D HL_NO_THREADS
CFLAGS = -Wall -O3 -I src -msse2 -mfpmath=sse -std=c11 -I include/pcre -I include/mikktspace -I include/minimp3 -D LIBHL_EXPORTS -D HL_NO_THREADS
LFLAGS = -L. -lhl
LIBFLAGS =
HLFLAGS = -ldl
Expand All @@ -18,7 +20,7 @@ PCRE = include/pcre/pcre_chartables.o include/pcre/pcre_compile.o include/pcre/p
include/pcre/pcre_newline.o include/pcre/pcre_string_utils.o include/pcre/pcre_tables.o include/pcre/pcre_xclass.o \
include/pcre/pcre16_ord2utf16.o include/pcre/pcre16_valid_utf16.o include/pcre/pcre_ucd.o

RUNTIME = src/gc.o
RUNTIME = src/alloc.o src/gc.o

STD = src/std/array.o src/std/buffer.o src/std/bytes.o src/std/cast.o src/std/date.o src/std/error.o src/std/debug.o \
src/std/file.o src/std/fun.o src/std/maps.o src/std/math.o src/std/obj.o src/std/random.o src/std/regexp.o \
Expand Down Expand Up @@ -104,7 +106,7 @@ ifdef DEBUG
CFLAGS += -g
endif

all: libhl hl libs
all: libhl hl libs libstatic

install:
mkdir -p $(INSTALL_DIR)
Expand All @@ -125,6 +127,9 @@ libs: $(LIBS)
libhl: ${LIB}
${CC} -o libhl.$(LIBEXT) -m${MARCH} ${LIBFLAGS} -shared ${LIB} -lpthread -lm

libstatic: ${LIB}
ar -rcs libhl.a ${LIB}

hlc: ${BOOT}
${CC} ${CFLAGS} -o hlc ${BOOT} ${LFLAGS}

Expand Down
6 changes: 6 additions & 0 deletions other/gctests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
main: main.c ../../src/gc.h bench/mandelbrot.c
gcc -Wall -Werror -pedantic \
-Wno-unused-function -Wno-unused-variable -Wno-gnu-zero-variadic-macro-arguments \
-O3 -std=c11 -o main -I ../../src main.c bench/mandelbrot.c ../../libhl.a

#-O3 -std=c11 -o main -I ../../src main.c bench/mandelbrot.c -lhl
149 changes: 149 additions & 0 deletions other/gctests/bench/mandelbrot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include "hl.h"
#include "gc.h"
#include "../utils.h"
#include "../suite.h"


#ifdef WIN32

#include <windows.h>
static double hires_timestamp(void) {
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart / (double)f.QuadPart;
}

#else

#include <sys/time.h>
#include <sys/resource.h>
static double hires_timestamp(void) {
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec * 1e-6;
}

#endif

static hl_module_context mctx = {
.functions_ptrs = NULL,
.functions_types = NULL
};

TEST_TYPE(rgb, 3, {0}, {
int r;
int g;
int b;
});
TEST_TYPE(complex, 2, {0}, {
double i;
double j;
});

#define MB_SIZE 25
#define MB_MAX_ITERATIONS 1000
#define MB_MAX_RAD (1 << 16)
#define MB_WIDTH (35 * MB_SIZE)
#define MB_HEIGHT (20 * MB_SIZE)

static hlt_rgb_t *createPalette(double inFraction) {
hlt_rgb_t *ret = (hlt_rgb_t *)hl_alloc_obj(&hlt_rgb);
ret->r = inFraction * 255;
ret->g = (1.0 - inFraction) * 255;
ret->b = (0.5 - abs(inFraction - 0.5)) * 2 * 255;
return ret;
}

static hlt_complex_t *createComplex(double inI, double inJ) {
hlt_complex_t *ret = (hlt_complex_t *)hl_alloc_obj(&hlt_complex);
ret->i = inI;
ret->j = inJ;
return ret;
}

static double complexLength2(hlt_complex_t *val) {
return val->i * val->i + val->j * val->j;
}

static hlt_complex_t *complexAdd(hlt_complex_t *val0, hlt_complex_t *val1) {
return createComplex(val0->i + val1->i, val0->j + val1->j);
}

static hlt_complex_t *complexSquare(hlt_complex_t *val) {
return createComplex(val->i * val->i - val->j * val->j, 2.0 * val->i * val->j);
}

BEGIN_BENCH_CASE(mandelbrot) {
hl_alloc_init(&mctx.alloc);

varray *palette = hl_alloc_array(&hlt_rgb, MB_MAX_ITERATIONS + 1);
hl_add_root((void *)&palette);
for (int i = 0; i < MB_MAX_ITERATIONS + 1; i++) {
hl_aptr(palette, hlt_rgb_t *)[i] = createPalette((double)i / MB_MAX_ITERATIONS);
}

/*
for (int i = 0; i < MB_SIZE; i++) {
printf("%d %d %d\n", palette->data[i]->r, palette->data[i]->g, palette->data[i]->b);
}
*/

varray *image = hl_alloc_array(&hlt_rgb, MB_WIDTH * MB_HEIGHT);
hl_add_root((void *)&image);
int outPixel = 0;
double scale = 0.1 / MB_SIZE;

hlt_complex_t *offset = NULL;
hlt_complex_t *val = NULL;
hl_add_root((void *)&offset);
hl_add_root((void *)&val);

for (int y = 0; y < MB_HEIGHT; y++) {
if ((y % 10) == 0) {
printf("%d: %d pages, %lu live objects, %lu live blocks, %lu bytes used, %lu collection cycles\n", y, gc_stats->total_pages, gc_stats->live_objects, gc_stats->live_blocks, gc_stats->total_memory, gc_stats->cycles);
}
for (int x = 0; x < MB_WIDTH; x++) {
int iteration = 0;

// "reduce_allocs"
/*
double offsetI = x * scale - 2.5;
double offsetJ = y * scale - 1.0;
double valI = 0.0;
double valJ = 0.0;
while (valI * valI + valJ * valJ < MB_MAX_RAD && iteration < MB_MAX_ITERATIONS) {
double vi = valI;
valI = valI * valI - valJ * valJ + offsetI;
valJ = 2.0 * vi * valJ + offsetJ;
iteration++;
}
*/
// normal
offset = createComplex(x * scale - 2.5, y * scale - 1);
val = createComplex(0.0, 0.0);
while (complexLength2(val) < MB_MAX_RAD && iteration < MB_MAX_ITERATIONS) {
val = complexSquare(val);
val = complexAdd(val, offset);
iteration++;
}

hl_aptr(image, hlt_rgb_t *)[outPixel++] = hl_aptr(palette, hlt_rgb_t *)[iteration];
}
}

FILE *f = fopen("mandelbrot.ppm", "w");
fprintf(f, "P6 %d %d 255\n", MB_WIDTH, MB_HEIGHT);
for (int i = 0; i < MB_WIDTH * MB_HEIGHT; i++) {
hlt_rgb_t *pixel = hl_aptr(image, hlt_rgb_t *)[i];
// printf("%d %p\n", i, pixel);
unsigned char r = pixel->r & 0xFF;
unsigned char g = pixel->g & 0xFF;
unsigned char b = pixel->b & 0xFF;
fwrite(&r, 1, 1, f);
fwrite(&g, 1, 1, f);
fwrite(&b, 1, 1, f);
}
fclose(f);
} END_BENCH_CASE
Loading