Here is the code:
http://pastebin.com/m4a405e67
One advantage to using a region allocator is that you can usually skip most
calls to free. Instead you can merge multiple free calls into a single reset
call. Here is simple example:
__________________________________________________ _________________
int main(void) {
allocator this_allocator;
/* create allocator instance with initial region size of 8192 and
a low-watermark region count of 2
*/
if (! allocator_create(&this_allocator, 8192, 2)) {
for (;;) {
int i;
for (i = 1; i < 1024; ++i) {
allocator_request(&this_allocator, i);
}
allocator_reset(&this_allocator);
}
allocator_destroy(&this_allocator);
}
return 0;
}
__________________________________________________ _________________
The above program will never run out of memory... However, this algorihtm
does not prohibit you from using free. For example:
__________________________________________________ _________________
int main(void) {
allocator this_allocator;
if (! allocator_create(&this_allocator, 8192, 2)) {
for (;;) {
int i;
for (i = 1; i < 1024; ++i) {
void* const ptr = allocator_request(&this_allocator, i);
if (ptr) {
allocator_release(&this_allocator, ptr);
}
}
}
allocator_destroy(&this_allocator);
}
return 0;
}
__________________________________________________ _________________
Do you have any helpful comments/suggestions/critiques?
Thanks.
--
Chris M. Thomasson
http://appcore.home.comcast.net