core/systems/HandleSys.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jan 25 22:36:38 2007 +0000 (2007-01-25)
changeset 370 d3a484f98f6d
parent 364 99f3e60a7f68
child 528 ea61550036b5
permissions -rw-r--r--
added license headers
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include "HandleSys.h"
    15 #include "ShareSys.h"
    16 #include "PluginSys.h"
    17 #include <assert.h>
    18 #include <string.h>
    19 
    20 HandleSystem g_HandleSys;
    21 
    22 QHandle *ignore_handle;
    23 
    24 inline HandleType_t TypeParent(HandleType_t type)
    25 {
    26 	return (type & ~HANDLESYS_SUBTYPE_MASK);
    27 }
    28 
    29 inline HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index)
    30 {
    31 	return g_HandleSys.GetHandle(token->ident, g_ShareSys.GetIdentRoot(), &ignore_handle, index);
    32 }
    33 
    34 HandleSystem::HandleSystem()
    35 {
    36 	m_Handles = new QHandle[HANDLESYS_MAX_HANDLES + 1];
    37 	memset(m_Handles, 0, sizeof(QHandle) * (HANDLESYS_MAX_HANDLES + 1));
    38 
    39 	m_Types = new QHandleType[HANDLESYS_TYPEARRAY_SIZE];
    40 	memset(m_Types, 0, sizeof(QHandleType) * HANDLESYS_TYPEARRAY_SIZE);
    41 
    42 	m_TypeLookup = sm_trie_create();
    43 	m_strtab = new BaseStringTable(512);
    44 
    45 	m_TypeTail = 0;
    46 }
    47 
    48 HandleSystem::~HandleSystem()
    49 {
    50 	delete [] m_Handles;
    51 	delete [] m_Types;
    52 	sm_trie_destroy(m_TypeLookup);
    53 	delete m_strtab;
    54 }
    55 
    56 
    57 HandleType_t HandleSystem::CreateType(const char *name, 
    58 									  IHandleTypeDispatch *dispatch, 
    59 									  HandleType_t parent, 
    60 									  const TypeAccess *typeAccess, 
    61 									  const HandleAccess *hndlAccess, 
    62 									  IdentityToken_t *ident,
    63 									  HandleError *err)
    64 {
    65 	if (!dispatch)
    66 	{
    67 		if (err)
    68 		{
    69 			*err = HandleError_Parameter;
    70 		}
    71 		return 0;
    72 	}
    73 
    74 	if (typeAccess && typeAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
    75 	{
    76 		if (err)
    77 		{
    78 			*err = HandleError_Version;
    79 		}
    80 		return 0;
    81 	}
    82 
    83 	if (hndlAccess && hndlAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
    84 	{
    85 		if (err)
    86 		{
    87 			*err = HandleError_Version;
    88 		}
    89 		return 0;
    90 	}
    91 
    92 	bool isChild = false;
    93 
    94 	if (parent != 0)
    95 	{
    96 		isChild = true;
    97 		if (parent & HANDLESYS_SUBTYPE_MASK)
    98 		{
    99 			if (err)
   100 			{
   101 				*err = HandleError_NoInherit;
   102 			}
   103 			return 0;
   104 		}
   105 		if (parent >= HANDLESYS_TYPEARRAY_SIZE
   106 			|| m_Types[parent].dispatch == NULL)
   107 		{
   108 			if (err)
   109 			{
   110 				*err = HandleError_Parameter;
   111 			}
   112 			return 0;
   113 		}
   114 		if (m_Types[parent].typeSec.access[HTypeAccess_Inherit] == false
   115 			&& (m_Types[parent].typeSec.ident != ident))
   116 		{
   117 			if (err)
   118 			{
   119 				*err = HandleError_Access;
   120 			}
   121 			return 0;
   122 		}
   123 	}
   124 
   125 	if (name && name[0] != '\0')
   126 	{
   127 		if (sm_trie_retrieve(m_TypeLookup, name, NULL))
   128 		{
   129 			if (err)
   130 			{
   131 				*err = HandleError_Parameter;
   132 			}
   133 			return 0;
   134 		}
   135 	}
   136 
   137 	unsigned int index;
   138 
   139 	if (isChild)
   140 	{
   141 		QHandleType *pParent = &m_Types[parent];
   142 		if (pParent->children >= HANDLESYS_MAX_SUBTYPES)
   143 		{
   144 			if (err)
   145 			{
   146 				*err = HandleError_Limit;
   147 			}
   148 			return 0;
   149 		}
   150 		index = 0;
   151 		for (unsigned int i=1; i<=HANDLESYS_MAX_SUBTYPES; i++)
   152 		{
   153 			if (m_Types[parent + i].dispatch == NULL)
   154 			{
   155 				index = parent + i;
   156 				break;
   157 			}
   158 		}
   159 		if (!index)
   160 		{
   161 			if (err)
   162 			{
   163 				*err = HandleError_Limit;
   164 			}
   165 			return 0;
   166 		}
   167 		pParent->children++;
   168 	} else {
   169 		if (m_FreeTypes == 0)
   170 		{
   171 			/* Reserve another index */
   172 			if (m_TypeTail >= HANDLESYS_TYPEARRAY_SIZE)
   173 			{
   174 				if (err)
   175 				{
   176 					*err = HandleError_Limit;
   177 				}
   178 				return 0;
   179 			} else {
   180 				m_TypeTail += (HANDLESYS_MAX_SUBTYPES + 1);
   181 				index = m_TypeTail;
   182 			}
   183 		} else {
   184 			/* The "free array" is compacted into the normal array for easiness */
   185 			index = m_Types[m_FreeTypes--].freeID;
   186 		}
   187 	}
   188 
   189 	QHandleType *pType = &m_Types[index];
   190 	
   191 	pType->dispatch = dispatch;
   192 	if (name && name[0] != '\0')
   193 	{
   194 		pType->nameIdx = m_strtab->AddString(name);
   195 		sm_trie_insert(m_TypeLookup, name, (void *)pType);
   196 	} else {
   197 		pType->nameIdx = -1;
   198 	}
   199 
   200 	pType->opened = 0;
   201 
   202 	if (typeAccess)
   203 	{
   204 		pType->typeSec = *typeAccess;
   205 	} else {
   206 		InitAccessDefaults(&pType->typeSec, NULL);
   207 		pType->typeSec.ident = ident;
   208 	}
   209 
   210 	if (hndlAccess)
   211 	{
   212 		pType->hndlSec = *hndlAccess;
   213 	} else {
   214 		InitAccessDefaults(NULL, &pType->hndlSec);
   215 	}
   216 
   217 	if (!isChild)
   218 	{
   219 		pType->children = 0;
   220 	}
   221 
   222 	return index;
   223 }
   224 
   225 bool HandleSystem::FindHandleType(const char *name, HandleType_t *type)
   226 {
   227 	QHandleType *_type;
   228 
   229 	if (!sm_trie_retrieve(m_TypeLookup, name, (void **)&_type))
   230 	{
   231 		return false;
   232 	}
   233 
   234 	unsigned int offset = _type - m_Types;
   235 
   236 	if (type)
   237 	{
   238 		*type = offset;
   239 	}
   240 
   241 	return true;
   242 }
   243 
   244 HandleError HandleSystem::MakePrimHandle(HandleType_t type, 
   245 						   QHandle **in_pHandle, 
   246 						   unsigned int *in_index, 
   247 						   Handle_t *in_handle,
   248 						   IdentityToken_t *owner,
   249 						   bool identity)
   250 {
   251 	unsigned int owner_index = 0;
   252 
   253 	if (owner && (IdentityHandle(owner, &owner_index) != HandleError_None))
   254 	{
   255 		return HandleError_Identity;
   256 	}
   257 
   258 	unsigned int handle;
   259 	if (m_FreeHandles == 0)
   260 	{
   261 		if (m_HandleTail >= HANDLESYS_MAX_HANDLES)
   262 		{
   263 			return HandleError_Limit;;
   264 		}
   265 		handle = ++m_HandleTail;
   266 	} else {
   267 		handle = m_Handles[m_FreeHandles--].freeID;
   268 	}
   269 
   270 	QHandle *pHandle = &m_Handles[handle];
   271 	
   272 	assert(pHandle->set == false);
   273 
   274 	if (++m_HSerial >= HANDLESYS_MAX_SERIALS)
   275 	{
   276 		m_HSerial = 1;
   277 	}
   278 
   279 	/* Set essential information */
   280 	pHandle->set = identity ? HandleSet_Identity : HandleSet_Used;
   281 	pHandle->refcount = 1;
   282 	pHandle->type = type;
   283 	pHandle->serial = m_HSerial;
   284 	pHandle->owner = owner;
   285 	pHandle->ch_next = 0;
   286 
   287 	/* Create the hash value */
   288 	Handle_t hash = pHandle->serial;
   289 	hash <<= 16;
   290 	hash |= handle;
   291 
   292 	/* Add a reference count to the type */
   293 	m_Types[type].opened++;
   294 
   295 	/* Output */
   296 	*in_pHandle = pHandle;
   297 	*in_index = handle;
   298 	*in_handle = hash;
   299 
   300 	/* Decode the identity token 
   301 	 * For now, we don't allow nested ownership
   302 	 */
   303 	if (owner && !identity)
   304 	{
   305 		QHandle *pIdentity = &m_Handles[owner_index];
   306 		if (pIdentity->ch_prev == 0)
   307 		{
   308 			pIdentity->ch_prev = handle;
   309 			pIdentity->ch_next = handle;
   310 			pHandle->ch_prev = 0;
   311 		} else {
   312 			/* Link previous node to us (forward) */
   313 			m_Handles[pIdentity->ch_next].ch_next = handle;
   314 			/* Link us to previous node (backwards) */
   315 			pHandle->ch_prev = pIdentity->ch_next;
   316 			/* Set new tail */
   317 			pIdentity->ch_next = handle;
   318 		}
   319 		pIdentity->refcount++;
   320 	} else {
   321 		pHandle->ch_prev = 0;
   322 	}
   323 
   324 	return HandleError_None;
   325 }
   326 
   327 void HandleSystem::SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken)
   328 {
   329 	if (!type
   330 		|| type >= HANDLESYS_TYPEARRAY_SIZE
   331 		|| m_Types[type].dispatch == NULL)
   332 	{
   333 		return;
   334 	}
   335 
   336 	m_Types[type].typeSec.ident = pToken;
   337 }
   338 
   339 Handle_t HandleSystem::CreateHandleEx(HandleType_t type, void *object, IdentityToken_t *owner, IdentityToken_t *ident, HandleError *err, bool identity)
   340 {
   341 	if (!type 
   342 		|| type >= HANDLESYS_TYPEARRAY_SIZE
   343 		|| m_Types[type].dispatch == NULL)
   344 	{
   345 		if (err)
   346 		{
   347 			*err = HandleError_Parameter;
   348 		}
   349 		return 0;
   350 	}
   351 
   352 	/* Check to see if we're allowed to create this handle type */
   353 	QHandleType *pType = &m_Types[type];
   354 	if (!pType->typeSec.access[HTypeAccess_Create]
   355 	&& (!pType->typeSec.ident
   356 		|| pType->typeSec.ident != ident))
   357 	{
   358 		if (err)
   359 		{
   360 			*err = HandleError_Access;
   361 		}
   362 		return 0;
   363 	}
   364 
   365 	unsigned int index;
   366 	Handle_t handle;
   367 	QHandle *pHandle;
   368 	HandleError _err;
   369 
   370 	if ((_err=MakePrimHandle(type, &pHandle, &index, &handle, owner, identity)) != HandleError_None)
   371 	{
   372 		if (err)
   373 		{
   374 			*err = _err;
   375 		}
   376 		return 0;
   377 	}
   378 
   379 	pHandle->object = object;
   380 	pHandle->clone = 0;
   381 
   382 	return handle;
   383 }
   384 
   385 Handle_t HandleSystem::CreateHandle(HandleType_t type, void *object, IdentityToken_t *owner, IdentityToken_t *ident, HandleError *err)
   386 {
   387 	return CreateHandleEx(type, object, owner, ident, err, false);
   388 }
   389 
   390 bool HandleSystem::TypeCheck(HandleType_t intype, HandleType_t outtype)
   391 {
   392 	/* Check the type inheritance */
   393 	if (intype & HANDLESYS_SUBTYPE_MASK)
   394 	{
   395 		if (intype != outtype
   396 			&& (TypeParent(intype) != TypeParent(outtype)))
   397 		{
   398 			return false;
   399 		}
   400 	} else {
   401 		if (intype != outtype)
   402 		{
   403 			return false;
   404 		}
   405 	}
   406 
   407 	return true;
   408 }
   409 
   410 HandleError HandleSystem::GetHandle(Handle_t handle,
   411 									IdentityToken_t *ident, 
   412 									QHandle **in_pHandle, 
   413 									unsigned int *in_index,
   414 									bool ignoreFree)
   415 {
   416 	unsigned int serial = (handle >> 16);
   417 	unsigned int index = (handle & HANDLESYS_HANDLE_MASK);
   418 
   419 	if (index == 0 || index == 0xFFFF)
   420 	{
   421 		return HandleError_Index;
   422 	}
   423 
   424 	QHandle *pHandle = &m_Handles[index];
   425 
   426 	if (!pHandle->set
   427 		|| (pHandle->set == HandleSet_Freed && !ignoreFree))
   428 	{
   429 		return HandleError_Freed;
   430 	} else if (pHandle->set == HandleSet_Identity
   431 			   && ident != g_ShareSys.GetIdentRoot())
   432 	{
   433 		/* Only IdentityHandle() can read this! */
   434 		return HandleError_Identity;
   435 	}
   436 	if (pHandle->serial != serial)
   437 	{
   438 		return HandleError_Changed;
   439 	}
   440 
   441 	*in_pHandle = pHandle;
   442 	*in_index = index;
   443 
   444 	return HandleError_None;
   445 }
   446 
   447 bool HandleSystem::CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity)
   448 {
   449 	QHandleType *pType = &m_Types[pHandle->type];
   450 	unsigned int access = pType->hndlSec.access[right];
   451 
   452 	/* Check if the type's identity matches */
   453 	if (access & HANDLE_RESTRICT_IDENTITY)
   454 	{
   455 		IdentityToken_t *owner = pType->typeSec.ident;
   456 		if (!owner
   457 			|| (!pSecurity || pSecurity->pIdentity != owner))
   458 		{
   459 			return false;
   460 		}
   461 	}
   462 
   463 	/* Check if the owner is allowed */
   464 	if (access & HANDLE_RESTRICT_OWNER)
   465 	{
   466 		IdentityToken_t *owner = pHandle->owner;
   467 		if (owner
   468 			&& (!pSecurity || pSecurity->pOwner != owner))
   469 		{
   470 			return false;
   471 		}
   472 	}
   473 
   474 	return true;
   475 }
   476 
   477 HandleError HandleSystem::CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner)
   478 {
   479 	/* Get a new Handle ID */
   480 	unsigned int new_index;
   481 	QHandle *pNewHandle;
   482 	Handle_t new_handle;
   483 	HandleError err;
   484 
   485 	if ((err=MakePrimHandle(pHandle->type, &pNewHandle, &new_index, &new_handle, newOwner)) != HandleError_None)
   486 	{
   487 		return err;
   488 	}
   489 
   490 	pNewHandle->clone = index;
   491 	pHandle->refcount++;
   492 
   493 	*newhandle = new_handle;
   494 
   495 	return HandleError_None;
   496 }
   497 
   498 HandleError HandleSystem::CloneHandle(Handle_t handle, Handle_t *newhandle, IdentityToken_t *newOwner, const HandleSecurity *pSecurity)
   499 {
   500 	HandleError err;
   501 	QHandle *pHandle;
   502 	unsigned int index;
   503 	IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
   504 
   505 	if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
   506 	{
   507 		return err;
   508 	}
   509 
   510 	/* Identities cannot be cloned */
   511 	if (pHandle->set == HandleSet_Identity)
   512 	{
   513 		return HandleError_Identity;
   514 	}
   515 
   516 	/* Check if the handle can be cloned */
   517 	if (!CheckAccess(pHandle, HandleAccess_Clone, pSecurity))
   518 	{
   519 		return HandleError_Access;
   520 	}
   521 
   522 	/* Make sure we're not cloning a clone */
   523 	if (pHandle->clone)
   524 	{
   525 		QHandle *pParent = &m_Handles[pHandle->clone];
   526 		return CloneHandle(pParent, pHandle->clone, newhandle, newOwner);
   527 	}
   528 
   529 	return CloneHandle(pHandle, index, newhandle, newOwner);
   530 }
   531 
   532 HandleError HandleSystem::FreeHandle(QHandle *pHandle, unsigned int index)
   533 {
   534 	QHandleType *pType = &m_Types[pHandle->type];
   535 
   536 	if (pHandle->clone)
   537 	{
   538 		/* If we're a clone, decrease the parent reference count */
   539 		QHandle *pMaster;
   540 		unsigned int master;
   541 
   542 		/* Note that if we ever have per-handle security, we would need to re-check
   543 		* the access on this Handle. */
   544 		master = pHandle->clone;
   545 		pMaster = &m_Handles[master];
   546 
   547 		/* Release the clone now */
   548 		ReleasePrimHandle(index);
   549 
   550 		/* Decrement the master's reference count */
   551 		if (--pMaster->refcount == 0)
   552 		{
   553 			/* Type should be the same but do this anyway... */
   554 			pType = &m_Types[pMaster->type];
   555 			pType->dispatch->OnHandleDestroy(pMaster->type, pMaster->object);
   556 			ReleasePrimHandle(master);
   557 		}
   558 	} else if (pHandle->set == HandleSet_Identity) {
   559 		/* If we're an identity, skip all this stuff!
   560 		 * NOTE: SHARESYS DOES NOT CARE ABOUT THE DESTRUCTOR
   561 		 */
   562 		ReleasePrimHandle(index);
   563 	} else {
   564 		/* Decrement, free if necessary */
   565 		if (--pHandle->refcount == 0)
   566 		{
   567 			pType->dispatch->OnHandleDestroy(pHandle->type, pHandle->object);
   568 			ReleasePrimHandle(index);
   569 		} else {
   570 			/* We must be cloned, so mark ourselves as freed */
   571 			pHandle->set = HandleSet_Freed;
   572 			/* Now, unlink us, so we're not being tracked by the owner */
   573 			if (pHandle->owner)
   574 			{
   575 				UnlinkHandleFromOwner(pHandle, index);
   576 			}
   577 		}
   578 	}
   579 
   580 	return HandleError_None;
   581 }
   582 
   583 HandleError HandleSystem::FreeHandle(Handle_t handle, const HandleSecurity *pSecurity)
   584 {
   585 	unsigned int index;
   586 	QHandle *pHandle;
   587 	HandleError err;
   588 	IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
   589 
   590 	if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
   591 	{
   592 		return err;
   593 	}
   594 
   595 	if (!CheckAccess(pHandle, HandleAccess_Delete, pSecurity))
   596 	{
   597 		return HandleError_Access;
   598 	}
   599 
   600 	return FreeHandle(pHandle, index);
   601 }
   602 
   603 HandleError HandleSystem::ReadHandle(Handle_t handle, HandleType_t type, const HandleSecurity *pSecurity, void **object)
   604 {
   605 	unsigned int index;
   606 	QHandle *pHandle;
   607 	HandleError err;
   608 	IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
   609 
   610 	if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
   611 	{
   612 		return err;
   613 	}
   614 
   615 	if (!CheckAccess(pHandle, HandleAccess_Read, pSecurity))
   616 	{
   617 		return HandleError_Access;
   618 	}
   619 
   620 	/* Check the type inheritance */
   621 	if (pHandle->type & HANDLESYS_SUBTYPE_MASK)
   622 	{
   623 		if (pHandle->type != type
   624 			&& (TypeParent(pHandle->type) != TypeParent(type)))
   625 		{
   626 			return HandleError_Type;
   627 		}
   628 	} else if (type) {
   629 		if (pHandle->type != type)
   630 		{
   631 			return HandleError_Type;
   632 		}
   633 	}
   634 
   635 	if (object)
   636 	{
   637 		/* if we're a clone, the rules change - object is ONLY in our reference */
   638 		if (pHandle->clone)
   639 		{
   640 			pHandle = &m_Handles[pHandle->clone];
   641 		}
   642 		*object = pHandle->object;
   643 	}
   644 
   645 	return HandleError_None;
   646 }
   647 
   648 void HandleSystem::UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index)
   649 {
   650 	/* Unlink us if necessary */
   651 	unsigned int ident_index;
   652 	if (IdentityHandle(pHandle->owner, &ident_index) != HandleError_None)
   653 	{
   654 		/* Uh-oh! */
   655 		assert(pHandle->owner == 0);
   656 		return;
   657 	}
   658 	/* Note that since 0 is an invalid handle, if any of these links are 0,
   659 	* the data can still be set.
   660 	*/
   661 	QHandle *pIdentity = &m_Handles[ident_index];
   662 
   663 	/* Unlink case: We're the head AND tail node */
   664 	if (index == pIdentity->ch_prev && index == pIdentity->ch_next)
   665 	{
   666 		pIdentity->ch_prev = 0;
   667 		pIdentity->ch_next = 0;
   668 	}
   669 	/* Unlink case: We're the head node */
   670 	else if (index == pIdentity->ch_prev) {
   671 		/* Link us to the next in the chain */
   672 		pIdentity->ch_prev = pHandle->ch_next;
   673 		/* Patch up the previous link */
   674 		m_Handles[pHandle->ch_next].ch_prev = 0;
   675 	}
   676 	/* Unlink case: We're the tail node */
   677 	else if (index == pIdentity->ch_next) {
   678 		/* Link us to the previous in the chain */
   679 		pIdentity->ch_next = pHandle->ch_prev;
   680 		/* Patch up the next link */
   681 		m_Handles[pHandle->ch_prev].ch_next = 0;
   682 	}
   683 	/* Unlink case: We're in the middle! */
   684 	else {
   685 		/* Patch the forward reference */
   686 		m_Handles[pHandle->ch_next].ch_prev = pHandle->ch_prev;
   687 		/* Patch the backward reference */
   688 		m_Handles[pHandle->ch_prev].ch_next = pHandle->ch_next;
   689 	}
   690 
   691 	/* Lastly, decrease the reference count */
   692 	pIdentity->refcount--;
   693 }
   694 
   695 void HandleSystem::ReleasePrimHandle(unsigned int index)
   696 {
   697 	QHandle *pHandle = &m_Handles[index];
   698 	HandleSet set = pHandle->set;
   699 
   700 	if (pHandle->owner && (set != HandleSet_Identity))
   701 	{
   702 		UnlinkHandleFromOwner(pHandle, index);
   703 	}
   704 
   705 	/* Were we an identity ourself? */
   706 	QHandle *pLocal;
   707 	if (set == HandleSet_Identity)
   708 	{
   709 		/* Extra work to do.  We need to find everything connected to this identity and release it. */
   710 		unsigned int ch_index;
   711 #if defined _DEBUG
   712 		unsigned int old_index = 0;
   713 #endif
   714 		while ((ch_index = pHandle->ch_next) != 0)
   715 		{
   716 			pLocal = &m_Handles[ch_index];
   717 #if defined _DEBUG
   718 			assert(old_index != ch_index);
   719 			assert(pLocal->set == HandleSet_Used);
   720 			old_index = ch_index;
   721 #endif
   722 			FreeHandle(pLocal, ch_index);
   723 		}
   724 	}
   725 
   726 	pHandle->set = HandleSet_None;
   727 	m_Types[pHandle->type].opened--;
   728 	m_Handles[++m_FreeHandles].freeID = index;
   729 }
   730 
   731 bool HandleSystem::RemoveType(HandleType_t type, IdentityToken_t *ident)
   732 {
   733 	if (type == 0 || type >= HANDLESYS_TYPEARRAY_SIZE)
   734 	{
   735 		return false;
   736 	}
   737 
   738 	QHandleType *pType = &m_Types[type];
   739 
   740 	if (pType->typeSec.ident
   741 		&& pType->typeSec.ident != ident)
   742 	{
   743 		return false;
   744 	}
   745 
   746 	if (pType->dispatch == NULL)
   747 	{
   748 		return false;
   749 	}
   750 
   751 	/* Remove children if we have to */
   752 	if (!(type & HANDLESYS_SUBTYPE_MASK))
   753 	{
   754 		QHandleType *childType;
   755 		for (unsigned int i=1; i<=HANDLESYS_MAX_SUBTYPES; i++)
   756 		{
   757 			childType = &m_Types[type + i];
   758 			if (childType->dispatch)
   759 			{
   760 				RemoveType(type + i, childType->typeSec.ident);
   761 			}
   762 		}
   763 		/* Link us into the free chain */
   764 		m_Types[++m_FreeTypes].freeID = type;
   765 	}
   766 
   767 	/* Invalidate the type now */
   768 	IHandleTypeDispatch *dispatch = pType->dispatch;
   769 	pType->dispatch = NULL;
   770 
   771 	/* Make sure nothing is using this type. */
   772 	if (pType->opened)
   773 	{
   774 		QHandle *pHandle;
   775 		for (unsigned int i=1; i<=m_HandleTail; i++)
   776 		{
   777 			pHandle = &m_Handles[i];
   778 			if (!pHandle->set || pHandle->type != type)
   779 			{
   780 				continue;
   781 			}
   782 			if (pHandle->clone)
   783 			{
   784 				/* Get parent */
   785 				QHandle *pOther = &m_Handles[pHandle->clone];
   786 				if (--pOther->refcount == 0)
   787 				{
   788 					/* Free! */
   789 					dispatch->OnHandleDestroy(type, pOther->object);
   790 					ReleasePrimHandle(pHandle->clone);
   791 				}
   792 				/* Unlink ourselves since we don't have a reference count */
   793 				ReleasePrimHandle(i);
   794 			} else {
   795 				/* If it's not a clone, we still have to check the reference count.
   796 				 * Either way, we'll be destroyed eventually because the handle types do not change.
   797 				 */
   798 				if (--pHandle->refcount == 0)
   799 				{
   800 					/* Free! */
   801 					dispatch->OnHandleDestroy(type, pHandle->object);
   802 					ReleasePrimHandle(i);
   803 				}
   804 			}
   805 			if (pType->opened == 0)
   806 			{
   807 				break;
   808 			}
   809 		}
   810 	}
   811 
   812 	return true;
   813 }
   814 
   815 bool HandleSystem::InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess)
   816 {
   817 	if (pTypeAccess)
   818 	{
   819 		if (pTypeAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
   820 		{
   821 			return false;
   822 		}
   823 		pTypeAccess->access[HTypeAccess_Create] = false;
   824 		pTypeAccess->access[HTypeAccess_Inherit] = false;
   825 		pTypeAccess->ident = NULL;
   826 	}
   827 
   828 	if (pHandleAccess)
   829 	{
   830 		if (pHandleAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
   831 		{
   832 			return false;
   833 		}
   834 
   835 		pHandleAccess->access[HandleAccess_Clone] = 0;
   836 		pHandleAccess->access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER;
   837 		pHandleAccess->access[HandleAccess_Read] = HANDLE_RESTRICT_IDENTITY;
   838 	}
   839 
   840 	return true;
   841 }