1 | /* $Id: hgsmimemalloc.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2017-2020 Oracle Corporation
|
---|
4 | *
|
---|
5 | * Permission is hereby granted, free of charge, to any person
|
---|
6 | * obtaining a copy of this software and associated documentation
|
---|
7 | * files (the "Software"), to deal in the Software without
|
---|
8 | * restriction, including without limitation the rights to use,
|
---|
9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
10 | * copies of the Software, and to permit persons to whom the
|
---|
11 | * Software is furnished to do so, subject to the following
|
---|
12 | * conditions:
|
---|
13 | *
|
---|
14 | * The above copyright notice and this permission notice shall be
|
---|
15 | * included in all copies or substantial portions of the Software.
|
---|
16 | *
|
---|
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
24 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*
|
---|
28 | * Memory allocator
|
---|
29 | * ----------------
|
---|
30 | *
|
---|
31 | * Implementation
|
---|
32 | * --------------
|
---|
33 | *
|
---|
34 | * Since the X.Org driver is single threaded and works using an allocate,
|
---|
35 | * submit and free pattern, we replace the generic allocator with a simple
|
---|
36 | * Boolean. Need more be said?
|
---|
37 | *
|
---|
38 | * bird> Yes, it's buggy. You never set fAllocated. See HGSMIMAAlloc().
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include <VBoxVideoIPRT.h>
|
---|
42 | #include <HGSMIMemAlloc.h>
|
---|
43 | #include <HGSMI.h>
|
---|
44 |
|
---|
45 | int HGSMIMAInit(HGSMIMADATA *pMA, const HGSMIAREA *pArea,
|
---|
46 | HGSMIOFFSET *paDescriptors, uint32_t cDescriptors, HGSMISIZE cbMaxBlock,
|
---|
47 | const HGSMIENV *pEnv)
|
---|
48 | {
|
---|
49 | (void)paDescriptors;
|
---|
50 | (void)cDescriptors;
|
---|
51 | (void)cbMaxBlock;
|
---|
52 | (void)pEnv;
|
---|
53 | if (!(pArea->cbArea < UINT32_C(0x80000000)))
|
---|
54 | return VERR_INVALID_PARAMETER;
|
---|
55 | if (!(pArea->cbArea >= HGSMI_MA_BLOCK_SIZE_MIN))
|
---|
56 | return VERR_INVALID_PARAMETER;
|
---|
57 |
|
---|
58 | pMA->area = *pArea;
|
---|
59 | pMA->fAllocated = false;
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void HGSMIMAUninit(HGSMIMADATA *pMA)
|
---|
64 | {
|
---|
65 | (void)pMA;
|
---|
66 | }
|
---|
67 |
|
---|
68 | static HGSMIOFFSET HGSMIMAPointerToOffset(const HGSMIMADATA *pMA, const void RT_UNTRUSTED_VOLATILE_GUEST *pv)
|
---|
69 | {
|
---|
70 | if (HGSMIAreaContainsPointer(&pMA->area, pv))
|
---|
71 | return HGSMIPointerToOffset(&pMA->area, pv);
|
---|
72 |
|
---|
73 | AssertFailed();
|
---|
74 | return HGSMIOFFSET_VOID;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void RT_UNTRUSTED_VOLATILE_GUEST *HGSMIMAOffsetToPointer(const HGSMIMADATA *pMA, HGSMIOFFSET off)
|
---|
78 | {
|
---|
79 | if (HGSMIAreaContainsOffset(&pMA->area, off))
|
---|
80 | return HGSMIOffsetToPointer(&pMA->area, off);
|
---|
81 |
|
---|
82 | AssertFailed();
|
---|
83 | return NULL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void RT_UNTRUSTED_VOLATILE_GUEST *HGSMIMAAlloc(HGSMIMADATA *pMA, HGSMISIZE cb)
|
---|
87 | {
|
---|
88 | (void)cb;
|
---|
89 | if (pMA->fAllocated)
|
---|
90 | return NULL;
|
---|
91 | HGSMIOFFSET off = pMA->area.offBase;
|
---|
92 | return HGSMIMAOffsetToPointer(pMA, off);
|
---|
93 | pMA->fAllocated = true; /** @todo r=bird: Errr. what's this doing *after* the return statement? */
|
---|
94 | }
|
---|
95 |
|
---|
96 | void HGSMIMAFree(HGSMIMADATA *pMA, void RT_UNTRUSTED_VOLATILE_GUEST *pv)
|
---|
97 | {
|
---|
98 | HGSMIOFFSET off = HGSMIMAPointerToOffset(pMA, pv);
|
---|
99 | if (off != HGSMIOFFSET_VOID)
|
---|
100 | pMA->fAllocated = false;
|
---|
101 | else
|
---|
102 | AssertFailed();
|
---|
103 | }
|
---|
104 |
|
---|