Initial WebM release
[libvpx.git] / vpx_mem / memory_manager / hmm_base.c
blob0cacc3f8fcd0a9bcf640d08863f38846a9e31918
1 /*
2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
8 */
11 /* This code is in the public domain.
12 ** Version: 1.1 Author: Walt Karas
15 #include "hmm_intrnl.h"
17 void U(init)(U(descriptor) *desc)
19 desc->avl_tree_root = 0;
20 desc->last_freed = 0;
23 /* Remove a free block from a bin's doubly-linked list when it is not,
24 ** the first block in the bin.
26 void U(dll_remove)(
27 /* Pointer to pointer record in the block to be removed. */
28 ptr_record *to_remove)
30 to_remove->prev->next = to_remove->next;
32 if (to_remove->next)
33 to_remove->next->prev = to_remove->prev;
36 /* Put a block into the free collection of a heap.
38 void U(into_free_collection)(
39 /* Pointer to heap descriptor. */
40 U(descriptor) *desc,
41 /* Pointer to head record of block. */
42 head_record *head_ptr)
44 ptr_record *ptr_rec_ptr = HEAD_TO_PTR_REC(head_ptr);
46 ptr_record *bin_front_ptr =
47 U(avl_insert)((U(avl_avl) *) & (desc->avl_tree_root), ptr_rec_ptr);
49 if (bin_front_ptr != ptr_rec_ptr)
51 /* The block was not inserted into the AVL tree because there is
52 ** already a bin for the size of the block. */
54 MARK_SUCCESSIVE_BLOCK_IN_FREE_BIN(head_ptr)
55 ptr_rec_ptr->self = ptr_rec_ptr;
57 /* Make the block the new second block in the bin's doubly-linked
58 ** list. */
59 ptr_rec_ptr->prev = bin_front_ptr;
60 ptr_rec_ptr->next = bin_front_ptr->next;
61 bin_front_ptr->next = ptr_rec_ptr;
63 if (ptr_rec_ptr->next)
64 ptr_rec_ptr->next->prev = ptr_rec_ptr;
66 else
67 /* Block is first block in new bin. */
68 ptr_rec_ptr->next = 0;
71 /* Allocate a block from a given bin. Returns a pointer to the payload
72 ** of the removed block. The "last freed" pointer must be null prior
73 ** to calling this function.
75 void *U(alloc_from_bin)(
76 /* Pointer to heap descriptor. */
77 U(descriptor) *desc,
78 /* Pointer to pointer record of first block in bin. */
79 ptr_record *bin_front_ptr,
80 /* Number of BAUs needed in the allocated block. If the block taken
81 ** from the bin is significantly larger than the number of BAUs needed,
82 ** the "extra" BAUs are split off to form a new free block. */
83 U(size_bau) n_baus)
85 head_record *head_ptr;
86 U(size_bau) rem_baus;
88 if (bin_front_ptr->next)
90 /* There are multiple blocks in this bin. Use the 2nd block in
91 ** the bin to avoid needless change to the AVL tree.
94 ptr_record *ptr_rec_ptr = bin_front_ptr->next;
95 head_ptr = PTR_REC_TO_HEAD(ptr_rec_ptr);
97 #ifdef AUDIT_FAIL
98 AUDIT_BLOCK(head_ptr)
99 #endif
101 U(dll_remove)(ptr_rec_ptr);
103 else
105 /* There is only one block in the bin, so it has to be removed
106 ** from the AVL tree.
109 head_ptr = PTR_REC_TO_HEAD(bin_front_ptr);
111 U(avl_remove)(
112 (U(avl_avl) *) &(desc->avl_tree_root), BLOCK_BAUS(head_ptr));
115 MARK_BLOCK_ALLOCATED(head_ptr)
117 rem_baus = BLOCK_BAUS(head_ptr) - n_baus;
119 if (rem_baus >= MIN_BLOCK_BAUS)
121 /* Since there are enough "extra" BAUs, split them off to form
122 ** a new free block.
125 head_record *rem_head_ptr =
126 (head_record *) BAUS_FORWARD(head_ptr, n_baus);
128 /* Change the next block's header to reflect the fact that the
129 ** block preceeding it is now smaller.
131 SET_PREV_BLOCK_BAUS(
132 BAUS_FORWARD(head_ptr, head_ptr->block_size), rem_baus)
134 head_ptr->block_size = n_baus;
136 rem_head_ptr->previous_block_size = n_baus;
137 rem_head_ptr->block_size = rem_baus;
139 desc->last_freed = rem_head_ptr;
142 return(HEAD_TO_PTR_REC(head_ptr));
145 /* Take a block out of the free collection.
147 void U(out_of_free_collection)(
148 /* Descriptor of heap that block is in. */
149 U(descriptor) *desc,
150 /* Pointer to head of block to take out of free collection. */
151 head_record *head_ptr)
153 ptr_record *ptr_rec_ptr = HEAD_TO_PTR_REC(head_ptr);
155 if (ptr_rec_ptr->self == ptr_rec_ptr)
156 /* Block is not the front block in its bin, so all we have to
157 ** do is take it out of the bin's doubly-linked list. */
158 U(dll_remove)(ptr_rec_ptr);
159 else
161 ptr_record *next = ptr_rec_ptr->next;
163 if (next)
164 /* Block is the front block in its bin, and there is at least
165 ** one other block in the bin. Substitute the next block for
166 ** the front block. */
167 U(avl_subst)((U(avl_avl) *) &(desc->avl_tree_root), next);
168 else
169 /* Block is the front block in its bin, but there is no other
170 ** block in the bin. Eliminate the bin. */
171 U(avl_remove)(
172 (U(avl_avl) *) &(desc->avl_tree_root), BLOCK_BAUS(head_ptr));
176 void U(free)(U(descriptor) *desc, void *payload_ptr)
178 /* Flags if coalesce with adjacent block. */
179 int coalesce;
181 head_record *fwd_head_ptr;
182 head_record *free_head_ptr = PTR_REC_TO_HEAD(payload_ptr);
184 desc->num_baus_can_shrink = 0;
186 #ifdef HMM_AUDIT_FAIL
188 AUDIT_BLOCK(free_head_ptr)
190 /* Make sure not freeing an already free block. */
191 if (!IS_BLOCK_ALLOCATED(free_head_ptr))
192 HMM_AUDIT_FAIL
194 if (desc->avl_tree_root)
195 /* Audit root block in AVL tree. */
196 AUDIT_BLOCK(PTR_REC_TO_HEAD(desc->avl_tree_root))
198 #endif
200 fwd_head_ptr =
201 (head_record *) BAUS_FORWARD(free_head_ptr, free_head_ptr->block_size);
203 if (free_head_ptr->previous_block_size)
205 /* Coalesce with backward block if possible. */
207 head_record *bkwd_head_ptr =
208 (head_record *) BAUS_BACKWARD(
209 free_head_ptr, free_head_ptr->previous_block_size);
211 #ifdef HMM_AUDIT_FAIL
212 AUDIT_BLOCK(bkwd_head_ptr)
213 #endif
215 if (bkwd_head_ptr == (head_record *)(desc->last_freed))
217 desc->last_freed = 0;
218 coalesce = 1;
220 else if (IS_BLOCK_ALLOCATED(bkwd_head_ptr))
221 coalesce = 0;
222 else
224 U(out_of_free_collection)(desc, bkwd_head_ptr);
225 coalesce = 1;
228 if (coalesce)
230 bkwd_head_ptr->block_size += free_head_ptr->block_size;
231 SET_PREV_BLOCK_BAUS(fwd_head_ptr, BLOCK_BAUS(bkwd_head_ptr))
232 free_head_ptr = bkwd_head_ptr;
236 if (fwd_head_ptr->block_size == 0)
238 /* Block to be freed is last block before dummy end-of-chunk block. */
239 desc->end_of_shrinkable_chunk =
240 BAUS_FORWARD(fwd_head_ptr, DUMMY_END_BLOCK_BAUS);
241 desc->num_baus_can_shrink = BLOCK_BAUS(free_head_ptr);
243 if (PREV_BLOCK_BAUS(free_head_ptr) == 0)
244 /* Free block is the entire chunk, so shrinking can eliminate
245 ** entire chunk including dummy end block. */
246 desc->num_baus_can_shrink += DUMMY_END_BLOCK_BAUS;
248 else
250 /* Coalesce with forward block if possible. */
252 #ifdef HMM_AUDIT_FAIL
253 AUDIT_BLOCK(fwd_head_ptr)
254 #endif
256 if (fwd_head_ptr == (head_record *)(desc->last_freed))
258 desc->last_freed = 0;
259 coalesce = 1;
261 else if (IS_BLOCK_ALLOCATED(fwd_head_ptr))
262 coalesce = 0;
263 else
265 U(out_of_free_collection)(desc, fwd_head_ptr);
266 coalesce = 1;
269 if (coalesce)
271 free_head_ptr->block_size += fwd_head_ptr->block_size;
273 fwd_head_ptr =
274 (head_record *) BAUS_FORWARD(
275 fwd_head_ptr, BLOCK_BAUS(fwd_head_ptr));
277 SET_PREV_BLOCK_BAUS(fwd_head_ptr, BLOCK_BAUS(free_head_ptr))
279 if (fwd_head_ptr->block_size == 0)
281 /* Coalesced block to be freed is last block before dummy
282 ** end-of-chunk block. */
283 desc->end_of_shrinkable_chunk =
284 BAUS_FORWARD(fwd_head_ptr, DUMMY_END_BLOCK_BAUS);
285 desc->num_baus_can_shrink = BLOCK_BAUS(free_head_ptr);
287 if (PREV_BLOCK_BAUS(free_head_ptr) == 0)
288 /* Free block is the entire chunk, so shrinking can
289 ** eliminate entire chunk including dummy end block. */
290 desc->num_baus_can_shrink += DUMMY_END_BLOCK_BAUS;
295 if (desc->last_freed)
297 /* There is a last freed block, but it is not adjacent to the
298 ** block being freed by this call to free, so put the last
299 ** freed block into the free collection.
302 #ifdef HMM_AUDIT_FAIL
303 AUDIT_BLOCK(desc->last_freed)
304 #endif
306 U(into_free_collection)(desc, (head_record *)(desc->last_freed));
309 desc->last_freed = free_head_ptr;
312 void U(new_chunk)(U(descriptor) *desc, void *start, U(size_bau) n_baus)
314 #ifdef HMM_AUDIT_FAIL
316 if (desc->avl_tree_root)
317 /* Audit root block in AVL tree. */
318 AUDIT_BLOCK(PTR_REC_TO_HEAD(desc->avl_tree_root))
319 #endif
321 #undef HEAD_PTR
322 #define HEAD_PTR ((head_record *) start)
324 /* Make the chunk one big free block followed by a dummy end block.
327 n_baus -= DUMMY_END_BLOCK_BAUS;
329 HEAD_PTR->previous_block_size = 0;
330 HEAD_PTR->block_size = n_baus;
332 U(into_free_collection)(desc, HEAD_PTR);
334 /* Set up the dummy end block. */
335 start = BAUS_FORWARD(start, n_baus);
336 HEAD_PTR->previous_block_size = n_baus;
337 HEAD_PTR->block_size = 0;
339 #undef HEAD_PTR
342 #ifdef HMM_AUDIT_FAIL
344 /* Function that does audit fail actions defined my preprocessor symbol,
345 ** and returns a dummy integer value.
347 int U(audit_block_fail_dummy_return)(void)
349 HMM_AUDIT_FAIL
351 /* Dummy return. */
352 return(0);
355 #endif
357 /* AVL Tree instantiation. */
359 #ifdef HMM_AUDIT_FAIL
361 /* The AVL tree generic package passes an ACCESS of 1 when it "touches"
362 ** a child node for the first time during a particular operation. I use
363 ** this feature to audit only one time (per operation) the free blocks
364 ** that are tree nodes. Since the root node is not a child node, it has
365 ** to be audited directly.
368 /* The pain you feel while reading these macros will not be in vain. It
369 ** will remove all doubt from you mind that C++ inline functions are
370 ** a very good thing.
373 #define AVL_GET_LESS(H, ACCESS) \
374 (((ACCESS) ? AUDIT_BLOCK_AS_EXPR(PTR_REC_TO_HEAD(H)) : 0), (H)->self)
375 #define AVL_GET_GREATER(H, ACCESS) \
376 (((ACCESS) ? AUDIT_BLOCK_AS_EXPR(PTR_REC_TO_HEAD(H)) : 0), (H)->prev)
378 #else
380 #define AVL_GET_LESS(H, ACCESS) ((H)->self)
381 #define AVL_GET_GREATER(H, ACCESS) ((H)->prev)
383 #endif
385 #define AVL_SET_LESS(H, LH) (H)->self = (LH);
386 #define AVL_SET_GREATER(H, GH) (H)->prev = (GH);
388 /* high bit of high bit of
389 ** block_size previous_block_size balance factor
390 ** ----------- ------------------- --------------
391 ** 0 0 n/a (block allocated)
392 ** 0 1 1
393 ** 1 0 -1
394 ** 1 1 0
397 #define AVL_GET_BALANCE_FACTOR(H) \
398 ((((head_record *) (PTR_REC_TO_HEAD(H)))->block_size & \
399 HIGH_BIT_BAU_SIZE) ? \
400 (((head_record *) (PTR_REC_TO_HEAD(H)))->previous_block_size & \
401 HIGH_BIT_BAU_SIZE ? 0 : -1) : 1)
403 #define AVL_SET_BALANCE_FACTOR(H, BF) \
405 register head_record *p = \
406 (head_record *) PTR_REC_TO_HEAD(H); \
407 register int bal_f = (BF); \
409 if (bal_f <= 0) \
410 p->block_size |= HIGH_BIT_BAU_SIZE; \
411 else \
412 p->block_size &= ~HIGH_BIT_BAU_SIZE; \
413 if (bal_f >= 0) \
414 p->previous_block_size |= HIGH_BIT_BAU_SIZE; \
415 else \
416 p->previous_block_size &= ~HIGH_BIT_BAU_SIZE; \
419 #define COMPARE_KEY_KEY(K1, K2) ((K1) == (K2) ? 0 : ((K1) > (K2) ? 1 : -1))
421 #define AVL_COMPARE_KEY_NODE(K, H) \
422 COMPARE_KEY_KEY(K, BLOCK_BAUS(PTR_REC_TO_HEAD(H)))
424 #define AVL_COMPARE_NODE_NODE(H1, H2) \
425 COMPARE_KEY_KEY(BLOCK_BAUS(PTR_REC_TO_HEAD(H1)), \
426 BLOCK_BAUS(PTR_REC_TO_HEAD(H2)))
428 #define AVL_NULL ((ptr_record *) 0)
430 #define AVL_IMPL_MASK \
431 ( AVL_IMPL_INSERT | AVL_IMPL_SEARCH | AVL_IMPL_REMOVE | AVL_IMPL_SUBST )
433 #include "cavl_impl.h"