From a82c9f2d092bb7edd2effbbaad0b3750cde90153 Mon Sep 17 00:00:00 2001 From: erg Date: Sun, 19 Feb 2006 21:27:53 +0000 Subject: [PATCH] safe_malloc added-safe malloc to misc.c added return check of VirtualAlloc() --- native/gc.c | 10 ++-------- native/misc.c | 8 ++++++++ native/misc.h | 1 + native/stack.c | 4 +--- native/unix/memory.c | 4 +--- native/win32/memory.c | 6 +++--- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/native/gc.c b/native/gc.c index 8e62ca39f4..2d2529a8ec 100644 --- a/native/gc.c +++ b/native/gc.c @@ -28,23 +28,17 @@ void init_arena(CELL gens, CELL young_size, CELL aging_size) CELL cards_size = total_size / CARD_SIZE; gen_count = gens; - generations = malloc(sizeof(ZONE) * gen_count); - - if(generations == 0) - fatal_error("Cannot allocate zone head array",0); + generations = safe_malloc(sizeof(ZONE) * gen_count); heap_start = (CELL)(alloc_bounded_block(total_size)->start); heap_end = heap_start + total_size; - cards = malloc(cards_size); + cards = safe_malloc(cards_size); cards_end = cards + cards_size; cards_offset = (CELL)cards - (heap_start >> CARD_BITS); alloter = heap_start; - if(heap_start == 0) - fatal_error("Cannot allocate data heap",0); - alloter = init_zone(&tenured,aging_size,alloter); alloter = init_zone(&prior,aging_size,alloter); diff --git a/native/misc.c b/native/misc.c index fd8bb199a0..b209445aaf 100644 --- a/native/misc.c +++ b/native/misc.c @@ -1,5 +1,13 @@ #include "factor.h" +void *safe_malloc(size_t size) +{ + void *ptr = malloc(size); + if(ptr == 0) + fatal_error("malloc() failed", 0); + return ptr; +} + void primitive_exit(void) { exit(to_fixnum(dpop())); diff --git a/native/misc.h b/native/misc.h index 2a8c7c40d4..f6ebfd330d 100644 --- a/native/misc.h +++ b/native/misc.h @@ -1,3 +1,4 @@ +void *safe_malloc(size_t size); void primitive_exit(void); void primitive_os_env(void); void primitive_eq(void); diff --git a/native/stack.c b/native/stack.c index 1ea915c474..7de629efbd 100644 --- a/native/stack.c +++ b/native/stack.c @@ -34,9 +34,7 @@ void save_stacks(void) /* called on entry into a compiled callback */ void nest_stacks(void) { - STACKS *new_stacks = malloc(sizeof(STACKS)); - if(new_stacks == NULL) - fatal_error("Cannot allocate saved stacks struct",0); + STACKS *new_stacks = safe_malloc(sizeof(STACKS)); /* note that these register values are not necessarily valid stack pointers. they are merely saved non-volatile registers, and are diff --git a/native/unix/memory.c b/native/unix/memory.c index c2c7f38f52..096ee423b1 100644 --- a/native/unix/memory.c +++ b/native/unix/memory.c @@ -17,9 +17,7 @@ BOUNDED_BLOCK *alloc_bounded_block(CELL size) if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1) fatal_error("Cannot protect high guard page",(CELL)array); - BOUNDED_BLOCK *retval = malloc(sizeof(BOUNDED_BLOCK)); - if(retval == NULL) - fatal_error("Cannot allocate BOUNDED_BLOCK struct",0); + BOUNDED_BLOCK *retval = safe_malloc(sizeof(BOUNDED_BLOCK)); retval->start = (CELL)(array + pagesize); retval->size = size; diff --git a/native/win32/memory.c b/native/win32/memory.c index b0054a6ed7..71ba895213 100644 --- a/native/win32/memory.c +++ b/native/win32/memory.c @@ -8,6 +8,8 @@ BOUNDED_BLOCK *alloc_bounded_block(CELL size) GetSystemInfo(&si); mem = (char *)VirtualAlloc(NULL, si.dwPageSize*2 + size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + if(!mem) + fatal_error("VirtualAlloc() failed in alloc_bounded_block()",0); if (!VirtualProtect(mem, si.dwPageSize, PAGE_NOACCESS, &ignore)) fatal_error("Cannot allocate low guard page", (CELL)mem); @@ -15,9 +17,7 @@ BOUNDED_BLOCK *alloc_bounded_block(CELL size) if (!VirtualProtect(mem+size+si.dwPageSize, si.dwPageSize, PAGE_NOACCESS, &ignore)) fatal_error("Cannot allocate high guard page", (CELL)mem); - BOUNDED_BLOCK *retval = malloc(sizeof(BOUNDED_BLOCK)); - if(retval == NULL) - fatal_error("Cannot allocate BOUNDED_BLOCK struct",0); + BOUNDED_BLOCK *retval = safe_malloc(sizeof(BOUNDED_BLOCK)); retval->start = mem + si.dwPageSize; retval->size = size;