|
Dynamic Memory Allocation Performance |
|
|
|
|
Written by SVTechie
|
|
Sunday, 09 April 2006 |
|
Page 6 of 7
This implementation is example function library for pool based Linked List Implementation in ANSI C. Two functions, malloceq and freeeq are implemented, to replace malloc/free. Malloceq allocates big memory with memory pool size of 400 bytes. Allocation calls will return pointer in the buffer pool if buffer space is available. In case if buffer space is not enough, a new buffer is allocated. Following is the code snippet for this algorithm. Please note, that following doesn't implement all checks and balances. Please contact me if you want to use this function or need more explaination.
Pool Based Link List Implementation
|
|
#include <stdio.h>
//----------------------------------------------------
// Buffer Management Function
#ifndef BUF_SIZE
#define BUF_SIZE 400
#endif
typedef struct bufdef {
char buf[BUF_SIZE];
int numAlloc;
int freeSize;
struct bufdef *next;
} buf;
// ----------------------------------------------------
// malloc Equivalent Function
buf *bufstr = NULL;
void * malloceq (unsigned int size) {
buf *tmp;
static buf *bufcur;
int i;
void *ptr;
if (bufstr == NULL) {
// Create New Buffer Node
bufstr = (buf *) malloc (sizeof(buf));
bufstr->next = NULL;
bufstr->freeSize = BUF_SIZE;
bufstr->numAlloc = 0;
bufcur = bufstr;
// } else if ((cur->freeSize != 0) && (cur->freeSize < size)) {
// Make this buffer slightly bigger
// Lets not support it right now.
// This functionality requires to be implemented at system
// Level
// }
} else if (bufcur->freeSize < size) {
tmp = (buf *) malloc (sizeof(buf));
tmp->next = NULL;
tmp->freeSize = BUF_SIZE;
tmp->numAlloc = 0;
bufcur->next = tmp;
bufcur = tmp;
}
// Allocate Memory and return pointer
bufcur->numAlloc++;
ptr = (void *)bufcur - bufcur->freeSize + BUF_SIZE;
bufcur->freeSize -= size;
// printf ("Pointer %x - Allocated - %d Size = %d\n", bufcur, bufcur->numAlloc, bufcur->freeSize);
return ptr;
}
// ----------------------------------------------------
// Free thy pointer
int freeeq (void *ptr) {
buf *tmp, *prev;
// Get buffer it belongs to
for (tmp = bufstr; tmp != NULL; tmp = tmp->next) {
if ((ptr >= (void*) tmp) & (ptr <= (void *)tmp + BUF_SIZE)) {
// printf ("Found Pointer %x in buf %x\n", ptr, tmp);
break;
}
prev = tmp;
}
// if (tmp == NULL) {
// printf ("Not able to find allocated pointer %x\n", ptr);
// return 0;
// } else {
tmp->numAlloc--;
if (tmp->numAlloc == 0) {
// Free this buffer
// Check if starting buffer
if (tmp == bufstr) {
bufstr = tmp->next;
} else {
prev->next = tmp->next;
}
free (tmp);
}
// }
return 1;
}
// ----------------------------------------------------
// Original Code Starts Here
typedef struct nodedef {
int data;
struct nodedef *next;
} node;
void main () {
node *str, *tmp, *cur;
int i;
str = cur = NULL;
for (i = 0; i < NUM_REQUESTS; i++) {
// Create New Node
tmp = (node *) malloceq (sizeof(node));
tmp->data = i;
tmp->next = NULL;
// Insert in the list
if (str == NULL) {
// First Node
str = tmp;
} else {
// Insert at End (pointed by cur)
cur->next = tmp;
}
cur = tmp;
}
// Deallocate each node
tmp = str;
for (cur = str->next; cur != NULL; cur = cur->next) {
freeeq (tmp);
tmp = cur;
}
}
|
|
|
Last Updated ( Saturday, 06 May 2006 )
|