WIP: Arrows can be created

This commit is contained in:
Adrien Bourmault 2021-09-28 17:40:34 +02:00
parent 6125043513
commit 55d3d3f130
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
5 changed files with 95 additions and 25 deletions

View File

@ -26,7 +26,7 @@
<space-param> <space-param>
<dimension>1</dimension> <dimension>1</dimension>
<size>39</size> <size x="12" y="0" z="0"/>
<site_multiplicity>2</site_multiplicity> <site_multiplicity>2</site_multiplicity>
</space-param> </space-param>

View File

@ -53,10 +53,13 @@ struct Arrow_t {
int y; int y;
int z; int z;
int siteId; int siteId;
int weight;
} typedef Arrow_t; } typedef Arrow_t;
struct ArrowArray_t { struct ArrowArray_t {
size_t size; size_t size;
unsigned int freeSlotCount;
Arrow_t **freeSlots;
pthread_spinlock_t lock; pthread_spinlock_t lock;
Arrow_t *array; Arrow_t *array;
} typedef ArrowArray_t; } typedef ArrowArray_t;
@ -64,6 +67,7 @@ struct ArrowArray_t {
struct Site_t { struct Site_t {
char *label; char *label;
int nArrow; int nArrow;
Arrow_t *arrowsPtr;
} typedef Site_t; } typedef Site_t;
struct SpaceUnit_t { struct SpaceUnit_t {

View File

@ -20,7 +20,84 @@
//=-------------------------------------------------------------------------=// //=-------------------------------------------------------------------------=//
#include "../include/base.h" #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;
}

View File

@ -74,6 +74,10 @@ int ModelLoad(int id)
(Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*)); (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
loadedModel[loadedModelSize-1] = knownModel[id-1]; loadedModel[loadedModelSize-1] = knownModel[id-1];
// Parse model
ParseModelXML(loadedModel[loadedModelSize-1]);
return loadedModelSize; return loadedModelSize;
} }
@ -83,10 +87,10 @@ int ModelUnload(int id)
if (id > loadedModelSize) if (id > loadedModelSize)
return -1; return -1;
if (loadedModel[id-1]->scheduler) { SchedContentDestroy(loadedModel[id-1]->scheduler);
SchedDestroy(loadedModel[id-1]->scheduler); SchedDestroy(loadedModel[id-1]->scheduler);
loadedModel[id-1]->scheduler = NULL; loadedModel[id-1]->scheduler = NULL;
}
// Prevent fragmentation by moving data in the newly freed slot // Prevent fragmentation by moving data in the newly freed slot
if (id-1 < loadedModelSize) { if (id-1 < loadedModelSize) {
@ -117,21 +121,8 @@ int ModelRun(int id)
if (loadedModel[id-1]->isRunning) if (loadedModel[id-1]->isRunning)
return 0; 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->nMaxThread = knownModel[id-1]->nmaxThread;
loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; 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]->scheduler->pleaseStop = false;
loadedModel[id-1]->isRunning = true; loadedModel[id-1]->isRunning = true;
@ -156,7 +147,6 @@ int ModelStop(int id)
// Wait for Shceduler to stop // Wait for Shceduler to stop
SchedWait(loadedModel[id-1]->scheduler); SchedWait(loadedModel[id-1]->scheduler);
SchedContentDestroy(loadedModel[id-1]->scheduler);
// Disable running bit // Disable running bit
loadedModel[id-1]->isRunning = false; loadedModel[id-1]->isRunning = false;

View File

@ -470,9 +470,9 @@ int ParseModelXML(Model_t *model)
{(const xmlChar *)"max_cycles", parseStubFieldXML, model}, {(const xmlChar *)"max_cycles", parseStubFieldXML, model},
// SPACE // SPACE
{(const xmlChar *)"space_param", parseParentFieldXML, model}, {(const xmlChar *)"space_param", parseParentFieldXML, model},
{(const xmlChar *)"dimension", parseStubFieldXML, model}, {(const xmlChar *)"dimension", parseDimensionFieldXML, model},
{(const xmlChar *)"size", parseStubFieldXML, model}, {(const xmlChar *)"size", parseSizeFieldXML, model},
{(const xmlChar *)"site_multiplicity", parseStubFieldXML, model}, {(const xmlChar *)"site_multiplicity", parseSiteMulFieldXML, model},
// TODO lacking implementation (model side) // TODO lacking implementation (model side)
{(const xmlChar *)"boundaries", parseStubFieldXML, model}, {(const xmlChar *)"boundaries", parseStubFieldXML, model},
@ -481,11 +481,10 @@ int ParseModelXML(Model_t *model)
{(const xmlChar *)"objects", parseParentFieldXML, model}, {(const xmlChar *)"objects", parseParentFieldXML, model},
{(const xmlChar *)"object", parseParentFieldPropsXML, model}, {(const xmlChar *)"object", parseParentFieldPropsXML, model},
// SPACE
{(const xmlChar *)"space", parseParentFieldPropsXML, model},
// SAVESTATES // SAVESTATES
{(const xmlChar *)"savestates", parseParentFieldPropsXML, model}, {(const xmlChar *)"savestates", parseParentFieldPropsXML, model},
// SPACE
{(const xmlChar *)"space", parseParentFieldPropsXML, model},
// TRANSITIONS // TRANSITIONS
{(const xmlChar *)"transitions", parseParentFieldPropsXML, model}, {(const xmlChar *)"transitions", parseParentFieldPropsXML, model},