2007-01-25 23:36:38 +01:00
|
|
|
/**
|
|
|
|
* ===============================================================
|
|
|
|
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
|
|
|
|
* ===============================================================
|
|
|
|
*
|
|
|
|
* This file is not open source and may not be copied without explicit
|
|
|
|
* written permission of AlliedModders LLC. This file may not be redistributed
|
|
|
|
* in whole or significant part.
|
|
|
|
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
2006-12-16 23:27:18 +01:00
|
|
|
#ifndef _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
|
|
|
|
#define _INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
|
|
|
|
|
|
|
|
#include <IHandleSys.h>
|
|
|
|
#include "sm_globals.h"
|
|
|
|
#include "sm_trie.h"
|
|
|
|
#include "sourcemod.h"
|
|
|
|
#include "sm_memtable.h"
|
|
|
|
|
2006-12-17 10:56:45 +01:00
|
|
|
#define HANDLESYS_MAX_HANDLES (1<<14)
|
2006-12-16 23:27:18 +01:00
|
|
|
#define HANDLESYS_MAX_TYPES (1<<9)
|
|
|
|
#define HANDLESYS_MAX_SUBTYPES 0xF
|
|
|
|
#define HANDLESYS_SUBTYPE_MASK 0xF
|
|
|
|
#define HANDLESYS_TYPEARRAY_SIZE (HANDLESYS_MAX_TYPES * (HANDLESYS_MAX_SUBTYPES + 1))
|
|
|
|
#define HANDLESYS_MAX_SERIALS 0xFFFF
|
|
|
|
#define HANDLESYS_SERIAL_MASK 0xFFFF0000
|
|
|
|
#define HANDLESYS_HANDLE_MASK 0x0000FFFF
|
|
|
|
|
2006-12-30 00:18:13 +01:00
|
|
|
/**
|
|
|
|
* The QHandle is a nasty structure that compacts the handle system into a big vector.
|
|
|
|
* The members of the vector each encapsulate one Handle, however, they also act as nodes
|
|
|
|
* in an inlined linked list and an inlined vector.
|
|
|
|
*
|
|
|
|
* The first of these lists is the 'freeID' list. Each node from 1 to N (where N
|
|
|
|
* is the number of free nodes) has a 'freeID' that specifies a free Handle ID. This
|
|
|
|
* is a quick hack to get around allocating a second base vector.
|
|
|
|
*
|
|
|
|
* The second vector is the identity linked list. An identity has its own handle, so
|
|
|
|
* these handles are used as sentinel nodes for index linking. They point to the first and last
|
2007-02-14 02:25:45 +01:00
|
|
|
* index into the handle array. Each subsequent Handle who is owned by that identity is mapped into
|
2006-12-30 00:18:13 +01:00
|
|
|
* that list. This lets owning identities be unloaded in O(n) time.
|
|
|
|
*
|
|
|
|
* Eventually, there may be a third list for type chains.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum HandleSet
|
|
|
|
{
|
|
|
|
HandleSet_None = 0,
|
2007-01-02 02:44:46 +01:00
|
|
|
HandleSet_Used, /* The Handle is in use */
|
|
|
|
HandleSet_Freed, /* The "master" Handle of a clone chain is freed */
|
|
|
|
HandleSet_Identity, /* The Handle is a special identity */
|
2006-12-30 00:18:13 +01:00
|
|
|
};
|
|
|
|
|
2006-12-16 23:27:18 +01:00
|
|
|
struct QHandle
|
|
|
|
{
|
2006-12-17 10:56:45 +01:00
|
|
|
HandleType_t type; /* Handle type */
|
|
|
|
void *object; /* Unmaintained object pointer */
|
2006-12-30 00:18:13 +01:00
|
|
|
IdentityToken_t *owner; /* Identity of object which owns this */
|
2006-12-17 10:56:45 +01:00
|
|
|
unsigned int serial; /* Serial no. for sanity checking */
|
|
|
|
unsigned int refcount; /* Reference count for safe destruction */
|
2007-01-04 03:08:27 +01:00
|
|
|
unsigned int clone; /* If non-zero, this is our cloned parent index */
|
2006-12-30 00:18:13 +01:00
|
|
|
HandleSet set; /* Information about the handle's state */
|
2007-02-26 03:47:03 +01:00
|
|
|
bool access_special; /* Whether or not access rules are special or type-derived */
|
|
|
|
HandleAccess sec; /* Security rules */
|
2006-12-30 00:18:13 +01:00
|
|
|
/* The following variables are unrelated to the Handle array, and used
|
|
|
|
* as an inlined chain of information */
|
|
|
|
unsigned int freeID; /* ID of a free handle in the free handle chain */
|
|
|
|
/* Indexes into the handle array for owner membership.
|
|
|
|
* For identity roots, these are treated as the head/tail. */
|
2007-01-11 08:29:09 +01:00
|
|
|
unsigned int ch_prev; /* chained previous handle */
|
|
|
|
unsigned int ch_next; /* chained next handle */
|
2006-12-16 23:27:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct QHandleType
|
|
|
|
{
|
|
|
|
IHandleTypeDispatch *dispatch;
|
|
|
|
unsigned int freeID;
|
|
|
|
unsigned int children;
|
2007-01-04 03:08:27 +01:00
|
|
|
TypeAccess typeSec;
|
|
|
|
HandleAccess hndlSec;
|
2006-12-16 23:27:18 +01:00
|
|
|
unsigned int opened;
|
|
|
|
int nameIdx;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HandleSystem :
|
|
|
|
public IHandleSys
|
|
|
|
{
|
2006-12-30 00:18:13 +01:00
|
|
|
friend HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index);
|
|
|
|
friend class ShareSystem;
|
2006-12-16 23:27:18 +01:00
|
|
|
public:
|
|
|
|
HandleSystem();
|
|
|
|
~HandleSystem();
|
|
|
|
public: //IHandleSystem
|
2007-01-04 03:08:27 +01:00
|
|
|
|
|
|
|
HandleType_t CreateType(const char *name,
|
|
|
|
IHandleTypeDispatch *dispatch,
|
|
|
|
HandleType_t parent,
|
|
|
|
const TypeAccess *typeAccess,
|
|
|
|
const HandleAccess *hndlAccess,
|
|
|
|
IdentityToken_t *ident,
|
|
|
|
HandleError *err);
|
|
|
|
|
2006-12-30 00:18:13 +01:00
|
|
|
bool RemoveType(HandleType_t type, IdentityToken_t *ident);
|
2007-01-04 03:08:27 +01:00
|
|
|
|
2006-12-16 23:27:18 +01:00
|
|
|
bool FindHandleType(const char *name, HandleType_t *type);
|
2007-01-04 03:08:27 +01:00
|
|
|
|
2006-12-16 23:27:18 +01:00
|
|
|
Handle_t CreateHandle(HandleType_t type,
|
2007-01-04 03:08:27 +01:00
|
|
|
void *object,
|
|
|
|
IdentityToken_t *owner,
|
|
|
|
IdentityToken_t *ident,
|
|
|
|
HandleError *err);
|
2007-01-11 08:29:09 +01:00
|
|
|
|
2007-01-04 03:08:27 +01:00
|
|
|
HandleError FreeHandle(Handle_t handle, const HandleSecurity *pSecurity);
|
|
|
|
|
|
|
|
HandleError CloneHandle(Handle_t handle,
|
|
|
|
Handle_t *newhandle,
|
|
|
|
IdentityToken_t *newOwner,
|
|
|
|
const HandleSecurity *pSecurity);
|
|
|
|
|
|
|
|
HandleError ReadHandle(Handle_t handle,
|
|
|
|
HandleType_t type,
|
|
|
|
const HandleSecurity *pSecurity,
|
|
|
|
void **object);
|
|
|
|
|
|
|
|
bool InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess);
|
|
|
|
|
2006-12-30 00:18:13 +01:00
|
|
|
bool TypeCheck(HandleType_t intype, HandleType_t outtype);
|
2007-02-26 03:47:03 +01:00
|
|
|
|
|
|
|
virtual Handle_t CreateHandleEx(HandleType_t type,
|
|
|
|
void *object,
|
|
|
|
const HandleSecurity *pSec,
|
|
|
|
const HandleAccess *pAccess,
|
|
|
|
HandleError *err);
|
2006-12-30 00:18:13 +01:00
|
|
|
protected:
|
2006-12-17 10:56:45 +01:00
|
|
|
/**
|
|
|
|
* Decodes a handle with sanity and security checking.
|
|
|
|
*/
|
|
|
|
HandleError GetHandle(Handle_t handle,
|
2006-12-30 00:18:13 +01:00
|
|
|
IdentityToken_t *ident,
|
2006-12-17 10:56:45 +01:00
|
|
|
QHandle **pHandle,
|
|
|
|
unsigned int *index,
|
2007-01-02 02:44:46 +01:00
|
|
|
bool ignoreFree=false);
|
2006-12-17 10:56:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a basic handle and sets its reference count to 1.
|
|
|
|
* Does not do any type or security checking.
|
|
|
|
*/
|
2006-12-30 00:18:13 +01:00
|
|
|
HandleError MakePrimHandle(HandleType_t type,
|
|
|
|
QHandle **pHandle,
|
|
|
|
unsigned int *index,
|
|
|
|
HandleType_t *handle,
|
2007-01-11 08:29:09 +01:00
|
|
|
IdentityToken_t *owner,
|
|
|
|
bool identity=false);
|
2006-12-17 10:56:45 +01:00
|
|
|
|
|
|
|
/**
|
2006-12-30 00:18:13 +01:00
|
|
|
* Frees a primitive handle. Does no object freeing, only reference count, bookkeepping,
|
|
|
|
* and linked list maintenance.
|
2007-01-11 08:29:09 +01:00
|
|
|
* If used on an Identity handle, destroys all Handles under that identity.
|
2006-12-17 10:56:45 +01:00
|
|
|
*/
|
|
|
|
void ReleasePrimHandle(unsigned int index);
|
2006-12-30 00:18:13 +01:00
|
|
|
|
|
|
|
/**
|
2007-01-11 08:29:09 +01:00
|
|
|
* Sets the security owner of a type.
|
2006-12-30 00:18:13 +01:00
|
|
|
*/
|
|
|
|
void SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken);
|
|
|
|
|
2007-01-11 08:29:09 +01:00
|
|
|
/**
|
|
|
|
* Helper function to check access rights.
|
2006-12-30 00:18:13 +01:00
|
|
|
*/
|
2007-01-04 03:08:27 +01:00
|
|
|
bool CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity);
|
2007-01-11 08:29:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Some wrappers for internal functions, so we can pass indexes instead of encoded handles.
|
|
|
|
*/
|
|
|
|
HandleError FreeHandle(QHandle *pHandle, unsigned int index);
|
|
|
|
void UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index);
|
|
|
|
HandleError CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner);
|
2007-02-26 03:47:03 +01:00
|
|
|
Handle_t CreateHandleInt(HandleType_t type, void *object, const HandleSecurity *pSec, HandleError *err, const HandleAccess *pAccess, bool identity);
|
2006-12-16 23:27:18 +01:00
|
|
|
private:
|
|
|
|
QHandle *m_Handles;
|
|
|
|
QHandleType *m_Types;
|
|
|
|
Trie *m_TypeLookup;
|
|
|
|
unsigned int m_TypeTail;
|
|
|
|
unsigned int m_FreeTypes;
|
|
|
|
unsigned int m_HandleTail;
|
|
|
|
unsigned int m_FreeHandles;
|
|
|
|
unsigned int m_HSerial;
|
|
|
|
BaseStringTable *m_strtab;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern HandleSystem g_HandleSys;
|
|
|
|
|
|
|
|
#endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_
|