diff --git a/include/base.h b/include/base.h index f6abc91..2256ab9 100644 --- a/include/base.h +++ b/include/base.h @@ -161,6 +161,7 @@ struct { int nmaxThread; int nmaxCycles; int siteNumber; + bool isRunning; Scheduler_t *scheduler; Supervisor_t *supervisor; } typedef Model_t; diff --git a/include/scheduler.h b/include/scheduler.h index 0149738..74ea80d 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -33,11 +33,24 @@ void SchedInit(Scheduler_t *scheduler); // -------------------------------------------------------------------------- // static inline void SchedDestroy(Scheduler_t *scheduler) { - free(scheduler->globalDrawingSpace->space); - free(scheduler->globalDrawingSpace); - - free(scheduler->arrowArray->array); - free(scheduler->arrowArray); + if (scheduler) { + if (scheduler->globalDrawingSpace) { + if (scheduler->globalDrawingSpace->space) { + free(scheduler->globalDrawingSpace->space); + scheduler->globalDrawingSpace->space = NULL; + } + free(scheduler->globalDrawingSpace); + scheduler->globalDrawingSpace = NULL; + } + if (scheduler->arrowArray) { + if (scheduler->arrowArray->array) { + free(scheduler->arrowArray->array); + scheduler->arrowArray->array = NULL; + } + free(scheduler->arrowArray); + scheduler->arrowArray = NULL; + } + } } // -------------------------------------------------------------------------- // diff --git a/src/cli.c b/src/cli.c index 1525b03..892b174 100644 --- a/src/cli.c +++ b/src/cli.c @@ -104,7 +104,7 @@ void connectedCommunication(int sockfd) curLineLength = 0; continueReadLine = 1; historyModifier = -1; - while (continueReadLine && (curChar = getch())) { + while (continueReadLine && (curChar = getch())) { //TODO empty line in history switch (curChar) { diff --git a/src/main.c b/src/main.c index b1c15d1..3762f50 100644 --- a/src/main.c +++ b/src/main.c @@ -53,6 +53,7 @@ int main(int argc, char **argv) ServerDestroy(server0); ModelSystemDestroy(); free(server0); + server0 = NULL; return returnValue; } diff --git a/src/model.c b/src/model.c index 84ea909..2685dc5 100644 --- a/src/model.c +++ b/src/model.c @@ -102,22 +102,6 @@ int ModelLoad(int id) // TODO unload ! // Creating structure for the Scheduler knownModel[id-1]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); - knownModel[id-1]->scheduler->globalDrawingSpace = - (Space_t*) calloc(1, sizeof(Space_t)); // TODO free this - - knownModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; - knownModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; - - // Preparing global drawing space - ModelPrepareSpace(knownModel[id-1]->scheduler->globalDrawingSpace, - knownModel[id-1]); - - knownModel[id-1]->scheduler->arrowArray = - (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this - - ModelPrepareArrows(knownModel[id-1]->scheduler->globalDrawingSpace, - knownModel[id-1]->scheduler->arrowArray, knownModel[id-1]); - loadedModel = (Model_t**) realloc(loadedModel, ++loadedModelSize * sizeof(Model_t*)); @@ -129,14 +113,37 @@ int ModelLoad(int id) // TODO unload ! int ModelRun(int id) { - if (id <= 0 || id > loadedModelSize) { + if (id <= 0 || id > loadedModelSize) return 0; - } - if (!loadedModel[id-1]->scheduler) { + + if (!loadedModel[id-1]->scheduler) return 0; - } + + if (loadedModel[id-1]->isRunning) + return 0; + + loadedModel[id-1]->scheduler->globalDrawingSpace = + (Space_t*) calloc(1, sizeof(Space_t)); // TODO free this + + loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; + loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; + + // Preparing global drawing space + ModelPrepareSpace(loadedModel[id-1]->scheduler->globalDrawingSpace, + loadedModel[id-1]); + + loadedModel[id-1]->scheduler->arrowArray = + (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this + + ModelPrepareArrows(loadedModel[id-1]->scheduler->globalDrawingSpace, + loadedModel[id-1]->scheduler->arrowArray, + loadedModel[id-1]); + + loadedModel[id-1]->scheduler->pleaseStop = false; + loadedModel[id-1]->isRunning = true; SchedInit(loadedModel[id-1]->scheduler); + printLog("Model %d launched\n", id); return 1; } @@ -156,14 +163,16 @@ int ModelStop(int id) SchedWait(loadedModel[id-1]->scheduler); SchedDestroy(loadedModel[id-1]->scheduler); - loadedModel[id-1]->scheduler->pleaseStop = false; + loadedModel[id-1]->isRunning = false; return 1; } void ModelDelete(int id) { free(knownModel[id]->name); + knownModel[id]->name = NULL; free(knownModel[id]); + knownModel[id] = NULL; } void ModelShutdown(void) @@ -190,7 +199,9 @@ void ModelSystemDestroy(void) ModelDelete(i); } free(loadedModel); + loadedModel = NULL; free(knownModel); + knownModel = NULL; } /* -------------------------------------------------------------------------- */ diff --git a/src/scheduler.c b/src/scheduler.c index f5254d8..496309c 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -57,6 +57,7 @@ static Center_t *findWorkArea(Center_t *centersList, Arrow_t *electedArrow, || (zmax && (abs(electedArrow->z - currentCenter->z) <= ruleRadius)) ){ free(newCenter); + newCenter = NULL; //printLog("Can't find a free area\n"); return NULL; } @@ -213,7 +214,9 @@ static void *schedulerMain(void *scheduler) printLog("Scheduler #%lu offline\n", args->id); free(workerArray); + workerArray = NULL; free(centersList); + centersList = NULL; return NULL; } diff --git a/src/server.c b/src/server.c index 6c65d63..af13a26 100644 --- a/src/server.c +++ b/src/server.c @@ -112,8 +112,10 @@ void *serverCommunicationInstance(void *server) } close(args->sockfd); - if (argv) + if (argv) { free(argv); + argv = NULL; + } printLog("Disconnected from %s:%d\n", clientIP, clientPort); return NULL; }