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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. {
  22. rpn_if_t *res;
  23. int err;
  24. res = malloc(sizeof(rpn_if_t));
  25. if(!res)
  26. {
  27. goto error;
  28. }
  29. memcpy(&res->params, params, sizeof(rpn_if_param_t));
  30. res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
  31. -1, 0);
  32. if(res->mem == (void*)-1)
  33. {
  34. goto mmap_err;
  35. }
  36. res->rpn_res = malloc(sizeof(rpn_value_t) *
  37. (params->rpn_sz + params->rpn_argc));
  38. if(!res->rpn_res)
  39. {
  40. goto rpn_malloc_err;
  41. }
  42. res->rpn_args = &(res->rpn_res[params->rpn_sz]);
  43. res->rpn = malloc(sizeof(rpn_expr_t) * params->rpn_sz);
  44. if(!res->rpn)
  45. {
  46. goto rpn_expr_err;
  47. }
  48. return res;
  49. rpn_expr_err:
  50. err = errno;
  51. free(res->rpn_res);
  52. rpn_malloc_err:
  53. err = errno;
  54. munmap(res->mem, params->mem_sz);
  55. mmap_err:
  56. err = errno;
  57. free(res);
  58. error:
  59. err = errno;
  60. errno = err;
  61. return NULL;
  62. }
  63. void rpn_if_free(rpn_if_t* rif)
  64. {
  65. size_t i;
  66. for(i=0; i<rif->params.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->params.mem_sz);
  73. free(rif);
  74. }
  75. size_t rpn_if_step(rpn_if_t *rif, size_t pos)
  76. {
  77. size_t i;
  78. size_t newpos;
  79. rif->params.arg_f(rif, pos, rif->rpn_args);
  80. /* WRONG ! rif->rpn_args is in rif structure and do not have to be
  81. given as argument... */
  82. for(i=0; i<rif->params.rpn_sz; i++)
  83. {
  84. rif->rpn_res[i] = rpn_expr_eval(&(rif->rpn[i]), rif->rpn_args);
  85. }
  86. //rif->params.res_f(rif, &newpos, rif->rpn_res);
  87. /* MEGA WRONG ! rif->rpn_res is in rif structure and do not have to be
  88. given as argument... */
  89. rif->params.res_f(rif, &newpos, NULL);
  90. return newpos;
  91. }
  92. int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
  93. {
  94. return rpn_if_rpn_upd_rng(rif, rpns, rif->params.rpn_sz, 0);
  95. }
  96. int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
  97. size_t offset)
  98. {
  99. size_t i;
  100. for(i=offset; i<offset+sz; i++)
  101. {
  102. /**@todo continue implementation */
  103. }
  104. return 0;
  105. }