1 | /* $Id: bs3-cmn-MemAlloc.c 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3MemAlloc
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2019 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "bs3kit-template-header.h"
|
---|
32 | #include "bs3-cmn-memory.h"
|
---|
33 | #include <iprt/asm.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | #undef Bs3MemAlloc
|
---|
37 | BS3_CMN_DEF(void BS3_FAR *, Bs3MemAlloc,(BS3MEMKIND enmKind, size_t cb))
|
---|
38 | {
|
---|
39 | void BS3_FAR *pvRet;
|
---|
40 | uint8_t idxSlabList;
|
---|
41 |
|
---|
42 | #if ARCH_BITS == 16
|
---|
43 | /* Don't try allocate memory which address we cannot return. */
|
---|
44 | if ( enmKind != BS3MEMKIND_REAL
|
---|
45 | && BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode))
|
---|
46 | enmKind = BS3MEMKIND_REAL;
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | idxSlabList = bs3MemSizeToSlabListIndex(cb);
|
---|
50 | if (idxSlabList < BS3_MEM_SLAB_LIST_COUNT)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * Try allocate a chunk from the list.
|
---|
54 | */
|
---|
55 | PBS3SLABHEAD pHead = enmKind == BS3MEMKIND_REAL
|
---|
56 | ? &g_aBs3LowSlabLists[idxSlabList]
|
---|
57 | : &g_aBs3UpperTiledSlabLists[idxSlabList];
|
---|
58 |
|
---|
59 | BS3_ASSERT(g_aBs3LowSlabLists[idxSlabList].cbChunk >= cb);
|
---|
60 | pvRet = Bs3SlabListAlloc(pHead);
|
---|
61 | if (pvRet)
|
---|
62 | { /* likely */ }
|
---|
63 | else
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | * Grow the list.
|
---|
67 | */
|
---|
68 | PBS3SLABCTL pNew = (PBS3SLABCTL)Bs3SlabAlloc( enmKind == BS3MEMKIND_REAL
|
---|
69 | ? &g_Bs3Mem4KLow.Core
|
---|
70 | : &g_Bs3Mem4KUpperTiled.Core);
|
---|
71 | BS3_ASSERT(((uintptr_t)pNew & 0xfff) == 0);
|
---|
72 | if (pNew)
|
---|
73 | {
|
---|
74 | uint16_t const cbHdr = g_cbBs3SlabCtlSizesforLists[idxSlabList];
|
---|
75 | BS3_XPTR_AUTO(void, pvNew);
|
---|
76 | BS3_XPTR_SET(void, pvNew, pNew);
|
---|
77 |
|
---|
78 | Bs3SlabInit(pNew, cbHdr, BS3_XPTR_GET_FLAT(void, pvNew) + cbHdr, _4K - cbHdr, pHead->cbChunk);
|
---|
79 | Bs3SlabListAdd(pHead, pNew);
|
---|
80 |
|
---|
81 | pvRet = Bs3SlabListAlloc(pHead);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | /*
|
---|
88 | * Allocate one or more pages.
|
---|
89 | */
|
---|
90 | size_t const cbAligned = RT_ALIGN_Z(cb, _4K);
|
---|
91 | uint16_t const cPages = cbAligned >> 12 /* div _4K */;
|
---|
92 | PBS3SLABCTL pSlabCtl = enmKind == BS3MEMKIND_REAL
|
---|
93 | ? &g_Bs3Mem4KLow.Core : &g_Bs3Mem4KUpperTiled.Core;
|
---|
94 |
|
---|
95 | pvRet = Bs3SlabAllocEx(pSlabCtl,
|
---|
96 | cPages,
|
---|
97 | cPages <= _64K / _4K ? BS3_SLAB_ALLOC_F_SAME_TILE : 0);
|
---|
98 | }
|
---|
99 | return pvRet;
|
---|
100 | }
|
---|
101 |
|
---|