From 55d3d3f130f233943827ab02fbdc9ec595c8b872 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 28 Sep 2021 17:40:34 +0200 Subject: [PATCH] WIP: Arrows can be created --- debian/var/models/example.xml | 2 +- include/base.h | 4 ++ src/arrows.c | 77 +++++++++++++++++++++++++++++++++++ src/model.c | 26 ++++-------- src/parsing.c | 11 +++-- 5 files changed, 95 insertions(+), 25 deletions(-) diff --git a/debian/var/models/example.xml b/debian/var/models/example.xml index 4a23fd4..7855bf1 100644 --- a/debian/var/models/example.xml +++ b/debian/var/models/example.xml @@ -26,7 +26,7 @@ 1 - 39 + 2 diff --git a/include/base.h b/include/base.h index ed2dc5f..14881e2 100644 --- a/include/base.h +++ b/include/base.h @@ -53,10 +53,13 @@ struct Arrow_t { int y; int z; int siteId; + int weight; } typedef Arrow_t; struct ArrowArray_t { size_t size; + unsigned int freeSlotCount; + Arrow_t **freeSlots; pthread_spinlock_t lock; Arrow_t *array; } typedef ArrowArray_t; @@ -64,6 +67,7 @@ struct ArrowArray_t { struct Site_t { char *label; int nArrow; + Arrow_t *arrowsPtr; } typedef Site_t; struct SpaceUnit_t { diff --git a/src/arrows.c b/src/arrows.c index 0e6419e..b808069 100644 --- a/src/arrows.c +++ b/src/arrows.c @@ -20,7 +20,84 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" +#include "../include/arrows.h" /* -------------------------------------------------------------------------- */ +static inline int location(Space_t *space, int x, int y, int z) { + return x + y * (space->xMax+1) + z * (space->xMax+1) * (space->zMax+1); +} + +Arrow_t *ArrowAdd(Scheduler_t *scheduler, int x, int y, int z, int siteId, + int weight) { + + ArrowArray_t *arrowArray = scheduler->arrowArray; + Space_t *drawingSpace = scheduler->globalDrawingSpace; + Site_t *currentSite = &drawingSpace->space[ + location(drawingSpace, x, y, z) + ].sites[siteId]; + Arrow_t *curArrow; + + // Check if there is a free slot to store the a new arrow + if (arrowArray->freeSlotCount == 0) { + // Realloc array to create free slot + arrowArray->array = realloc(arrowArray->array, arrowArray->size + 1); + curArrow = &arrowArray->array[arrowArray->size + 1]; + arrowArray->size++; + } else { + // Reuse an unused free slot + curArrow = arrowArray->freeSlots[0]; + + // Delete the free slot reference (it's our *PROPERTY* now !) + if (!(memmove(&arrowArray->freeSlots[0], + &arrowArray->freeSlots[0] + sizeof(arrowArray->freeSlots[0]), + arrowArray->freeSlotCount - 1 + ))) + return NULL; + arrowArray->freeSlotCount--; + } + + // Assign values to the new arrow + curArrow->x = x; + curArrow->y = y; + curArrow->z = z; + curArrow->siteId = siteId; + + // Store a reference of this new arrow in space (to find it quickly later) + currentSite->arrowsPtr = curArrow; + currentSite->nArrow++; + + return curArrow; +} + +bool ArrowRemove(Scheduler_t *scheduler, int x, int y, int z, int siteId, + int weight) { + + ArrowArray_t *arrowArray = scheduler->arrowArray; + Space_t *drawingSpace = scheduler->globalDrawingSpace; + Site_t *currentSite = &drawingSpace->space[ + location(drawingSpace, x, y, z) + ].sites[siteId]; + Arrow_t *curArrow; + + // Look for a corresponding arrow in that site + if (currentSite->nArrow) { + // Get an arrow from space unit at siteId + curArrow = currentSite->arrowsPtr; + + // Mark arrow for deletion + arrowArray->freeSlots = + realloc(arrowArray->freeSlots, arrowArray->freeSlotCount + 1); + if (!arrowArray->freeSlots) + return false; + arrowArray->freeSlots[arrowArray->freeSlotCount] = curArrow; + arrowArray->freeSlotCount++; + + currentSite->arrowsPtr = NULL; + + currentSite->nArrow--; + } + + return false; +} diff --git a/src/model.c b/src/model.c index 21491e0..3fdc969 100644 --- a/src/model.c +++ b/src/model.c @@ -74,6 +74,10 @@ int ModelLoad(int id) (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*)); loadedModel[loadedModelSize-1] = knownModel[id-1]; + + // Parse model + ParseModelXML(loadedModel[loadedModelSize-1]); + return loadedModelSize; } @@ -83,10 +87,10 @@ int ModelUnload(int id) if (id > loadedModelSize) return -1; - if (loadedModel[id-1]->scheduler) { - SchedDestroy(loadedModel[id-1]->scheduler); - loadedModel[id-1]->scheduler = NULL; - } + SchedContentDestroy(loadedModel[id-1]->scheduler); + + SchedDestroy(loadedModel[id-1]->scheduler); + loadedModel[id-1]->scheduler = NULL; // Prevent fragmentation by moving data in the newly freed slot if (id-1 < loadedModelSize) { @@ -117,21 +121,8 @@ int ModelRun(int id) if (loadedModel[id-1]->isRunning) return 0; - // - loadedModel[id-1]->scheduler->globalDrawingSpace = - (Space_t*) calloc(1, sizeof(Space_t)); - loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; - - // Preparing global drawing space - // TODO Load space - - loadedModel[id-1]->scheduler->arrowArray = - (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); - - // TODO Load arrows - loadedModel[id-1]->scheduler->pleaseStop = false; loadedModel[id-1]->isRunning = true; @@ -156,7 +147,6 @@ int ModelStop(int id) // Wait for Shceduler to stop SchedWait(loadedModel[id-1]->scheduler); - SchedContentDestroy(loadedModel[id-1]->scheduler); // Disable running bit loadedModel[id-1]->isRunning = false; diff --git a/src/parsing.c b/src/parsing.c index edcecb5..73d4d8d 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -470,9 +470,9 @@ int ParseModelXML(Model_t *model) {(const xmlChar *)"max_cycles", parseStubFieldXML, model}, // SPACE {(const xmlChar *)"space_param", parseParentFieldXML, model}, - {(const xmlChar *)"dimension", parseStubFieldXML, model}, - {(const xmlChar *)"size", parseStubFieldXML, model}, - {(const xmlChar *)"site_multiplicity", parseStubFieldXML, model}, + {(const xmlChar *)"dimension", parseDimensionFieldXML, model}, + {(const xmlChar *)"size", parseSizeFieldXML, model}, + {(const xmlChar *)"site_multiplicity", parseSiteMulFieldXML, model}, // TODO lacking implementation (model side) {(const xmlChar *)"boundaries", parseStubFieldXML, model}, @@ -481,11 +481,10 @@ int ParseModelXML(Model_t *model) {(const xmlChar *)"objects", parseParentFieldXML, model}, {(const xmlChar *)"object", parseParentFieldPropsXML, model}, - // SPACE - {(const xmlChar *)"space", parseParentFieldPropsXML, model}, - // SAVESTATES {(const xmlChar *)"savestates", parseParentFieldPropsXML, model}, + // SPACE + {(const xmlChar *)"space", parseParentFieldPropsXML, model}, // TRANSITIONS {(const xmlChar *)"transitions", parseParentFieldPropsXML, model},