core/HandleSys.h
changeset 2411 8dfaa3d730b5
parent 2289 44045ead61cd
child 2959 1a7109501676
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/HandleSys.h	Sun Nov 16 16:06:18 2008 -0800
     1.3 @@ -0,0 +1,225 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
    1.36 +#define _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
    1.37 +
    1.38 +#include <IHandleSys.h>
    1.39 +#include <stdio.h>
    1.40 +#include "sm_globals.h"
    1.41 +#include "sm_trie.h"
    1.42 +#include "sourcemod.h"
    1.43 +#include "sm_memtable.h"
    1.44 +
    1.45 +#define HANDLESYS_MAX_HANDLES		(1<<14)
    1.46 +#define HANDLESYS_MAX_TYPES			(1<<9)
    1.47 +#define HANDLESYS_MAX_SUBTYPES		0xF
    1.48 +#define HANDLESYS_SUBTYPE_MASK		0xF
    1.49 +#define HANDLESYS_TYPEARRAY_SIZE	(HANDLESYS_MAX_TYPES * (HANDLESYS_MAX_SUBTYPES + 1))
    1.50 +#define HANDLESYS_MAX_SERIALS		0xFFFF
    1.51 +#define HANDLESYS_SERIAL_MASK		0xFFFF0000
    1.52 +#define HANDLESYS_HANDLE_MASK		0x0000FFFF
    1.53 +
    1.54 +#define HANDLESYS_MEMUSAGE_MIN_VERSION		3
    1.55 +
    1.56 +/**
    1.57 + *   The QHandle is a nasty structure that compacts the handle system into a big vector.
    1.58 + * The members of the vector each encapsulate one Handle, however, they also act as nodes
    1.59 + * in an inlined linked list and an inlined vector.
    1.60 + *
    1.61 + *   The first of these lists is the 'freeID' list.  Each node from 1 to N (where N
    1.62 + * is the number of free nodes) has a 'freeID' that specifies a free Handle ID.  This
    1.63 + * is a quick hack to get around allocating a second base vector.
    1.64 + *
    1.65 + *   The second vector is the identity linked list.  An identity has its own handle, so
    1.66 + * these handles are used as sentinel nodes for index linking.  They point to the first and last
    1.67 + * index into the handle array.  Each subsequent Handle who is owned by that identity is mapped into
    1.68 + * that list.  This lets owning identities be unloaded in O(n) time.
    1.69 + *
    1.70 + *   Eventually, there may be a third list for type chains.
    1.71 + */
    1.72 +
    1.73 +enum HandleSet
    1.74 +{
    1.75 +	HandleSet_None = 0,
    1.76 +	HandleSet_Used,			/* The Handle is in use */
    1.77 +	HandleSet_Freed,		/* The "master" Handle of a clone chain is freed */
    1.78 +	HandleSet_Identity,		/* The Handle is a special identity */
    1.79 +};
    1.80 +
    1.81 +struct QHandle
    1.82 +{
    1.83 +	HandleType_t type;			/* Handle type */
    1.84 +	void *object;				/* Unmaintained object pointer */
    1.85 +	IdentityToken_t *owner;		/* Identity of object which owns this */
    1.86 +	unsigned int serial;		/* Serial no. for sanity checking */
    1.87 +	unsigned int refcount;		/* Reference count for safe destruction */
    1.88 +	unsigned int clone;			/* If non-zero, this is our cloned parent index */
    1.89 +	HandleSet set;				/* Information about the handle's state */
    1.90 +	bool access_special;		/* Whether or not access rules are special or type-derived */
    1.91 +	bool is_destroying;			/* Whether or not the handle is being destroyed */
    1.92 +	HandleAccess sec;			/* Security rules */
    1.93 +	/* The following variables are unrelated to the Handle array, and used 
    1.94 +	 * as an inlined chain of information */
    1.95 +	unsigned int freeID;		/* ID of a free handle in the free handle chain */
    1.96 +	/* Indexes into the handle array for owner membership.
    1.97 +	 * For identity roots, these are treated as the head/tail. */
    1.98 +	unsigned int ch_prev;		/* chained previous handle */
    1.99 +	unsigned int ch_next;		/* chained next handle */
   1.100 +};
   1.101 +
   1.102 +struct QHandleType
   1.103 +{
   1.104 +	IHandleTypeDispatch *dispatch;
   1.105 +	unsigned int freeID;
   1.106 +	unsigned int children;
   1.107 +	TypeAccess typeSec;
   1.108 +	HandleAccess hndlSec;
   1.109 +	unsigned int opened;
   1.110 +	int nameIdx;
   1.111 +};
   1.112 +
   1.113 +typedef void (HANDLE_REPORTER)(const char *str, ...);
   1.114 +
   1.115 +class HandleSystem : 
   1.116 +	public IHandleSys
   1.117 +{
   1.118 +	friend HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index);
   1.119 +	friend class ShareSystem;
   1.120 +public:
   1.121 +	HandleSystem();
   1.122 +	~HandleSystem();
   1.123 +public: //IHandleSystem
   1.124 +
   1.125 +	HandleType_t CreateType(const char *name,
   1.126 +		IHandleTypeDispatch *dispatch,
   1.127 +		HandleType_t parent,
   1.128 +		const TypeAccess *typeAccess,
   1.129 +		const HandleAccess *hndlAccess,
   1.130 +		IdentityToken_t *ident,
   1.131 +		HandleError *err);
   1.132 +
   1.133 +	bool RemoveType(HandleType_t type, IdentityToken_t *ident);
   1.134 +
   1.135 +	bool FindHandleType(const char *name, HandleType_t *type);
   1.136 +
   1.137 +	Handle_t CreateHandle(HandleType_t type, 
   1.138 +		void *object,
   1.139 +		IdentityToken_t *owner,
   1.140 +		IdentityToken_t *ident,
   1.141 +		HandleError *err);
   1.142 +
   1.143 +	HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity);
   1.144 +
   1.145 +	HandleError CloneHandle(Handle_t handle, 
   1.146 +		Handle_t *newhandle, 
   1.147 +		IdentityToken_t *newOwner,
   1.148 +		const HandleSecurity *pSecurity);
   1.149 +
   1.150 +	HandleError ReadHandle(Handle_t handle, 
   1.151 +		HandleType_t type, 
   1.152 +		const HandleSecurity *pSecurity,
   1.153 +		void **object);
   1.154 +
   1.155 +	bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess);
   1.156 +
   1.157 +	bool TypeCheck(HandleType_t intype, HandleType_t outtype);
   1.158 +
   1.159 +	virtual Handle_t CreateHandleEx(HandleType_t type, 
   1.160 +		void *object,
   1.161 +		const HandleSecurity *pSec,
   1.162 +		const HandleAccess *pAccess,
   1.163 +		HandleError *err);
   1.164 +
   1.165 +	void Dump(HANDLE_REPORTER rep);
   1.166 +protected:
   1.167 +	/**
   1.168 +	 * Decodes a handle with sanity and security checking.
   1.169 +	 */
   1.170 +	HandleError GetHandle(Handle_t handle, 
   1.171 +						  IdentityToken_t *ident, 
   1.172 +						  QHandle **pHandle, 
   1.173 +						  unsigned int *index,
   1.174 +						  bool ignoreFree=false);
   1.175 +
   1.176 +	/**
   1.177 +	 * Creates a basic handle and sets its reference count to 1.
   1.178 +	 * Does not do any type or security checking.
   1.179 +	 */
   1.180 +	HandleError MakePrimHandle(HandleType_t type, 
   1.181 +							   QHandle **pHandle, 
   1.182 +							   unsigned int *index, 
   1.183 +							   HandleType_t *handle,
   1.184 +							   IdentityToken_t *owner,
   1.185 +							   bool identity=false);
   1.186 +
   1.187 +	/**
   1.188 +	 * Frees a primitive handle.  Does no object freeing, only reference count, bookkeeping, 
   1.189 +	 * and linked list maintenance.
   1.190 +	 * If used on an Identity handle, destroys all Handles under that identity.
   1.191 +	 */
   1.192 +	void ReleasePrimHandle(unsigned int index);
   1.193 +
   1.194 +	/**
   1.195 +	 * Sets the security owner of a type.
   1.196 +	 */
   1.197 +	void SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken);
   1.198 +
   1.199 +	/**
   1.200 +	 * Helper function to check access rights.
   1.201 +	 */
   1.202 +	bool CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity);
   1.203 +
   1.204 +	/** 
   1.205 +	 * Some wrappers for internal functions, so we can pass indexes instead of encoded handles.
   1.206 +	 */
   1.207 +	HandleError FreeHandle(QHandle *pHandle, unsigned int index);
   1.208 +	void UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index);
   1.209 +	HandleError CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner);
   1.210 +	Handle_t CreateHandleInt(HandleType_t type, void *object, const HandleSecurity *pSec, HandleError *err, const HandleAccess *pAccess, bool identity);
   1.211 +
   1.212 +	bool TryAndFreeSomeHandles();
   1.213 +	HandleError TryAllocHandle(unsigned int *handle);
   1.214 +private:
   1.215 +	QHandle *m_Handles;
   1.216 +	QHandleType *m_Types;
   1.217 +	Trie *m_TypeLookup;
   1.218 +	unsigned int m_TypeTail;
   1.219 +	unsigned int m_FreeTypes;
   1.220 +	unsigned int m_HandleTail;
   1.221 +	unsigned int m_FreeHandles;
   1.222 +	unsigned int m_HSerial;
   1.223 +	BaseStringTable *m_strtab;
   1.224 +};
   1.225 +
   1.226 +extern HandleSystem g_HandleSys;
   1.227 +
   1.228 +#endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_