You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
61 lines
1.3 KiB
unsigned int global_heap_start = 0; |
|
unsigned int global_heap_end = 0; |
|
unsigned int global_heap_next = 0; |
|
|
|
void* malloc(num_bytes); |
|
void* calloc(number_elements, size); |
|
int set_heap_settings(param_start, param_end); |
|
unsigned int get_heap_next(); |
|
|
|
void* malloc(num_bytes) |
|
int num_bytes; |
|
{ |
|
unsigned int allocated_pointer = global_heap_next; |
|
global_heap_next = global_heap_next + num_bytes; |
|
if (global_heap_next > global_heap_end) |
|
{ |
|
return 0; /* No more memory */ |
|
} |
|
|
|
return allocated_pointer; |
|
} |
|
|
|
void* calloc(number_elements, size) |
|
unsigned int number_elements; |
|
unsigned int size; |
|
{ |
|
int i; |
|
char* temp_pointer; |
|
unsigned int num_bytes = number_elements * size; |
|
void* allocated_pointer = global_heap_next; |
|
global_heap_next = global_heap_next + num_bytes; |
|
if (global_heap_next > global_heap_end) |
|
{ |
|
return 0; /* No more memory */ |
|
} |
|
|
|
/* Since this is calloc, we set the memory to zero */ |
|
temp_pointer = allocated_pointer; |
|
for (i = 0; i < num_bytes; ++i) |
|
{ |
|
*temp_pointer = 0; |
|
temp_pointer++; |
|
} |
|
|
|
return allocated_pointer; |
|
} |
|
|
|
int set_heap_settings(param_start, param_end) |
|
unsigned int param_start; |
|
unsigned int param_end; |
|
{ |
|
global_heap_start = param_start; |
|
global_heap_next = param_start; |
|
global_heap_end = param_end; |
|
return 0; |
|
} |
|
|
|
unsigned int get_heap_next() |
|
{ |
|
return global_heap_next; |
|
} |