[69015] | 1 | /* $Id: HGSMIMemAlloc.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[50481] | 2 | /** @file
|
---|
[68672] | 3 | * VBox Host Guest Shared Memory Interface (HGSMI) - Memory allocator.
|
---|
[50481] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2014-2023 Oracle and/or its affiliates.
|
---|
[50481] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[50481] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[50481] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[50481] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[50481] | 35 | */
|
---|
| 36 |
|
---|
[76558] | 37 | #ifndef VBOX_INCLUDED_Graphics_HGSMIMemAlloc_h
|
---|
| 38 | #define VBOX_INCLUDED_Graphics_HGSMIMemAlloc_h
|
---|
[76507] | 39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 40 | # pragma once
|
---|
| 41 | #endif
|
---|
[50481] | 42 |
|
---|
[68672] | 43 | #include "HGSMIDefs.h"
|
---|
| 44 | #include "VBoxVideoIPRT.h"
|
---|
[50481] | 45 |
|
---|
| 46 |
|
---|
| 47 | /* Descriptor. */
|
---|
| 48 | #define HGSMI_MA_DESC_OFFSET_MASK UINT32_C(0xFFFFFFE0)
|
---|
| 49 | #define HGSMI_MA_DESC_FREE_MASK UINT32_C(0x00000010)
|
---|
| 50 | #define HGSMI_MA_DESC_ORDER_MASK UINT32_C(0x0000000F)
|
---|
| 51 |
|
---|
| 52 | #define HGSMI_MA_DESC_OFFSET(d) ((d) & HGSMI_MA_DESC_OFFSET_MASK)
|
---|
| 53 | #define HGSMI_MA_DESC_IS_FREE(d) (((d) & HGSMI_MA_DESC_FREE_MASK) != 0)
|
---|
| 54 | #define HGSMI_MA_DESC_ORDER(d) ((d) & HGSMI_MA_DESC_ORDER_MASK)
|
---|
| 55 |
|
---|
| 56 | #define HGSMI_MA_DESC_ORDER_BASE UINT32_C(5)
|
---|
| 57 |
|
---|
| 58 | #define HGSMI_MA_BLOCK_SIZE_MIN (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + 0))
|
---|
| 59 | #define HGSMI_MA_BLOCK_SIZE_MAX (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + HGSMI_MA_DESC_ORDER_MASK))
|
---|
| 60 |
|
---|
| 61 | /* HGSMI_MA_DESC_ORDER_BASE must correspond to HGSMI_MA_DESC_OFFSET_MASK. */
|
---|
| 62 | AssertCompile((~HGSMI_MA_DESC_OFFSET_MASK + 1) == HGSMI_MA_BLOCK_SIZE_MIN);
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | typedef struct HGSMIMABLOCK
|
---|
| 66 | {
|
---|
| 67 | RTLISTNODE nodeBlock;
|
---|
| 68 | RTLISTNODE nodeFree;
|
---|
| 69 | HGSMIOFFSET descriptor;
|
---|
| 70 | } HGSMIMABLOCK;
|
---|
| 71 |
|
---|
| 72 | typedef struct HGSMIMADATA
|
---|
| 73 | {
|
---|
| 74 | HGSMIAREA area;
|
---|
| 75 | HGSMIENV env;
|
---|
| 76 | HGSMISIZE cbMaxBlock;
|
---|
| 77 |
|
---|
| 78 | uint32_t cBlocks; /* How many blocks in the listBlocks. */
|
---|
| 79 | RTLISTANCHOR listBlocks; /* All memory blocks, sorted. */
|
---|
| 80 | RTLISTANCHOR aListFreeBlocks[HGSMI_MA_DESC_ORDER_MASK + 1]; /* For free blocks of each order. */
|
---|
| 81 | } HGSMIMADATA;
|
---|
| 82 |
|
---|
| 83 | RT_C_DECLS_BEGIN
|
---|
| 84 |
|
---|
| 85 | int HGSMIMAInit(HGSMIMADATA *pMA, const HGSMIAREA *pArea,
|
---|
| 86 | HGSMIOFFSET *paDescriptors, uint32_t cDescriptors, HGSMISIZE cbMaxBlock,
|
---|
| 87 | const HGSMIENV *pEnv);
|
---|
| 88 | void HGSMIMAUninit(HGSMIMADATA *pMA);
|
---|
| 89 |
|
---|
[71590] | 90 | void RT_UNTRUSTED_VOLATILE_HSTGST *HGSMIMAAlloc(HGSMIMADATA *pMA, HGSMISIZE cb);
|
---|
| 91 | void HGSMIMAFree(HGSMIMADATA *pMA, void RT_UNTRUSTED_VOLATILE_GUEST *pv);
|
---|
[50481] | 92 |
|
---|
| 93 | HGSMIMABLOCK *HGSMIMASearchOffset(HGSMIMADATA *pMA, HGSMIOFFSET off);
|
---|
| 94 |
|
---|
| 95 | uint32_t HGSMIPopCnt32(uint32_t u32);
|
---|
| 96 |
|
---|
| 97 | DECLINLINE(HGSMISIZE) HGSMIMAOrder2Size(HGSMIOFFSET order)
|
---|
| 98 | {
|
---|
| 99 | return (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + order));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | DECLINLINE(HGSMIOFFSET) HGSMIMASize2Order(HGSMISIZE cb)
|
---|
| 103 | {
|
---|
| 104 | HGSMIOFFSET order = HGSMIPopCnt32(cb - 1) - HGSMI_MA_DESC_ORDER_BASE;
|
---|
[64650] | 105 | #ifdef HGSMI_STRICT
|
---|
[50481] | 106 | Assert(HGSMIMAOrder2Size(order) == cb);
|
---|
[64650] | 107 | #endif
|
---|
[50481] | 108 | return order;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | RT_C_DECLS_END
|
---|
| 112 |
|
---|
[76585] | 113 | #endif /* !VBOX_INCLUDED_Graphics_HGSMIMemAlloc_h */
|
---|