• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 
12 /* This code is in the public domain.
13 ** Version: 1.1  Author: Walt Karas
14 */
15 
16 #include "hmm_intrnl.h"
17 
U(resize)18 int U(resize)(U(descriptor) *desc, void *mem, U(size_aau) n) {
19   U(size_aau) i;
20   head_record *next_head_ptr;
21   head_record *head_ptr = PTR_REC_TO_HEAD(mem);
22 
23   /* Flag. */
24   int next_block_free;
25 
26   /* Convert n from desired block size in AAUs to BAUs. */
27   n += HEAD_AAUS;
28   n = DIV_ROUND_UP(n, HMM_BLOCK_ALIGN_UNIT);
29 
30   if (n < MIN_BLOCK_BAUS)
31     n = MIN_BLOCK_BAUS;
32 
33 #ifdef HMM_AUDIT_FAIL
34 
35   AUDIT_BLOCK(head_ptr)
36 
37   if (!IS_BLOCK_ALLOCATED(head_ptr))
38     HMM_AUDIT_FAIL
39 
40     if (desc->avl_tree_root)
41       AUDIT_BLOCK(PTR_REC_TO_HEAD(desc->avl_tree_root))
42 
43 #endif
44 
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);
71 
72     next_head_ptr =
73       (head_record *) BAUS_FORWARD(head_ptr, (U(size_bau)) i);
74   }
75 
76   /* Set i to number of "extra" BAUs. */
77   i -= n;
78 
79   if (i < MIN_BLOCK_BAUS)
80     /* Not enough extra BAUs to be a block on their own, so just keep them
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. */
89 
90     head_record *rem_head_ptr;
91 
92     rem_head_ptr = (head_record *) BAUS_FORWARD(head_ptr, n);
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) {
98 #ifdef HMM_AUDIT_FAIL
99       AUDIT_BLOCK(desc->last_freed)
100 #endif
101 
102       U(into_free_collection)(desc, (head_record *)(desc->last_freed));
103 
104       desc->last_freed = 0;
105     }
106 
107     desc->last_freed = rem_head_ptr;
108   }
109 
110   head_ptr->block_size = (U(size_bau)) n;
111   next_head_ptr->previous_block_size = (U(size_bau)) i;
112 
113   return(0);
114 }
115