2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "HandleSys.h"
35 #include "common_logic.h"
37 #include "ExtensionSys.h"
38 #include "PluginSys.h"
40 HandleSystem g_HandleSys;
42 QHandle *ignore_handle;
44 inline HandleType_t TypeParent(HandleType_t type)
46 return (type & ~HANDLESYS_SUBTYPE_MASK);
49 inline HandleError IdentityHandle(IdentityToken_t *token, unsigned int *index)
51 return g_HandleSys.GetHandle(token->ident, g_ShareSys.GetIdentRoot(), &ignore_handle, index);
54 HandleSystem::HandleSystem()
56 m_Handles = new QHandle[HANDLESYS_MAX_HANDLES + 1];
57 memset(m_Handles, 0, sizeof(QHandle) * (HANDLESYS_MAX_HANDLES + 1));
59 m_Types = new QHandleType[HANDLESYS_TYPEARRAY_SIZE];
60 memset(m_Types, 0, sizeof(QHandleType) * HANDLESYS_TYPEARRAY_SIZE);
62 m_strtab = new BaseStringTable(512);
67 HandleSystem::~HandleSystem()
75 HandleType_t HandleSystem::CreateType(const char *name,
76 IHandleTypeDispatch *dispatch,
78 const TypeAccess *typeAccess,
79 const HandleAccess *hndlAccess,
80 IdentityToken_t *ident,
87 *err = HandleError_Parameter;
92 if (typeAccess && typeAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
96 *err = HandleError_Version;
101 if (hndlAccess && hndlAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
105 *err = HandleError_Version;
110 bool isChild = false;
115 if (parent & HANDLESYS_SUBTYPE_MASK)
119 *err = HandleError_NoInherit;
123 if (parent >= HANDLESYS_TYPEARRAY_SIZE
124 || m_Types[parent].dispatch == NULL)
128 *err = HandleError_Parameter;
132 if (m_Types[parent].typeSec.access[HTypeAccess_Inherit] == false
133 && (m_Types[parent].typeSec.ident != ident))
137 *err = HandleError_Access;
143 if (name && name[0] != '\0')
145 if (m_TypeLookup.retrieve(name))
149 *err = HandleError_Parameter;
159 QHandleType *pParent = &m_Types[parent];
160 if (pParent->children >= HANDLESYS_MAX_SUBTYPES)
164 *err = HandleError_Limit;
169 for (unsigned int i=1; i<=HANDLESYS_MAX_SUBTYPES; i++)
171 if (m_Types[parent + i].dispatch == NULL)
181 *err = HandleError_Limit;
187 if (m_FreeTypes == 0)
189 /* Reserve another index */
190 if (m_TypeTail >= HANDLESYS_TYPEARRAY_SIZE)
194 *err = HandleError_Limit;
198 m_TypeTail += (HANDLESYS_MAX_SUBTYPES + 1);
202 /* The "free array" is compacted into the normal array for easiness */
203 index = m_Types[m_FreeTypes--].freeID;
207 QHandleType *pType = &m_Types[index];
209 pType->dispatch = dispatch;
210 if (name && name[0] != '\0')
212 pType->nameIdx = m_strtab->AddString(name);
213 m_TypeLookup.insert(name, pType);
222 pType->typeSec = *typeAccess;
224 InitAccessDefaults(&pType->typeSec, NULL);
225 pType->typeSec.ident = ident;
230 pType->hndlSec = *hndlAccess;
232 InitAccessDefaults(NULL, &pType->hndlSec);
243 bool HandleSystem::FindHandleType(const char *name, HandleType_t *aResult)
246 if (!m_TypeLookup.retrieve(name, &type))
250 *aResult = type - m_Types;
255 HandleError HandleSystem::TryAllocHandle(unsigned int *handle)
257 if (m_FreeHandles == 0)
259 if (m_HandleTail >= HANDLESYS_MAX_HANDLES)
261 return HandleError_Limit;;
263 *handle = ++m_HandleTail;
267 *handle = m_Handles[m_FreeHandles--].freeID;
270 return HandleError_None;
273 HandleError HandleSystem::MakePrimHandle(HandleType_t type,
274 QHandle **in_pHandle,
275 unsigned int *in_index,
277 IdentityToken_t *owner,
281 unsigned int owner_index = 0;
283 if (owner && (IdentityHandle(owner, &owner_index) != HandleError_None))
285 return HandleError_Identity;
289 if ((err = TryAllocHandle(&handle)) != HandleError_None)
291 if (!TryAndFreeSomeHandles()
292 || (err = TryAllocHandle(&handle)) != HandleError_None)
298 QHandle *pHandle = &m_Handles[handle];
300 assert(pHandle->set == false);
302 if (++m_HSerial >= HANDLESYS_MAX_SERIALS)
307 /* Set essential information */
308 pHandle->set = identity ? HandleSet_Identity : HandleSet_Used;
309 pHandle->refcount = 1;
310 pHandle->type = type;
311 pHandle->serial = m_HSerial;
312 pHandle->owner = owner;
313 pHandle->ch_next = 0;
314 pHandle->access_special = false;
315 pHandle->is_destroying = false;
317 /* Create the hash value */
318 Handle_t hash = pHandle->serial;
322 /* Add a reference count to the type */
323 m_Types[type].opened++;
326 *in_pHandle = pHandle;
330 /* Decode the identity token
331 * For now, we don't allow nested ownership
333 if (owner && !identity)
335 QHandle *pIdentity = &m_Handles[owner_index];
336 if (pIdentity->ch_prev == 0)
338 pIdentity->ch_prev = handle;
339 pIdentity->ch_next = handle;
340 pHandle->ch_prev = 0;
344 /* Link previous node to us (forward) */
345 m_Handles[pIdentity->ch_next].ch_next = handle;
346 /* Link us to previous node (backwards) */
347 pHandle->ch_prev = pIdentity->ch_next;
349 pIdentity->ch_next = handle;
351 pIdentity->refcount++;
355 pHandle->ch_prev = 0;
358 return HandleError_None;
361 void HandleSystem::SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken)
364 || type >= HANDLESYS_TYPEARRAY_SIZE
365 || m_Types[type].dispatch == NULL)
370 m_Types[type].typeSec.ident = pToken;
373 Handle_t HandleSystem::CreateHandleInt(HandleType_t type,
375 const HandleSecurity *pSec,
377 const HandleAccess *pAccess,
380 IdentityToken_t *ident;
381 IdentityToken_t *owner;
385 ident = pSec->pIdentity;
386 owner = pSec->pOwner;
393 || type >= HANDLESYS_TYPEARRAY_SIZE
394 || m_Types[type].dispatch == NULL)
398 *err = HandleError_Parameter;
403 /* Check to see if we're allowed to create this handle type */
404 QHandleType *pType = &m_Types[type];
405 if (!pType->typeSec.access[HTypeAccess_Create]
406 && (!pType->typeSec.ident
407 || pType->typeSec.ident != ident))
411 *err = HandleError_Access;
421 if ((_err=MakePrimHandle(type, &pHandle, &index, &handle, owner, identity)) != HandleError_None)
432 pHandle->access_special = true;
433 pHandle->sec = *pAccess;
436 pHandle->object = object;
442 Handle_t HandleSystem::CreateHandleEx(HandleType_t type, void *object, const HandleSecurity *pSec, const HandleAccess *pAccess, HandleError *err)
444 return CreateHandleInt(type, object, pSec, err, pAccess, false);
447 Handle_t HandleSystem::CreateHandle(HandleType_t type, void *object, IdentityToken_t *owner, IdentityToken_t *ident, HandleError *err)
451 sec.pIdentity = ident;
454 return CreateHandleEx(type, object, &sec, NULL, err);
457 bool HandleSystem::TypeCheck(HandleType_t intype, HandleType_t outtype)
459 /* Check the type inheritance */
460 if (intype & HANDLESYS_SUBTYPE_MASK)
462 if (intype != outtype
463 && (TypeParent(intype) != TypeParent(outtype)))
468 if (intype != outtype)
477 HandleError HandleSystem::GetHandle(Handle_t handle,
478 IdentityToken_t *ident,
479 QHandle **in_pHandle,
480 unsigned int *in_index,
483 unsigned int serial = (handle >> 16);
484 unsigned int index = (handle & HANDLESYS_HANDLE_MASK);
486 if (index == 0 || index > m_HandleTail || index > HANDLESYS_MAX_HANDLES)
488 return HandleError_Index;
491 QHandle *pHandle = &m_Handles[index];
494 || (pHandle->set == HandleSet_Freed && !ignoreFree))
496 return HandleError_Freed;
497 } else if (pHandle->set == HandleSet_Identity
498 && ident != g_ShareSys.GetIdentRoot())
500 /* Only IdentityHandle() can read this! */
501 return HandleError_Identity;
503 if (pHandle->serial != serial)
505 return HandleError_Changed;
508 *in_pHandle = pHandle;
511 return HandleError_None;
514 bool HandleSystem::CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity)
516 QHandleType *pType = &m_Types[pHandle->type];
519 if (pHandle->access_special)
521 access = pHandle->sec.access[right];
523 access = pType->hndlSec.access[right];
526 /* Check if the type's identity matches */
527 if (access & HANDLE_RESTRICT_IDENTITY)
529 IdentityToken_t *owner = pType->typeSec.ident;
531 || (!pSecurity || pSecurity->pIdentity != owner))
537 /* Check if the owner is allowed */
538 if (access & HANDLE_RESTRICT_OWNER)
540 IdentityToken_t *owner = pHandle->owner;
542 && (!pSecurity || pSecurity->pOwner != owner))
551 HandleError HandleSystem::CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner)
553 /* Get a new Handle ID */
554 unsigned int new_index;
559 if ((err=MakePrimHandle(pHandle->type, &pNewHandle, &new_index, &new_handle, newOwner)) != HandleError_None)
564 /* Assign permissions from parent */
565 if (pHandle->access_special)
567 pNewHandle->access_special = true;
568 pNewHandle->sec = pHandle->sec;
571 pNewHandle->clone = index;
572 pNewHandle->object = NULL;
575 *newhandle = new_handle;
577 return HandleError_None;
580 HandleError HandleSystem::CloneHandle(Handle_t handle, Handle_t *newhandle, IdentityToken_t *newOwner, const HandleSecurity *pSecurity)
585 IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
587 if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
592 /* Identities cannot be cloned */
593 if (pHandle->set == HandleSet_Identity)
595 return HandleError_Identity;
598 /* Check if the handle can be cloned */
599 if (!CheckAccess(pHandle, HandleAccess_Clone, pSecurity))
601 return HandleError_Access;
604 /* Make sure we're not cloning a clone */
607 QHandle *pParent = &m_Handles[pHandle->clone];
608 return CloneHandle(pParent, pHandle->clone, newhandle, newOwner);
611 return CloneHandle(pHandle, index, newhandle, newOwner);
614 Handle_t HandleSystem::FastCloneHandle(QHandle *pHandle, unsigned int index)
617 return FastCloneHandle(&m_Handles[pHandle->clone], pHandle->clone);
620 if (CloneHandle(pHandle, index, &hndl, g_pCoreIdent) != HandleError_None)
626 Handle_t HandleSystem::FastCloneHandle(Handle_t hndl)
631 GetHandleUnchecked(hndl, pHandle, index);
633 return FastCloneHandle(pHandle, index);
636 void HandleSystem::GetHandleUnchecked(Handle_t hndl, QHandle *& pHandle, unsigned int &index)
639 unsigned int serial = (hndl >> 16);
641 index = (hndl & HANDLESYS_HANDLE_MASK);
643 assert(index != 0 && index <= m_HandleTail && index < HANDLESYS_MAX_HANDLES);
645 pHandle = &m_Handles[index];
647 assert(pHandle->set && pHandle->set != HandleSet_Freed);
648 assert(pHandle->serial == serial);
651 HandleError HandleSystem::FreeHandle(QHandle *pHandle, unsigned int index)
653 if (pHandle->is_destroying)
655 /* Someone tried to free this recursively.
656 * We'll just ignore this safely.
658 return HandleError_None;
661 QHandleType *pType = &m_Types[pHandle->type];
665 /* If we're a clone, decrease the parent reference count */
669 /* Note that if we ever have per-handle security, we would need to re-check
670 * the access on this Handle. */
671 master = pHandle->clone;
672 pMaster = &m_Handles[master];
674 /* Release the clone now */
675 pHandle->is_destroying = true;
676 ReleasePrimHandle(index);
678 /* Decrement the master's reference count */
679 if (--pMaster->refcount == 0)
681 /* Type should be the same but do this anyway... */
682 pType = &m_Types[pMaster->type];
683 pMaster->is_destroying = true;
686 pType->dispatch->OnHandleDestroy(pMaster->type, pMaster->object);
688 ReleasePrimHandle(master);
690 } else if (pHandle->set == HandleSet_Identity) {
691 /* If we're an identity, skip all this stuff!
692 * NOTE: SHARESYS DOES NOT CARE ABOUT THE DESTRUCTOR
694 pHandle->is_destroying = true;
695 ReleasePrimHandle(index);
697 /* Decrement, free if necessary */
698 if (--pHandle->refcount == 0)
700 pHandle->is_destroying = true;
703 pType->dispatch->OnHandleDestroy(pHandle->type, pHandle->object);
705 ReleasePrimHandle(index);
707 /* We must be cloned, so mark ourselves as freed */
708 pHandle->set = HandleSet_Freed;
709 /* Now, unlink us, so we're not being tracked by the owner */
712 UnlinkHandleFromOwner(pHandle, index);
717 return HandleError_None;
720 HandleError HandleSystem::FreeHandle(Handle_t handle, const HandleSecurity *pSecurity)
725 IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
727 if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
732 if (!CheckAccess(pHandle, HandleAccess_Delete, pSecurity))
734 return HandleError_Access;
737 return FreeHandle(pHandle, index);
740 HandleError HandleSystem::ReadHandle(Handle_t handle, HandleType_t type, const HandleSecurity *pSecurity, void **object)
745 IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
747 if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
752 if (!CheckAccess(pHandle, HandleAccess_Read, pSecurity))
754 return HandleError_Access;
757 /* Check the type inheritance */
758 if (pHandle->type & HANDLESYS_SUBTYPE_MASK)
760 if (pHandle->type != type
761 && (TypeParent(pHandle->type) != TypeParent(type)))
763 return HandleError_Type;
766 if (pHandle->type != type)
768 return HandleError_Type;
774 /* if we're a clone, the rules change - object is ONLY in our reference */
777 pHandle = &m_Handles[pHandle->clone];
779 *object = pHandle->object;
782 return HandleError_None;
785 void HandleSystem::UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index)
787 /* Unlink us if necessary */
788 unsigned int ident_index;
789 if (IdentityHandle(pHandle->owner, &ident_index) != HandleError_None)
792 assert(pHandle->owner == 0);
796 pHandle->owner = NULL;
798 /* Note that since 0 is an invalid handle, if any of these links are 0,
799 * the data can still be set.
801 QHandle *pIdentity = &m_Handles[ident_index];
803 /* Unlink case: We're the head AND tail node */
804 if (index == pIdentity->ch_prev && index == pIdentity->ch_next)
806 pIdentity->ch_prev = 0;
807 pIdentity->ch_next = 0;
809 /* Unlink case: We're the head node */
810 else if (index == pIdentity->ch_prev) {
811 /* Link us to the next in the chain */
812 pIdentity->ch_prev = pHandle->ch_next;
813 /* Patch up the previous link */
814 m_Handles[pHandle->ch_next].ch_prev = 0;
816 /* Unlink case: We're the tail node */
817 else if (index == pIdentity->ch_next) {
818 /* Link us to the previous in the chain */
819 pIdentity->ch_next = pHandle->ch_prev;
820 /* Patch up the next link */
821 m_Handles[pHandle->ch_prev].ch_next = 0;
823 /* Unlink case: We're in the middle! */
825 /* Patch the forward reference */
826 m_Handles[pHandle->ch_next].ch_prev = pHandle->ch_prev;
827 /* Patch the backward reference */
828 m_Handles[pHandle->ch_prev].ch_next = pHandle->ch_next;
831 /* Lastly, decrease the reference count */
832 pIdentity->refcount--;
835 void HandleSystem::ReleasePrimHandle(unsigned int index)
837 QHandle *pHandle = &m_Handles[index];
838 HandleSet set = pHandle->set;
840 if (pHandle->owner && (set != HandleSet_Identity))
842 UnlinkHandleFromOwner(pHandle, index);
845 /* Were we an identity ourself? */
847 if (set == HandleSet_Identity)
849 /* Extra work to do. We need to find everything connected to this identity and release it. */
850 unsigned int ch_index;
852 unsigned int old_index = 0;
854 while ((ch_index = pHandle->ch_next) != 0)
856 pLocal = &m_Handles[ch_index];
858 assert(old_index != ch_index);
859 assert(pLocal->set == HandleSet_Used);
860 old_index = ch_index;
862 FreeHandle(pLocal, ch_index);
866 pHandle->set = HandleSet_None;
867 m_Types[pHandle->type].opened--;
868 m_Handles[++m_FreeHandles].freeID = index;
871 bool HandleSystem::RemoveType(HandleType_t type, IdentityToken_t *ident)
873 if (type == 0 || type >= HANDLESYS_TYPEARRAY_SIZE)
878 QHandleType *pType = &m_Types[type];
880 if (pType->typeSec.ident
881 && pType->typeSec.ident != ident)
886 if (pType->dispatch == NULL)
891 /* Remove children if we have to */
892 if (!(type & HANDLESYS_SUBTYPE_MASK))
894 QHandleType *childType;
895 for (unsigned int i=1; i<=HANDLESYS_MAX_SUBTYPES; i++)
897 childType = &m_Types[type + i];
898 if (childType->dispatch)
900 RemoveType(type + i, childType->typeSec.ident);
903 /* Link us into the free chain */
904 m_Types[++m_FreeTypes].freeID = type;
907 /* Invalidate the type now */
908 IHandleTypeDispatch *dispatch = pType->dispatch;
909 pType->dispatch = NULL;
911 /* Make sure nothing is using this type. */
915 for (unsigned int i=1; i<=m_HandleTail; i++)
917 pHandle = &m_Handles[i];
918 if (!pHandle->set || pHandle->type != type)
923 FreeHandle(pHandle, i);
925 if (pType->opened == 0)
932 /* Remove it from the type cache. */
933 if (pType->nameIdx != -1)
935 const char *typeName;
937 typeName = m_strtab->GetString(pType->nameIdx);
938 m_TypeLookup.remove(typeName);
944 bool HandleSystem::InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess)
948 if (pTypeAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
952 pTypeAccess->access[HTypeAccess_Create] = false;
953 pTypeAccess->access[HTypeAccess_Inherit] = false;
954 pTypeAccess->ident = NULL;
959 if (pHandleAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
964 pHandleAccess->access[HandleAccess_Clone] = 0;
965 pHandleAccess->access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER;
966 pHandleAccess->access[HandleAccess_Read] = HANDLE_RESTRICT_IDENTITY;
972 #define HANDLE_LOG_VERY_BAD(message, ...) \
973 smcore.LogFatal(message, ##__VA_ARGS__); \
974 smcore.LogError(message, ##__VA_ARGS__);
976 bool HandleSystem::TryAndFreeSomeHandles()
978 IPlugin *highest_owner = NULL;
979 unsigned int highest_handle_count = 0;
981 /* Search all plugins */
982 for (IPluginIterator *pl_iter = g_PluginSys.GetPluginIterator(); pl_iter->MorePlugins(); pl_iter->NextPlugin())
984 IPlugin *plugin = pl_iter->GetPlugin();
985 IdentityToken_t *identity = plugin->GetIdentity();
986 unsigned int handle_count = 0;
988 if (identity == NULL)
993 /* Search all handles */
994 for (unsigned int i = 1; i <= m_HandleTail; i++)
996 if (m_Handles[i].set != HandleSet_Used)
1000 if (m_Handles[i].owner == identity)
1006 if (handle_count > highest_handle_count)
1008 highest_owner = plugin;
1009 highest_handle_count = handle_count;
1013 if (highest_owner == NULL || highest_handle_count == 0)
1018 HANDLE_LOG_VERY_BAD("[SM] MEMORY LEAK DETECTED IN PLUGIN (file \"%s\")", highest_owner->GetFilename());
1019 HANDLE_LOG_VERY_BAD("[SM] Unloading plugin to free %d handles.", highest_handle_count);
1020 HANDLE_LOG_VERY_BAD("[SM] Contact the author(s) of this plugin to correct this error.", highest_handle_count);
1021 HANDLE_LOG_VERY_BAD("--------------------------------------------------------------------------");
1023 const IdentityToken_t *pIdentity = highest_owner->GetIdentity();
1024 unsigned int total = 0, highest_index = 0, total_size = 0, size;
1025 unsigned int * pCount = new unsigned int[HANDLESYS_TYPEARRAY_SIZE+1];
1026 memset(pCount, 0, ((HANDLESYS_TYPEARRAY_SIZE + 1) * sizeof(unsigned int)));
1028 for (unsigned int i = 1; i <= m_HandleTail; ++i)
1030 const QHandle &Handle = m_Handles[i];
1031 if (Handle.set != HandleSet_Used || Handle.owner != pIdentity)
1036 ++pCount[Handle.type];
1039 if (Handle.type >= highest_index)
1041 highest_index = ((Handle.type) + 1);
1044 if (Handle.clone != 0)
1049 if (m_Types[Handle.type].dispatch->GetHandleApproxSize(Handle.type, Handle.object, &size))
1055 const char * pTypeName = NULL;
1057 for (unsigned int i = 0; i < highest_index; ++i)
1061 continue; /* We may have gaps, it's fine. */
1064 if (m_Types[i].nameIdx != -1)
1066 pTypeName = m_strtab->GetString(m_Types[i].nameIdx);
1073 HANDLE_LOG_VERY_BAD("Type\t%-20.20s|\tCount\t%u", pTypeName, pCount[i]);
1076 HANDLE_LOG_VERY_BAD("-- Approximately %d bytes of memory are in use by (%u) Handles.\n", total_size, total);
1079 highest_owner->GetBaseContext()->ThrowNativeErrorEx(SP_ERROR_MEMACCESS, "Memory leak");
1081 return scripts->UnloadPlugin(highest_owner);
1084 void HandleSystem::Dump(HANDLE_REPORTER rep)
1086 unsigned int total_size = 0;
1087 rep("%-10.10s\t%-20.20s\t%-20.20s\t%-10.10s", "Handle", "Owner", "Type", "Memory");
1088 rep("--------------------------------------------------------------------------");
1089 for (unsigned int i = 1; i <= m_HandleTail; i++)
1091 if (m_Handles[i].set != HandleSet_Used)
1096 unsigned int index = (m_Handles[i].serial << 16) | i;
1097 /* Determine the owner */
1098 const char *owner = "UNKNOWN";
1099 if (m_Handles[i].owner)
1101 IdentityToken_t *pOwner = m_Handles[i].owner;
1102 if (pOwner == g_pCoreIdent)
1106 else if (pOwner == scripts->GetIdentity())
1108 owner = "PLUGINSYS";
1112 IExtension *ext = g_Extensions.GetExtensionFromIdent(pOwner);
1115 owner = ext->GetFilename();
1119 SMPlugin *pPlugin = scripts->FindPluginByIdentity(pOwner);
1122 owner = pPlugin->GetFilename();
1131 const char *type = "ANON";
1132 QHandleType *pType = &m_Types[m_Handles[i].type];
1133 unsigned int size = 0;
1134 unsigned int parentIdx;
1136 if (pType->nameIdx != -1)
1138 type = m_strtab->GetString(pType->nameIdx);
1141 if ((parentIdx = m_Handles[i].clone) != 0)
1143 if (m_Handles[parentIdx].refcount > 0)
1150 bresult = pType->dispatch->GetHandleApproxSize(m_Handles[parentIdx].type, m_Handles[parentIdx].object, &size);
1155 bresult = pType->dispatch->GetHandleApproxSize(m_Handles[i].type, m_Handles[i].object, &size);
1158 if (pType->dispatch->GetDispatchVersion() < HANDLESYS_MEMUSAGE_MIN_VERSION
1161 rep("0x%08x\t%-20.20s\t%-20.20s\t%-10.10s", index, owner, type, "-1");
1166 smcore.Format(buffer, sizeof(buffer), "%d", size);
1167 rep("0x%08x\t%-20.20s\t%-20.20s\t%-10.10s", index, owner, type, buffer);
1171 rep("-- Approximately %d bytes of memory are in use by Handles.\n", total_size);