Skip to content

Commit

Permalink
Merge pull request #6 from discosat/dynamic-store-lazy-allocation
Browse files Browse the repository at this point in the history
allocate memory lazily in dynamic proc store
  • Loading branch information
NValsted authored May 1, 2024
2 parents 776767b + 33bab6d commit 8fcab91
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ proc run 1 # Run the procedure to log data points when the vehicle is within th

One can then imagine an extended scenario where there is e.g. a third node responsible for some actuator that must react based on the sensor node, which adds another layer of coordination to the system - all this can be orchestrated using the DSL!

### Utilizing reserved, pre-programmable procedure slots
There is also support for pre-programmed procedures in reserved slots, which can be used to simplify the DSL code or take care of more complex operations. For example, one can write a function compiled into the application on node 1 that calculates the euclidean distance between points defined by (`lat`, `lon`) and (`target_lat`, `target_lon`) and stores the result in the `dist` parameter. For the sake of example, let's assumed this procedure is available in slot 0. Then the geo-fencing setup procedure can be simplified as follows:
### Utilizing reserved, pre-programmable procedure slots (Not yet fully implemented)
There is also (_planned_) support for pre-programmed procedures in reserved slots, which can be used to simplify the DSL code or take care of more complex operations. For example, one can write a function compiled into the application on node 1 that calculates the euclidean distance between points defined by (`lat`, `lon`) and (`target_lat`, `target_lon`) and stores the result in the `dist` parameter. For the sake of example, let's assumed this procedure is available in slot 0. Then the geo-fencing setup procedure can be simplified as follows:
```bash
# procedure 1 (geo-fencing setup)
proc new
Expand Down
2 changes: 1 addition & 1 deletion include/csp_proc/proc_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int __attribute__((weak)) delete_proc(uint8_t slot);
/**
* Reset the procedure storage.
*/
void __attribute__((weak)) reset_proc_store();
int __attribute__((weak)) reset_proc_store();

/**
* Initialize the procedure storage and any necessary resources.
Expand Down
70 changes: 28 additions & 42 deletions src/store/proc_store_dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#include <csp_proc/proc_mutex.h>
#include <csp_proc/proc_memory.h>

proc_t * proc_store = NULL;
proc_t * proc_store[MAX_PROC_SLOT + 1];
proc_mutex_t * proc_store_mutex = NULL;

int _delete_proc(uint8_t slot) {
if (slot < 0 || slot > MAX_PROC_SLOT) {
return -1;
}
for (int i = 0; i < proc_store[slot].instruction_count; i++) {
proc_free_instruction(&proc_store[slot].instructions[i]);
if (proc_store[slot] != NULL) {
for (int i = 0; i < proc_store[slot]->instruction_count; i++) {
proc_free_instruction(&proc_store[slot]->instructions[i]);
}
proc_free(proc_store[slot]);
proc_store[slot] = NULL;
}
proc_store[slot].instruction_count = 0;
return 0;
}

Expand All @@ -25,7 +28,7 @@ int delete_proc(uint8_t slot) {
return ret;
}

void reset_proc_store() {
int reset_proc_store() {
if (proc_mutex_take(proc_store_mutex) != PROC_MUTEX_OK) {
return PROC_MUTEX_ERR;
}
Expand All @@ -40,18 +43,14 @@ int proc_store_init() {
if (proc_store_mutex == NULL) {
return -1;
}
proc_store = (proc_t *)proc_calloc(MAX_PROC_SLOT + 1, sizeof(proc_t));
if (proc_store == NULL) {
proc_mutex_destroy(proc_store_mutex);
return -1;
}
return 0;
}

void destroy_proc_store() {
if (proc_store != NULL) {
proc_free(proc_store);
proc_store = NULL;
for (int i = 0; i < MAX_PROC_SLOT + 1; i++) {
_delete_proc(i);
}
}
if (proc_store_mutex != NULL) {
proc_mutex_destroy(proc_store_mutex);
Expand All @@ -60,43 +59,30 @@ void destroy_proc_store() {
}

int set_proc(proc_t * proc, uint8_t slot, int overwrite) {
if (slot < 0 || slot > MAX_PROC_SLOT) {
return -1;
}
if (proc_mutex_take(proc_store_mutex) != PROC_MUTEX_OK) {
return PROC_MUTEX_ERR;
}
// Free any existing procedure in the slot
if (proc_store[slot] != NULL) {
if (!overwrite) {
return -1;
}

int ret = -1;
if (proc_store[slot].instruction_count == 0) {
_delete_proc(slot);
proc_store[slot] = *proc;
ret = slot;
} else if (overwrite) {
_delete_proc(slot);
proc_store[slot] = *proc;
ret = slot;
proc_free(proc_store[slot]);
}
proc_mutex_give(proc_store_mutex);
return ret;
}

proc_t * get_proc(uint8_t slot) {
if (slot < 0 || slot > MAX_PROC_SLOT) {
return NULL;
proc_store[slot] = proc_malloc(sizeof(proc_t));
if (proc_store[slot] == NULL) {
return -1;
}

if (proc_mutex_take(proc_store_mutex) != PROC_MUTEX_OK) {
return PROC_MUTEX_ERR;
}
memcpy(proc_store[slot], proc, sizeof(proc_t));

return slot;
}

if (proc_store[slot].instruction_count == 0) {
proc_mutex_give(proc_store_mutex);
proc_t * get_proc(uint8_t slot) {
if (proc_store[slot] == NULL) {
return NULL;
}

proc_mutex_give(proc_store_mutex);
return &proc_store[slot];
return proc_store[slot];
}

int * get_proc_slots() {
Expand All @@ -107,7 +93,7 @@ int * get_proc_slots() {
return NULL;
}
for (int i = 0; i < MAX_PROC_SLOT + 1; i++) {
if (proc_store[i].instruction_count > 0) {
if (proc_store[i] != NULL) {
slots[count++] = i;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/proc_store_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int delete_proc(uint8_t slot) {
return ret;
}

void reset_proc_store() {
int reset_proc_store() {
if (proc_mutex_take(proc_store_mutex) != PROC_MUTEX_OK) {
return PROC_MUTEX_ERR;
}
Expand Down

0 comments on commit 8fcab91

Please sign in to comment.