#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "mem.h"
Go to the source code of this file.
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 168 of file mem.c.
174 if (
p->nEntriesUsed ==
p->nEntriesAlloc )
176 assert(
p->pEntriesFree == NULL );
177 if (
p->nChunks ==
p->nChunksAlloc )
179 p->nChunksAlloc *= 2;
182 p->pEntriesFree =
ABC_ALLOC(
char,
p->nEntrySize *
p->nChunkSize );
183 p->nMemoryAlloc +=
p->nEntrySize *
p->nChunkSize;
185 pTemp =
p->pEntriesFree;
186 for ( i = 1; i <
p->nChunkSize; i++ )
188 *((
char **)pTemp) = pTemp +
p->nEntrySize;
189 pTemp +=
p->nEntrySize;
192 *((
char **)pTemp) = NULL;
194 p->pChunks[
p->nChunks++ ] =
p->pEntriesFree;
196 p->nEntriesAlloc +=
p->nChunkSize;
200 if (
p->nEntriesMax <
p->nEntriesUsed )
201 p->nEntriesMax =
p->nEntriesUsed;
203 pTemp =
p->pEntriesFree;
204 p->pEntriesFree = *((
char **)pTemp);
#define ABC_REALLOC(type, obj, num)
#define ABC_ALLOC(type, num)
void Mem_FixedEntryRecycle |
( |
Mem_Fixed_t * |
p, |
|
|
char * |
pEntry |
|
) |
| |
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 219 of file mem.c.
224 *((
char **)pEntry) =
p->pEntriesFree;
225 p->pEntriesFree = pEntry;
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 293 of file mem.c.
295 return p->nEntriesMax;
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 277 of file mem.c.
279 return p->nMemoryAlloc;
Function*************************************************************
Synopsis []
Description [Relocates all the memory except the first chunk.]
SideEffects []
SeeAlso []
Definition at line 239 of file mem.c.
245 for ( i = 1; i <
p->nChunks; i++ )
249 pTemp =
p->pChunks[0];
250 for ( i = 1; i <
p->nChunkSize; i++ )
252 *((
char **)pTemp) = pTemp +
p->nEntrySize;
253 pTemp +=
p->nEntrySize;
256 *((
char **)pTemp) = NULL;
258 p->pEntriesFree =
p->pChunks[0];
260 p->nMemoryAlloc =
p->nEntrySize *
p->nChunkSize;
262 p->nEntriesAlloc =
p->nChunkSize;
FUNCTION DEFINITIONS ///.
Function*************************************************************
Synopsis [Allocates memory pieces of fixed size.]
Description [The size of the chunk is computed as the minimum of 1024 entries and 64K. Can only work with entry size at least 4 byte long.]
SideEffects []
SeeAlso []
Definition at line 100 of file mem.c.
107 p->nEntrySize = nEntrySize;
108 p->nEntriesAlloc = 0;
110 p->pEntriesFree = NULL;
112 if ( nEntrySize * (1 << 10) < (1<<16) )
113 p->nChunkSize = (1 << 10);
115 p->nChunkSize = (1<<16) / nEntrySize;
116 if ( p->nChunkSize < 8 )
119 p->nChunksAlloc = 64;
121 p->pChunks =
ABC_ALLOC(
char *, p->nChunksAlloc );
#define ABC_ALLOC(type, num)
typedefABC_NAMESPACE_HEADER_START struct Mem_Fixed_t_ Mem_Fixed_t
DECLARATIONS ///.
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 139 of file mem.c.
146 printf(
"Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
147 p->nEntrySize,
p->nChunkSize,
p->nChunks );
148 printf(
" Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
149 p->nEntriesUsed,
p->nEntriesMax,
p->nEntrySize *
p->nEntriesUsed,
p->nMemoryAlloc );
151 for ( i = 0; i <
p->nChunks; i++ )
char* Mem_FlexEntryFetch |
( |
Mem_Flex_t * |
p, |
|
|
int |
nBytes |
|
) |
| |
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 372 of file mem.c.
#define ABC_REALLOC(type, obj, num)
#define ABC_ALLOC(type, num)
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 445 of file mem.c.
Function*************************************************************
Synopsis []
Description [Relocates all the memory except the first chunk.]
SideEffects []
SeeAlso []
Definition at line 417 of file mem.c.
423 for ( i = 1; i < p->
nChunks; i++ )
Function*************************************************************
Synopsis [Allocates entries of flexible size.]
Description [Can only work with entry size at least 4 byte long.]
SideEffects []
SeeAlso []
Definition at line 311 of file mem.c.
#define ABC_ALLOC(type, num)
void Mem_FlexStop |
( |
Mem_Flex_t * |
p, |
|
|
int |
fVerbose |
|
) |
| |
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 343 of file mem.c.
350 printf(
"Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
352 printf(
" Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
355 for ( i = 0; i < p->
nChunks; i++ )
char* Mem_StepEntryFetch |
( |
Mem_Step_t * |
p, |
|
|
int |
nBytes |
|
) |
| |
Function*************************************************************
Synopsis [Creates the entry.]
Description []
SideEffects []
SeeAlso []
Definition at line 537 of file mem.c.
#define ABC_REALLOC(type, obj, num)
#define ABC_ALLOC(type, num)
char * Mem_FixedEntryFetch(Mem_Fixed_t *p)
void Mem_StepEntryRecycle |
( |
Mem_Step_t * |
p, |
|
|
char * |
pEntry, |
|
|
int |
nBytes |
|
) |
| |
Function*************************************************************
Synopsis [Recycles the entry.]
Description []
SideEffects []
SeeAlso []
Definition at line 570 of file mem.c.
void Mem_FixedEntryRecycle(Mem_Fixed_t *p, char *pEntry)
Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
Definition at line 593 of file mem.c.
595 int i, nMemTotal = 0;
596 for ( i = 0; i < p->
nMems; i++ )
597 nMemTotal += p->
pMems[i]->nMemoryAlloc;
Function*************************************************************
Synopsis [Starts the hierarchical memory manager.]
Description [This manager can allocate entries of any size. Iternally they are mapped into the entries with the number of bytes equal to the power of 2. The smallest entry size is 8 bytes. The next one is 16 bytes etc. So, if the user requests 6 bytes, he gets 8 byte entry. If we asks for 25 bytes, he gets 32 byte entry etc. The input parameters "nSteps" says how many fixed memory managers are employed internally. Calling this procedure with nSteps equal to 10 results in 10 hierarchically arranged internal memory managers, which can allocate up to 4096 (1Kb) entries. Requests for larger entries are handed over to malloc() and then ABC_FREE()ed.]
SideEffects []
SeeAlso []
Definition at line 474 of file mem.c.
483 for ( i = 0; i < p->
nMems; i++ )
489 for ( k = 1; k <= 4; k++ )
492 for ( k = (4<<i)+1; k <= (8<<i); k++ )
#define ABC_ALLOC(type, num)
for(p=first;p->value< newval;p=p->next)
Mem_Fixed_t * Mem_FixedStart(int nEntrySize)
FUNCTION DEFINITIONS ///.
typedefABC_NAMESPACE_HEADER_START struct Mem_Fixed_t_ Mem_Fixed_t
DECLARATIONS ///.
void Mem_StepStop |
( |
Mem_Step_t * |
p, |
|
|
int |
fVerbose |
|
) |
| |
Function*************************************************************
Synopsis [Stops the memory manager.]
Description []
SideEffects []
SeeAlso []
Definition at line 510 of file mem.c.
513 for ( i = 0; i < p->
nMems; i++ )
void Mem_FixedStop(Mem_Fixed_t *p, int fVerbose)