From 1c0967863515e22915aa2b202a168835a6e9d1fd Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 14 Oct 2021 08:34:19 +0200 Subject: [PATCH] Some cleanup --- Makefile | 2 -- src/main.c | 17 +++++++---------- src/model.c | 19 ++++++++++--------- src/parsing.c | 2 +- src/scheduler.c | 6 +++--- src/tests/centers.c | 41 ++++++++++++++++++---------------------- src/tests/test_realloc.c | 2 +- 7 files changed, 40 insertions(+), 49 deletions(-) diff --git a/Makefile b/Makefile index 93765a8..1d5cc68 100644 --- a/Makefile +++ b/Makefile @@ -137,5 +137,3 @@ run-cli: all @bin/gem-graph-ctl run-tests: tests @bin/tests/scheduler - -include $(DEPS) diff --git a/src/main.c b/src/main.c index 052c66a..42ded6c 100644 --- a/src/main.c +++ b/src/main.c @@ -28,7 +28,7 @@ static Server_t *server; -static void SigIntTermHandler(int signum) +static void sigtermHandler(int signum) { server->pleaseStop = true; printLog("Server stopping\n"); @@ -37,9 +37,6 @@ static void SigIntTermHandler(int signum) printLog("All model shutted down\n"); } -// TODO parse args to discover : -// - config files directory -// - models & users directories int main(int argc, char **argv) { int options; @@ -51,17 +48,17 @@ int main(int argc, char **argv) switch (options) { case 'C': printLog("Config : %s\n", optarg); - parameters.configDir = (char*) calloc(1, strlen(optarg) + 1); + parameters.configDir = calloc(1, strlen(optarg) + 1); strcpy(parameters.configDir, optarg); break; case 'M': printLog("Models : %s\n", optarg); - parameters.modelDir = (char*) calloc(1, strlen(optarg) + 1); + parameters.modelDir = calloc(1, strlen(optarg) + 1); strcpy(parameters.modelDir, optarg); break; case 'U': printLog("Users : %s\n", optarg); - parameters.userDir = (char*) calloc(1, strlen(optarg) + 1); + parameters.userDir = calloc(1, strlen(optarg) + 1); strcpy(parameters.userDir, optarg); break; case ':': @@ -112,14 +109,14 @@ int main(int argc, char **argv) printLog("Starting gem-graph-server...\n"); // Register new interrupt handler - signal(SIGINT, SigIntTermHandler); - signal(SIGTERM, SigIntTermHandler); + signal(SIGINT, sigtermHandler); + signal(SIGTERM, sigtermHandler); // Initializing random generator t = time(&t); srand((unsigned) t); - server = (Server_t*) calloc(1, sizeof(Server_t)); + server = calloc(1, sizeof(*server)); // Initializing model system ModelSystemInit(¶meters); diff --git a/src/model.c b/src/model.c index 77a89e8..db08dbd 100644 --- a/src/model.c +++ b/src/model.c @@ -67,7 +67,8 @@ int ModelLoad(int id) // Creating structure for the Scheduler - knownModel[id-1]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); + knownModel[id-1]->scheduler = + calloc(1, sizeof(*knownModel[id-1]->scheduler)); loadedModelSize++; @@ -165,9 +166,9 @@ void ModelAddToKnown(Model_t **newModel) knownModelSize++; // create socket knownModel = - (Model_t**) realloc(knownModel, knownModelSize * sizeof(Model_t*)); + (Model_t**) realloc(knownModel, knownModelSize * sizeof(*knownModel)); // populate socket - knownModel[knownModelSize-1] = (Model_t*) calloc(1, sizeof(Model_t)); + knownModel[knownModelSize-1] = calloc(1, sizeof(**knownModel)); // populate model knownModel[knownModelSize-1]->id = knownModelSize; // return value @@ -177,16 +178,16 @@ void ModelAddToKnown(Model_t **newModel) // continue. model population knownModel[knownModelSize-1]->name = - (char *) calloc(1, sizeof(char) * MODEL_STRING_SIZE); + calloc(1, sizeof(char) * MODEL_STRING_SIZE); knownModel[knownModelSize-1]->filename = - (char *) calloc(1, sizeof(char) * MODEL_STRING_SIZE); + calloc(1, sizeof(char) * MODEL_STRING_SIZE); knownModel[knownModelSize-1]->owner = - (char *) calloc(1, sizeof(char) * MODEL_STRING_SIZE); + calloc(1, sizeof(char) * MODEL_STRING_SIZE); knownModel[knownModelSize-1]->version = - (char *) calloc(1, sizeof(char) * MODEL_STRING_SIZE); + calloc(1, sizeof(char) * MODEL_STRING_SIZE); knownModel[knownModelSize-1]->space_xMax = XMAX; knownModel[knownModelSize-1]->space_yMax = YMAX; @@ -233,9 +234,9 @@ void ModelSystemInit(Parameters_t *parameters) Model_t *newModel; char *extensionPosition; - loadedModel = (Model_t**) calloc(1, sizeof(Model_t*)); + loadedModel = calloc(1, sizeof(*loadedModel)); - knownModel = (Model_t**) calloc(1, sizeof(Model_t*)); + knownModel = calloc(1, sizeof(*knownModel)); knownModelSize = 0; loadedModelSize = 0; diff --git a/src/parsing.c b/src/parsing.c index 822f9d3..53f846e 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -353,7 +353,7 @@ int ParseModelIdentityXML(Model_t *model, Parameters_t *params) }; // Allocating space for schema file path - schemPath = (char*) calloc(1, strlen(params->modelDir) + schemPath = calloc(1, strlen(params->modelDir) + strlen("/schemas/model_ .xmls")); printLog("Preparsing model %s\n", model->name); diff --git a/src/scheduler.c b/src/scheduler.c index 7bfde69..00dddb4 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -52,7 +52,7 @@ static Center_t *findWorkArea(Center_t *centersList, Arrow_t *electedArrow, register Center_t *currentCenter, *newCenter; currentCenter = centersList->next; - newCenter = (Center_t*) malloc(sizeof(Center_t)); + newCenter = malloc(sizeof(Center_t)); while (currentCenter){ if ( (xmax && (abs(electedArrow->x - currentCenter->x) <= ruleRadius)) @@ -101,9 +101,9 @@ static void *schedulerMain(void *scheduler) printLog("%d threads available.\n", ncpu); // Data structures - workerArray = (Worker_t*) calloc(ncpu, sizeof(Worker_t)); + workerArray = calloc(ncpu, sizeof(*workerArray)); nworker = 0; - centersList = (Center_t*) calloc(1, sizeof(Center_t)); + centersList = calloc(1, sizeof(*centersList)); // Initiate the arrowArray lock if (err = ArrowsInitLock(args->arrowArray), err != 0) { diff --git a/src/tests/centers.c b/src/tests/centers.c index d416006..b11344b 100644 --- a/src/tests/centers.c +++ b/src/tests/centers.c @@ -46,14 +46,14 @@ bool TestCenterRemove Center_t *anyCenter = NULL; // adding a something to a bad pointer - centerRemove(anyCenter); + CenterRemove(anyCenter); //printf("* Status of centers list after deleting NULL\n"); //printCenters(anyCenter); assert(anyCenter == NULL); - anyCenter = (Center_t*) calloc(1, sizeof(Center_t)); + anyCenter = calloc(1, sizeof(*anyCenter)); //printf("* Initial status of centers list\n"); //printCenters(anyCenter); @@ -63,25 +63,20 @@ bool TestCenterRemove assert(anyCenter != NULL); // Deleting a lonely pointer - centerRemove(anyCenter); + CenterRemove(anyCenter); //printf("* Status of centers list after deleting a lonely center\n"); //printCenters(anyCenter); - anyCenter = (Center_t*) calloc(1, sizeof(Center_t)); - centerAdd(anyCenter, (Center_t*) calloc(1, sizeof(Center_t))); - centerAdd(anyCenter, (Center_t*) calloc(1, sizeof(Center_t))); - centerAdd(anyCenter, (Center_t*) calloc(1, sizeof(Center_t))); - centerAdd(anyCenter, (Center_t*) calloc(1, sizeof(Center_t))); - centerAdd(anyCenter, (Center_t*) calloc(1, sizeof(Center_t))); + anyCenter = calloc(1, sizeof(*anyCenter)); Center_t *oldfirst = anyCenter->next; Center_t *oldsecond = anyCenter->next->next; // Deleting a the first pointer - centerRemove(anyCenter->next); + CenterRemove(anyCenter->next); //printf("* Status of centers list after deleting the head center\n"); //printCenters(anyCenter); @@ -96,14 +91,14 @@ bool TestCenterAdd(void) Center_t *anyCenter = NULL; // adding a something to a bad pointer - centerAdd(anyCenter, NULL); + CenterAdd(anyCenter, NULL); //printf("* Status of centers list after adding something to NULL\n"); //printCenters(anyCenter); assert(anyCenter == NULL); - anyCenter = (Center_t*) calloc(1, sizeof(Center_t)); + anyCenter = calloc(1, sizeof(Center_t)); //printf("* Initial status of centers list\n"); //printCenters(anyCenter); @@ -113,7 +108,7 @@ bool TestCenterAdd(void) assert(anyCenter != NULL); // adding a bad pointer - centerAdd(anyCenter, NULL); + CenterAdd(anyCenter, NULL); //printf("* Status of centers list after adding NULL\n"); //printCenters(anyCenter); @@ -123,8 +118,8 @@ bool TestCenterAdd(void) assert(anyCenter != NULL); // adding a good pointer - Center_t *goodpointer = (Center_t*) calloc(1, sizeof(Center_t)); - centerAdd(anyCenter, goodpointer); + Center_t *goodpointer = calloc(1, sizeof(Center_t)); + CenterAdd(anyCenter, goodpointer); //printf("* Status of centers list after adding a center\n"); //printCenters(anyCenter); @@ -136,8 +131,8 @@ bool TestCenterAdd(void) assert(goodpointer->next == NULL); // adding another good pointer - Center_t *newgoodpointer = (Center_t*) calloc(1, sizeof(Center_t)); - centerAdd(anyCenter, newgoodpointer); + Center_t *newgoodpointer = calloc(1, sizeof(Center_t)); + CenterAdd(anyCenter, newgoodpointer); //printf("* Status of centers list after adding another center\n"); //printCenters(anyCenter); @@ -149,10 +144,10 @@ bool TestCenterAdd(void) assert(newgoodpointer->next == goodpointer); // adding another good pointer - Center_t *strangepointer = (Center_t*) calloc(1, sizeof(Center_t)); + Center_t *strangepointer = calloc(1, sizeof(Center_t)); strangepointer->next = (Center_t*)0xCAFEBABE; strangepointer->prev = (Center_t*)0xCAFEBABE; - centerAdd(anyCenter, strangepointer); + CenterAdd(anyCenter, strangepointer); //printf("* Status of centers list after adding a strange center\n"); //printCenters(anyCenter); @@ -170,13 +165,13 @@ bool TestCenterAdd(void) int main(int argc, char **argv) { - printf("\n==== Testing centers.c/centerAdd() ====\n"); + printf("\n==== Testing centers.c/CenterAdd() ====\n"); TestCenterAdd(); - printf("\n==== Testing centers.c/centerAdd() ==== : OK\n"); + printf("\n==== Testing centers.c/CenterAdd() ==== : OK\n"); - printf("\n==== Testing centers.c/centerRemove() ====\n"); + printf("\n==== Testing centers.c/CenterRemove() ====\n"); TestCenterRemove(); - printf("\n==== Testing centers.c/centerRemove() ==== : OK\n"); + printf("\n==== Testing centers.c/CenterRemove() ==== : OK\n"); return 0; } diff --git a/src/tests/test_realloc.c b/src/tests/test_realloc.c index 1ddeab6..6a28911 100644 --- a/src/tests/test_realloc.c +++ b/src/tests/test_realloc.c @@ -3,7 +3,7 @@ int main(void) { - int *a = (int*) malloc(7 * sizeof(int)); + int *a = malloc(7 * sizeof(int)); printf("Salut! Tableau d'entiers à l'adresse %p\n", a);