OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 | 11 |
12 /* This code is in the public domain. | 12 /* This code is in the public domain. |
13 ** Version: 1.1 Author: Walt Karas | 13 ** Version: 1.1 Author: Walt Karas |
14 */ | 14 */ |
15 | 15 |
16 #include "hmm_intrnl.h" | 16 #include "hmm_intrnl.h" |
17 | 17 |
18 int U(resize)(U(descriptor) *desc, void *mem, U(size_aau) n) | 18 int U(resize)(U(descriptor) *desc, void *mem, U(size_aau) n) { |
19 { | 19 U(size_aau) i; |
20 U(size_aau) i; | 20 head_record *next_head_ptr; |
21 head_record *next_head_ptr; | 21 head_record *head_ptr = PTR_REC_TO_HEAD(mem); |
22 head_record *head_ptr = PTR_REC_TO_HEAD(mem); | |
23 | 22 |
24 /* Flag. */ | 23 /* Flag. */ |
25 int next_block_free; | 24 int next_block_free; |
26 | 25 |
27 /* Convert n from desired block size in AAUs to BAUs. */ | 26 /* Convert n from desired block size in AAUs to BAUs. */ |
28 n += HEAD_AAUS; | 27 n += HEAD_AAUS; |
29 n = DIV_ROUND_UP(n, HMM_BLOCK_ALIGN_UNIT); | 28 n = DIV_ROUND_UP(n, HMM_BLOCK_ALIGN_UNIT); |
30 | 29 |
31 if (n < MIN_BLOCK_BAUS) | 30 if (n < MIN_BLOCK_BAUS) |
32 n = MIN_BLOCK_BAUS; | 31 n = MIN_BLOCK_BAUS; |
33 | 32 |
34 #ifdef HMM_AUDIT_FAIL | 33 #ifdef HMM_AUDIT_FAIL |
35 | 34 |
36 AUDIT_BLOCK(head_ptr) | 35 AUDIT_BLOCK(head_ptr) |
37 | 36 |
38 if (!IS_BLOCK_ALLOCATED(head_ptr)) | 37 if (!IS_BLOCK_ALLOCATED(head_ptr)) |
39 HMM_AUDIT_FAIL | 38 HMM_AUDIT_FAIL |
40 | 39 |
41 if (desc->avl_tree_root) | 40 if (desc->avl_tree_root) |
42 AUDIT_BLOCK(PTR_REC_TO_HEAD(desc->avl_tree_root)) | 41 AUDIT_BLOCK(PTR_REC_TO_HEAD(desc->avl_tree_root)) |
43 | 42 |
44 #endif | 43 #endif |
45 | 44 |
46 i = head_ptr->block_size; | 45 i = head_ptr->block_size; |
| 46 |
| 47 next_head_ptr = |
| 48 (head_record *) BAUS_FORWARD(head_ptr, head_ptr->block_size); |
| 49 |
| 50 next_block_free = |
| 51 (next_head_ptr == desc->last_freed) || |
| 52 !IS_BLOCK_ALLOCATED(next_head_ptr); |
| 53 |
| 54 if (next_block_free) |
| 55 /* Block can expand into next free block. */ |
| 56 i += BLOCK_BAUS(next_head_ptr); |
| 57 |
| 58 if (n > i) |
| 59 /* Not enough room for block to expand. */ |
| 60 return(-1); |
| 61 |
| 62 if (next_block_free) { |
| 63 #ifdef HMM_AUDIT_FAIL |
| 64 AUDIT_BLOCK(next_head_ptr) |
| 65 #endif |
| 66 |
| 67 if (next_head_ptr == desc->last_freed) |
| 68 desc->last_freed = 0; |
| 69 else |
| 70 U(out_of_free_collection)(desc, next_head_ptr); |
47 | 71 |
48 next_head_ptr = | 72 next_head_ptr = |
49 (head_record *) BAUS_FORWARD(head_ptr, head_ptr->block_size); | 73 (head_record *) BAUS_FORWARD(head_ptr, (U(size_bau)) i); |
| 74 } |
50 | 75 |
51 next_block_free = | 76 /* Set i to number of "extra" BAUs. */ |
52 (next_head_ptr == desc->last_freed) || | 77 i -= n; |
53 !IS_BLOCK_ALLOCATED(next_head_ptr); | |
54 | 78 |
55 if (next_block_free) | 79 if (i < MIN_BLOCK_BAUS) |
56 /* Block can expand into next free block. */ | 80 /* Not enough extra BAUs to be a block on their own, so just keep them |
57 i += BLOCK_BAUS(next_head_ptr); | 81 ** in the block being resized. |
| 82 */ |
| 83 { |
| 84 n += i; |
| 85 i = n; |
| 86 } else { |
| 87 /* There are enough "leftover" BAUs in the next block to |
| 88 ** form a remainder block. */ |
58 | 89 |
59 if (n > i) | 90 head_record *rem_head_ptr; |
60 /* Not enough room for block to expand. */ | |
61 return(-1); | |
62 | 91 |
63 if (next_block_free) | 92 rem_head_ptr = (head_record *) BAUS_FORWARD(head_ptr, n); |
64 { | 93 |
| 94 rem_head_ptr->previous_block_size = (U(size_bau)) n; |
| 95 rem_head_ptr->block_size = (U(size_bau)) i; |
| 96 |
| 97 if (desc->last_freed) { |
65 #ifdef HMM_AUDIT_FAIL | 98 #ifdef HMM_AUDIT_FAIL |
66 AUDIT_BLOCK(next_head_ptr) | 99 AUDIT_BLOCK(desc->last_freed) |
67 #endif | 100 #endif |
68 | 101 |
69 if (next_head_ptr == desc->last_freed) | 102 U(into_free_collection)(desc, (head_record *)(desc->last_freed)); |
70 desc->last_freed = 0; | |
71 else | |
72 U(out_of_free_collection)(desc, next_head_ptr); | |
73 | 103 |
74 next_head_ptr = | 104 desc->last_freed = 0; |
75 (head_record *) BAUS_FORWARD(head_ptr, (U(size_bau)) i); | |
76 } | 105 } |
77 | 106 |
78 /* Set i to number of "extra" BAUs. */ | 107 desc->last_freed = rem_head_ptr; |
79 i -= n; | 108 } |
80 | 109 |
81 if (i < MIN_BLOCK_BAUS) | 110 head_ptr->block_size = (U(size_bau)) n; |
82 /* Not enough extra BAUs to be a block on their own, so just keep them | 111 next_head_ptr->previous_block_size = (U(size_bau)) i; |
83 ** in the block being resized. | |
84 */ | |
85 { | |
86 n += i; | |
87 i = n; | |
88 } | |
89 else | |
90 { | |
91 /* There are enough "leftover" BAUs in the next block to | |
92 ** form a remainder block. */ | |
93 | 112 |
94 head_record *rem_head_ptr; | 113 return(0); |
95 | |
96 rem_head_ptr = (head_record *) BAUS_FORWARD(head_ptr, n); | |
97 | |
98 rem_head_ptr->previous_block_size = (U(size_bau)) n; | |
99 rem_head_ptr->block_size = (U(size_bau)) i; | |
100 | |
101 if (desc->last_freed) | |
102 { | |
103 #ifdef HMM_AUDIT_FAIL | |
104 AUDIT_BLOCK(desc->last_freed) | |
105 #endif | |
106 | |
107 U(into_free_collection)(desc, (head_record *)(desc->last_freed)); | |
108 | |
109 desc->last_freed = 0; | |
110 } | |
111 | |
112 desc->last_freed = rem_head_ptr; | |
113 } | |
114 | |
115 head_ptr->block_size = (U(size_bau)) n; | |
116 next_head_ptr->previous_block_size = (U(size_bau)) i; | |
117 | |
118 return(0); | |
119 } | 114 } |
OLD | NEW |