Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpn_ifs.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "rpn_ifs.h"
  2. rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap)
  3. {
  4. rpn_ifs_t *res;
  5. int err;
  6. if(!(res = malloc(sizeof(rpn_ifs_t))))
  7. {
  8. goto error;
  9. }
  10. bzero(res, sizeof(rpn_ifs_t));
  11. memcpy(&(res->params), params, sizeof(rpn_if_param_t));
  12. if(memmap)
  13. {
  14. res->self_mem = 0;
  15. res->mem = memmap;
  16. }
  17. else
  18. {
  19. res->self_mem = 1;
  20. res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
  21. -1, 0);
  22. if(res->mem == (void*)-1)
  23. {
  24. goto mmap_err;
  25. }
  26. }
  27. return res;
  28. mmap_err:
  29. err = errno;
  30. free(res);
  31. error:
  32. err = errno;
  33. errno = err;
  34. return NULL;
  35. }
  36. void rpn_ifs_free(rpn_ifs_t *rifs)
  37. {
  38. while(rifs->if_sz)
  39. {
  40. rpn_if_free(rifs->rpn_if[--(rifs->if_sz)]);
  41. }
  42. if(rifs->self_mem)
  43. {
  44. munmap(rifs->mem, rifs->params.mem_sz);
  45. }
  46. if(rifs->rpn_if)
  47. {
  48. free(rifs->rpn_if);
  49. }
  50. if(rifs->weight)
  51. {
  52. free(rifs->weight);
  53. }
  54. }
  55. size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight)
  56. {
  57. size_t res;
  58. void *tmp;
  59. res = rifs->if_sz + 1;
  60. if(!(tmp = realloc(rifs->rpn_if, sizeof(rpn_if_t*) * res)))
  61. {
  62. return 0;
  63. }
  64. rifs->rpn_if = tmp;
  65. if(!(tmp = realloc(rifs->weight, sizeof(unsigned int) * res)))
  66. {
  67. return 0;
  68. }
  69. rifs->weight = tmp;
  70. rifs->weight[rifs->if_sz] = weight;
  71. //WRONG expr ARGUMENT !!!
  72. rifs->rpn_if[rifs->if_sz] = rpn_if_new(&(rifs->params), *exprs, rifs->mem);
  73. if(!rifs->rpn_if[rifs->if_sz])
  74. {
  75. return 0;
  76. }
  77. rifs->if_sz++;
  78. if(rpn_ifs_weight_update(rifs) < 0)
  79. {
  80. rpn_ifs_del_if(rifs, res); // don't attempt to ceck for errors..
  81. return 0;
  82. }
  83. return res;
  84. }