-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_functions.cpp
More file actions
176 lines (129 loc) · 3.64 KB
/
Copy pathstack_functions.cpp
File metadata and controls
176 lines (129 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "config.h"
#include <string.h>
#include <math.h>
#include "log.h"
#include <malloc.h>
#include <assert.h>
#ifdef DEBUG
#include "check_stack.hpp"
#else
#include "stack_functions.hpp"
#endif
void stack_ctor_ (Stack_t *stack, size_t capacity, const char * stack_name, const char * func_name, const char * file_name, const int line)
{
assert (stack);
stack->capacity = capacity;
stack->size = 0;
#ifdef CANARYCHECK
stack->data = create_data_canary (stack);
stack->left_canary = LEFT_CANARY;
stack->right_canary = RIGHT_CANARY;
#else
stack->data = (Elem_t *) calloc (1, stack->capacity*sizeof(Elem_t));
#endif
fill_info (stack, func_name, file_name, stack_name, line);
for (unsigned index = 0; index < capacity; index++)
{
stack_data_fill (&stack->data[index]);;
}
#ifdef HASHCHECK
calculate_stack_hash (stack);
#endif
#ifdef DEBUG
assert_ok (stack);
#endif
}
void fill_info (Stack_t *stack, const char *func_name, const char *file_name, const char *stack_name, int line)
{
stack->stack_info.func_name = func_name;
stack->stack_info.file_name = file_name;
stack->stack_info.stack_name = stack_name;
stack->stack_info.line = line;
}
void stack_data_fill (int *stack_data)
{
assert (stack_data);
*stack_data = int_poison;
}
void stack_data_fill (double *stack_data)
{
assert (stack_data);
*stack_data = NAN;
}
void stack_dtor (Stack_t *stack)
{
#ifdef DEBUG
assert_ok (stack);
#endif
stack->capacity = -1;
stack->size = -2;
free (stack->data);
stack = NULL;
}
void *recalloc(void *ptr, size_t num, size_t size)
{
size_t temp_size = _msize(ptr);
ptr = realloc (ptr, size*num);
if (!ptr and (num*size > temp_size))
memset ((char *) ptr + temp_size, 0, num*size - temp_size);
return ptr;
}
void stack_realloc (Stack_t *stack)
{
stack->capacity *= multiple;
if (stack->capacity == 0)
stack->capacity++;
#ifdef CANARYCHECK
stack->data = recreate_data_canary (stack);
#else
stack->data = (Elem_t*) recalloc (stack->data,stack->capacity*sizeof (Elem_t), 1);
#endif
for (unsigned index = stack->size; index < stack->capacity; index++)
stack_data_fill(&stack->data[index]);
}
void stack_push (Stack_t *stack, Elem_t value)
{
#ifdef DEBUG
assert_ok (stack);
#endif
if (stack->size >= stack->capacity)
stack_realloc (stack);
stack->data[stack->size++] = value;
#ifdef HASHCHECK
calculate_stack_hash (stack);
#endif
}
Elem_t stack_pop (Stack_t *stack, int *err)
{
#ifdef DEBUG
assert_ok (stack);
#endif
Elem_t value = 0;
if (err) *err = 1;
if (stack->size > 0)
{
value = stack->data[--stack->size];
stack_data_fill (&stack->data[stack->size]);
stack_fit (stack);
if (err) *err = 0;
#ifdef HASHCHECK
calculate_stack_hash (stack);
#endif
}
return value;
}
void stack_fit (Stack_t *stack)
{
#ifdef DEBUG
assert (stack);
#endif
if (stack->size*multiple <= stack->capacity and stack->size != 0)
{
stack->capacity /= multiple;
#ifdef CANARYCHECK
stack->data = recreate_data_canary (stack);
#else
stack->data = (Elem_t*) recalloc (stack->data,stack->capacity*sizeof (Elem_t), 1);
#endif
}
}