1 | /* $Id: bs3-cmn-SlabAllocEx.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3SlabAllocEx
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2020 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 <iprt/asm.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | #undef Bs3SlabAllocEx
|
---|
36 | BS3_CMN_DEF(void BS3_FAR *, Bs3SlabAllocEx,(PBS3SLABCTL pSlabCtl, uint16_t cChunks, uint16_t fFlags))
|
---|
37 | {
|
---|
38 | BS3_ASSERT(cChunks > 0);
|
---|
39 | if (pSlabCtl->cFreeChunks >= cChunks)
|
---|
40 | {
|
---|
41 | int32_t iBit = ASMBitFirstClear(&pSlabCtl->bmAllocated, pSlabCtl->cChunks);
|
---|
42 | if (iBit >= 0)
|
---|
43 | {
|
---|
44 | BS3_ASSERT(!ASMBitTest(&pSlabCtl->bmAllocated, iBit));
|
---|
45 |
|
---|
46 | while ((uint32_t)iBit + cChunks <= pSlabCtl->cChunks)
|
---|
47 | {
|
---|
48 | /* Check that we've got the requested number of free chunks here. */
|
---|
49 | uint16_t i;
|
---|
50 | for (i = 1; i < cChunks; i++)
|
---|
51 | if (ASMBitTest(&pSlabCtl->bmAllocated, iBit + i))
|
---|
52 | break;
|
---|
53 | if (i == cChunks)
|
---|
54 | {
|
---|
55 | /* Check if the chunks are all in the same tiled segment. */
|
---|
56 | BS3_XPTR_AUTO(void, pvRet);
|
---|
57 | BS3_XPTR_SET_FLAT(void, pvRet,
|
---|
58 | BS3_XPTR_GET_FLAT(uint8_t, pSlabCtl->pbStart) + ((uint32_t)iBit << pSlabCtl->cChunkShift));
|
---|
59 | if ( !(fFlags & BS3_SLAB_ALLOC_F_SAME_TILE)
|
---|
60 | || (BS3_XPTR_GET_FLAT(void, pvRet) >> 16)
|
---|
61 | == ((BS3_XPTR_GET_FLAT(void, pvRet) + ((uint32_t)cChunks << pSlabCtl->cChunkShift) - 1) >> 16) )
|
---|
62 | {
|
---|
63 | /* Complete the allocation. */
|
---|
64 | void *fpRet;
|
---|
65 | for (i = 0; i < cChunks; i++)
|
---|
66 | ASMBitSet(&pSlabCtl->bmAllocated, iBit + i);
|
---|
67 | pSlabCtl->cFreeChunks -= cChunks;
|
---|
68 | fpRet = BS3_XPTR_GET(void, pvRet);
|
---|
69 | #if ARCH_BITS == 16
|
---|
70 | BS3_ASSERT(fpRet != NULL);
|
---|
71 | #endif
|
---|
72 | return fpRet;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * We're crossing a tiled segment boundrary.
|
---|
77 | * Skip to the start of the next segment and retry there.
|
---|
78 | * (We already know that the first chunk in the next tiled
|
---|
79 | * segment is free, otherwise we wouldn't have a crossing.)
|
---|
80 | */
|
---|
81 | BS3_ASSERT(((uint32_t)cChunks << pSlabCtl->cChunkShift) <= _64K);
|
---|
82 | i = BS3_XPTR_GET_FLAT_LOW(void, pvRet);
|
---|
83 | i = UINT16_C(0) - i;
|
---|
84 | i >>= pSlabCtl->cChunkShift;
|
---|
85 | iBit += i;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | /*
|
---|
90 | * Continue searching.
|
---|
91 | */
|
---|
92 | iBit = ASMBitNextClear(&pSlabCtl->bmAllocated, pSlabCtl->cChunks, iBit + i);
|
---|
93 | if (iBit < 0)
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 |
|
---|