12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include "rpn_ifs.h"
-
- rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap)
- {
- rpn_ifs_t *res;
- int err;
-
- if(!(res = malloc(sizeof(rpn_ifs_t))))
- {
- goto error;
- }
- bzero(res, sizeof(rpn_ifs_t));
-
- memcpy(&(res->params), params, sizeof(rpn_if_param_t));
-
- if(memmap)
- {
- res->self_mem = 0;
- res->mem = memmap;
- }
- else
- {
- res->self_mem = 1;
- res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
- -1, 0);
- if(res->mem == (void*)-1)
- {
- goto mmap_err;
- }
- }
-
- return res;
-
- mmap_err:
- err = errno;
- free(res);
- error:
- err = errno;
- errno = err;
- return NULL;
- }
-
- void rpn_ifs_free(rpn_ifs_t *rifs)
- {
- while(rifs->if_sz)
- {
- rpn_if_free(rifs->rpn_if[--(rifs->if_sz)]);
- }
- if(rifs->self_mem)
- {
- munmap(rifs->mem, rifs->params.mem_sz);
- }
- if(rifs->rpn_if)
- {
- free(rifs->rpn_if);
- }
- if(rifs->weight)
- {
- free(rifs->weight);
- }
- }
-
- size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight)
- {
- size_t res;
- void *tmp;
-
- res = rifs->if_sz + 1;
-
- if(!(tmp = realloc(rifs->rpn_if, sizeof(rpn_if_t*) * res)))
- {
- return 0;
- }
- rifs->rpn_if = tmp;
-
- if(!(tmp = realloc(rifs->weight, sizeof(unsigned int) * res)))
- {
- return 0;
- }
- rifs->weight = tmp;
-
- rifs->weight[rifs->if_sz] = weight;
- //WRONG expr ARGUMENT !!!
- rifs->rpn_if[rifs->if_sz] = rpn_if_new(&(rifs->params), *exprs, rifs->mem);
- if(!rifs->rpn_if[rifs->if_sz])
- {
- return 0;
- }
-
- rifs->if_sz++;
- if(rpn_ifs_weight_update(rifs) < 0)
- {
- rpn_ifs_del_if(rifs, res); // don't attempt to ceck for errors..
- return 0;
- }
- return res;
- }
|