From 8b22467e34c1b831574bda9d06419fb0426b3d81 Mon Sep 17 00:00:00 2001 From: LEdoian Date: Wed, 14 Feb 2018 16:52:44 +0100 Subject: [PATCH] Add methods for flushing trie and tasks list --- grow.c | 5 +++++ task.c | 40 ++++++++++++++++++++++++++++++++++++++++ task.h | 6 ++++++ trie.c | 52 +++++++++++++++++++++++++++++++++++++++------------- trie.h | 1 + 5 files changed, 91 insertions(+), 13 deletions(-) diff --git a/grow.c b/grow.c index 128c5eb..efe175b 100644 --- a/grow.c +++ b/grow.c @@ -22,6 +22,11 @@ struct grow *grow_init(bool dealloc) { } bool grow_push (void *val, struct grow *g) { + if (g == NULL) { + syslog(cfg_log_facility | LOG_WARNING, "grow: attempt to use NULL as growing array"); + syslog(cfg_log_facility | LOG_INFO, "grow: use grow_init() first!"); + return false; + } if (g -> alloc <= g -> elems) { uint64_t new_size = g -> alloc == 0? 1 : 2* (g -> alloc); void **new_arr = realloc(g -> arr, new_size * sizeof(void *)); diff --git a/task.c b/task.c index 0b513fa..a79abe1 100644 --- a/task.c +++ b/task.c @@ -2,6 +2,46 @@ * task.c: task management */ +#include +#include +#include + #include "task.h" +#include "grow.h" +#include "async.h" + +static struct grow *tasks = NULL; + + +bool task_add(struct task t) { + bool retval = true; + struct task *p_t = malloc(sizeof(struct task)); + if(p_t == NULL) { + syslog(cfg_log_facility | LOG_ERR, "task: could not allocate space for task: %m"); + return false; + } + *p_t = t; + if (tasks == NULL) { + tasks = grow_init(true); + if (tasks == NULL) { + return false; + } + } + uint64_t id = tasks -> elems; // This relies on implementation detail of our growing array. + retval &= grow_push(p_t, tasks); + + // Create a timer for the task + // store timerid in ((struct task *)(tasks -> arr[id])) -> timerid +} + +bool task_disable(uint64_t id); +bool task_enable(uint64_t id); +bool task_delete(uint64_t id); +struct grow *task_list(void); +struct task task_details(uint64_t id); +void task_recalc(uint64_t id); +bool task_load(void); +bool task_save(void); +void task_flush(void); diff --git a/task.h b/task.h index ff35454..96a4f22 100644 --- a/task.h +++ b/task.h @@ -10,6 +10,10 @@ #include #include +enum task_check { // whether to check formula in signal handler or when variable changes + SIGNAL, VARCHG +}; + struct task { // Time info struct itimerspec times; @@ -20,6 +24,7 @@ struct task { // Formula to be checked when running char *formula; // in RPN, NUL terminated + enum task_check when; bool run; // dynamic programming // Disabling/enabling @@ -37,5 +42,6 @@ void task_recalc(uint64_t id); bool task_load(void); bool task_save(void); +void task_flush(void); #endif diff --git a/trie.c b/trie.c index 49e0cef..b0758c9 100644 --- a/trie.c +++ b/trie.c @@ -11,6 +11,10 @@ #include "config.h" #include "task.h" +enum dfs_list { + VERTICES, VARIABLES +}; + static struct trie *trie_base = NULL; static int indexof(char c) { @@ -143,24 +147,39 @@ bool trie_load(const char fn[]) { return true; } -static bool trie_dfs(char prefix[], int prefixlen, struct grow *list, struct trie *vertex) { +static bool trie_dfs(char prefix[], uint64_t prefixlen, struct grow *list, struct trie *vertex, enum dfs_list style) { if (vertex == NULL) {// Very special case; return true; } else { bool retval = true; - if (vertex -> def) { - prefix[prefixlen] = '\0'; - // Add to list - struct trie_list *l = malloc(sizeof(struct trie_list)); - l -> val = vertex -> val; - l -> name = malloc((prefixlen + 1) * sizeof(char)); - strcpy(l->name, prefix); - grow_push(l, list); + if (style == VARIABLES) { + if (prefixlen > cfg_var_name_max_len) { + syslog(cfg_log_facility | LOG_NOTICE, "trie: dfs: too long variable name in trie"); + return false; + } + if (vertex -> def) { + prefix[prefixlen] = '\0'; + // Add to list + struct trie_list *l = malloc(sizeof(struct trie_list)); + if (l == NULL) { + syslog(cfg_log_facility|LOG_WARNING, "trie: dfs: cannot create list element: %m"); + return false; + } + l -> val = vertex -> val; + l -> name = malloc((prefixlen + 1) * sizeof(char)); + strcpy(l->name, prefix); + retval &=grow_push(l, list); + } + } else if (style == VERTICES) { + // Add pointers to all vertices + retval &= grow_push(vertex, list); } for (int i = 0; i<37; i++) { if (vertex -> children[i] != NULL) { - prefix[prefixlen] = charof(i); - retval &= trie_dfs(prefix, prefixlen + 1, list, vertex -> children[i]); + if (style == VARIABLES) { + prefix[prefixlen] = charof(i); + } + retval &= trie_dfs(prefix, prefixlen + 1, list, vertex -> children[i], style); } } return retval; @@ -178,7 +197,7 @@ bool trie_save(const char fn[]) { struct grow *list = grow_init(true); char prefix[cfg_var_name_max_len +1]; prefix[cfg_var_name_max_len +1] = '\0'; - trie_dfs(prefix, 0, list, trie_base); + trie_dfs(prefix, 0, list, trie_base, VARIABLES); for (uint64_t i = 0; i < list -> elems; i++) { struct trie_list l = *(struct trie_list *)(list -> arr[i]); @@ -192,7 +211,7 @@ struct grow *trie_list(void) { struct grow *list = grow_init(true); char prefix[cfg_var_name_max_len +1]; prefix[cfg_var_name_max_len +1] = '\0'; - trie_dfs(prefix, 0, list, trie_base); + trie_dfs(prefix, 0, list, trie_base, VARIABLES); return list; } @@ -245,3 +264,10 @@ bool trie_unassoc(char var[], uint64_t task) { } } } + +void trie_drop (void) { + struct grow *list = grow_init(true); + trie_dfs("", 0, list, trie_base, VERTICES); + grow_drop(list); + return; +} diff --git a/trie.h b/trie.h index 7218e94..8276c89 100644 --- a/trie.h +++ b/trie.h @@ -40,5 +40,6 @@ bool trie_unassoc(char var[], uint64_t task); bool trie_load(const char fn[]); bool trie_save(const char fn[]); +void trie_flush(void); #endif