gcsx_rundata.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* GCSx
  2. ** RUNDATA.H
  3. **
  4. ** Runtime data support- stacks, variables, hashes, arrays
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. const char memFail[] = "Out of Memory Error";
  25. void createStack(Stack* s) {
  26. s->data = NULL;
  27. s->top = NULL;
  28. s->allocSize = NULL;
  29. }
  30. void destroyStack(Stack* s) {
  31. free(s->data);
  32. }
  33. void increaseStack(Stack* s) {
  34. int size;
  35. int dist;
  36. if (s->allocSize == NULL) {
  37. size = 16;
  38. dist = 0;
  39. }
  40. else {
  41. // @TODO: need to catch runaway recursion at some obscene stack size
  42. size = (s->allocSize - s->data) << 1;
  43. dist = s->top - s->data;
  44. }
  45. s->data = (StackEntry*)realloc(s->data, size * sizeof(StackEntry));
  46. if (s->data == NULL) {
  47. fatalCrash(1, memFail);
  48. }
  49. s->top = s->data + dist;
  50. s->allocSize = s->data + size;
  51. }