2 /*-----------------------------------------------------------------------
\r
3 File : MemoryManager.cpp
\r
4 Function : Reserves large blocks of memory and gives pointers to small
\r
5 portions of that block when requested.
\r
6 This improves performance since a unique "LARGE ALLOCATION"
\r
7 of memory is needed (a unique call to malloc).
\r
8 It is also responsible of freeing memory.
\r
9 Last change: 10/03/2004
\r
10 Purpose : Improve hash performance.
\r
11 ------------------------------------------------------------------------*/
\r
12 #include "MemoryManager.h"
\r
15 /*------------------------------------------------------------------
\r
17 ------------------------------------------------------------------ */
\r
18 MemoryManager createMemoryManager(void) {
\r
20 mm = (MemoryManager) malloc (sizeof(struct sMem));
\r
22 createNewMemoryBlock(mm);
\r
26 /*------------------------------------------------------------------
\r
28 ------------------------------------------------------------------ */
\r
29 void destroyMemoryManager (MemoryManager mm){
\r
31 for (i=0; i<=mm->currentBlock;i++) free(mm->BLOCKS[i]);
\r
32 printf("\n[destroying MemManager] ...Freed %u bytes... RAM", LARGE_BLOCK_SIZE* (mm->currentBlock+1));
\r
37 /*------------------------------------------------------------------
\r
38 createNewMemoryBlock method
\r
39 Allocates a new memory block of size "LARGE_BLOCK_SIZE" and adds it to
\r
41 ------------------------------------------------------------------ */
\r
43 void createNewMemoryBlock (MemoryManager mm) {
\r
44 mm->BLOCKS[mm->currentBlock] = (byte *) malloc (LARGE_BLOCK_SIZE);
\r
46 if (mm->BLOCKS[mm->currentBlock] == NULL) {
\r
47 fprintf(stderr, "\nERROR...\nUnable to allocate enough memory. Exitting...\n");
\r
51 mm->remainderBytes = LARGE_BLOCK_SIZE;
\r
52 mm->availableByte = mm->BLOCKS[mm->currentBlock]; //points to the begining of the block
\r
56 /*------------------------------------------------------------------
\r
58 returns a pointer to a free block of memory of size "size"
\r
59 ------------------------------------------------------------------ */
\r
60 void getMemoryBlock (MemoryManager mm, byte **dst, const unsigned int size) {
\r
61 if (mm->remainderBytes < size) {
\r
63 createNewMemoryBlock(mm);
\r
64 //fprintf(stderr,"\new memory block");
\r
67 *dst = mm->availableByte; //points to a free size-block
\r
68 mm->remainderBytes -= (size);
\r
69 mm->availableByte += (size);
\r
73 /*------------------------------------------------------------------
\r
74 main, to make unit proofs
\r
75 ------------------------------------------------------------------ */
\r
77 int main(int argc, char* argv[])
\r
78 { byte *word, *word2;
\r
81 MemoryManager memMgr;
\r
82 memMgr=createMemoryManager();
\r
85 getMemoryBlock(memMgr,&word,size);
\r
87 fprintf(stderr,"pasei getblock\n");
\r
88 strcpy((char *)(word), "01234567890123456789012345678901234567890123456789");
\r
90 fprintf(stderr,"pasei strcpy \n");
\r
91 fprintf(stderr,"\n%s",word);
\r
92 getMemoryBlock(memMgr,&word2,size);
\r
93 strcpy((char *)(word2), "soy la word 2");
\r
95 for (i=0;i<100000;i++) {
\r
97 getMemoryBlock(memMgr,&word,size);
\r
100 fprintf(stderr,"\n final %s",word2);
\r
102 destroyMemoryManager(memMgr);
\r