Create branch new-trunk
[SXSI/TextCollection.git] / swcsa / utils / MemoryManager.c
1 \r
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
13 \r
14 \r
15 /*------------------------------------------------------------------\r
16  Constructor method\r
17  ------------------------------------------------------------------ */\r
18 MemoryManager createMemoryManager(void) {\r
19         MemoryManager mm;\r
20         mm = (MemoryManager) malloc (sizeof(struct sMem));\r
21         mm->currentBlock=0;\r
22         createNewMemoryBlock(mm);       \r
23         return mm;\r
24 }\r
25 \r
26 /*------------------------------------------------------------------\r
27  Destructor method\r
28  ------------------------------------------------------------------ */\r
29 void destroyMemoryManager (MemoryManager mm){\r
30         register int i;\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
33         free(mm);\r
34 \r
35 }\r
36 \r
37 /*------------------------------------------------------------------\r
38  createNewMemoryBlock method\r
39  Allocates a new memory block of size "LARGE_BLOCK_SIZE" and adds it to\r
40  vector BLOCKS\r
41  ------------------------------------------------------------------ */\r
42 \r
43 void createNewMemoryBlock (MemoryManager mm) {\r
44         mm->BLOCKS[mm->currentBlock] = (byte *) malloc (LARGE_BLOCK_SIZE);\r
45 \r
46         if (mm->BLOCKS[mm->currentBlock] == NULL) {\r
47                 fprintf(stderr, "\nERROR...\nUnable to allocate enough memory. Exitting...\n");\r
48                 exit(0);\r
49         }\r
50 \r
51         mm->remainderBytes = LARGE_BLOCK_SIZE;\r
52         mm->availableByte       = mm->BLOCKS[mm->currentBlock];  //points to the begining of the block\r
53 }\r
54 \r
55 \r
56 /*------------------------------------------------------------------\r
57  getBlock method\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
62                 mm->currentBlock++;\r
63                 createNewMemoryBlock(mm);\r
64                 //fprintf(stderr,"\new memory block");\r
65         }\r
66 \r
67         *dst = mm->availableByte;                       //points to a free size-block\r
68         mm->remainderBytes -= (size);\r
69         mm->availableByte  += (size);\r
70 }\r
71 \r
72 \r
73 /*------------------------------------------------------------------\r
74  main, to make unit proofs\r
75  ------------------------------------------------------------------ */\r
76 /*\r
77 int main(int argc, char* argv[])\r
78 {  byte *word, *word2;\r
79         unsigned int size;\r
80 int i;\r
81         MemoryManager memMgr;\r
82         memMgr=createMemoryManager();\r
83 \r
84         size = 100;\r
85         getMemoryBlock(memMgr,&word,size);\r
86 \r
87         fprintf(stderr,"pasei getblock\n");\r
88         strcpy((char *)(word), "01234567890123456789012345678901234567890123456789");\r
89         word[50]='\0';\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
94 \r
95         for (i=0;i<100000;i++) {\r
96                 size = 89;\r
97                 getMemoryBlock(memMgr,&word,size);\r
98         }\r
99 \r
100         fprintf(stderr,"\n  final %s",word2);\r
101 \r
102         destroyMemoryManager(memMgr);\r
103 \r
104 } */\r
105 \r