OpenNTF.org - Simple C memory manager
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
Simple C memory manager 
Rating:
Not Rated Yet 
Contributor:
Tom Lyne 
Category:
C/Cpp 
Type:
Example Code 
Notes Version:
R5.x, R6.x 
Last Modified:
09 Jan 2004 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
A simple memory management system for allocating memory and freeing it when it's no longer being used.


allocate blocks with mm_allocate() and mm_allocate_block()
free blocks with mm_free()
finish with mm_free_all() to make sure everything has been free()'d

*********************** mm.h *************************************************
// mm.h
// memory management header

#ifndef _TOM_MM
#define _TOM_MM

typedef struct _memory_block
{
struct memory_block *next_block;
size_t block_size;
void *block;
} memory_block;

typedef struct _memory_manager
{
memory_block *start_block;
long total_allocated; // size in bytes of total 'block' size allocated
long number_allocated; // number of memory_block(s) allocated
} memory_manager;

memory_manager *mm_g_master;

// functions
memory_block *mm_allocate_block(size_t);
void mm_free_block(void *);
void mm_free_all();

// macros
#define mm_allocate(sz) mm_allocate_block(sz)->block

#endif
*************************************************************************

********************** mm.c ********************************************
// mm.c
// memory management

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "mm.h"


// allocate block
// allocates a memory block and adds it
// to the list of allocated blocks
memory_block *mm_allocate_block(size_t sz)
{
memory_block *tmp=NULL, *pos1=NULL, *pos2=NULL;

if(mm_g_master == NULL)
{
// memory manager not created
// so create it
if(! (mm_g_master = (memory_manager*)malloc(sizeof(memory_manager))) )
{
// could not create manager
// so return null
return (0);
}
mm_g_master->start_block = NULL;
mm_g_master->total_allocated = 0;
mm_g_master->number_allocated = 0;
}

// memory manager ok
// so allocate memory_block
if(!(tmp = (memory_block*)malloc(sizeof(memory_block))) )
{
// memoy_block not allocated
// so return null
return (0);
}

if(!(tmp->block = malloc(sz)))
{
// problem allocating block
// so return null
free(tmp);
return (0);
}

tmp->block_size = sz;
memset(tmp->block, '\0', sz);
tmp->next_block = 0;

// walk through memory buffer list
// until a free position is found
pos1 = pos2 = mm_g_master->start_block;

while(pos1)
{
pos2 = pos1;
pos1 = (memory_block*)pos2->next_block;
}

if(!pos2)
{
// no memory blocks yet created
// so must be at top of list
mm_g_master->start_block = tmp;
} else
{
// at least 1 memory block created
// so not at top of list
pos2->next_block = tmp;
}

// add up list totals
// total list size = sizeof(memory_manager) +
// (mm_g_master->number_allocated * sizeof(memory_block))
mm_g_master->total_allocated += sz;
mm_g_master->number_allocated++;

return (tmp);
}

// free block
// free a memory block
// argument can be a memory_block* or memory_block->block
void mm_free_block(void *p)
{
memory_block *pos1=NULL, *pos2=NULL;

if(!mm_g_master)
return;

if(!p)
return;

// loop through memory_block list
// until a match is found
pos1 = pos2 = mm_g_master->start_block;

while( (pos1 != NULL) && (pos1 != (memory_block*)p) && (pos1->block != p) )
{
pos2 = pos1;
pos1 = (memory_block*)pos2->next_block;
}

if(pos1 == NULL)
return;

if( (pos2 == (memory_block*)p) || (pos2->block == p) )
{
// block to free is first block
mm_g_master->start_block = pos2->next_block;
mm_g_master->total_allocated -= pos2->block_size;
mm_g_master->number_allocated --;
free(pos2->block);
free(pos2);
} else
{
// block to free is not first block
pos2->next_block = pos1->next_block;
mm_g_master->total_allocated -= pos1->block_size;
mm_g_master->number_allocated --;
free(pos1->block);
free(pos1);
}
}

// free all
// free everything in memory manager list
// frees everything in list
void mm_free_all()
{
memory_block *pos1=NULL, *pos2=NULL;

if(!mm_g_master)
return;

// loop through and delete all blocks
pos1 = pos2 = mm_g_master->start_block;

while(pos1)
{
pos1 = pos2->next_block;
free(pos2->block);
free(pos2);
pos2 = pos1;
}

free(mm_g_master);
}
**************************************************************************

Usage / Example

memory_block *p;
char *pv=NULL, *ptmp=NULL;

pv = (char *)mm_allocate(5);
strcpy(pv, "1111");
fprintf(out, "contents of pv: %s\n", pv);
pv = (char *)mm_allocate(5);
strcpy(pv, "aaaa");
fprintf(out, "contents of pv: %s\n", pv);
pv = (char *)mm_allocate(5);
strcpy(pv, "bbbb");
fprintf(out, "contents of pv: %s\n", pv);
pv = (char *)mm_allocate(5);
strcpy(pv, "cccc");
fprintf(out, "contents of pv: %s\n", pv);
ptmp = pv;

pv = (char *)mm_allocate(5);
strcpy(pv, "dddd");
fprintf(out, "contents of pv: %s\n", pv);

p = mm_allocate_block(5);

fprintf(out, "p created\nfilling 1st p->block...\n");
strcpy((char *)p->block, "help");
fprintf(out, "p filled with %s\n", (char *)p->block);

fprintf(out, "stats:\n\ttotal_allocated: %d\n\tnumber_allocated: %d\n", mm_g_master->total_allocated, mm_g_master->number_allocated);

mm_free_block(ptmp);
mm_free_block(p);

fprintf(out, "stats:\n\ttotal_allocated: %d\n\tnumber_allocated: %d\n", mm_g_master->total_allocated, mm_g_master->number_allocated);

mm_free_all();

Output:

contents of pv: 1111
contents of pv: aaaa
contents of pv: bbbb
contents of pv: cccc
contents of pv: dddd
p created
filling 1st p->block...
p filled with help
stats:
total_allocated: 30
number_allocated: 6
stats:
total_allocated: 20
number_allocated: 4
 Comments

No documents found

 Add your comment!