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