Line data Source code
1 : /* Copyright 2017 noseglasses <shinynoseglasses@gmail.com>
2 : *
3 : * This program is free software: you can redistribute it and/or modify
4 : * it under the terms of the GNU Lesser General Public License as published by
5 : * the Free Software Foundation, either version 3 of the License, or
6 : * (at your option) any later version.
7 : *
8 : * This program is distributed in the hope that it will be useful,
9 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 : * GNU Lesser General Public License for more details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 : */
16 :
17 : #include "detail/ppg_furcation_detail.h"
18 : #include "detail/ppg_context_detail.h"
19 : #include "detail/ppg_pattern_detail.h"
20 : #include "ppg_debug.h"
21 : #include "detail/ppg_malloc_detail.h"
22 :
23 : #include <stdlib.h>
24 : #include <assert.h>
25 : #include <string.h>
26 :
27 41 : void ppg_furcation_stack_init(PPG_Furcation_Stack *stack)
28 : {
29 41 : stack->furcations = NULL;
30 41 : stack->n_furcations = 0;
31 41 : stack->cur_furcation = -1;
32 41 : stack->max_furcations = 0;
33 41 : }
34 :
35 41 : void ppg_furcation_stack_resize(PPG_Furcation_Stack *stack,
36 : PPG_Count new_size)
37 : {
38 41 : PPG_ASSERT(stack);
39 :
40 41 : if(new_size <= stack->max_furcations) { return; }
41 :
42 41 : PPG_Furcation *new_furcations
43 41 : = (PPG_Furcation*)PPG_MALLOC(sizeof(PPG_Furcation)*new_size);
44 :
45 41 : stack->max_furcations = new_size;
46 :
47 41 : if(stack->furcations && (stack->n_furcations > 0)) {
48 0 : memcpy(new_furcations, stack->furcations,
49 0 : sizeof(PPG_Furcation)*stack->n_furcations);
50 : }
51 :
52 41 : stack->furcations = new_furcations;
53 : }
54 :
55 41 : void ppg_furcation_stack_restore(PPG_Furcation_Stack *stack)
56 : {
57 41 : PPG_ASSERT(stack);
58 :
59 41 : PPG_Count saved_size = stack->max_furcations;
60 :
61 : // Enable this method to be used for initialization
62 : // during context compilation
63 : //
64 41 : if(ppg_context && ppg_context->tree_depth > saved_size) {
65 32 : saved_size = ppg_context->tree_depth;
66 : }
67 :
68 41 : stack->furcations = NULL;
69 41 : stack->max_furcations = 0;
70 :
71 41 : ppg_furcation_stack_resize(stack, saved_size);
72 41 : }
73 :
74 41 : void ppg_furcation_stack_free(PPG_Furcation_Stack *stack)
75 : {
76 41 : if(!stack->furcations) { return; }
77 :
78 32 : free(stack->furcations);
79 :
80 32 : stack->furcations = NULL;
81 : }
82 :
|