1 | /* $Id: HGCMObjects.h 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HGCMObjects - Host-Guest Communication Manager objects header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef MAIN_INCLUDED_HGCMObjects_h
|
---|
29 | #define MAIN_INCLUDED_HGCMObjects_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/avl.h>
|
---|
36 | #include <iprt/critsect.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 |
|
---|
39 | class HGCMObject;
|
---|
40 |
|
---|
41 | typedef struct ObjectAVLCore
|
---|
42 | {
|
---|
43 | AVLU32NODECORE AvlCore;
|
---|
44 | HGCMObject *pSelf;
|
---|
45 | } ObjectAVLCore;
|
---|
46 |
|
---|
47 | typedef enum
|
---|
48 | {
|
---|
49 | HGCMOBJ_CLIENT,
|
---|
50 | HGCMOBJ_THREAD,
|
---|
51 | HGCMOBJ_MSG,
|
---|
52 | HGCMOBJ_SizeHack = 0x7fffffff
|
---|
53 | } HGCMOBJ_TYPE;
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * A referenced object.
|
---|
58 | */
|
---|
59 | class HGCMReferencedObject
|
---|
60 | {
|
---|
61 | private:
|
---|
62 | int32_t volatile m_cRefs;
|
---|
63 | HGCMOBJ_TYPE m_enmObjType;
|
---|
64 |
|
---|
65 | protected:
|
---|
66 | virtual ~HGCMReferencedObject()
|
---|
67 | {}
|
---|
68 |
|
---|
69 | public:
|
---|
70 | HGCMReferencedObject(HGCMOBJ_TYPE enmObjType)
|
---|
71 | : m_cRefs(0) /** @todo change to 1! */
|
---|
72 | , m_enmObjType(enmObjType)
|
---|
73 | {}
|
---|
74 |
|
---|
75 | void Reference()
|
---|
76 | {
|
---|
77 | int32_t cRefs = ASMAtomicIncS32(&m_cRefs);
|
---|
78 | NOREF(cRefs);
|
---|
79 | Log(("Reference(%p/%d): cRefs = %d\n", this, m_enmObjType, cRefs));
|
---|
80 | }
|
---|
81 |
|
---|
82 | void Dereference()
|
---|
83 | {
|
---|
84 | int32_t cRefs = ASMAtomicDecS32(&m_cRefs);
|
---|
85 | Log(("Dereference(%p/%d): cRefs = %d \n", this, m_enmObjType, cRefs));
|
---|
86 | AssertRelease(cRefs >= 0);
|
---|
87 |
|
---|
88 | if (cRefs)
|
---|
89 | { /* likely */ }
|
---|
90 | else
|
---|
91 | delete this;
|
---|
92 | }
|
---|
93 |
|
---|
94 | HGCMOBJ_TYPE Type()
|
---|
95 | {
|
---|
96 | return m_enmObjType;
|
---|
97 | }
|
---|
98 | };
|
---|
99 |
|
---|
100 |
|
---|
101 | class HGCMObject : public HGCMReferencedObject
|
---|
102 | {
|
---|
103 | private:
|
---|
104 | friend uint32_t hgcmObjMake(HGCMObject *pObject, uint32_t u32HandleIn);
|
---|
105 |
|
---|
106 | ObjectAVLCore m_core;
|
---|
107 |
|
---|
108 | protected:
|
---|
109 | virtual ~HGCMObject()
|
---|
110 | {}
|
---|
111 |
|
---|
112 | public:
|
---|
113 | HGCMObject(HGCMOBJ_TYPE enmObjType)
|
---|
114 | : HGCMReferencedObject(enmObjType)
|
---|
115 | {}
|
---|
116 |
|
---|
117 | uint32_t Handle()
|
---|
118 | {
|
---|
119 | return (uint32_t)m_core.AvlCore.Key;
|
---|
120 | }
|
---|
121 | };
|
---|
122 |
|
---|
123 | int hgcmObjInit();
|
---|
124 | void hgcmObjUninit();
|
---|
125 |
|
---|
126 | uint32_t hgcmObjGenerateHandle(HGCMObject *pObject);
|
---|
127 | uint32_t hgcmObjAssignHandle(HGCMObject *pObject, uint32_t u32Handle);
|
---|
128 |
|
---|
129 | void hgcmObjDeleteHandle(uint32_t handle);
|
---|
130 |
|
---|
131 | HGCMObject *hgcmObjReference(uint32_t handle, HGCMOBJ_TYPE enmObjType);
|
---|
132 | void hgcmObjDereference(HGCMObject *pObject);
|
---|
133 |
|
---|
134 | uint32_t hgcmObjQueryHandleCount();
|
---|
135 | void hgcmObjSetHandleCount(uint32_t u32HandleCount);
|
---|
136 |
|
---|
137 |
|
---|
138 | #endif /* !MAIN_INCLUDED_HGCMObjects_h */
|
---|