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 
4 #include "util.h"
5 #include "datastack.h"
6 
7 #define STACK_DELTA    100
8 
9 int
DataStack_init(Stats * pstats,DataStack * pdata_stack)10 DataStack_init(Stats *pstats, DataStack *pdata_stack)
11 {
12     pdata_stack->depth = -1;
13     pdata_stack->stack = NULL;
14     pdata_stack->alloc = 0;
15     return RET_OK;
16 }
17 
18 void
DataStack_dealloc(Stats * pstats,DataStack * pdata_stack)19 DataStack_dealloc(Stats *pstats, DataStack *pdata_stack)
20 {
21     PyMem_Free(pdata_stack->stack);
22 }
23 
24 int
DataStack_grow(Stats * pstats,DataStack * pdata_stack)25 DataStack_grow(Stats *pstats, DataStack *pdata_stack)
26 {
27     pdata_stack->depth++;
28     if (pdata_stack->depth >= pdata_stack->alloc) {
29         /* We've outgrown our data_stack array: make it bigger. */
30         int bigger = pdata_stack->alloc + STACK_DELTA;
31         DataStackEntry * bigger_data_stack = PyMem_Realloc(pdata_stack->stack, bigger * sizeof(DataStackEntry));
32         STATS( pstats->stack_reallocs++; )
33         if (bigger_data_stack == NULL) {
34             PyErr_NoMemory();
35             pdata_stack->depth--;
36             return RET_ERROR;
37         }
38         pdata_stack->stack = bigger_data_stack;
39         pdata_stack->alloc = bigger;
40     }
41     return RET_OK;
42 }
43