dataStack.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This file is part of the PikaScript project.
  3. * http://github.com/pikastech/pikascript
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon 李昂 liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. #include "dataStack.h"
  28. #include "dataQueue.h"
  29. void stack_reset(Stack* stack) {
  30. stack->sp = (uint8_t*)arg_getContent(stack->stack_pyload);
  31. stack->sp_size = (int16_t*)arg_getContent(stack->stack_size_array);
  32. stack->top = 0;
  33. }
  34. int32_t stack_init(Stack* stack) {
  35. stack->stack_pyload =
  36. arg_setContent(NULL, NULL, PIKA_STACK_BUFF_SIZE);
  37. stack->stack_size_array =
  38. arg_setContent(NULL, NULL, PIKA_STACK_BUFF_SIZE / 4);
  39. stack_reset(stack);
  40. return 0;
  41. };
  42. void stack_pushSize(Stack* stack, int16_t size) {
  43. *(stack->sp_size) = size;
  44. stack->sp_size++;
  45. }
  46. int16_t stack_popSize(Stack* stack) {
  47. stack->sp_size--;
  48. return *(stack->sp_size);
  49. }
  50. int32_t stack_deinit(Stack* stack) {
  51. arg_deinit(stack->stack_pyload);
  52. arg_deinit(stack->stack_size_array);
  53. return 0;
  54. }
  55. void stack_pushPyload(Stack* stack, uint8_t* content, size_t size) {
  56. __platform_memcpy(stack->sp, content, size);
  57. stack->sp += size;
  58. }
  59. uint8_t* stack_popPyload(Stack* stack, size_t size) {
  60. stack->sp -= size;
  61. return stack->sp;
  62. }
  63. int32_t stack_pushArg(Stack* stack, Arg* arg) {
  64. stack->top++;
  65. size_t size = arg_getTotleSize(arg);
  66. //! if you unsure about the __impl_pikaMalloc, uncomment this to force alignment
  67. #if PIKA_ARG_ALIGN_ENABLE
  68. /* force alignment to avoid unaligned access */
  69. size = (size + 4 - 1) & ~(4 - 1);
  70. #endif
  71. stack_pushSize(stack, size);
  72. stack_pushPyload(stack, arg, size);
  73. arg_deinit(arg);
  74. return 0;
  75. }
  76. int32_t stack_pushStr(Stack* stack, char* str) {
  77. Arg* newArg = arg_setStr(NULL, "", str);
  78. return stack_pushArg(stack, newArg);
  79. }
  80. Arg* stack_popArg(Stack* stack) {
  81. if (stack->top == 0) {
  82. return NULL;
  83. }
  84. stack->top--;
  85. int16_t size = stack_popSize(stack);
  86. Arg* res = arg_copy((Arg*)stack_popPyload(stack, size));
  87. return res;
  88. }
  89. char* stack_popStr(Stack* stack, char* outBuff) {
  90. Arg* arg = stack_popArg(stack);
  91. strcpy(outBuff, arg_getStr(arg));
  92. arg_deinit(arg);
  93. return outBuff;
  94. }
  95. int8_t stack_getTop(Stack* stack) {
  96. return stack->top;
  97. }