core/systems/HandleSys.h
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/systems/HandleSys.h	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,223 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2007 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 -/**
    1.55 - *   The QHandle is a nasty structure that compacts the handle system into a big vector.
    1.56 - * The members of the vector each encapsulate one Handle, however, they also act as nodes
    1.57 - * in an inlined linked list and an inlined vector.
    1.58 - *
    1.59 - *   The first of these lists is the 'freeID' list.  Each node from 1 to N (where N
    1.60 - * is the number of free nodes) has a 'freeID' that specifies a free Handle ID.  This
    1.61 - * is a quick hack to get around allocating a second base vector.
    1.62 - *
    1.63 - *   The second vector is the identity linked list.  An identity has its own handle, so
    1.64 - * these handles are used as sentinel nodes for index linking.  They point to the first and last
    1.65 - * index into the handle array.  Each subsequent Handle who is owned by that identity is mapped into
    1.66 - * that list.  This lets owning identities be unloaded in O(n) time.
    1.67 - *
    1.68 - *   Eventually, there may be a third list for type chains.
    1.69 - */
    1.70 -
    1.71 -enum HandleSet
    1.72 -{
    1.73 -	HandleSet_None = 0,
    1.74 -	HandleSet_Used,			/* The Handle is in use */
    1.75 -	HandleSet_Freed,		/* The "master" Handle of a clone chain is freed */
    1.76 -	HandleSet_Identity,		/* The Handle is a special identity */
    1.77 -};
    1.78 -
    1.79 -struct QHandle
    1.80 -{
    1.81 -	HandleType_t type;			/* Handle type */
    1.82 -	void *object;				/* Unmaintained object pointer */
    1.83 -	IdentityToken_t *owner;		/* Identity of object which owns this */
    1.84 -	unsigned int serial;		/* Serial no. for sanity checking */
    1.85 -	unsigned int refcount;		/* Reference count for safe destruction */
    1.86 -	unsigned int clone;			/* If non-zero, this is our cloned parent index */
    1.87 -	HandleSet set;				/* Information about the handle's state */
    1.88 -	bool access_special;		/* Whether or not access rules are special or type-derived */
    1.89 -	bool is_destroying;			/* Whether or not the handle is being destroyed */
    1.90 -	HandleAccess sec;			/* Security rules */
    1.91 -	/* The following variables are unrelated to the Handle array, and used 
    1.92 -	 * as an inlined chain of information */
    1.93 -	unsigned int freeID;		/* ID of a free handle in the free handle chain */
    1.94 -	/* Indexes into the handle array for owner membership.
    1.95 -	 * For identity roots, these are treated as the head/tail. */
    1.96 -	unsigned int ch_prev;		/* chained previous handle */
    1.97 -	unsigned int ch_next;		/* chained next handle */
    1.98 -};
    1.99 -
   1.100 -struct QHandleType
   1.101 -{
   1.102 -	IHandleTypeDispatch *dispatch;
   1.103 -	unsigned int freeID;
   1.104 -	unsigned int children;
   1.105 -	TypeAccess typeSec;
   1.106 -	HandleAccess hndlSec;
   1.107 -	unsigned int opened;
   1.108 -	int nameIdx;
   1.109 -};
   1.110 -
   1.111 -typedef void (HANDLE_REPORTER)(const char *str, ...);
   1.112 -
   1.113 -class HandleSystem : 
   1.114 -	public IHandleSys
   1.115 -{
   1.116 -	friend HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index);
   1.117 -	friend class ShareSystem;
   1.118 -public:
   1.119 -	HandleSystem();
   1.120 -	~HandleSystem();
   1.121 -public: //IHandleSystem
   1.122 -
   1.123 -	HandleType_t CreateType(const char *name,
   1.124 -		IHandleTypeDispatch *dispatch,
   1.125 -		HandleType_t parent,
   1.126 -		const TypeAccess *typeAccess,
   1.127 -		const HandleAccess *hndlAccess,
   1.128 -		IdentityToken_t *ident,
   1.129 -		HandleError *err);
   1.130 -
   1.131 -	bool RemoveType(HandleType_t type, IdentityToken_t *ident);
   1.132 -
   1.133 -	bool FindHandleType(const char *name, HandleType_t *type);
   1.134 -
   1.135 -	Handle_t CreateHandle(HandleType_t type, 
   1.136 -		void *object,
   1.137 -		IdentityToken_t *owner,
   1.138 -		IdentityToken_t *ident,
   1.139 -		HandleError *err);
   1.140 -
   1.141 -	HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity);
   1.142 -
   1.143 -	HandleError CloneHandle(Handle_t handle, 
   1.144 -		Handle_t *newhandle, 
   1.145 -		IdentityToken_t *newOwner,
   1.146 -		const HandleSecurity *pSecurity);
   1.147 -
   1.148 -	HandleError ReadHandle(Handle_t handle, 
   1.149 -		HandleType_t type, 
   1.150 -		const HandleSecurity *pSecurity,
   1.151 -		void **object);
   1.152 -
   1.153 -	bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess);
   1.154 -
   1.155 -	bool TypeCheck(HandleType_t intype, HandleType_t outtype);
   1.156 -
   1.157 -	virtual Handle_t CreateHandleEx(HandleType_t type, 
   1.158 -		void *object,
   1.159 -		const HandleSecurity *pSec,
   1.160 -		const HandleAccess *pAccess,
   1.161 -		HandleError *err);
   1.162 -
   1.163 -	void Dump(HANDLE_REPORTER rep);
   1.164 -protected:
   1.165 -	/**
   1.166 -	 * Decodes a handle with sanity and security checking.
   1.167 -	 */
   1.168 -	HandleError GetHandle(Handle_t handle, 
   1.169 -						  IdentityToken_t *ident, 
   1.170 -						  QHandle **pHandle, 
   1.171 -						  unsigned int *index,
   1.172 -						  bool ignoreFree=false);
   1.173 -
   1.174 -	/**
   1.175 -	 * Creates a basic handle and sets its reference count to 1.
   1.176 -	 * Does not do any type or security checking.
   1.177 -	 */
   1.178 -	HandleError MakePrimHandle(HandleType_t type, 
   1.179 -							   QHandle **pHandle, 
   1.180 -							   unsigned int *index, 
   1.181 -							   HandleType_t *handle,
   1.182 -							   IdentityToken_t *owner,
   1.183 -							   bool identity=false);
   1.184 -
   1.185 -	/**
   1.186 -	 * Frees a primitive handle.  Does no object freeing, only reference count, bookkeeping, 
   1.187 -	 * and linked list maintenance.
   1.188 -	 * If used on an Identity handle, destroys all Handles under that identity.
   1.189 -	 */
   1.190 -	void ReleasePrimHandle(unsigned int index);
   1.191 -
   1.192 -	/**
   1.193 -	 * Sets the security owner of a type.
   1.194 -	 */
   1.195 -	void SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken);
   1.196 -
   1.197 -	/**
   1.198 -	 * Helper function to check access rights.
   1.199 -	 */
   1.200 -	bool CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity);
   1.201 -
   1.202 -	/** 
   1.203 -	 * Some wrappers for internal functions, so we can pass indexes instead of encoded handles.
   1.204 -	 */
   1.205 -	HandleError FreeHandle(QHandle *pHandle, unsigned int index);
   1.206 -	void UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index);
   1.207 -	HandleError CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner);
   1.208 -	Handle_t CreateHandleInt(HandleType_t type, void *object, const HandleSecurity *pSec, HandleError *err, const HandleAccess *pAccess, bool identity);
   1.209 -
   1.210 -	bool TryAndFreeSomeHandles();
   1.211 -	HandleError TryAllocHandle(unsigned int *handle);
   1.212 -private:
   1.213 -	QHandle *m_Handles;
   1.214 -	QHandleType *m_Types;
   1.215 -	Trie *m_TypeLookup;
   1.216 -	unsigned int m_TypeTail;
   1.217 -	unsigned int m_FreeTypes;
   1.218 -	unsigned int m_HandleTail;
   1.219 -	unsigned int m_FreeHandles;
   1.220 -	unsigned int m_HSerial;
   1.221 -	BaseStringTable *m_strtab;
   1.222 -};
   1.223 -
   1.224 -extern HandleSystem g_HandleSys;
   1.225 -
   1.226 -#endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_