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(alloc)18 void *U(alloc)(U(descriptor) *desc, U(size_aau) n) {
19 #ifdef HMM_AUDIT_FAIL
20 
21   if (desc->avl_tree_root)
22     AUDIT_BLOCK(PTR_REC_TO_HEAD(desc->avl_tree_root))
23 #endif
24 
25     if (desc->last_freed) {
26 #ifdef HMM_AUDIT_FAIL
27       AUDIT_BLOCK(desc->last_freed)
28 #endif
29 
30       U(into_free_collection)(desc, (head_record *)(desc->last_freed));
31 
32       desc->last_freed = 0;
33     }
34 
35   /* Add space for block header. */
36   n += HEAD_AAUS;
37 
38   /* Convert n from number of address alignment units to block alignment
39   ** units. */
40   n = DIV_ROUND_UP(n, HMM_BLOCK_ALIGN_UNIT);
41 
42   if (n < MIN_BLOCK_BAUS)
43     n = MIN_BLOCK_BAUS;
44 
45   {
46     /* Search for the first node of the bin containing the smallest
47     ** block big enough to satisfy request. */
48     ptr_record *ptr_rec_ptr =
49       U(avl_search)(
50         (U(avl_avl) *) & (desc->avl_tree_root), (U(size_bau)) n,
51         AVL_GREATER_EQUAL);
52 
53     /* If an approprate bin is found, satisfy the allocation request,
54     ** otherwise return null pointer. */
55     return(ptr_rec_ptr ?
56            U(alloc_from_bin)(desc, ptr_rec_ptr, (U(size_bau)) n) : 0);
57   }
58 }
59