Refactor, complete trie, extract growing array
parent
d54692a622
commit
c9692341e7
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* grow.c: generic growing array implementation
|
||||
*/
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include "grow.h"
|
||||
#include "config.h"
|
||||
|
||||
struct grow *grow_init(bool dealloc) {
|
||||
struct grow *g = malloc(sizeof(struct grow));
|
||||
if (g == NULL) {
|
||||
syslog(cfg_log_facility | LOG_ERR, "grow: Could not initialize growing array: %m");
|
||||
return NULL;
|
||||
}
|
||||
g -> arr = NULL;
|
||||
g -> elems = 0;
|
||||
g -> alloc = 0;
|
||||
g -> active_elems = 0;
|
||||
g -> dealloc = dealloc;
|
||||
return g;
|
||||
}
|
||||
|
||||
bool grow_push (void *val, struct grow *g) {
|
||||
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 *));
|
||||
if (new_arr == NULL) {
|
||||
syslog(cfg_log_facility | LOG_ERR, "grow: could not resize growinging array: %m");
|
||||
return false;
|
||||
} else {
|
||||
g -> arr = new_arr;
|
||||
g -> alloc = new_size;
|
||||
}
|
||||
}
|
||||
g -> arr[g -> elems] = val;
|
||||
g -> elems++;
|
||||
g -> active_elems++;
|
||||
return true;
|
||||
}
|
||||
|
||||
void *grow_pop (struct grow *g) {
|
||||
if (g -> elems == 0) {
|
||||
return NULL;
|
||||
} else {
|
||||
g -> elems--;
|
||||
g -> active_elems--;
|
||||
return g -> arr[g -> elems + 1];
|
||||
}
|
||||
}
|
||||
|
||||
void grow_drop (struct grow *g) {
|
||||
if (g -> dealloc == true) {
|
||||
for(uint64_t i = 0; i < g -> elems; i++) {
|
||||
free(g->arr[i]);
|
||||
}
|
||||
}
|
||||
free(g -> arr);
|
||||
free(g);
|
||||
return;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* grow.h: declaration of generic growing array
|
||||
*/
|
||||
|
||||
#ifndef GROW_H
|
||||
#define GROW_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct grow {
|
||||
void **arr;
|
||||
uint64_t elems;
|
||||
uint64_t alloc;
|
||||
uint64_t active_elems;
|
||||
bool dealloc;
|
||||
};
|
||||
|
||||
struct grow *grow_init(bool dealloc);
|
||||
bool grow_push (void *val, struct grow *g);
|
||||
void *grow_pop (struct grow *g);
|
||||
void grow_drop (struct grow *g);
|
||||
|
||||
#endif
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* task.h: declares functions for manipulation with tasks
|
||||
*/
|
||||
|
||||
#ifndef TASK_H
|
||||
#define TASK_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct task {
|
||||
// Time info
|
||||
struct itimerspec times;
|
||||
timer_t timerid;
|
||||
|
||||
// Command to run
|
||||
char **argv; //NULL terminated list of NUL terminated strings
|
||||
|
||||
// Formula to be checked when running
|
||||
char *formula; // in RPN, NUL terminated
|
||||
bool run; // dynamic programming
|
||||
|
||||
// Disabling/enabling
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
bool task_add(struct task t);
|
||||
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);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue