VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-MemAlloc.c@ 61533

Last change on this file since 61533 was 60676, checked in by vboxsync, 9 years ago

bs3kit: updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: bs3-cmn-MemAlloc.c 60676 2016-04-24 11:04:57Z 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#undef Bs3MemAlloc
36BS3_CMN_DEF(void BS3_FAR *, Bs3MemAlloc,(BS3MEMKIND enmKind, size_t cb))
37{
38 void BS3_FAR *pvRet;
39 uint8_t idxSlabList;
40
41#if ARCH_BITS == 16
42 /* Don't try allocate memory which address we cannot return. */
43 if ( enmKind != BS3MEMKIND_REAL
44 && BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode))
45 enmKind = BS3MEMKIND_REAL;
46#endif
47
48 idxSlabList = bs3MemSizeToSlabListIndex(cb);
49 if (idxSlabList < BS3_MEM_SLAB_LIST_COUNT)
50 {
51 /*
52 * Try allocate a chunk from the list.
53 */
54 PBS3SLABHEAD pHead = enmKind == BS3MEMKIND_REAL
55 ? &g_aBs3LowSlabLists[idxSlabList]
56 : &g_aBs3UpperTiledSlabLists[idxSlabList];
57
58 BS3_ASSERT(g_aBs3LowSlabLists[idxSlabList].cbChunk >= cb);
59 pvRet = Bs3SlabListAlloc(pHead);
60 if (pvRet)
61 { /* likely */ }
62 else
63 {
64 /*
65 * Grow the list.
66 */
67 PBS3SLABCTL pNew = (PBS3SLABCTL)Bs3SlabAlloc( enmKind == BS3MEMKIND_REAL
68 ? &g_Bs3Mem4KLow.Core
69 : &g_Bs3Mem4KUpperTiled.Core);
70 BS3_ASSERT(((uintptr_t)pNew & 0xfff) == 0);
71 if (pNew)
72 {
73 uint16_t const cbHdr = g_cbBs3SlabCtlSizesforLists[idxSlabList];
74 BS3_XPTR_AUTO(void, pvNew);
75 BS3_XPTR_SET(void, pvNew, pNew);
76
77 Bs3SlabInit(pNew, cbHdr, BS3_XPTR_GET_FLAT(void, pvNew) + cbHdr, _4K - cbHdr, pHead->cbChunk);
78 Bs3SlabListAdd(pHead, pNew);
79
80 pvRet = Bs3SlabListAlloc(pHead);
81 }
82 }
83 }
84 else
85 {
86 /*
87 * Allocate one or more pages.
88 */
89 size_t const cbAligned = RT_ALIGN_Z(cb, _4K);
90 uint16_t const cPages = cbAligned >> 12 /* div _4K */;
91 PBS3SLABCTL pSlabCtl = enmKind == BS3MEMKIND_REAL
92 ? &g_Bs3Mem4KLow.Core : &g_Bs3Mem4KUpperTiled.Core;
93
94 pvRet = Bs3SlabAllocEx(pSlabCtl,
95 cPages,
96 cPages <= _64K / _4K ? BS3_SLAB_ALLOC_F_SAME_TILE : 0);
97 }
98 return pvRet;
99}
100
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette