safe_malloc
added-safe malloc to misc.c added return check of VirtualAlloc()
parent
ba79f3178a
commit
a82c9f2d09
10
native/gc.c
10
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;
|
CELL cards_size = total_size / CARD_SIZE;
|
||||||
|
|
||||||
gen_count = gens;
|
gen_count = gens;
|
||||||
generations = malloc(sizeof(ZONE) * gen_count);
|
generations = safe_malloc(sizeof(ZONE) * gen_count);
|
||||||
|
|
||||||
if(generations == 0)
|
|
||||||
fatal_error("Cannot allocate zone head array",0);
|
|
||||||
|
|
||||||
heap_start = (CELL)(alloc_bounded_block(total_size)->start);
|
heap_start = (CELL)(alloc_bounded_block(total_size)->start);
|
||||||
heap_end = heap_start + total_size;
|
heap_end = heap_start + total_size;
|
||||||
|
|
||||||
cards = malloc(cards_size);
|
cards = safe_malloc(cards_size);
|
||||||
cards_end = cards + cards_size;
|
cards_end = cards + cards_size;
|
||||||
cards_offset = (CELL)cards - (heap_start >> CARD_BITS);
|
cards_offset = (CELL)cards - (heap_start >> CARD_BITS);
|
||||||
|
|
||||||
alloter = heap_start;
|
alloter = heap_start;
|
||||||
|
|
||||||
if(heap_start == 0)
|
|
||||||
fatal_error("Cannot allocate data heap",0);
|
|
||||||
|
|
||||||
alloter = init_zone(&tenured,aging_size,alloter);
|
alloter = init_zone(&tenured,aging_size,alloter);
|
||||||
alloter = init_zone(&prior,aging_size,alloter);
|
alloter = init_zone(&prior,aging_size,alloter);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
#include "factor.h"
|
#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)
|
void primitive_exit(void)
|
||||||
{
|
{
|
||||||
exit(to_fixnum(dpop()));
|
exit(to_fixnum(dpop()));
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
void *safe_malloc(size_t size);
|
||||||
void primitive_exit(void);
|
void primitive_exit(void);
|
||||||
void primitive_os_env(void);
|
void primitive_os_env(void);
|
||||||
void primitive_eq(void);
|
void primitive_eq(void);
|
||||||
|
|
|
@ -34,9 +34,7 @@ void save_stacks(void)
|
||||||
/* called on entry into a compiled callback */
|
/* called on entry into a compiled callback */
|
||||||
void nest_stacks(void)
|
void nest_stacks(void)
|
||||||
{
|
{
|
||||||
STACKS *new_stacks = malloc(sizeof(STACKS));
|
STACKS *new_stacks = safe_malloc(sizeof(STACKS));
|
||||||
if(new_stacks == NULL)
|
|
||||||
fatal_error("Cannot allocate saved stacks struct",0);
|
|
||||||
|
|
||||||
/* note that these register values are not necessarily valid stack
|
/* note that these register values are not necessarily valid stack
|
||||||
pointers. they are merely saved non-volatile registers, and are
|
pointers. they are merely saved non-volatile registers, and are
|
||||||
|
|
|
@ -17,9 +17,7 @@ BOUNDED_BLOCK *alloc_bounded_block(CELL size)
|
||||||
if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
|
if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
|
||||||
fatal_error("Cannot protect high guard page",(CELL)array);
|
fatal_error("Cannot protect high guard page",(CELL)array);
|
||||||
|
|
||||||
BOUNDED_BLOCK *retval = malloc(sizeof(BOUNDED_BLOCK));
|
BOUNDED_BLOCK *retval = safe_malloc(sizeof(BOUNDED_BLOCK));
|
||||||
if(retval == NULL)
|
|
||||||
fatal_error("Cannot allocate BOUNDED_BLOCK struct",0);
|
|
||||||
|
|
||||||
retval->start = (CELL)(array + pagesize);
|
retval->start = (CELL)(array + pagesize);
|
||||||
retval->size = size;
|
retval->size = size;
|
||||||
|
|
|
@ -8,6 +8,8 @@ BOUNDED_BLOCK *alloc_bounded_block(CELL size)
|
||||||
|
|
||||||
GetSystemInfo(&si);
|
GetSystemInfo(&si);
|
||||||
mem = (char *)VirtualAlloc(NULL, si.dwPageSize*2 + size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
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))
|
if (!VirtualProtect(mem, si.dwPageSize, PAGE_NOACCESS, &ignore))
|
||||||
fatal_error("Cannot allocate low guard page", (CELL)mem);
|
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))
|
if (!VirtualProtect(mem+size+si.dwPageSize, si.dwPageSize, PAGE_NOACCESS, &ignore))
|
||||||
fatal_error("Cannot allocate high guard page", (CELL)mem);
|
fatal_error("Cannot allocate high guard page", (CELL)mem);
|
||||||
|
|
||||||
BOUNDED_BLOCK *retval = malloc(sizeof(BOUNDED_BLOCK));
|
BOUNDED_BLOCK *retval = safe_malloc(sizeof(BOUNDED_BLOCK));
|
||||||
if(retval == NULL)
|
|
||||||
fatal_error("Cannot allocate BOUNDED_BLOCK struct",0);
|
|
||||||
|
|
||||||
retval->start = mem + si.dwPageSize;
|
retval->start = mem + si.dwPageSize;
|
||||||
retval->size = size;
|
retval->size = size;
|
||||||
|
|
Loading…
Reference in New Issue