1 #ifndef _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
2 #define _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
4 #include <IHandleSys.h>
5 #include "sm_globals.h"
8 #include "sm_memtable.h"
10 #define HANDLESYS_MAX_HANDLES (1<<14)
11 #define HANDLESYS_MAX_TYPES (1<<9)
12 #define HANDLESYS_MAX_SUBTYPES 0xF
13 #define HANDLESYS_SUBTYPE_MASK 0xF
14 #define HANDLESYS_TYPEARRAY_SIZE (HANDLESYS_MAX_TYPES * (HANDLESYS_MAX_SUBTYPES + 1))
15 #define HANDLESYS_MAX_SERIALS 0xFFFF
16 #define HANDLESYS_SERIAL_MASK 0xFFFF0000
17 #define HANDLESYS_HANDLE_MASK 0x0000FFFF
20 * The QHandle is a nasty structure that compacts the handle system into a big vector.
21 * The members of the vector each encapsulate one Handle, however, they also act as nodes
22 * in an inlined linked list and an inlined vector.
24 * The first of these lists is the 'freeID' list. Each node from 1 to N (where N
25 * is the number of free nodes) has a 'freeID' that specifies a free Handle ID. This
26 * is a quick hack to get around allocating a second base vector.
28 * The second vector is the identity linked list. An identity has its own handle, so
29 * these handles are used as sentinel nodes for index linking. They point to the first and last
30 * index into the handle array. Each subsequent Handle who is owned by that indentity is mapped into
31 * that list. This lets owning identities be unloaded in O(n) time.
33 * Eventually, there may be a third list for type chains.
39 HandleSet_Used, /* The Handle is in use */
40 HandleSet_Freed, /* The "master" Handle of a clone chain is freed */
41 HandleSet_Identity, /* The Handle is a special identity */
46 HandleType_t type; /* Handle type */
47 void *object; /* Unmaintained object pointer */
48 IdentityToken_t *owner; /* Identity of object which owns this */
49 unsigned int serial; /* Serial no. for sanity checking */
50 unsigned int refcount; /* Reference count for safe destruction */
51 unsigned int clone; /* If non-zero, this is our cloned parent index */
52 HandleSet set; /* Information about the handle's state */
53 /* The following variables are unrelated to the Handle array, and used
54 * as an inlined chain of information */
55 unsigned int freeID; /* ID of a free handle in the free handle chain */
56 /* Indexes into the handle array for owner membership.
57 * For identity roots, these are treated as the head/tail. */
58 unsigned int ch_prev; /* chained previous handle or HEAD */
59 unsigned int ch_next; /* chained next handle or TAIL */
64 IHandleTypeDispatch *dispatch;
66 unsigned int children;
76 friend HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index);
77 friend class ShareSystem;
81 public: //IHandleSystem
83 HandleType_t CreateType(const char *name,
84 IHandleTypeDispatch *dispatch,
86 const TypeAccess *typeAccess,
87 const HandleAccess *hndlAccess,
88 IdentityToken_t *ident,
91 bool RemoveType(HandleType_t type, IdentityToken_t *ident);
93 bool FindHandleType(const char *name, HandleType_t *type);
95 Handle_t CreateHandle(HandleType_t type,
97 IdentityToken_t *owner,
98 IdentityToken_t *ident,
100 HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity);
102 HandleError CloneHandle(Handle_t handle,
104 IdentityToken_t *newOwner,
105 const HandleSecurity *pSecurity);
107 HandleError ReadHandle(Handle_t handle,
109 const HandleSecurity *pSecurity,
112 bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess);
114 bool TypeCheck(HandleType_t intype, HandleType_t outtype);
117 * Decodes a handle with sanity and security checking.
119 HandleError GetHandle(Handle_t handle,
120 IdentityToken_t *ident,
123 bool ignoreFree=false);
126 * Creates a basic handle and sets its reference count to 1.
127 * Does not do any type or security checking.
129 HandleError MakePrimHandle(HandleType_t type,
132 HandleType_t *handle,
133 IdentityToken_t *owner);
136 * Frees a primitive handle. Does no object freeing, only reference count, bookkeepping,
137 * and linked list maintenance.
139 void ReleasePrimHandle(unsigned int index);
142 * Sets the security owner of a type
144 void SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken);
147 * Marks a handle as an identity.
148 * This prevents it from being tampered with by outside stuff
150 void MarkHandleAsIdentity(Handle_t handle);
152 bool CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity);
155 QHandleType *m_Types;
157 unsigned int m_TypeTail;
158 unsigned int m_FreeTypes;
159 unsigned int m_HandleTail;
160 unsigned int m_FreeHandles;
161 unsigned int m_HSerial;
162 BaseStringTable *m_strtab;
165 extern HandleSystem g_HandleSys;
167 #endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_