Working on memory dynamic allocation

This commit is contained in:
Ugo Cirmignani
2018-12-23 19:37:29 +01:00
parent 29a065a480
commit dab352a26a
13 changed files with 42 additions and 26 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -153,6 +153,7 @@ struct workerData
ftpCommandDataType ftpCommand;
DYNV_VectorGenericDataType directoryInfo;
FILE *theStorFile;
DYNMEM_MemoryTable_DataType *memoryTable;
} typedef workerDataType;
struct clientData
@ -205,6 +206,8 @@ struct clientData
unsigned long long int connectionTimeStamp;
unsigned long long int lastActivityTimeStamp;
DYNMEM_MemoryTable_DataType *memoryTable;
} typedef clientDataType;
struct loginFails
@ -271,7 +274,7 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization);
void resetClientData(ftpDataType *data, int clientId, int isInitialization);
int compareStringCaseInsensitive(char *stringIn, char* stringRef, int stringLenght);
int isCharInString(char *theString, int stringLen, char theChar);
void destroyConfigurationVectorElement(void * data);
void destroyConfigurationVectorElement(DYNV_VectorGenericDataType *theVector);
#ifdef __cplusplus
}
#endif

View File

@ -42,10 +42,17 @@ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_Ve
static int searchParameter(char *name, DYNV_VectorGenericDataType *parametersVector);
static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *parametersVector);
void destroyConfigurationVectorElement(void * data)
void destroyConfigurationVectorElement(DYNV_VectorGenericDataType *theVector)
{
free(((parameter_DataType *)data)->value);
free(((parameter_DataType *)data)->name);
int i;
for (i = 0; i < theVector->Size; i++)
{
printf("\n(parameter_DataType *)theVector->Data[%d])->value = %ld", i, ((parameter_DataType *)theVector->Data)->value);
DYNMEM_free(((parameter_DataType *)theVector->Data[i])->value, &theVector->memoryTable);
printf("\n(parameter_DataType *)theVector->Data[%d])->name = %ld", i, ((parameter_DataType *)theVector->Data)->name);
DYNMEM_free(((parameter_DataType *)theVector->Data[i])->name, &theVector->memoryTable);
DYNMEM_free(theVector->Data[i], &theVector->memoryTable);
}
}
/* Public Functions */
@ -93,6 +100,7 @@ void configurationRead(ftpParameters_DataType *ftpParameters)
}
DYNV_VectorGeneric_Destroy(&configParameters, destroyConfigurationVectorElement);
printf("\n\nconfigParameters.memoryTable = %d ***", configParameters.memoryTable);
return;
}
@ -317,8 +325,8 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet
case STATE_STORE:
{
parameter_DataType parameter;
parameter.name = malloc(nameIndex+1);
parameter.value = malloc(valueIndex+1);
parameter.name = DYNMEM_malloc(nameIndex+1, &parametersVector->memoryTable);
parameter.value = DYNMEM_malloc(valueIndex+1, &parametersVector->memoryTable);
strcpy(parameter.name, name);
strcpy(parameter.value, value);
parameter.name[nameIndex] = '\0';
@ -340,8 +348,8 @@ static int readConfigurationFile(char *path, DYNV_VectorGenericDataType *paramet
valueIndex > 0)
{
parameter_DataType parameter;
parameter.name = malloc(nameIndex+1);
parameter.value = malloc(valueIndex+1);
parameter.name = DYNMEM_malloc(nameIndex+1, &parametersVector->memoryTable);
parameter.value = DYNMEM_malloc(valueIndex+1, &parametersVector->memoryTable);
strcpy(parameter.name, name);
strcpy(parameter.value, value);
parameter.name[nameIndex] = '\0';
@ -569,9 +577,13 @@ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_Ve
userData.ownerShip.groupOwnerString = NULL;
userData.ownerShip.userOwnerString = NULL;
userData.name = malloc(strlen(((parameter_DataType *) parametersVector->Data[searchUserIndex])->value) + 1);
userData.password = malloc(strlen(((parameter_DataType *) parametersVector->Data[searchPasswordIndex])->value) + 1);
userData.homePath = malloc(strlen(((parameter_DataType *) parametersVector->Data[searchHomeIndex])->value) + 1);
userData.name = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchUserIndex])->value) + 1), &ftpParameters->usersVector.memoryTable);
userData.password = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchPasswordIndex])->value) + 1), &ftpParameters->usersVector.memoryTable);
userData.homePath = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchHomeIndex])->value) + 1), &ftpParameters->usersVector.memoryTable);
strcpy(userData.name, ((parameter_DataType *) parametersVector->Data[searchUserIndex])->value);
strcpy(userData.password, ((parameter_DataType *) parametersVector->Data[searchPasswordIndex])->value);
@ -584,8 +596,8 @@ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_Ve
if (searchUserOwnerIndex != -1 &&
searchGroupOwnerIndex != -1)
{
userData.ownerShip.groupOwnerString = malloc(strlen(((parameter_DataType *) parametersVector->Data[searchGroupOwnerIndex])->value) + 1);
userData.ownerShip.userOwnerString = malloc(strlen(((parameter_DataType *) parametersVector->Data[searchUserOwnerIndex])->value) + 1);
userData.ownerShip.groupOwnerString = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchGroupOwnerIndex])->value) + 1), &ftpParameters->usersVector.memoryTable);
userData.ownerShip.userOwnerString = DYNMEM_malloc((strlen(((parameter_DataType *) parametersVector->Data[searchUserOwnerIndex])->value) + 1), &ftpParameters->usersVector.memoryTable);
strcpy(userData.ownerShip.groupOwnerString, ((parameter_DataType *) parametersVector->Data[searchGroupOwnerIndex])->value);
strcpy(userData.ownerShip.userOwnerString, ((parameter_DataType *) parametersVector->Data[searchUserOwnerIndex])->value);

