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