adding basic oom tests

This commit is contained in:
Alex Shinn 2011-08-19 01:43:44 +09:00
parent cdd83b20cf
commit 54f913fa2a
6 changed files with 52 additions and 21 deletions

View file

@ -214,6 +214,9 @@ test-basic: chibi-scheme$(EXE)
fi; \
done
test-memory:
./tests/memory/memory-tests.sh
test-build:
MAKE=$(MAKE) ./tests/build/build-tests.sh

2
eval.c
View file

@ -1737,7 +1737,7 @@ sexp sexp_find_module_file (sexp ctx, const char *file) {
dirlen = sexp_string_length(sexp_car(ls));
slash = dir[dirlen-1] == '/';
len = dirlen+filelen+2-slash;
path = (char*) malloc(len);
path = (char*) sexp_malloc(len);
if (! path) return sexp_global(ctx, SEXP_G_OOM_ERROR);
memcpy(path, dir, dirlen);
if (! slash) path[dirlen] = '/';

23
gc.c
View file

@ -48,6 +48,27 @@ void sexp_free_heap (sexp_heap heap) {
}
#endif
#if SEXP_USE_LIMITED_MALLOC
static sexp_sint_t allocated_bytes=0, max_allocated_bytes=-1;
void* sexp_malloc(size_t size) {
char* max_alloc;
void* res;
if (max_allocated_bytes < 0) {
max_alloc = getenv("CHIBI_MAX_ALLOC");
max_allocated_bytes = max_alloc ? atoi(max_alloc) : 8192000; /* 8MB */
}
if (max_allocated_bytes > 0 && allocated_bytes + size > max_allocated_bytes)
return NULL;
if (!(res = malloc(size))) return NULL;
allocated_bytes += size;
return res;
}
/* TODO: subtract freed memory from max_allocated_bytes */
void sexp_free(void* ptr) {
free(ptr);
}
#endif
sexp_uint_t sexp_allocated_bytes (sexp ctx, sexp x) {
sexp_uint_t res;
sexp t;
@ -369,7 +390,7 @@ sexp_heap sexp_make_heap (size_t size, size_t max_size) {
h = mmap(NULL, sexp_heap_pad_size(size), PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_ANON|MAP_PRIVATE, 0, 0);
#else
h = malloc(sexp_heap_pad_size(size));
h = sexp_malloc(sexp_heap_pad_size(size));
#endif
if (! h) return NULL;
h->size = size;

View file

@ -301,6 +301,10 @@
#define SEXP_USE_MALLOC 0
#endif
#ifndef SEXP_USE_LIMITED_MALLOC
#define SEXP_USE_LIMITED_MALLOC 0
#endif
#ifndef SEXP_USE_MMAP_GC
#define SEXP_USE_MMAP_GC 0
#endif
@ -509,6 +513,10 @@
#define SEXP_USE_FLONUMS 0
#undef SEXP_USE_BIGNUMS
#define SEXP_USE_BIGNUMS 0
#undef SEXP_USE_RATIOS
#define SEXP_USE_RATIOS 0
#undef SEXP_USE_COMPLEX
#define SEXP_USE_COMPLEX 0
#undef SEXP_USE_SIMPLIFY
#define SEXP_USE_SIMPLIFY 0
#endif

View file

@ -397,6 +397,14 @@ struct sexp_struct {
#define SEXP_CLOSE SEXP_MAKE_IMMEDIATE(6) /* internal use */
#define SEXP_RAWDOT SEXP_MAKE_IMMEDIATE(7) /* internal use */
#if SEXP_USE_LIMITED_MALLOC
void* sexp_malloc(size_t size);
void sexp_free(void* ptr);
#else
#define sexp_malloc malloc
#define sexp_free free
#endif
#if SEXP_USE_BOEHM
#define sexp_gc_var(ctx, x, y) sexp x;
@ -406,9 +414,6 @@ struct sexp_struct {
#include "gc/gc.h"
#define sexp_alloc(ctx, size) GC_malloc(size)
#define sexp_alloc_atomic(ctx, size) GC_malloc_atomic(size)
#define sexp_realloc(ctx, x, size) GC_realloc(x, size)
#define sexp_free(ctx, x)
#define sexp_deep_free(ctx, x)
#else
@ -433,19 +438,11 @@ struct sexp_struct {
#define sexp_gc_release(ctx, x, y) (sexp_context_saves(ctx) = y.next)
#if SEXP_USE_MALLOC
#define sexp_alloc(ctx, size) malloc(size)
#define sexp_alloc_atomic(ctx, size) malloc(size)
#define sexp_realloc(ctx, x, size) realloc(x, size)
#define sexp_free(ctx, x) free(x)
void sexp_deep_free(sexp ctx, sexp obj);
#define sexp_alloc(ctx, size) sexp_malloc(size)
#define sexp_alloc_atomic(ctx, size) sexp_malloc(size)
#else /* native gc */
void *sexp_alloc(sexp ctx, size_t size);
void* sexp_alloc(sexp ctx, size_t size);
#define sexp_alloc_atomic sexp_alloc
void *sexp_realloc(sexp ctx, sexp x, size_t size);
#define sexp_free(ctx, x)
#define sexp_deep_free(ctx, x)
#endif
#endif

12
sexp.c
View file

@ -1213,7 +1213,7 @@ sexp sexp_make_input_string_port_op (sexp ctx sexp_api_params(self, n), sexp str
sexp sexp_make_output_string_port_op (sexp ctx sexp_api_params(self, n)) {
sexp res = sexp_make_output_port(ctx, NULL, SEXP_FALSE);
if (sexp_exceptionp(res)) return res;
sexp_port_buf(res) = (char*) malloc(SEXP_PORT_BUFFER_SIZE);
sexp_port_buf(res) = (char*) sexp_malloc(SEXP_PORT_BUFFER_SIZE);
if (!sexp_port_buf(res)) {
res = sexp_global(ctx, SEXP_G_OOM_ERROR);
} else {
@ -1557,7 +1557,7 @@ sexp sexp_read_string (sexp ctx, sexp in) {
}
buf[i++] = c;
if (i >= size) { /* expand buffer w/ malloc(), later free() it */
tmp = (char*) malloc(size*2);
tmp = (char*) sexp_malloc(size*2);
if (!tmp) {res = sexp_global(ctx, SEXP_G_OOM_ERROR); break;}
memcpy(tmp, buf, i);
if (size != INIT_STRING_BUFFER_SIZE) free(buf);
@ -1566,8 +1566,10 @@ sexp sexp_read_string (sexp ctx, sexp in) {
}
}
buf[i] = '\0';
if (!sexp_exceptionp(res)) res = sexp_c_string(ctx, buf, i);
if (!sexp_exceptionp(res)) {
buf[i] = '\0';
res = sexp_c_string(ctx, buf, i);
}
if (size != INIT_STRING_BUFFER_SIZE) free(buf);
return res;
}
@ -1596,7 +1598,7 @@ sexp sexp_read_symbol (sexp ctx, sexp in, int init, int internp) {
}
buf[i++] = c;
if (i >= size) { /* expand buffer w/ malloc(), later free() it */
tmp = (char*) malloc(size*2);
tmp = (char*) sexp_malloc(size*2);
memcpy(tmp, buf, i);
if (size != INIT_STRING_BUFFER_SIZE) free(buf);
buf = tmp;