Executable memory is now provided by MM:S's allocator, this should reduce virtual memory usage.
3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #ifndef _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
33 #define _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
35 #include <IHandleSys.h>
37 #include "sm_globals.h"
39 #include "sourcemod.h"
40 #include "sm_memtable.h"
42 #define HANDLESYS_MAX_HANDLES (1<<14)
43 #define HANDLESYS_MAX_TYPES (1<<9)
44 #define HANDLESYS_MAX_SUBTYPES 0xF
45 #define HANDLESYS_SUBTYPE_MASK 0xF
46 #define HANDLESYS_TYPEARRAY_SIZE (HANDLESYS_MAX_TYPES * (HANDLESYS_MAX_SUBTYPES + 1))
47 #define HANDLESYS_MAX_SERIALS 0xFFFF
48 #define HANDLESYS_SERIAL_MASK 0xFFFF0000
49 #define HANDLESYS_HANDLE_MASK 0x0000FFFF
51 #define HANDLESYS_MEMUSAGE_MIN_VERSION 3
54 * The QHandle is a nasty structure that compacts the handle system into a big vector.
55 * The members of the vector each encapsulate one Handle, however, they also act as nodes
56 * in an inlined linked list and an inlined vector.
58 * The first of these lists is the 'freeID' list. Each node from 1 to N (where N
59 * is the number of free nodes) has a 'freeID' that specifies a free Handle ID. This
60 * is a quick hack to get around allocating a second base vector.
62 * The second vector is the identity linked list. An identity has its own handle, so
63 * these handles are used as sentinel nodes for index linking. They point to the first and last
64 * index into the handle array. Each subsequent Handle who is owned by that identity is mapped into
65 * that list. This lets owning identities be unloaded in O(n) time.
67 * Eventually, there may be a third list for type chains.
73 HandleSet_Used, /* The Handle is in use */
74 HandleSet_Freed, /* The "master" Handle of a clone chain is freed */
75 HandleSet_Identity, /* The Handle is a special identity */
80 HandleType_t type; /* Handle type */
81 void *object; /* Unmaintained object pointer */
82 IdentityToken_t *owner; /* Identity of object which owns this */
83 unsigned int serial; /* Serial no. for sanity checking */
84 unsigned int refcount; /* Reference count for safe destruction */
85 unsigned int clone; /* If non-zero, this is our cloned parent index */
86 HandleSet set; /* Information about the handle's state */
87 bool access_special; /* Whether or not access rules are special or type-derived */
88 bool is_destroying; /* Whether or not the handle is being destroyed */
89 HandleAccess sec; /* Security rules */
90 /* The following variables are unrelated to the Handle array, and used
91 * as an inlined chain of information */
92 unsigned int freeID; /* ID of a free handle in the free handle chain */
93 /* Indexes into the handle array for owner membership.
94 * For identity roots, these are treated as the head/tail. */
95 unsigned int ch_prev; /* chained previous handle */
96 unsigned int ch_next; /* chained next handle */
101 IHandleTypeDispatch *dispatch;
103 unsigned int children;
105 HandleAccess hndlSec;
110 typedef void (HANDLE_REPORTER)(const char *str, ...);
115 friend HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index);
116 friend class ShareSystem;
120 public: //IHandleSystem
122 HandleType_t CreateType(const char *name,
123 IHandleTypeDispatch *dispatch,
125 const TypeAccess *typeAccess,
126 const HandleAccess *hndlAccess,
127 IdentityToken_t *ident,
130 bool RemoveType(HandleType_t type, IdentityToken_t *ident);
132 bool FindHandleType(const char *name, HandleType_t *type);
134 Handle_t CreateHandle(HandleType_t type,
136 IdentityToken_t *owner,
137 IdentityToken_t *ident,
140 HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity);
142 HandleError CloneHandle(Handle_t handle,
144 IdentityToken_t *newOwner,
145 const HandleSecurity *pSecurity);
147 HandleError ReadHandle(Handle_t handle,
149 const HandleSecurity *pSecurity,
152 bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess);
154 bool TypeCheck(HandleType_t intype, HandleType_t outtype);
156 virtual Handle_t CreateHandleEx(HandleType_t type,
158 const HandleSecurity *pSec,
159 const HandleAccess *pAccess,
162 void Dump(HANDLE_REPORTER rep);
165 * Decodes a handle with sanity and security checking.
167 HandleError GetHandle(Handle_t handle,
168 IdentityToken_t *ident,
171 bool ignoreFree=false);
174 * Creates a basic handle and sets its reference count to 1.
175 * Does not do any type or security checking.
177 HandleError MakePrimHandle(HandleType_t type,
180 HandleType_t *handle,
181 IdentityToken_t *owner,
182 bool identity=false);
185 * Frees a primitive handle. Does no object freeing, only reference count, bookkeeping,
186 * and linked list maintenance.
187 * If used on an Identity handle, destroys all Handles under that identity.
189 void ReleasePrimHandle(unsigned int index);
192 * Sets the security owner of a type.
194 void SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken);
197 * Helper function to check access rights.
199 bool CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity);
202 * Some wrappers for internal functions, so we can pass indexes instead of encoded handles.
204 HandleError FreeHandle(QHandle *pHandle, unsigned int index);
205 void UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index);
206 HandleError CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner);
207 Handle_t CreateHandleInt(HandleType_t type, void *object, const HandleSecurity *pSec, HandleError *err, const HandleAccess *pAccess, bool identity);
209 bool TryAndFreeSomeHandles();
210 HandleError TryAllocHandle(unsigned int *handle);
213 QHandleType *m_Types;
215 unsigned int m_TypeTail;
216 unsigned int m_FreeTypes;
217 unsigned int m_HandleTail;
218 unsigned int m_FreeHandles;
219 unsigned int m_HSerial;
220 BaseStringTable *m_strtab;
223 extern HandleSystem g_HandleSys;
225 #endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_