1 | /* $Id: bs3-cmn-MemAlloc.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3MemAlloc
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "bs3kit-template-header.h"
|
---|
42 | #include "bs3-cmn-memory.h"
|
---|
43 | #include <iprt/asm.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | #undef Bs3MemAlloc
|
---|
47 | BS3_CMN_DEF(void BS3_FAR *, Bs3MemAlloc,(BS3MEMKIND enmKind, size_t cb))
|
---|
48 | {
|
---|
49 | void BS3_FAR *pvRet;
|
---|
50 | uint8_t idxSlabList;
|
---|
51 |
|
---|
52 | #if ARCH_BITS == 16
|
---|
53 | /* Don't try allocate memory which address we cannot return. */
|
---|
54 | if ( enmKind != BS3MEMKIND_REAL
|
---|
55 | && BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode))
|
---|
56 | enmKind = BS3MEMKIND_REAL;
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | idxSlabList = bs3MemSizeToSlabListIndex(cb);
|
---|
60 | if (idxSlabList < BS3_MEM_SLAB_LIST_COUNT)
|
---|
61 | {
|
---|
62 | /*
|
---|
63 | * Try allocate a chunk from the list.
|
---|
64 | */
|
---|
65 | PBS3SLABHEAD pHead = enmKind == BS3MEMKIND_REAL
|
---|
66 | ? &g_aBs3LowSlabLists[idxSlabList]
|
---|
67 | : &g_aBs3UpperTiledSlabLists[idxSlabList];
|
---|
68 |
|
---|
69 | BS3_ASSERT(g_aBs3LowSlabLists[idxSlabList].cbChunk >= cb);
|
---|
70 | pvRet = Bs3SlabListAlloc(pHead);
|
---|
71 | if (pvRet)
|
---|
72 | { /* likely */ }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | /*
|
---|
76 | * Grow the list.
|
---|
77 | */
|
---|
78 | PBS3SLABCTL pNew = (PBS3SLABCTL)Bs3SlabAlloc( enmKind == BS3MEMKIND_REAL
|
---|
79 | ? &g_Bs3Mem4KLow.Core
|
---|
80 | : &g_Bs3Mem4KUpperTiled.Core);
|
---|
81 | BS3_ASSERT(((uintptr_t)pNew & 0xfff) == 0);
|
---|
82 | if (pNew)
|
---|
83 | {
|
---|
84 | uint16_t const cbHdr = g_cbBs3SlabCtlSizesforLists[idxSlabList];
|
---|
85 | BS3_XPTR_AUTO(void, pvNew);
|
---|
86 | BS3_XPTR_SET(void, pvNew, pNew);
|
---|
87 |
|
---|
88 | Bs3SlabInit(pNew, cbHdr, BS3_XPTR_GET_FLAT(void, pvNew) + cbHdr, _4K - cbHdr, pHead->cbChunk);
|
---|
89 | Bs3SlabListAdd(pHead, pNew);
|
---|
90 |
|
---|
91 | pvRet = Bs3SlabListAlloc(pHead);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | /*
|
---|
98 | * Allocate one or more pages.
|
---|
99 | */
|
---|
100 | size_t const cbAligned = RT_ALIGN_Z(cb, _4K);
|
---|
101 | uint16_t const cPages = cbAligned >> 12 /* div _4K */;
|
---|
102 | PBS3SLABCTL pSlabCtl = enmKind == BS3MEMKIND_REAL
|
---|
103 | ? &g_Bs3Mem4KLow.Core : &g_Bs3Mem4KUpperTiled.Core;
|
---|
104 |
|
---|
105 | pvRet = Bs3SlabAllocEx(pSlabCtl,
|
---|
106 | cPages,
|
---|
107 | cPages <= _64K / _4K ? BS3_SLAB_ALLOC_F_SAME_TILE : 0);
|
---|
108 | }
|
---|
109 | return pvRet;
|
---|
110 | }
|
---|
111 |
|
---|