Changeset View
Changeset View
Standalone View
Standalone View
patsolve/memory.cpp
Show All 36 Lines | |||||
37 | MemoryManager::inscode MemoryManager::insert_node(TREE *n, int d, TREE **tree, TREE **node) | 37 | MemoryManager::inscode MemoryManager::insert_node(TREE *n, int d, TREE **tree, TREE **node) | ||
38 | { | 38 | { | ||
39 | int c; | 39 | int c; | ||
40 | quint8 *key, *tkey; | 40 | quint8 *key, *tkey; | ||
41 | TREE *t; | 41 | TREE *t; | ||
42 | 42 | | |||
43 | key = (quint8 *)n + sizeof(TREE); | 43 | key = (quint8 *)n + sizeof(TREE); | ||
44 | n->depth = d; | 44 | n->depth = d; | ||
45 | n->left = n->right = NULL; | 45 | n->left = n->right = nullptr; | ||
46 | *node = n; | 46 | *node = n; | ||
47 | t = *tree; | 47 | t = *tree; | ||
48 | if (t == NULL) { | 48 | if (t == nullptr) { | ||
49 | *tree = n; | 49 | *tree = n; | ||
50 | return NEW; | 50 | return NEW; | ||
51 | } | 51 | } | ||
52 | while (1) { | 52 | while (true) { | ||
53 | tkey = (quint8 *)t + sizeof(TREE); | 53 | tkey = (quint8 *)t + sizeof(TREE); | ||
54 | c = memcmp(key, tkey, Pilebytes); | 54 | c = memcmp(key, tkey, Pilebytes); | ||
55 | if (c == 0) { | 55 | if (c == 0) { | ||
56 | break; | 56 | break; | ||
57 | } | 57 | } | ||
58 | if (c < 0) { | 58 | if (c < 0) { | ||
59 | if (t->left == NULL) { | 59 | if (t->left == nullptr) { | ||
60 | t->left = n; | 60 | t->left = n; | ||
61 | return NEW; | 61 | return NEW; | ||
62 | } | 62 | } | ||
63 | t = t->left; | 63 | t = t->left; | ||
64 | } else { | 64 | } else { | ||
65 | if (t->right == NULL) { | 65 | if (t->right == nullptr) { | ||
66 | t->right = n; | 66 | t->right = n; | ||
67 | return NEW; | 67 | return NEW; | ||
68 | } | 68 | } | ||
69 | t = t->right; | 69 | t = t->right; | ||
70 | } | 70 | } | ||
71 | } | 71 | } | ||
72 | 72 | | |||
73 | /* We get here if it's already in the tree. Don't add it again. | 73 | /* We get here if it's already in the tree. Don't add it again. | ||
Show All 25 Lines | 97 | { | |||
99 | TREELIST *tl, *last; | 99 | TREELIST *tl, *last; | ||
100 | 100 | | |||
101 | /* Pick a bucket, any bucket. */ | 101 | /* Pick a bucket, any bucket. */ | ||
102 | 102 | | |||
103 | bucket = cluster % TBUCKETS; | 103 | bucket = cluster % TBUCKETS; | ||
104 | 104 | | |||
105 | /* Find the tree in this bucket with that cluster number. */ | 105 | /* Find the tree in this bucket with that cluster number. */ | ||
106 | 106 | | |||
107 | last = NULL; | 107 | last = nullptr; | ||
108 | for (tl = Treelist[bucket]; tl; tl = tl->next) { | 108 | for (tl = Treelist[bucket]; tl; tl = tl->next) { | ||
109 | if (tl->cluster == cluster) { | 109 | if (tl->cluster == cluster) { | ||
110 | break; | 110 | break; | ||
111 | } | 111 | } | ||
112 | last = tl; | 112 | last = tl; | ||
113 | } | 113 | } | ||
114 | 114 | | |||
115 | /* If we didn't find it, make a new one and add it to the list. */ | 115 | /* If we didn't find it, make a new one and add it to the list. */ | ||
116 | 116 | | |||
117 | if (tl == NULL) { | 117 | if (tl == nullptr) { | ||
118 | tl = mm_allocate(TREELIST); | 118 | tl = mm_allocate(TREELIST); | ||
119 | if (tl == NULL) { | 119 | if (tl == nullptr) { | ||
120 | return NULL; | 120 | return nullptr; | ||
121 | } | 121 | } | ||
122 | tl->tree = NULL; | 122 | tl->tree = nullptr; | ||
123 | tl->cluster = cluster; | 123 | tl->cluster = cluster; | ||
124 | tl->next = NULL; | 124 | tl->next = nullptr; | ||
125 | if (last == NULL) { | 125 | if (last == nullptr) { | ||
126 | Treelist[bucket] = tl; | 126 | Treelist[bucket] = tl; | ||
127 | } else { | 127 | } else { | ||
128 | last->next = tl; | 128 | last->next = tl; | ||
129 | } | 129 | } | ||
130 | } | 130 | } | ||
131 | 131 | | |||
132 | return tl; | 132 | return tl; | ||
133 | } | 133 | } | ||
134 | 134 | | |||
135 | /* Block storage. Reduces overhead, and can be freed quickly. */ | 135 | /* Block storage. Reduces overhead, and can be freed quickly. */ | ||
136 | 136 | | |||
137 | BLOCK *MemoryManager::new_block(void) | 137 | BLOCK *MemoryManager::new_block(void) | ||
138 | { | 138 | { | ||
139 | BLOCK *b; | 139 | BLOCK *b; | ||
140 | 140 | | |||
141 | b = mm_allocate(BLOCK); | 141 | b = mm_allocate(BLOCK); | ||
142 | if (b == NULL) { | 142 | if (b == nullptr) { | ||
143 | return NULL; | 143 | return nullptr; | ||
144 | } | 144 | } | ||
145 | b->block = new_array(quint8, BLOCKSIZE); | 145 | b->block = new_array(quint8, BLOCKSIZE); | ||
146 | if (b->block == NULL) { | 146 | if (b->block == nullptr) { | ||
147 | MemoryManager::free_ptr(b); | 147 | MemoryManager::free_ptr(b); | ||
148 | return NULL; | 148 | return nullptr; | ||
149 | } | 149 | } | ||
150 | b->ptr = b->block; | 150 | b->ptr = b->block; | ||
151 | b->remain = BLOCKSIZE; | 151 | b->remain = BLOCKSIZE; | ||
152 | b->next = NULL; | 152 | b->next = nullptr; | ||
153 | 153 | | |||
154 | return b; | 154 | return b; | ||
155 | } | 155 | } | ||
156 | 156 | | |||
157 | /* Like new(), only from the current block. Make a new block if necessary. */ | 157 | /* Like new(), only from the current block. Make a new block if necessary. */ | ||
158 | 158 | | |||
159 | quint8 *MemoryManager::new_from_block(size_t s) | 159 | quint8 *MemoryManager::new_from_block(size_t s) | ||
160 | { | 160 | { | ||
161 | quint8 *p; | 161 | quint8 *p; | ||
162 | BLOCK *b; | 162 | BLOCK *b; | ||
163 | 163 | | |||
164 | b = Block; | 164 | b = Block; | ||
165 | if (s > b->remain) { | 165 | if (s > b->remain) { | ||
166 | b = new_block(); | 166 | b = new_block(); | ||
167 | if (b == NULL) { | 167 | if (b == nullptr) { | ||
168 | return NULL; | 168 | return nullptr; | ||
169 | } | 169 | } | ||
170 | b->next = Block; | 170 | b->next = Block; | ||
171 | Block = b; | 171 | Block = b; | ||
172 | } | 172 | } | ||
173 | 173 | | |||
174 | p = b->ptr; | 174 | p = b->ptr; | ||
175 | b->remain -= s; | 175 | b->remain -= s; | ||
176 | b->ptr += s; | 176 | b->ptr += s; | ||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | |||||
223 | 223 | | |||
224 | /* Allocate some space and return a pointer to it. See new() in util.h. */ | 224 | /* Allocate some space and return a pointer to it. See new() in util.h. */ | ||
225 | 225 | | |||
226 | void *MemoryManager::allocate_memory(size_t s) | 226 | void *MemoryManager::allocate_memory(size_t s) | ||
227 | { | 227 | { | ||
228 | void *x; | 228 | void *x; | ||
229 | 229 | | |||
230 | if (s > Mem_remain) { | 230 | if (s > Mem_remain) { | ||
231 | return NULL; | 231 | return nullptr; | ||
232 | } | 232 | } | ||
233 | 233 | | |||
234 | if ((x = (void *)malloc(s)) == NULL) { | 234 | // use calloc to ensure that the memory is zeroed | ||
235 | return NULL; | 235 | if ((x = calloc(1, s)) == nullptr) { | ||
236 | return nullptr; | ||||
236 | } | 237 | } | ||
237 | 238 | | |||
238 | memset( x, 0, s ); | | |||
239 | Mem_remain -= s; | 239 | Mem_remain -= s; | ||
240 | return x; | 240 | return x; | ||
241 | } | 241 | } |