1 | /* $Id: bs3-cmn-MemAlloc.c 59941 2016-03-07 15:13:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3MemAlloc
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*********************************************************************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *********************************************************************************************************************************/
|
---|
30 | #include "bs3kit-template-header.h"
|
---|
31 | #include "bs3-cmn-memory.h"
|
---|
32 | #include <iprt/asm.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | BS3_DECL(void BS3_FAR *) Bs3MemAlloc(BS3MEMKIND enmKind, size_t cb)
|
---|
36 | {
|
---|
37 | void BS3_FAR *pvRet;
|
---|
38 | uint8_t idxSlabList = bs3MemSizeToSlabListIndex(cb);
|
---|
39 | if (idxSlabList < BS3_MEM_SLAB_LIST_COUNT)
|
---|
40 | {
|
---|
41 | /*
|
---|
42 | * Try allocate a chunk from the list.
|
---|
43 | */
|
---|
44 | PBS3SLABHEAD pHead = enmKind == BS3MEMKIND_REAL
|
---|
45 | ? &BS3_DATA_NM(g_aBs3LowSlabLists)[idxSlabList]
|
---|
46 | : &BS3_DATA_NM(g_aBs3UpperTiledSlabLists)[idxSlabList];
|
---|
47 |
|
---|
48 | BS3_ASSERT(BS3_DATA_NM(g_aBs3LowSlabLists)[idxSlabList].cbChunk >= cb);
|
---|
49 | pvRet = Bs3SlabListAlloc(pHead);
|
---|
50 | if (pvRet)
|
---|
51 | { /* likely */ }
|
---|
52 | else
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Grow the list.
|
---|
56 | */
|
---|
57 | PBS3SLABCTL pNew = (PBS3SLABCTL)Bs3SlabAlloc( enmKind == BS3MEMKIND_REAL
|
---|
58 | ? &BS3_DATA_NM(g_Bs3Mem4KLow).Core
|
---|
59 | : &BS3_DATA_NM(g_Bs3Mem4KUpperTiled).Core);
|
---|
60 | BS3_ASSERT(((uintptr_t)pNew & 0xfff) == 0);
|
---|
61 | if (pNew)
|
---|
62 | {
|
---|
63 | uint16_t const cbHdr = BS3_DATA_NM(g_cbBs3SlabCtlSizesforLists)[idxSlabList];
|
---|
64 | BS3_XPTR_AUTO(void, pvNew);
|
---|
65 | BS3_XPTR_SET(void, pvNew, pNew);
|
---|
66 |
|
---|
67 | Bs3SlabInit(pNew, cbHdr, BS3_XPTR_GET_FLAT(void, pvNew) + cbHdr, _4K - cbHdr, pHead->cbChunk);
|
---|
68 | Bs3SlabListAdd(pHead, pNew);
|
---|
69 |
|
---|
70 | pvRet = Bs3SlabListAlloc(pHead);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * Allocate one or more pages.
|
---|
78 | */
|
---|
79 | size_t const cbAligned = RT_ALIGN_Z(cb, _4K);
|
---|
80 | uint16_t const cPages = cbAligned >> 12 /* div _4K */;
|
---|
81 | PBS3SLABCTL pSlabCtl = enmKind == BS3MEMKIND_REAL
|
---|
82 | ? &BS3_DATA_NM(g_Bs3Mem4KLow).Core : &BS3_DATA_NM(g_Bs3Mem4KUpperTiled).Core;
|
---|
83 |
|
---|
84 | pvRet = Bs3SlabAllocEx(pSlabCtl,
|
---|
85 | cPages,
|
---|
86 | cPages <= _64K / _4K ? BS3_SLAB_ALLOC_F_SAME_TILE : 0);
|
---|
87 | }
|
---|
88 | return pvRet;
|
---|
89 | }
|
---|
90 |
|
---|