View File

@ -90,7 +90,7 @@ void *DYNMEM_malloc(size_t bytes, DYNMEM_MemoryTable_DataType **memoryListHead)
}
//printf("\nElement size: %ld", (*memoryListHead)->size);
//printf("\nElement address: %ld", (long int) (*memoryListHead)->address);
printf("\nElement address: %ld", (long int) (*memoryListHead)->address);
//printf("\nElement nextElement: %ld",(long int) (*memoryListHead)->nextElement);
//printf("\nElement previousElement: %ld",(long int) (*memoryListHead)->previousElement);
@ -172,15 +172,20 @@ void DYNMEM_free(void *f_address, DYNMEM_MemoryTable_DataType ** memoryListHead)
if(!found)
{
printf("\n\nMemory address : %ld not found\n\n", f_address);
fflush(0);
//Debug TRAP
//char *theData ="c";
//strcpy(theData, "ciaociaociao");
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));
printf("\nFree of %ld", f_address);
free(f_address);
if(found->previousElement)
found->previousElement->nextElement = found->nextElement;

View File

@ -118,16 +118,12 @@ void DYNV_VectorGeneric_SoftPopBack(DYNV_VectorGenericDataType *TheVector)
TheVector->Size--;
}
void DYNV_VectorGeneric_Destroy(DYNV_VectorGenericDataType *TheVector, void (*DeleteElementFunction)(void *TheElementToDelete))
void DYNV_VectorGeneric_Destroy(DYNV_VectorGenericDataType *TheVector, void (*DeleteElementFunction)(DYNV_VectorGenericDataType *TheVector))
{
int i;
for (i = 0; i < TheVector->Size; i++)
{
printf("\n Deleting element : %d", i);
fflush(0);
DeleteElementFunction((void *) TheVector->Data[i]);
DYNMEM_free(TheVector->Data[i], &TheVector->memoryTable);
}
//printf("\n Deleting vector element %d address %ld", i,TheVector->Data[i]);
DeleteElementFunction(TheVector);
//DYNMEM_free(TheVector->Data[i], &TheVector->memoryTable);
DYNMEM_free(TheVector->Data, &TheVector->memoryTable);
DYNMEM_free(TheVector->ElementSize, &TheVector->memoryTable);

View File

@ -69,7 +69,7 @@ void DYNV_VectorGeneric_InitWithSearchFunction(DYNV_VectorGenericDataType *TheVe
void DYNV_VectorGeneric_PushBack(DYNV_VectorGenericDataType *TheVectorGeneric, void * TheElementData, int TheElementSize);
void DYNV_VectorGeneric_PopBack(DYNV_VectorGenericDataType *TheVector, void (*DeleteElementFunction)(void *TheElementToDelete));
void DYNV_VectorGeneric_SoftPopBack(DYNV_VectorGenericDataType *TheVector);
void DYNV_VectorGeneric_Destroy(DYNV_VectorGenericDataType *TheVector, void (*DeleteElementFunction)(void *TheElementToDelete));
void DYNV_VectorGeneric_Destroy(DYNV_VectorGenericDataType *TheVector, void (*DeleteElementFunction)(DYNV_VectorGenericDataType *TheVector));
void DYNV_VectorGeneric_SoftDestroy(DYNV_VectorGenericDataType *TheVector);
void DYNV_VectorGeneric_DeleteAt(DYNV_VectorGenericDataType *TheVector, int index, void (*DeleteElementFunction)(void *TheElementToDelete));