diff --git a/Makefile b/Makefile index 1756300..e793ca7 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ OUTPATH=./build/ SOURCE_MODULES_PATH=./library/ #FOR DEBUG PURPOSE -#CFLAGSTEMP=-c -Wall -I. -g -O0 -CFLAGSTEMP=-c -Wall -I. +CFLAGSTEMP=-c -Wall -I. -g -O0 +#CFLAGSTEMP=-c -Wall -I. OPTIMIZATION=-O3 HEADERS=-I LIBPATH=./build/modules/ @@ -19,8 +19,8 @@ ENABLE_LARGE_FILE_SUPPORT=-D LARGE_FILE_SUPPORT_ENABLED -D _LARGEFILE64_SOURCE ENABLE_OPENSSL_SUPPORT= #TO ENABLE OPENSSL SUPPORT UNCOMMENT NEXT 2 LINES -ENABLE_OPENSSL_SUPPORT=-D OPENSSL_ENABLED -LIBS=-lpthread -lssl -lcrypto +#ENABLE_OPENSSL_SUPPORT=-D OPENSSL_ENABLED +#LIBS=-lpthread -lssl -lcrypto CFLAGS=$(CFLAGSTEMP) $(ENABLE_LARGE_FILE_SUPPORT) $(ENABLE_OPENSSL_SUPPORT) @@ -36,8 +36,8 @@ start: end: @echo Build process end -uFTP: uFTP.c fileManagement.o configRead.o logFunctions.o ftpCommandElaborate.o ftpData.o ftpServer.o daemon.o signals.o connection.o openSsl.o - @$(CC) $(ENABLE_LARGE_FILE_SUPPORT) $(ENABLE_OPENSSL_SUPPORT) uFTP.c $(LIBPATH)dynamicVectors.o $(LIBPATH)fileManagement.o $(LIBPATH)configRead.o $(LIBPATH)logFunctions.o $(LIBPATH)ftpCommandElaborate.o $(LIBPATH)ftpData.o $(LIBPATH)ftpServer.o $(LIBPATH)daemon.o $(LIBPATH)signals.o $(LIBPATH)connection.o $(LIBPATH)openSsl.o -o $(OUTPATH)uFTP $(LIBS) +uFTP: uFTP.c fileManagement.o configRead.o logFunctions.o ftpCommandElaborate.o ftpData.o ftpServer.o daemon.o signals.o connection.o openSsl.o dynamicMemory.o errorHandling.o + @$(CC) $(ENABLE_LARGE_FILE_SUPPORT) $(ENABLE_OPENSSL_SUPPORT) uFTP.c $(LIBPATH)dynamicVectors.o $(LIBPATH)fileManagement.o $(LIBPATH)configRead.o $(LIBPATH)logFunctions.o $(LIBPATH)ftpCommandElaborate.o $(LIBPATH)ftpData.o $(LIBPATH)ftpServer.o $(LIBPATH)daemon.o $(LIBPATH)signals.o $(LIBPATH)connection.o $(LIBPATH)openSsl.o $(LIBPATH)dynamicMemory.o $(LIBPATH)errorHandling.o -o $(OUTPATH)uFTP $(LIBS) daemon.o: @$(CC) $(CFLAGS) $(SOURCE_MODULES_PATH)daemon.c -o $(LIBPATH)daemon.o @@ -51,6 +51,12 @@ openSsl.o: configRead.o: dynamicVectors.o fileManagement.o @$(CC) $(CFLAGS) $(SOURCE_MODULES_PATH)configRead.c -o $(LIBPATH)configRead.o +dynamicMemory.o: + @$(CC) $(CFLAGS) $(SOURCE_MODULES_PATH)dynamicMemory.c -o $(LIBPATH)dynamicMemory.o + +errorHandling.o: + @$(CC) $(CFLAGS) $(SOURCE_MODULES_PATH)errorHandling.c -o $(LIBPATH)errorHandling.o + fileManagement.o: @$(CC) $(CFLAGS) $(SOURCE_MODULES_PATH)fileManagement.c -o $(LIBPATH)fileManagement.o diff --git a/build/modules/configRead.o b/build/modules/configRead.o index f60e28b..4cf54f2 100644 Binary files a/build/modules/configRead.o and b/build/modules/configRead.o differ diff --git a/build/modules/connection.o b/build/modules/connection.o index a9a96f5..dcce477 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/daemon.o b/build/modules/daemon.o index 1e60449..d372df3 100644 Binary files a/build/modules/daemon.o and b/build/modules/daemon.o differ diff --git a/build/modules/dynamicMemory.o b/build/modules/dynamicMemory.o new file mode 100644 index 0000000..a848e00 Binary files /dev/null and b/build/modules/dynamicMemory.o differ diff --git a/build/modules/dynamicVectors.o b/build/modules/dynamicVectors.o index 485add8..a8aca15 100644 Binary files a/build/modules/dynamicVectors.o and b/build/modules/dynamicVectors.o differ diff --git a/build/modules/errorHandling.o b/build/modules/errorHandling.o new file mode 100644 index 0000000..4df7f04 Binary files /dev/null and b/build/modules/errorHandling.o differ diff --git a/build/modules/fileManagement.o b/build/modules/fileManagement.o index 8bd8ca6..90fbc85 100644 Binary files a/build/modules/fileManagement.o and b/build/modules/fileManagement.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index ff40ebc..9150b06 100644 Binary files a/build/modules/ftpCommandElaborate.o and b/build/modules/ftpCommandElaborate.o differ diff --git a/build/modules/ftpData.o b/build/modules/ftpData.o index c330fb8..8ba997e 100644 Binary files a/build/modules/ftpData.o and b/build/modules/ftpData.o differ diff --git a/build/modules/ftpServer.o b/build/modules/ftpServer.o index 3b56810..4b62add 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/modules/logFunctions.o b/build/modules/logFunctions.o index ca9f615..a8ef6e7 100644 Binary files a/build/modules/logFunctions.o and b/build/modules/logFunctions.o differ diff --git a/build/modules/openSsl.o b/build/modules/openSsl.o index b85496d..ab3adde 100644 Binary files a/build/modules/openSsl.o and b/build/modules/openSsl.o differ diff --git a/build/modules/signals.o b/build/modules/signals.o index 66a36f9..5106668 100644 Binary files a/build/modules/signals.o and b/build/modules/signals.o differ diff --git a/build/uFTP b/build/uFTP index 876323d..d263c9a 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpData.c b/ftpData.c index f4df1eb..c63c812 100644 --- a/ftpData.c +++ b/ftpData.c @@ -636,7 +636,6 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) void resetClientData(ftpDataType *data, int clientId, int isInitialization) { - if (isInitialization != 1) { void *pReturn; @@ -650,7 +649,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) //SSL_free(data->clients[clientId].workerData.ssl); #endif } - + if (pthread_mutex_init(&data->clients[clientId].writeMutex, NULL) != 0) { printf("\nclientData->writeMutex init failed\n"); diff --git a/ftpData.h b/ftpData.h index 035b6fc..7391acb 100644 --- a/ftpData.h +++ b/ftpData.h @@ -30,6 +30,7 @@ #include #include #include "library/dynamicVectors.h" +#include "library/dynamicMemory.h" #define CLIENT_COMMAND_STRING_SIZE 4096 @@ -233,6 +234,7 @@ struct ftpData ipDataType serverIp; ftpParameters_DataType ftpParameters; DYNV_VectorGenericDataType loginFailsVector; + DYNMEM_MemoryTable_DataType *generalDynamicMemoryTable; } typedef ftpDataType; struct ftpListData diff --git a/ftpServer.c b/ftpServer.c index d6ec03b..4143f6f 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -42,6 +42,9 @@ #include "library/signals.h" #include "library/openSsl.h" #include "library/connection.h" +#include "library/dynamicMemory.h" +#include "library/errorHandling.h" + #include "ftpServer.h" #include "ftpData.h" #include "ftpCommandsElaborate.h" @@ -455,8 +458,7 @@ void runFtpServer(void) printf("\nHello uFTP server v%s starting..\n", UFTP_SERVER_VERSION); /* Needed for Select*/ - static int processingSock = 0, - returnCode = 0; + static int processingSock = 0, returnCode = 0; /* Handle signals */ signalHandlerInstall(); @@ -479,7 +481,6 @@ void runFtpServer(void) /* the maximum socket fd is now the main socket descriptor */ ftpData.connectionData.maxSocketFD = ftpData.connectionData.theMainSocket+1; - //Endless loop ftp process while (1) @@ -867,6 +868,15 @@ void deallocateMemory(void) { printf("\n Deallocating the memory.. "); + printf("\nDYNMEM_freeAll called"); + printf("\nElement size: %ld", ftpData.generalDynamicMemoryTable->size); + printf("\nElement address: %ld", (long int) ftpData.generalDynamicMemoryTable->address); + printf("\nElement nextElement: %ld",(long int) ftpData.generalDynamicMemoryTable->nextElement); + printf("\nElement previousElement: %ld",(long int) ftpData.generalDynamicMemoryTable->previousElement); + + DYNMEM_freeAll(&ftpData.generalDynamicMemoryTable); + //printf("\n ftpData.generalDynamicMemoryTable = %ld", ftpData.generalDynamicMemoryTable); + #ifdef OPENSSL_ENABLED SSL_CTX_free(ftpData.serverCtx); cleanupOpenssl(); diff --git a/library/configRead.c b/library/configRead.c index 47cc5d3..9cc8420 100644 --- a/library/configRead.c +++ b/library/configRead.c @@ -33,6 +33,7 @@ #include "openSsl.h" #include "fileManagement.h" #include "daemon.h" +#include "dynamicMemory.h" #define PARAMETER_SIZE_LIMIT 1024 @@ -43,8 +44,8 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet void destroyConfigurationVectorElement(void * data) { - free( ((parameter_DataType *) data)->value); - free( ((parameter_DataType *) data)->name); + free(((parameter_DataType *)data)->value); + free(((parameter_DataType *)data)->name); } /* Public Functions */ @@ -91,9 +92,8 @@ void configurationRead(ftpParameters_DataType *ftpParameters) exit(1); } - DYNV_VectorGeneric_Destroy(&configParameters, destroyConfigurationVectorElement); - + return; } @@ -123,6 +123,8 @@ void initFtpData(ftpDataType *ftpData) /* Intializes random number generator */ srand(time(NULL)); + ftpData->generalDynamicMemoryTable = NULL; + #ifdef OPENSSL_ENABLED initOpenssl(); ftpData->serverCtx = createServerContext(); @@ -132,7 +134,16 @@ void initFtpData(ftpDataType *ftpData) #endif ftpData->connectedClients = 0; - ftpData->clients = (clientDataType *) calloc( sizeof(clientDataType), ftpData->ftpParameters.maxClients); + ftpData->clients = (clientDataType *) DYNMEM_malloc((sizeof(clientDataType) * ftpData->ftpParameters.maxClients), &ftpData->generalDynamicMemoryTable); + + printf("\nDYNMEM_malloc called"); + printf("\nElement location: %ld", (long int) ftpData->generalDynamicMemoryTable); + fflush(0); + printf("\nElement size: %ld", ftpData->generalDynamicMemoryTable->size); + printf("\nElement address: %ld", (long int) ftpData->generalDynamicMemoryTable->address); + printf("\nElement nextElement: %ld",(long int) ftpData->generalDynamicMemoryTable->nextElement); + printf("\nElement previousElement: %ld",(long int) ftpData->generalDynamicMemoryTable->previousElement); + ftpData->serverIp.ip[0] = 127; ftpData->serverIp.ip[1] = 0; @@ -318,7 +329,6 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet valueIndex = 0; state = STATE_START; printf("\nParameter read: %s = %s", parameter.name, parameter.value); - parametersVector->PushBack(parametersVector, ¶meter, sizeof(parameter_DataType)); } break; @@ -344,7 +354,8 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet parametersVector->PushBack(parametersVector, ¶meter, sizeof(parameter_DataType)); } - if (theFileSize > 0) { + if (theFileSize > 0) + { free(theFileContent); } @@ -363,7 +374,6 @@ static int searchParameter(char *name, DYNV_VectorGenericDataType *parametersVec return i; } } - return returnCode; } diff --git a/library/dynamicMemory.c b/library/dynamicMemory.c index 342cca2..440e2f4 100644 --- a/library/dynamicMemory.c +++ b/library/dynamicMemory.c @@ -9,13 +9,27 @@ #include #include #include "dynamicMemory.h" +#include "errorHandling.h" //total memory allocated static unsigned long long int theTotalMemory; +static pthread_mutex_t memoryCountMutex; void DYNMEM_Init(void) { + static int state = 0; + + if(state != 0) + return; + + state = 1; + theTotalMemory = 0; + + if (pthread_mutex_init(&memoryCountMutex, NULL) != 0) + { + report_error_q("Unable to init the mutex", __FILE__, __LINE__, 0); + } } unsigned long long int DYNMEM_GetTotalMemory(void) @@ -23,96 +37,178 @@ unsigned long long int DYNMEM_GetTotalMemory(void) return theTotalMemory; } -unsigned long long int DYNMEM_IncreaseMemoryCounter(void) -{ - return theTotalMemory; -} -unsigned long long int DYNMEM_DecreaseMemoryCounter(void) +unsigned long long int DYNMEM_IncreaseMemoryCounter(unsigned long long int theSize) { + pthread_mutex_lock(&memoryCountMutex); + theTotalMemory = theTotalMemory + theSize; + pthread_mutex_unlock(&memoryCountMutex); return theTotalMemory; } -void * DYNMEM_malloc(size_t bytes) +unsigned long long int DYNMEM_DecreaseMemoryCounter(unsigned long long int theSize) +{ + pthread_mutex_lock(&memoryCountMutex); + theTotalMemory = theTotalMemory - theSize; + pthread_mutex_unlock(&memoryCountMutex); + return theTotalMemory; +} + +void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead) { void *memory = NULL; - - DYNMEM_MemoryTable_DataType *new_item = NULL; - + DYNMEM_MemoryTable_DataType *newItem = NULL; memory = calloc(bytes,1); - new_item = calloc(1,sizeof(struct memory_list)); + newItem = calloc(1, sizeof(DYNMEM_MemoryTable_DataType)); + + printf("Allocating new item in memory, size of %d", bytes); + if(memory) { - if(new_item == NULL) + if(newItem == NULL) { - report_error_q("Memory allocation error, no room for memory list item.", - __FILE__,__LINE__, 0); + report_error_q("Memory allocation error, no room for memory list item.",__FILE__,__LINE__, 0); } - global_memory_count += bytes + sizeof(struct memory_list); + DYNMEM_IncreaseMemoryCounter(bytes + sizeof(DYNMEM_MemoryTable_DataType)); - new_item->address = memory; - new_item->size = bytes; - new_item->next = NULL; - new_item->prev = NULL; - if(memory_list_head) { - new_item->next = memory_list_head; - memory_list_head->prev = new_item; - memory_list_head = new_item; - } else { - memory_list_head = new_item; + newItem->address = memory; + newItem->size = bytes; + newItem->nextElement = NULL; + newItem->previousElement = NULL; + + if( (*memoryListHead) != NULL) + { + newItem->nextElement = *memoryListHead; + (*memoryListHead)->previousElement = newItem; + (*memoryListHead) = newItem; + } + else + { + //printf("\nmemoryListHead = %ld", (int) *memoryListHead); + *memoryListHead = newItem; + //printf("\nmemoryListHead = newItem %ld", (int) *memoryListHead); + } + + //printf("\nElement size: %ld", (*memoryListHead)->size); + //printf("\nElement address: %ld", (long int) (*memoryListHead)->address); + //printf("\nElement nextElement: %ld",(long int) (*memoryListHead)->nextElement); + //printf("\nElement previousElement: %ld",(long int) (*memoryListHead)->previousElement); + + return memory; } - return memory; - } else { - report_error_q("Memory allocation error, out of memory.", - __FILE__,__LINE__,0); - return NULL; + else + { + report_error_q("Memory allocation error, out of memory.", __FILE__,__LINE__,0); + return NULL; } } -void DYNMEM_free(void *f_address) { -memory_list *temp = NULL,*found = NULL; -if(f_address == NULL) -return; -for(temp=memory_list_head;temp!=NULL;temp = temp->next) { -if(temp->address == f_address) { -found = temp; -break; -} -} -if(!found) { -report_error_q("Unable to free memory not previously allocated", -__FILE__,__LINE__,0); -// Report this as an error -} -global_memory_count -= found->size + sizeof(struct memory_list); -free(f_address); -if(found->prev) -found->prev->next = found->next; -if(found->next) -found->next->prev = found->prev; -if(found == memory_list_head) -memory_list_head = found->next; -free(found); +void *DYNMEM_realloc(void *theMemoryAddress, size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead) +{ + void *newMemory = NULL; + newMemory = realloc(theMemoryAddress, bytes); + + //printf("Reallocating item in memory, size of %d", bytes); + + if(newMemory) + { + DYNMEM_MemoryTable_DataType *temp = NULL,*found = NULL; + for( temp = (*memoryListHead); temp!=NULL; temp = temp->nextElement) + { + if(temp->address == theMemoryAddress) + { + found = temp; + break; + } + } + + if(!found) + { + report_error_q("Unable to free memory not previously allocated",__FILE__,__LINE__, 0); + // Report this as an error + } + + if (found->size > bytes) + { + DYNMEM_DecreaseMemoryCounter((found->size-bytes)); + } + else if (found->size < bytes) + { + DYNMEM_IncreaseMemoryCounter((bytes-found->size)); + } + + found->address = newMemory; + found->size = bytes; + + //printf("\nElement size: %ld", (*found)->size); + //printf("\nElement address: %ld", (long int) (*found)->address); + //printf("\nElement nextElement: %ld",(long int) (*found)->nextElement); + //printf("\nElement previousElement: %ld",(long int) (*found)->previousElement); + + return newMemory; + } + else + { + report_error_q("Memory reallocation error, out of memory.", __FILE__,__LINE__,0); + return NULL; + } } +void DYNMEM_free(void *f_address, DYNMEM_MemoryTable_DataType ** memoryListHead) +{ + DYNMEM_MemoryTable_DataType *temp = NULL,*found = NULL; -void DYNMEM_freeAll(void) { -memory_list *temp = NULL; -while(memory_list_head) { -free(memory_list_head->address); -309Chapter 13 -temp = memory_list_head->next; -free(memory_list_head); -memory_list_head = temp; -} -} -void DYNMEM_memoryInit(void) { -static int state = 0; -if(state != 0) -return; -state = 1; -memory_list_head = NULL; -global_memory_count = 0; -atexit(w_free_all); + if(f_address == NULL) + return; + + for(temp=(*memoryListHead); temp!=NULL; temp = temp->nextElement) + { + if(temp->address == f_address) + { + found = temp; + break; + } + } + + if(!found) + { + //Debug TRAP + //char *theData ="c"; + //strcpy(theData, "ciaociaociao"); + report_error_q("Unable to free memory not previously allocated",__FILE__,__LINE__, 1); + // Report this as an error + } + + DYNMEM_DecreaseMemoryCounter(found->size + sizeof(DYNMEM_MemoryTable_DataType)); + + free(f_address); + if(found->previousElement) + found->previousElement->nextElement = found->nextElement; + + if(found->nextElement) + found->nextElement->previousElement = found->previousElement; + + if(found == (*memoryListHead)) + (*memoryListHead) = found->nextElement; + + free(found); } +void DYNMEM_freeAll(DYNMEM_MemoryTable_DataType **memoryListHead) +{ + printf("\nDYNMEM_freeAll called"); + printf("\nElement size: %ld", (*memoryListHead)->size); + printf("\nElement address: %ld", (long int) (*memoryListHead)->address); + printf("\nElement nextElement: %ld",(long int) (*memoryListHead)->nextElement); + printf("\nElement previousElement: %ld",(long int) (*memoryListHead)->previousElement); + + DYNMEM_MemoryTable_DataType *temp = NULL; + while((*memoryListHead) != NULL) + { + printf("\nFree table element"); + free((*memoryListHead)->address); + temp = (*memoryListHead)->nextElement; + free((*memoryListHead)); + (*memoryListHead) = temp; + } +} diff --git a/library/dynamicMemory.h b/library/dynamicMemory.h index 6a7f29b..c48c99b 100644 --- a/library/dynamicMemory.h +++ b/library/dynamicMemory.h @@ -14,18 +14,17 @@ typedef struct DYNMEM_MemoryTable_DataType size_t size; struct DYNMEM_MemoryTable_DataType *nextElement; struct DYNMEM_MemoryTable_DataType *previousElement; -}; - -void DYNMEM_Init(void); +} DYNMEM_MemoryTable_DataType; unsigned long long int DYNMEM_GetTotalMemory(void); -unsigned long long int DYNMEM_IncreaseMemoryCounter(void); -unsigned long long int DYNMEM_DecreaseMemoryCounter(void); +unsigned long long int DYNMEM_IncreaseMemoryCounter(unsigned long long int theSize); +unsigned long long int DYNMEM_DecreaseMemoryCounter(unsigned long long int theSize); +void DYNMEM_Init(void); +void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType ** memoryListHead); +void *DYNMEM_realloc(void *theMemoryAddress, size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead); +void DYNMEM_free(void *f_address, DYNMEM_MemoryTable_DataType ** memoryListHead); +void DYNMEM_freeAll(DYNMEM_MemoryTable_DataType ** memoryListHead); -void *DYNMEM_malloc(size_t bytes); -void DYNMEM_free(void *f_address); -void DYNMEM_freeAll(void); -void DYNMEM_memoryInit(void); #endif /* LIBRARY_DYNAMICMEMORY_H_ */ diff --git a/library/dynamicVectors.c b/library/dynamicVectors.c index afa1758..c34f84a 100644 --- a/library/dynamicVectors.c +++ b/library/dynamicVectors.c @@ -28,12 +28,14 @@ #include #include "dynamicVectors.h" +#include "dynamicMemory.h" void DYNV_VectorGeneric_Init(DYNV_VectorGenericDataType *TheVectorGeneric) { TheVectorGeneric->Size = 0; TheVectorGeneric->Data = NULL; TheVectorGeneric->ElementSize = NULL; + TheVectorGeneric->memoryTable = NULL; //Functions Pointers TheVectorGeneric->DeleteAt = (void *)DYNV_VectorGeneric_DeleteAt; @@ -52,28 +54,25 @@ void DYNV_VectorGeneric_InitWithSearchFunction(DYNV_VectorGenericDataType *TheVe void DYNV_VectorGeneric_PushBack(DYNV_VectorGenericDataType *TheVectorGeneric, void * TheElementData, int TheElementSize) { - - //printf("\nPush back Address of TheVector = %lX", TheVectorGeneric); - if (TheVectorGeneric->Data != NULL) { - TheVectorGeneric->Data = (void **) realloc(TheVectorGeneric->Data, sizeof(void *) * (TheVectorGeneric->Size+1)); + TheVectorGeneric->Data = (void **) DYNMEM_realloc(TheVectorGeneric->Data, sizeof(void *) * (TheVectorGeneric->Size+1), &TheVectorGeneric->memoryTable); } else { - TheVectorGeneric->Data = (void **) malloc (sizeof(void *) * (TheVectorGeneric->Size+1)); + TheVectorGeneric->Data = (void **) DYNMEM_malloc (sizeof(void *) * (TheVectorGeneric->Size+1), &TheVectorGeneric->memoryTable); } if (TheVectorGeneric->ElementSize != NULL) { - TheVectorGeneric->ElementSize = (int *) realloc (TheVectorGeneric->ElementSize, sizeof(int) * (TheVectorGeneric->Size+1)); + TheVectorGeneric->ElementSize = (int *) DYNMEM_realloc (TheVectorGeneric->ElementSize, sizeof(int) * (TheVectorGeneric->Size+1), &TheVectorGeneric->memoryTable); } else { - TheVectorGeneric->ElementSize = (int *) malloc (sizeof(int)); + TheVectorGeneric->ElementSize = (int *) DYNMEM_malloc (sizeof(int), &TheVectorGeneric->memoryTable); } - TheVectorGeneric->Data[TheVectorGeneric->Size] = (void *) calloc(1, TheElementSize); + TheVectorGeneric->Data[TheVectorGeneric->Size] = (void *) DYNMEM_malloc(TheElementSize, &TheVectorGeneric->memoryTable); memcpy(TheVectorGeneric->Data[TheVectorGeneric->Size], TheElementData, TheElementSize); TheVectorGeneric->ElementSize[TheVectorGeneric->Size] = TheElementSize; TheVectorGeneric->Size++; @@ -82,16 +81,16 @@ void DYNV_VectorGeneric_PushBack(DYNV_VectorGenericDataType *TheVectorGeneric, v void DYNV_VectorGeneric_PopBack(DYNV_VectorGenericDataType *TheVector, void (*DeleteElementFunction)(void *TheElementToDelete)) { DeleteElementFunction((void *) TheVector->Data[TheVector->Size-1]); - free(TheVector->Data[TheVector->Size-1]); + DYNMEM_free(TheVector->Data[TheVector->Size-1], &TheVector->memoryTable); if (TheVector->Size > 1) { - TheVector->Data = (void **) realloc(TheVector->Data, sizeof(void *) * (TheVector->Size-1)); - TheVector->ElementSize = (int *) realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size-1)); + TheVector->Data = (void **) DYNMEM_realloc(TheVector->Data, sizeof(void *) * (TheVector->Size-1), &TheVector->memoryTable); + TheVector->ElementSize = (int *) DYNMEM_realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size-1), &TheVector->memoryTable); } else { - free(TheVector->Data); - free(TheVector->ElementSize); + DYNMEM_free(TheVector->Data, &TheVector->memoryTable); + DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable); TheVector->Data = NULL; TheVector->ElementSize = NULL; } @@ -101,17 +100,17 @@ void DYNV_VectorGeneric_PopBack(DYNV_VectorGenericDataType *TheVector, void (*De void DYNV_VectorGeneric_SoftPopBack(DYNV_VectorGenericDataType *TheVector) { - free(TheVector->Data[TheVector->Size-1]); + DYNMEM_free(TheVector->Data[TheVector->Size-1], &TheVector->memoryTable); if (TheVector->Size > 1) { - TheVector->Data = (void **) realloc(TheVector->Data, sizeof(void *) * (TheVector->Size-1)); - TheVector->ElementSize = (int *) realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size-1)); + TheVector->Data = (void **) DYNMEM_realloc(TheVector->Data, sizeof(void *) * (TheVector->Size-1), &TheVector->memoryTable); + TheVector->ElementSize = (int *) DYNMEM_realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size-1), &TheVector->memoryTable); } else { - free(TheVector->Data); - free(TheVector->ElementSize); + DYNMEM_free(TheVector->Data, &TheVector->memoryTable); + DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable); TheVector->Data = NULL; TheVector->ElementSize = NULL; } @@ -124,12 +123,14 @@ void DYNV_VectorGeneric_Destroy(DYNV_VectorGenericDataType *TheVector, void (*De int i; for (i = 0; i < TheVector->Size; i++) { + printf("\n Deleting element : %d", i); + fflush(0); DeleteElementFunction((void *) TheVector->Data[i]); - free(TheVector->Data[i]); + DYNMEM_free(TheVector->Data[i], &TheVector->memoryTable); } - free(TheVector->Data); - free(TheVector->ElementSize); + DYNMEM_free(TheVector->Data, &TheVector->memoryTable); + DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable); TheVector->Data = NULL; TheVector->ElementSize = NULL; TheVector->Size = 0; @@ -140,10 +141,10 @@ void DYNV_VectorGeneric_SoftDestroy(DYNV_VectorGenericDataType *TheVector) int i; for (i = 0; i < TheVector->Size; i++) { - free(TheVector->Data[i]); + DYNMEM_free(TheVector->Data[i], &TheVector->memoryTable); } - free(TheVector->Data); - free(TheVector->ElementSize); + DYNMEM_free(TheVector->Data, &TheVector->memoryTable); + DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable); TheVector->Data = NULL; TheVector->ElementSize = NULL; @@ -182,6 +183,7 @@ void DYNV_VectorString_Init(DYNV_VectorString_DataType *TheVector) TheVector->Size = 0; TheVector->Data = NULL; TheVector->ElementSize = NULL; + TheVector->memoryTable = NULL; //Functions Pointers TheVector->DeleteAt = (void *)DYNV_VectorString_DeleteAt; @@ -196,23 +198,23 @@ void DYNV_VectorString_PushBack(DYNV_VectorString_DataType *TheVector, char * Th if (TheVector->Data != NULL) { - TheVector->Data = (char **)realloc(TheVector->Data, sizeof(char *) * (TheVector->Size+1)); + TheVector->Data = (char **)DYNMEM_realloc(TheVector->Data, sizeof(char *) * (TheVector->Size+1), &TheVector->memoryTable); } else { - TheVector->Data = (char **) malloc (sizeof(char *) * (TheVector->Size+1)); + TheVector->Data = (char **) DYNMEM_malloc (sizeof(char *) * (TheVector->Size+1), &TheVector->memoryTable); } if (TheVector->ElementSize != NULL) { - TheVector->ElementSize = (int *) realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size+1)); + TheVector->ElementSize = (int *) DYNMEM_realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size+1), &TheVector->memoryTable); } else { - TheVector->ElementSize = (int *) malloc (sizeof(int) * 1); + TheVector->ElementSize = (int *) DYNMEM_malloc (sizeof(int) * 1, &TheVector->memoryTable); } - TheVector->Data[TheVector->Size] = (char *) calloc(sizeof(char), StringLenght + 1); + TheVector->Data[TheVector->Size] = (char *) DYNMEM_malloc((StringLenght + 1), &TheVector->memoryTable); for (i = 0; i < StringLenght; i++ ) { @@ -226,17 +228,17 @@ void DYNV_VectorString_PushBack(DYNV_VectorString_DataType *TheVector, char * Th void DYNV_VectorString_PopBack(DYNV_VectorString_DataType *TheVector) { - free(TheVector->Data[TheVector->Size-1]); + DYNMEM_free(TheVector->Data[TheVector->Size-1], &TheVector->memoryTable); if (TheVector->Size > 1) { - TheVector->Data = (char **)realloc(TheVector->Data, sizeof(char *) * (TheVector->Size-1)); - TheVector->ElementSize = (int *) realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size-1)); + TheVector->Data = (char **)DYNMEM_realloc(TheVector->Data, sizeof(char *) * (TheVector->Size-1), &TheVector->memoryTable); + TheVector->ElementSize = (int *) DYNMEM_realloc (TheVector->ElementSize, sizeof(int) * (TheVector->Size-1), &TheVector->memoryTable); } else { - free(TheVector->Data); - free(TheVector->ElementSize); + DYNMEM_free(TheVector->Data, &TheVector->memoryTable); + DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable); TheVector->Data = NULL; TheVector->ElementSize = NULL; } @@ -249,14 +251,14 @@ void DYNV_VectorString_Destroy(DYNV_VectorString_DataType *TheVector) int i; for (i = 0; i < TheVector->Size; i++) { - free(TheVector->Data[i]); + DYNMEM_free(TheVector->Data[i], &TheVector->memoryTable); } if (TheVector->Data != NULL) - free(TheVector->Data); + DYNMEM_free(TheVector->Data, &TheVector->memoryTable); if (TheVector->ElementSize != NULL) - free(TheVector->ElementSize); + DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable); TheVector->Data = NULL; TheVector->ElementSize = NULL; diff --git a/library/dynamicVectors.h b/library/dynamicVectors.h index f1bcdbf..015f866 100644 --- a/library/dynamicVectors.h +++ b/library/dynamicVectors.h @@ -25,12 +25,15 @@ #ifndef DYNAMIC_VECTORS_H #define DYNAMIC_VECTORS_H +#include "dynamicMemory.h" + #ifdef __cplusplus extern "C" { #endif struct DYNV_VectorString { + DYNMEM_MemoryTable_DataType *memoryTable; char **Data; int Size; int *ElementSize; @@ -42,6 +45,7 @@ struct DYNV_VectorString struct DYNV_VectorGenericDataStruct { + DYNMEM_MemoryTable_DataType *memoryTable; void **Data; int Size; int *ElementSize; diff --git a/library/errorHandling.c b/library/errorHandling.c index 85fbee3..d4a4459 100644 --- a/library/errorHandling.c +++ b/library/errorHandling.c @@ -13,7 +13,6 @@ void report_error(const char *msg, const char *file, int line_no, int use_perror) { - fprintf(stderr,"[%s:%d] ",file,line_no); if(use_perror != 0) @@ -28,6 +27,6 @@ void report_error(const char *msg, const char *file, int line_no, int use_perror void report_error_q(const char *msg, const char *file, int line_no, int use_perror) { - report_error(msg,file,line_no,use_perror); + report_error(msg, file, line_no, use_perror); exit(EXIT_FAILURE); } diff --git a/library/errorHandling.h b/library/errorHandling.h index 87359da..11c1986 100644 --- a/library/errorHandling.h +++ b/library/errorHandling.h @@ -9,8 +9,6 @@ #define LIBRARY_ERRORHANDLING_H_ void report_error_q(const char *msg, const char *file,int line_no, int use_perror); -void report_error(const char *msg, const char *file, int line_no, intuse_perror); - - +void report_error(const char *msg, const char *file, int line_no, int use_perror); #endif /* LIBRARY_ERRORHANDLING_H_ */