1 | /** @file
|
---|
2 | * VBox Host Guest Shared Memory Interface (HGSMI) - Memory allocator.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2014-2016 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | #ifndef ___VBox_Graphics_HGSMIMemAlloc_h
|
---|
28 | #define ___VBox_Graphics_HGSMIMemAlloc_h
|
---|
29 |
|
---|
30 | #include "HGSMIDefs.h"
|
---|
31 | #include "VBoxVideoIPRT.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /* Descriptor. */
|
---|
35 | #define HGSMI_MA_DESC_OFFSET_MASK UINT32_C(0xFFFFFFE0)
|
---|
36 | #define HGSMI_MA_DESC_FREE_MASK UINT32_C(0x00000010)
|
---|
37 | #define HGSMI_MA_DESC_ORDER_MASK UINT32_C(0x0000000F)
|
---|
38 |
|
---|
39 | #define HGSMI_MA_DESC_OFFSET(d) ((d) & HGSMI_MA_DESC_OFFSET_MASK)
|
---|
40 | #define HGSMI_MA_DESC_IS_FREE(d) (((d) & HGSMI_MA_DESC_FREE_MASK) != 0)
|
---|
41 | #define HGSMI_MA_DESC_ORDER(d) ((d) & HGSMI_MA_DESC_ORDER_MASK)
|
---|
42 |
|
---|
43 | #define HGSMI_MA_DESC_ORDER_BASE UINT32_C(5)
|
---|
44 |
|
---|
45 | #define HGSMI_MA_BLOCK_SIZE_MIN (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + 0))
|
---|
46 | #define HGSMI_MA_BLOCK_SIZE_MAX (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + HGSMI_MA_DESC_ORDER_MASK))
|
---|
47 |
|
---|
48 | /* HGSMI_MA_DESC_ORDER_BASE must correspond to HGSMI_MA_DESC_OFFSET_MASK. */
|
---|
49 | AssertCompile((~HGSMI_MA_DESC_OFFSET_MASK + 1) == HGSMI_MA_BLOCK_SIZE_MIN);
|
---|
50 |
|
---|
51 |
|
---|
52 | typedef struct HGSMIMABLOCK
|
---|
53 | {
|
---|
54 | RTLISTNODE nodeBlock;
|
---|
55 | RTLISTNODE nodeFree;
|
---|
56 | HGSMIOFFSET descriptor;
|
---|
57 | } HGSMIMABLOCK;
|
---|
58 |
|
---|
59 | typedef struct HGSMIMADATA
|
---|
60 | {
|
---|
61 | HGSMIAREA area;
|
---|
62 | HGSMIENV env;
|
---|
63 | HGSMISIZE cbMaxBlock;
|
---|
64 |
|
---|
65 | uint32_t cBlocks; /* How many blocks in the listBlocks. */
|
---|
66 | RTLISTANCHOR listBlocks; /* All memory blocks, sorted. */
|
---|
67 | RTLISTANCHOR aListFreeBlocks[HGSMI_MA_DESC_ORDER_MASK + 1]; /* For free blocks of each order. */
|
---|
68 | } HGSMIMADATA;
|
---|
69 |
|
---|
70 | RT_C_DECLS_BEGIN
|
---|
71 |
|
---|
72 | int HGSMIMAInit(HGSMIMADATA *pMA, const HGSMIAREA *pArea,
|
---|
73 | HGSMIOFFSET *paDescriptors, uint32_t cDescriptors, HGSMISIZE cbMaxBlock,
|
---|
74 | const HGSMIENV *pEnv);
|
---|
75 | void HGSMIMAUninit(HGSMIMADATA *pMA);
|
---|
76 |
|
---|
77 | void *HGSMIMAAlloc(HGSMIMADATA *pMA, HGSMISIZE cb);
|
---|
78 | void HGSMIMAFree(HGSMIMADATA *pMA, void *pv);
|
---|
79 |
|
---|
80 | HGSMIMABLOCK *HGSMIMASearchOffset(HGSMIMADATA *pMA, HGSMIOFFSET off);
|
---|
81 |
|
---|
82 | uint32_t HGSMIPopCnt32(uint32_t u32);
|
---|
83 |
|
---|
84 | DECLINLINE(HGSMISIZE) HGSMIMAOrder2Size(HGSMIOFFSET order)
|
---|
85 | {
|
---|
86 | return (UINT32_C(1) << (HGSMI_MA_DESC_ORDER_BASE + order));
|
---|
87 | }
|
---|
88 |
|
---|
89 | DECLINLINE(HGSMIOFFSET) HGSMIMASize2Order(HGSMISIZE cb)
|
---|
90 | {
|
---|
91 | HGSMIOFFSET order = HGSMIPopCnt32(cb - 1) - HGSMI_MA_DESC_ORDER_BASE;
|
---|
92 | #ifdef HGSMI_STRICT
|
---|
93 | Assert(HGSMIMAOrder2Size(order) == cb);
|
---|
94 | #endif
|
---|
95 | return order;
|
---|
96 | }
|
---|
97 |
|
---|
98 | RT_C_DECLS_END
|
---|
99 |
|
---|
100 | #endif /* !___VBox_Graphics_HGSMIMemAlloc_h */
|
---|