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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(const rpn_if_param_t *params, rpn_expr_t *rpn,
  21. rpn_value_t *memmap)
  22. {
  23. rpn_if_t *res;
  24. size_t i;
  25. int err;
  26. res = malloc(sizeof(rpn_if_t));
  27. if(!res)
  28. {
  29. goto error;
  30. }
  31. //memcpy(&res->params, params, sizeof(rpn_if_param_t));
  32. res->params = params;
  33. if(memmap)
  34. {
  35. res->self_mem = 0;
  36. res->mem = memmap;
  37. }
  38. else
  39. {
  40. res->self_mem = 1;
  41. res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
  42. -1, 0);
  43. if(res->mem == (void*)-1)
  44. {
  45. goto mmap_err;
  46. }
  47. }
  48. res->rpn_res = malloc(sizeof(rpn_value_t) *
  49. (params->rpn_sz + params->rpn_argc));
  50. if(!res->rpn_res)
  51. {
  52. goto rpn_malloc_err;
  53. }
  54. res->rpn_args = &(res->rpn_res[params->rpn_sz]);
  55. res->rpn = malloc(sizeof(rpn_expr_t*) * params->rpn_sz);
  56. if(!res->rpn)
  57. {
  58. goto rpn_expr_err;
  59. }
  60. for(i=0; i<params->rpn_sz; i++)
  61. {
  62. if(rpn_expr_init(&(res->rpn[i]), params->rpn_stack_sz,
  63. params->rpn_argc) < 0)
  64. {
  65. goto rpn_init_error;
  66. }
  67. }
  68. return res;
  69. rpn_init_error:
  70. err = errno;
  71. while(i)
  72. {
  73. rpn_expr_close(&(res->rpn[i]));
  74. i--;
  75. }
  76. rpn_expr_err:
  77. err = errno;
  78. free(res->rpn_res);
  79. rpn_malloc_err:
  80. err = errno;
  81. if(res->self_mem)
  82. {
  83. munmap(res->mem, params->mem_sz);
  84. }
  85. mmap_err:
  86. err = errno;
  87. free(res);
  88. error:
  89. err = errno;
  90. errno = err;
  91. return NULL;
  92. }
  93. void rpn_if_free(rpn_if_t* rif)
  94. {
  95. size_t i;
  96. for(i=0; i<rif->params->rpn_sz; i++)
  97. {
  98. rpn_expr_close(&(rif->rpn[i]));
  99. }
  100. free(rif->rpn);
  101. free(rif->rpn_res);
  102. if(rif->self_mem)
  103. {
  104. munmap(rif->mem, rif->params->mem_sz);
  105. }
  106. free(rif);
  107. }
  108. size_t rpn_if_step(rpn_if_t *rif, size_t pos)
  109. {
  110. size_t i;
  111. size_t newpos;
  112. rif->params->arg_f(rif, pos, rif->rpn_args);
  113. /* WRONG ! rif->rpn_args is in rif structure and do not have to be
  114. given as argument... */
  115. for(i=0; i<rif->params->rpn_sz; i++)
  116. {
  117. rif->rpn_res[i] = rpn_expr_eval(&(rif->rpn[i]), rif->rpn_args);
  118. }
  119. //rif->params->res_f(rif, &newpos, rif->rpn_res);
  120. /* MEGA WRONG ! rif->rpn_res is in rif structure and do not have to be
  121. given as argument... */
  122. rif->params->res_f(rif, &newpos, NULL);
  123. return newpos;
  124. }
  125. int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
  126. {
  127. return rpn_if_rpn_upd_rng(rif, rpns, rif->params->rpn_sz, 0);
  128. }
  129. int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
  130. size_t offset)
  131. {
  132. size_t i;
  133. for(i=offset; i<offset+sz; i++)
  134. {
  135. /**@todo continue implementation */
  136. }
  137. return 0;
  138. }