--- /dev/null
+\r
+/*-----------------------------------------------------------------------\r
+ File : MemoryManager.cpp\r
+ Function : Reserves large blocks of memory and gives pointers to small\r
+ portions of that block when requested.\r
+ This improves performance since a unique "LARGE ALLOCATION"\r
+ of memory is needed (a unique call to malloc).\r
+ It is also responsible of freeing memory.\r
+ Last change: 10/03/2004\r
+ Purpose : Improve hash performance.\r
+ ------------------------------------------------------------------------*/\r
+#include "MemoryManager.h"\r
+\r
+\r
+/*------------------------------------------------------------------\r
+ Constructor method\r
+ ------------------------------------------------------------------ */\r
+MemoryManager createMemoryManager(void) {\r
+ MemoryManager mm;\r
+ mm = (MemoryManager) malloc (sizeof(struct sMem));\r
+ mm->currentBlock=0;\r
+ createNewMemoryBlock(mm); \r
+ return mm;\r
+}\r
+\r
+/*------------------------------------------------------------------\r
+ Destructor method\r
+ ------------------------------------------------------------------ */\r
+void destroyMemoryManager (MemoryManager mm){\r
+ register int i;\r
+ for (i=0; i<=mm->currentBlock;i++) free(mm->BLOCKS[i]);\r
+ printf("\n[destroying MemManager] ...Freed %u bytes... RAM", LARGE_BLOCK_SIZE* (mm->currentBlock+1));\r
+ free(mm);\r
+\r
+}\r
+\r
+/*------------------------------------------------------------------\r
+ createNewMemoryBlock method\r
+ Allocates a new memory block of size "LARGE_BLOCK_SIZE" and adds it to\r
+ vector BLOCKS\r
+ ------------------------------------------------------------------ */\r
+\r
+void createNewMemoryBlock (MemoryManager mm) {\r
+ mm->BLOCKS[mm->currentBlock] = (byte *) malloc (LARGE_BLOCK_SIZE);\r
+\r
+ if (mm->BLOCKS[mm->currentBlock] == NULL) {\r
+ fprintf(stderr, "\nERROR...\nUnable to allocate enough memory. Exitting...\n");\r
+ exit(0);\r
+ }\r
+\r
+ mm->remainderBytes = LARGE_BLOCK_SIZE;\r
+ mm->availableByte = mm->BLOCKS[mm->currentBlock]; //points to the begining of the block\r
+}\r
+\r
+\r
+/*------------------------------------------------------------------\r
+ getBlock method\r
+ returns a pointer to a free block of memory of size "size"\r
+ ------------------------------------------------------------------ */\r
+void getMemoryBlock (MemoryManager mm, byte **dst, const unsigned int size) {\r
+ if (mm->remainderBytes < size) {\r
+ mm->currentBlock++;\r
+ createNewMemoryBlock(mm);\r
+ //fprintf(stderr,"\new memory block");\r
+ }\r
+\r
+ *dst = mm->availableByte; //points to a free size-block\r
+ mm->remainderBytes -= (size);\r
+ mm->availableByte += (size);\r
+}\r
+\r
+\r
+/*------------------------------------------------------------------\r
+ main, to make unit proofs\r
+ ------------------------------------------------------------------ */\r
+/*\r
+int main(int argc, char* argv[])\r
+{ byte *word, *word2;\r
+ unsigned int size;\r
+int i;\r
+ MemoryManager memMgr;\r
+ memMgr=createMemoryManager();\r
+\r
+ size = 100;\r
+ getMemoryBlock(memMgr,&word,size);\r
+\r
+ fprintf(stderr,"pasei getblock\n");\r
+ strcpy((char *)(word), "01234567890123456789012345678901234567890123456789");\r
+ word[50]='\0';\r
+ fprintf(stderr,"pasei strcpy \n");\r
+ fprintf(stderr,"\n%s",word);\r
+ getMemoryBlock(memMgr,&word2,size);\r
+ strcpy((char *)(word2), "soy la word 2");\r
+\r
+ for (i=0;i<100000;i++) {\r
+ size = 89;\r
+ getMemoryBlock(memMgr,&word,size);\r
+ }\r
+\r
+ fprintf(stderr,"\n final %s",word2);\r
+\r
+ destroyMemoryManager(memMgr);\r
+\r
+} */\r
+\r