datastack.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 */
  2. /* For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt */
  3. #include "util.h"
  4. #include "datastack.h"
  5. #define STACK_DELTA 100
  6. int
  7. DataStack_init(Stats *pstats, DataStack *pdata_stack)
  8. {
  9. pdata_stack->depth = -1;
  10. pdata_stack->stack = NULL;
  11. pdata_stack->alloc = 0;
  12. return RET_OK;
  13. }
  14. void
  15. DataStack_dealloc(Stats *pstats, DataStack *pdata_stack)
  16. {
  17. PyMem_Free(pdata_stack->stack);
  18. }
  19. int
  20. DataStack_grow(Stats *pstats, DataStack *pdata_stack)
  21. {
  22. pdata_stack->depth++;
  23. if (pdata_stack->depth >= pdata_stack->alloc) {
  24. /* We've outgrown our data_stack array: make it bigger. */
  25. int bigger = pdata_stack->alloc + STACK_DELTA;
  26. DataStackEntry * bigger_data_stack = PyMem_Realloc(pdata_stack->stack, bigger * sizeof(DataStackEntry));
  27. STATS( pstats->stack_reallocs++; )
  28. if (bigger_data_stack == NULL) {
  29. PyErr_NoMemory();
  30. pdata_stack->depth--;
  31. return RET_ERROR;
  32. }
  33. pdata_stack->stack = bigger_data_stack;
  34. pdata_stack->alloc = bigger;
  35. }
  36. return RET_OK;
  37. }