1
0
Fork 0

Add methods for flushing trie and tasks list

master
LEdoian 7 years ago
parent c9692341e7
commit 8b22467e34

@ -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 *));

@ -2,6 +2,46 @@
* task.c: task management
*/
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#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);

@ -10,6 +10,10 @@
#include <stdbool.h>
#include <stdint.h>
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

@ -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 (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);
grow_push(l, list);
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) {
if (style == VARIABLES) {
prefix[prefixlen] = charof(i);
retval &= trie_dfs(prefix, prefixlen + 1, list, vertex -> children[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;
}

@ -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

Loading…
Cancel
Save