stack.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "precomp_wasabi_bfc.h"
  2. #include "stack.h"
  3. //#define STACK_SIZE_INCREMENT 250
  4. // going from 250 to 32 decreases size of VM working set by several megabytes
  5. #define STACK_SIZE_INCREMENT 32
  6. StackBase::StackBase()
  7. {
  8. nslots = 0;
  9. cur = 0;
  10. stack = NULL;
  11. }
  12. StackBase::~StackBase()
  13. {
  14. if (stack != NULL) FREE(stack);
  15. }
  16. int StackBase::push(void *item, int sizeofT, int increment)
  17. {
  18. if (increment <= 0) increment = STACK_SIZE_INCREMENT;
  19. if (stack == NULL) {
  20. nslots = increment;
  21. stack = (char*)MALLOC(sizeofT * nslots);
  22. } else if (cur >= nslots) {
  23. int newnslots = nslots + increment;
  24. stack = (char*)REALLOC(stack, sizeofT*newnslots);
  25. nslots = newnslots;
  26. }
  27. MEMCPY(stack + cur*sizeofT, item, sizeofT);
  28. cur++;
  29. return cur;
  30. }
  31. int StackBase::peek() {
  32. return cur;
  33. }
  34. int StackBase::peekAt(void *ptr, int n, int sizeofT) {
  35. if (ptr != NULL) MEMCPY(ptr, stack + (cur-1-n)*sizeofT, sizeofT);
  36. return cur;
  37. }
  38. int StackBase::getRef(void **ptr, int n, int sizeofT) {
  39. if (ptr != NULL) *ptr = stack + (cur-1-n)*sizeofT;
  40. return cur;
  41. }
  42. void *StackBase::top(int sizeofT) {
  43. ASSERT(cur >= 0);
  44. if (cur == 0) return NULL;
  45. return stack + (cur-1)*sizeofT;
  46. }
  47. int StackBase::pop(void *ptr, int sizeofT) {
  48. ASSERT(cur >= 0);
  49. if (cur == 0) return 0;
  50. ASSERT(stack != NULL);
  51. --cur;
  52. if (ptr != NULL) MEMCPY(ptr, stack + cur*sizeofT, sizeofT);
  53. return 1;
  54. }
  55. int StackBase::isempty() {
  56. return cur == 0;
  57. }
  58. void StackBase::purge() {
  59. ASSERT(isempty());
  60. if (stack != NULL) {
  61. FREE(stack);
  62. stack = NULL;
  63. }
  64. }