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