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.8KB

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