| 1 | #include <malloc.h> |
| 2 | |
| 3 | #include "alloc.h" |
| 4 | |
| 5 | #include "element.h" |
| 6 | |
| 7 | /* global variables */ |
| 8 | |
| 9 | /* allocate new element */ |
| 10 | |
| 11 | element_t *newelement (func_t function, int nbops, int prio) |
| 12 | { |
| 13 | element_t *new = (element_t *) callocordie (1, sizeof (element_t)); |
| 14 | if (nbops) { |
| 15 | new->ops = (element_t **) callocordie (nbops, sizeof (element_t *)); |
| 16 | } |
| 17 | new->func = function; |
| 18 | new->nbops = nbops; |
| 19 | new->prio = prio; |
| 20 | |
| 21 | return new; |
| 22 | } |
| 23 | |
| 24 | /* desallocate element */ |
| 25 | |
| 26 | void delelement (element_t *root) |
| 27 | { |
| 28 | if ((root != NULL) && (root != ERROR_OP)) { |
| 29 | int i; |
| 30 | for (i = 0; i < root->nbops; i++) { |
| 31 | delelement (root->ops[i]); |
| 32 | } |
| 33 | if (root->nbops) { |
| 34 | free (root->ops); |
| 35 | } |
| 36 | free (root); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /* duplicate element */ |
| 41 | |
| 42 | element_t *dupelement (element_t *root) |
| 43 | { |
| 44 | element_t *tmp = NULL; |
| 45 | int i; |
| 46 | |
| 47 | if ((root == NULL) || (root == ERROR_OP)) { |
| 48 | return root; |
| 49 | } |
| 50 | tmp = newelement (root->func, root->nbops, root->prio); |
| 51 | tmp->value = root->value; |
| 52 | for (i = 0; i < root->nbops; i++) { |
| 53 | tmp->ops[i] = dupelement (root->ops[i]); |
| 54 | } |
| 55 | return tmp; |
| 56 | } |
| 57 | |
| 58 | /* vim: set ts=4 sw=4 et: */ |