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_if.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2020 Weber Yann
  3. *
  4. * This file is part of pyrpn.
  5. *
  6. * pyrpn is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * pyrpn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "rpn_if.h"
  20. rpn_if_t* rpn_if_new(size_t mem_sz, rpn_if_transfo_t *if2rpn,
  21. rpn_if_transfo_t *rpn2if, rpn_expr_t *rpn)
  22. {
  23. rpn_if_t *res;
  24. if(mem_sz != 0)
  25. {
  26. if((if2rpn->mem_sz != 0 && if2rpn->mem_sz != mem_sz) ||
  27. (rpn2if->mem_sz != 0 && rpn2if->mem_sz != mem_sz))
  28. {
  29. errno = EINVAL;
  30. return NULL;
  31. }
  32. }
  33. else
  34. {
  35. if(if2rpn->mem_sz == 0 && rpn2if->mem_sz == 0)
  36. {
  37. errno = EINVAL;
  38. return NULL;
  39. }
  40. if(if2rpn->mem_sz != rpn2if->mem_sz && if2rpn->mem_sz != 0 &&
  41. rpn2if->mem_sz != 0)
  42. {
  43. errno = EINVAL;
  44. return NULL;
  45. }
  46. mem_sz = if2rpn->mem_sz != 0?if2rpn->mem_sz:rpn2if->mem_sz;
  47. }
  48. if(if2rpn->data_sz != rpn2if->data_sz || if2rpn->data_sz == 0)
  49. {
  50. errno = EINVAL;
  51. return NULL;
  52. }
  53. res = _rpn_if_new(mem_sz, if2rpn->argc, rpn2if->argc, rpn2if->data_sz);
  54. if(!res)
  55. {
  56. return NULL;
  57. }
  58. res->if2rpn = if2rpn->if2rpn;
  59. res->rpn2if = rpn2if->rpn2if;
  60. res->rpn = rpn;
  61. return res;
  62. }
  63. void rpn_if_free(rpn_if_t* rif)
  64. {
  65. size_t i;
  66. for(i=0; i<rif->rpn_sz; i++)
  67. {
  68. rpn_expr_close(&(rif->rpn[i]));
  69. }
  70. free(rif->rpn);
  71. free(rif->rpn_res);
  72. munmap(rif->mem, rif->mem_sz);
  73. free(rif);
  74. }
  75. int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
  76. {
  77. return rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
  78. }
  79. int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
  80. size_t offset)
  81. {
  82. size_t i;
  83. for(i=offset; i<offset+sz; i++)
  84. {
  85. /**@todo continue implementation */
  86. }
  87. return 0;
  88. }
  89. rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count, size_t
  90. value_sz)
  91. {
  92. rpn_if_t *res;
  93. int err;
  94. res = malloc(sizeof(rpn_if_t));
  95. if(!res)
  96. {
  97. goto error;
  98. }
  99. res->rpn_sz = rpn_count;
  100. res->rpn_argc = rpn_argc;
  101. res->mem_sz = mem_sz;
  102. res->value_sz = value_sz;
  103. res->mem = mmap(NULL, mem_sz, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
  104. if(res->mem == (void*)-1)
  105. {
  106. goto mmap_err;
  107. }
  108. res->rpn_res = malloc(sizeof(unsigned long) * (rpn_count +rpn_argc));
  109. if(!res->rpn_res)
  110. {
  111. goto rpn_malloc_err;
  112. }
  113. res->rpn_args = &(res->rpn_res[res->rpn_sz]);
  114. res->rpn = malloc(sizeof(rpn_expr_t) * res->rpn_sz);
  115. if(!res->rpn)
  116. {
  117. goto rpn_expr_err;
  118. }
  119. return res;
  120. rpn_expr_err:
  121. err = errno;
  122. free(res->rpn_res);
  123. rpn_malloc_err:
  124. err = errno;
  125. munmap(res->mem, mem_sz);
  126. mmap_err:
  127. err = errno;
  128. free(res);
  129. error:
  130. err = errno;
  131. errno = err;
  132. return NULL;
  133. }