safe_malloc

added-safe malloc to misc.c
added return check of VirtualAlloc()
erg 2006-02-19 21:27:53 +00:00
parent ba79f3178a
commit a82c9f2d09
6 changed files with 16 additions and 17 deletions

View File

@ -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);

View File

@ -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()));

View File

@ -1,3 +1,4 @@
void *safe_malloc(size_t size);
void primitive_exit(void);
void primitive_os_env(void);
void primitive_eq(void);

View File

@ -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

View File

@ -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;

View File

@ -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;