core/systems/HandleSys.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 17 01:43:25 2007 +0000 (2007-11-17)
changeset 1687 47cc645edce6
parent 1638 2980312c1c94
child 1766 d30c252089be
permissions -rw-r--r--
added amb1169 - dumping handles to game logs
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
    33 #define _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
    34 
    35 #include <IHandleSys.h>
    36 #include <stdio.h>
    37 #include "sm_globals.h"
    38 #include "sm_trie.h"
    39 #include "sourcemod.h"
    40 #include "sm_memtable.h"
    41 
    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
    50 
    51 /**
    52  *   The QHandle is a nasty structure that compacts the handle system into a big vector.
    53  * The members of the vector each encapsulate one Handle, however, they also act as nodes
    54  * in an inlined linked list and an inlined vector.
    55  *
    56  *   The first of these lists is the 'freeID' list.  Each node from 1 to N (where N
    57  * is the number of free nodes) has a 'freeID' that specifies a free Handle ID.  This
    58  * is a quick hack to get around allocating a second base vector.
    59  *
    60  *   The second vector is the identity linked list.  An identity has its own handle, so
    61  * these handles are used as sentinel nodes for index linking.  They point to the first and last
    62  * index into the handle array.  Each subsequent Handle who is owned by that identity is mapped into
    63  * that list.  This lets owning identities be unloaded in O(n) time.
    64  *
    65  *   Eventually, there may be a third list for type chains.
    66  */
    67 
    68 enum HandleSet
    69 {
    70 	HandleSet_None = 0,
    71 	HandleSet_Used,			/* The Handle is in use */
    72 	HandleSet_Freed,		/* The "master" Handle of a clone chain is freed */
    73 	HandleSet_Identity,		/* The Handle is a special identity */
    74 };
    75 
    76 struct QHandle
    77 {
    78 	HandleType_t type;			/* Handle type */
    79 	void *object;				/* Unmaintained object pointer */
    80 	IdentityToken_t *owner;		/* Identity of object which owns this */
    81 	unsigned int serial;		/* Serial no. for sanity checking */
    82 	unsigned int refcount;		/* Reference count for safe destruction */
    83 	unsigned int clone;			/* If non-zero, this is our cloned parent index */
    84 	HandleSet set;				/* Information about the handle's state */
    85 	bool access_special;		/* Whether or not access rules are special or type-derived */
    86 	bool is_destroying;			/* Whether or not the handle is being destroyed */
    87 	HandleAccess sec;			/* Security rules */
    88 	/* The following variables are unrelated to the Handle array, and used 
    89 	 * as an inlined chain of information */
    90 	unsigned int freeID;		/* ID of a free handle in the free handle chain */
    91 	/* Indexes into the handle array for owner membership.
    92 	 * For identity roots, these are treated as the head/tail. */
    93 	unsigned int ch_prev;		/* chained previous handle */
    94 	unsigned int ch_next;		/* chained next handle */
    95 };
    96 
    97 struct QHandleType
    98 {
    99 	IHandleTypeDispatch *dispatch;
   100 	unsigned int freeID;
   101 	unsigned int children;
   102 	TypeAccess typeSec;
   103 	HandleAccess hndlSec;
   104 	unsigned int opened;
   105 	int nameIdx;
   106 };
   107 
   108 typedef void (HANDLE_REPORTER)(const char *str, ...);
   109 
   110 class HandleSystem : 
   111 	public IHandleSys
   112 {
   113 	friend HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index);
   114 	friend class ShareSystem;
   115 public:
   116 	HandleSystem();
   117 	~HandleSystem();
   118 public: //IHandleSystem
   119 
   120 	HandleType_t CreateType(const char *name,
   121 		IHandleTypeDispatch *dispatch,
   122 		HandleType_t parent,
   123 		const TypeAccess *typeAccess,
   124 		const HandleAccess *hndlAccess,
   125 		IdentityToken_t *ident,
   126 		HandleError *err);
   127 
   128 	bool RemoveType(HandleType_t type, IdentityToken_t *ident);
   129 
   130 	bool FindHandleType(const char *name, HandleType_t *type);
   131 
   132 	Handle_t CreateHandle(HandleType_t type, 
   133 		void *object,
   134 		IdentityToken_t *owner,
   135 		IdentityToken_t *ident,
   136 		HandleError *err);
   137 
   138 	HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity);
   139 
   140 	HandleError CloneHandle(Handle_t handle, 
   141 		Handle_t *newhandle, 
   142 		IdentityToken_t *newOwner,
   143 		const HandleSecurity *pSecurity);
   144 
   145 	HandleError ReadHandle(Handle_t handle, 
   146 		HandleType_t type, 
   147 		const HandleSecurity *pSecurity,
   148 		void **object);
   149 
   150 	bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess);
   151 
   152 	bool TypeCheck(HandleType_t intype, HandleType_t outtype);
   153 
   154 	virtual Handle_t CreateHandleEx(HandleType_t type, 
   155 		void *object,
   156 		const HandleSecurity *pSec,
   157 		const HandleAccess *pAccess,
   158 		HandleError *err);
   159 
   160 	void Dump(HANDLE_REPORTER rep);
   161 protected:
   162 	/**
   163 	 * Decodes a handle with sanity and security checking.
   164 	 */
   165 	HandleError GetHandle(Handle_t handle, 
   166 						  IdentityToken_t *ident, 
   167 						  QHandle **pHandle, 
   168 						  unsigned int *index,
   169 						  bool ignoreFree=false);
   170 
   171 	/**
   172 	 * Creates a basic handle and sets its reference count to 1.
   173 	 * Does not do any type or security checking.
   174 	 */
   175 	HandleError MakePrimHandle(HandleType_t type, 
   176 							   QHandle **pHandle, 
   177 							   unsigned int *index, 
   178 							   HandleType_t *handle,
   179 							   IdentityToken_t *owner,
   180 							   bool identity=false);
   181 
   182 	/**
   183 	 * Frees a primitive handle.  Does no object freeing, only reference count, bookkeeping, 
   184 	 * and linked list maintenance.
   185 	 * If used on an Identity handle, destroys all Handles under that identity.
   186 	 */
   187 	void ReleasePrimHandle(unsigned int index);
   188 
   189 	/**
   190 	 * Sets the security owner of a type.
   191 	 */
   192 	void SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken);
   193 
   194 	/**
   195 	 * Helper function to check access rights.
   196 	 */
   197 	bool CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity);
   198 
   199 	/** 
   200 	 * Some wrappers for internal functions, so we can pass indexes instead of encoded handles.
   201 	 */
   202 	HandleError FreeHandle(QHandle *pHandle, unsigned int index);
   203 	void UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index);
   204 	HandleError CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner);
   205 	Handle_t CreateHandleInt(HandleType_t type, void *object, const HandleSecurity *pSec, HandleError *err, const HandleAccess *pAccess, bool identity);
   206 
   207 	bool TryAndFreeSomeHandles();
   208 	HandleError TryAllocHandle(unsigned int *handle);
   209 private:
   210 	QHandle *m_Handles;
   211 	QHandleType *m_Types;
   212 	Trie *m_TypeLookup;
   213 	unsigned int m_TypeTail;
   214 	unsigned int m_FreeTypes;
   215 	unsigned int m_HandleTail;
   216 	unsigned int m_FreeHandles;
   217 	unsigned int m_HSerial;
   218 	BaseStringTable *m_strtab;
   219 };
   220 
   221 extern HandleSystem g_HandleSys;
   222 
   223 #endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_