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

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