Here is the code:
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_creat e(&this_allocat or, 8192, 2)) {
for (;;) {
int i;
for (i = 1; i < 1024; ++i) {
allocator_reque st(&this_alloca tor, i);
}
allocator_reset (&this_allocato r);
}
allocator_destr oy(&this_alloca tor);
}
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_creat e(&this_allocat or, 8192, 2)) {
for (;;) {
int i;
for (i = 1; i < 1024; ++i) {
void* const ptr = allocator_reque st(&this_alloca tor, i);
if (ptr) {
allocator_relea se(&this_alloca tor, ptr);
}
}
}
allocator_destr oy(&this_alloca tor);
}
return 0;
}
_______________ _______________ _______________ _______________ _______
Do you have any helpful comments/suggestions/critiques?
Thanks.
--
Chris M. Thomasson
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_creat e(&this_allocat or, 8192, 2)) {
for (;;) {
int i;
for (i = 1; i < 1024; ++i) {
allocator_reque st(&this_alloca tor, i);
}
allocator_reset (&this_allocato r);
}
allocator_destr oy(&this_alloca tor);
}
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_creat e(&this_allocat or, 8192, 2)) {
for (;;) {
int i;
for (i = 1; i < 1024; ++i) {
void* const ptr = allocator_reque st(&this_alloca tor, i);
if (ptr) {
allocator_relea se(&this_alloca tor, ptr);
}
}
}
allocator_destr oy(&this_alloca tor);
}
return 0;
}
_______________ _______________ _______________ _______________ _______
Do you have any helpful comments/suggestions/critiques?
Thanks.
--
Chris M. Thomasson
Comment