- #1
carl123
- 56
- 0
Salami Allocator
In this warm-up submission you will write a totally trivial allocator:
The function init allocator() reserves a given portion of memory for the allocator. Every call to my_malloc() cuts off the requested amount of memory from the remainder of the reserved memory. The call to my_free() does not free any memory. When all the memory has been used, and no more memory is left, subsequent calls to my_malloc() return NULL. For the implementation all you need is a pointer to the beginning of the remaining memory. Whenever memory is allocated, advance the pointer, until you reach the end.
This my interpretation of the problem so far. It seems right to me but I don't know if I'm doing something wrong. Any help will be appreciated.
In this warm-up submission you will write a totally trivial allocator:
The function init allocator() reserves a given portion of memory for the allocator. Every call to my_malloc() cuts off the requested amount of memory from the remainder of the reserved memory. The call to my_free() does not free any memory. When all the memory has been used, and no more memory is left, subsequent calls to my_malloc() return NULL. For the implementation all you need is a pointer to the beginning of the remaining memory. Whenever memory is allocated, advance the pointer, until you reach the end.
C:
int memAvail; // amount of memory to be made available to allocator
unsigned int init_allocator(unsigned int _basic_block_size, unsigned int _length){
/* This function initializes the memory allocator and makes a portion of
’_length’ bytes available. The allocator uses a ’_basic_block_size’ as
its minimal unit of allocation. The function returns the amount of
memory made available to the allocator. If an error occurred,
it returns 0.
*/
if (_basic_block_size < _length){
memAvail = _length/_basic_block_size;
return memAvail;
}
else{
return 0;
}
}
void *my_malloc(size_t _length) {
/* This preliminary implementation simply hands the call over the
the C standard library!
Of course this needs to be replaced by your implementation.
*/
int *ptr;
ptr = (int *) malloc(_length * sizeof(_length));
return ptr;
if (_length >= memAvail){
return NULL;
}
return malloc(_length);
}
Last edited by a moderator: