1 | /* $Id: HGSMIMemAlloc.h 71590 2018-03-31 18:34:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Host Guest Shared Memory Interface (HGSMI) - Memory allocator.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2017 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 | #ifndef ___VBox_Graphics_HGSMIMemAlloc_h
|
---|
29 | #define ___VBox_Graphics_HGSMIMemAlloc_h
|
---|
30 |
|
---|
31 | #include "HGSMIDefs.h"
|
---|
32 | #include "VBoxVideoIPRT.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | /* Descriptor. */
|
---|
36 | #define HGSMI_MA_DESC_OFFSET_MASK UINT32_C(0xFFFFFFE0)
|
---|
37 | #define HGSMI_MA_DESC_FREE_MASK UINT32_C(0x00000010)
|
---|
38 | #define HGSMI_MA_DESC_ORDER_MASK UINT32_C(0x0000000F)
|
---|
39 |
|
---|
40 | #define HGSMI_MA_DESC_OFFSET(d) ((d) & HGSMI_MA_DESC_OFFSET_MASK)
|
---|
41 | #define HGSMI_MA_DESC_IS_FREE(d) (((d) & HGSMI_MA_DESC_FREE_MASK) != 0)
|
---|
42 | #define HGSMI_MA_DESC_ORDER(d) ((d) & HGSMI_MA_DESC_ORDER_MASK)
|
---|
43 |
|
---|
44 | #define HGSMI_MA_DESC_ORDER_BASE UINT32_C(5)
|
---|
45 |
|
---|
46 | #define HGSMI_MA_BLOCK_SIZE_MIN (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + 0))
|
---|
47 | #define HGSMI_MA_BLOCK_SIZE_MAX (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + HGSMI_MA_DESC_ORDER_MASK))
|
---|
48 |
|
---|
49 | /* HGSMI_MA_DESC_ORDER_BASE must correspond to HGSMI_MA_DESC_OFFSET_MASK. */
|
---|
50 | AssertCompile((~HGSMI_MA_DESC_OFFSET_MASK + 1) == HGSMI_MA_BLOCK_SIZE_MIN);
|
---|
51 |
|
---|
52 |
|
---|
53 | typedef struct HGSMIMABLOCK
|
---|
54 | {
|
---|
55 | RTLISTNODE nodeBlock;
|
---|
56 | RTLISTNODE nodeFree;
|
---|
57 | HGSMIOFFSET descriptor;
|
---|
58 | } HGSMIMABLOCK;
|
---|
59 |
|
---|
60 | typedef struct HGSMIMADATA
|
---|
61 | {
|
---|
62 | HGSMIAREA area;
|
---|
63 | HGSMIENV env;
|
---|
64 | HGSMISIZE cbMaxBlock;
|
---|
65 |
|
---|
66 | uint32_t cBlocks; /* How many blocks in the listBlocks. */
|
---|
67 | RTLISTANCHOR listBlocks; /* All memory blocks, sorted. */
|
---|
68 | RTLISTANCHOR aListFreeBlocks[HGSMI_MA_DESC_ORDER_MASK + 1]; /* For free blocks of each order. */
|
---|
69 | } HGSMIMADATA;
|
---|
70 |
|
---|
71 | RT_C_DECLS_BEGIN
|
---|
72 |
|
---|
73 | int HGSMIMAInit(HGSMIMADATA *pMA, const HGSMIAREA *pArea,
|
---|
74 | HGSMIOFFSET *paDescriptors, uint32_t cDescriptors, HGSMISIZE cbMaxBlock,
|
---|
75 | const HGSMIENV *pEnv);
|
---|
76 | void HGSMIMAUninit(HGSMIMADATA *pMA);
|
---|
77 |
|
---|
78 | void RT_UNTRUSTED_VOLATILE_HSTGST *HGSMIMAAlloc(HGSMIMADATA *pMA, HGSMISIZE cb);
|
---|
79 | void HGSMIMAFree(HGSMIMADATA *pMA, void RT_UNTRUSTED_VOLATILE_GUEST *pv);
|
---|
80 |
|
---|
81 | HGSMIMABLOCK *HGSMIMASearchOffset(HGSMIMADATA *pMA, HGSMIOFFSET off);
|
---|
82 |
|
---|
83 | uint32_t HGSMIPopCnt32(uint32_t u32);
|
---|
84 |
|
---|
85 | DECLINLINE(HGSMISIZE) HGSMIMAOrder2Size(HGSMIOFFSET order)
|
---|
86 | {
|
---|
87 | return (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + order));
|
---|
88 | }
|
---|
89 |
|
---|
90 | DECLINLINE(HGSMIOFFSET) HGSMIMASize2Order(HGSMISIZE cb)
|
---|
91 | {
|
---|
92 | HGSMIOFFSET order = HGSMIPopCnt32(cb - 1) - HGSMI_MA_DESC_ORDER_BASE;
|
---|
93 | #ifdef HGSMI_STRICT
|
---|
94 | Assert(HGSMIMAOrder2Size(order) == cb);
|
---|
95 | #endif
|
---|
96 | return order;
|
---|
97 | }
|
---|
98 |
|
---|
99 | RT_C_DECLS_END
|
---|
100 |
|
---|
101 | #endif /* !___VBox_Graphics_HGSMIMemAlloc_h */
|
---|