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);
65 HandleSystem::~HandleSystem()
72 HandleType_t HandleSystem::CreateType(const char *name,
73 IHandleTypeDispatch *dispatch,
75 const TypeAccess *typeAccess,
76 const HandleAccess *hndlAccess,
77 IdentityToken_t *ident,
84 *err = HandleError_Parameter;
89 if (typeAccess && typeAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
93 *err = HandleError_Version;
98 if (hndlAccess && hndlAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
102 *err = HandleError_Version;
107 bool isChild = false;
112 if (parent & HANDLESYS_SUBTYPE_MASK)
116 *err = HandleError_NoInherit;
120 if (parent >= HANDLESYS_TYPEARRAY_SIZE
121 || m_Types[parent].dispatch == NULL)
125 *err = HandleError_Parameter;
129 if (m_Types[parent].typeSec.access[HTypeAccess_Inherit] == false
130 && (m_Types[parent].typeSec.ident != ident))
134 *err = HandleError_Access;
140 if (name && name[0] != '\0')
142 if (m_TypeLookup.contains(name))
145 *err = HandleError_Parameter;
154 QHandleType *pParent = &m_Types[parent];
155 if (pParent->children >= HANDLESYS_MAX_SUBTYPES)
159 *err = HandleError_Limit;
164 for (unsigned int i=1; i<=HANDLESYS_MAX_SUBTYPES; i++)
166 if (m_Types[parent + i].dispatch == NULL)
176 *err = HandleError_Limit;
182 if (m_FreeTypes == 0)
184 /* Reserve another index */
185 if (m_TypeTail >= HANDLESYS_TYPEARRAY_SIZE)
189 *err = HandleError_Limit;
193 m_TypeTail += (HANDLESYS_MAX_SUBTYPES + 1);
197 /* The "free array" is compacted into the normal array for easiness */
198 index = m_Types[m_FreeTypes--].freeID;
202 QHandleType *pType = &m_Types[index];
204 pType->dispatch = dispatch;
205 if (name && name[0] != '\0')
207 pType->name = new ke::AString(name);
208 m_TypeLookup.insert(name, pType);
215 pType->typeSec = *typeAccess;
217 InitAccessDefaults(&pType->typeSec, NULL);
218 pType->typeSec.ident = ident;
223 pType->hndlSec = *hndlAccess;
225 InitAccessDefaults(NULL, &pType->hndlSec);
236 bool HandleSystem::FindHandleType(const char *name, HandleType_t *aResult)
239 if (!m_TypeLookup.retrieve(name, &type))
243 *aResult = type - m_Types;
248 HandleError HandleSystem::TryAllocHandle(unsigned int *handle)
250 if (m_FreeHandles == 0)
252 if (m_HandleTail >= HANDLESYS_MAX_HANDLES)
254 return HandleError_Limit;;
256 *handle = ++m_HandleTail;
260 *handle = m_Handles[m_FreeHandles--].freeID;
263 return HandleError_None;
266 HandleError HandleSystem::MakePrimHandle(HandleType_t type,
267 QHandle **in_pHandle,
268 unsigned int *in_index,
270 IdentityToken_t *owner,
274 unsigned int owner_index = 0;
276 if (owner && (IdentityHandle(owner, &owner_index) != HandleError_None))
278 return HandleError_Identity;
282 if ((err = TryAllocHandle(&handle)) != HandleError_None)
284 if (!TryAndFreeSomeHandles()
285 || (err = TryAllocHandle(&handle)) != HandleError_None)
291 QHandle *pHandle = &m_Handles[handle];
293 assert(pHandle->set == false);
295 if (++m_HSerial >= HANDLESYS_MAX_SERIALS)
300 /* Set essential information */
301 pHandle->set = identity ? HandleSet_Identity : HandleSet_Used;
302 pHandle->refcount = 1;
303 pHandle->type = type;
304 pHandle->serial = m_HSerial;
305 pHandle->owner = owner;
306 pHandle->ch_next = 0;
307 pHandle->access_special = false;
308 pHandle->is_destroying = false;
310 /* Create the hash value */
311 Handle_t hash = pHandle->serial;
315 /* Add a reference count to the type */
316 m_Types[type].opened++;
319 *in_pHandle = pHandle;
323 /* Decode the identity token
324 * For now, we don't allow nested ownership
326 if (owner && !identity)
328 QHandle *pIdentity = &m_Handles[owner_index];
329 if (pIdentity->ch_prev == 0)
331 pIdentity->ch_prev = handle;
332 pIdentity->ch_next = handle;
333 pHandle->ch_prev = 0;
337 /* Link previous node to us (forward) */
338 m_Handles[pIdentity->ch_next].ch_next = handle;
339 /* Link us to previous node (backwards) */
340 pHandle->ch_prev = pIdentity->ch_next;
342 pIdentity->ch_next = handle;
344 pIdentity->refcount++;
348 pHandle->ch_prev = 0;
351 return HandleError_None;
354 void HandleSystem::SetTypeSecurityOwner(HandleType_t type, IdentityToken_t *pToken)
357 || type >= HANDLESYS_TYPEARRAY_SIZE
358 || m_Types[type].dispatch == NULL)
363 m_Types[type].typeSec.ident = pToken;
366 Handle_t HandleSystem::CreateHandleInt(HandleType_t type,
368 const HandleSecurity *pSec,
370 const HandleAccess *pAccess,
373 IdentityToken_t *ident;
374 IdentityToken_t *owner;
378 ident = pSec->pIdentity;
379 owner = pSec->pOwner;
386 || type >= HANDLESYS_TYPEARRAY_SIZE
387 || m_Types[type].dispatch == NULL)
391 *err = HandleError_Parameter;
396 /* Check to see if we're allowed to create this handle type */
397 QHandleType *pType = &m_Types[type];
398 if (!pType->typeSec.access[HTypeAccess_Create]
399 && (!pType->typeSec.ident
400 || pType->typeSec.ident != ident))
404 *err = HandleError_Access;
414 if ((_err=MakePrimHandle(type, &pHandle, &index, &handle, owner, identity)) != HandleError_None)
425 pHandle->access_special = true;
426 pHandle->sec = *pAccess;
429 pHandle->object = object;
435 Handle_t HandleSystem::CreateHandleEx(HandleType_t type, void *object, const HandleSecurity *pSec, const HandleAccess *pAccess, HandleError *err)
437 return CreateHandleInt(type, object, pSec, err, pAccess, false);
440 Handle_t HandleSystem::CreateHandle(HandleType_t type, void *object, IdentityToken_t *owner, IdentityToken_t *ident, HandleError *err)
444 sec.pIdentity = ident;
447 return CreateHandleEx(type, object, &sec, NULL, err);
450 bool HandleSystem::TypeCheck(HandleType_t intype, HandleType_t outtype)
452 /* Check the type inheritance */
453 if (intype & HANDLESYS_SUBTYPE_MASK)
455 if (intype != outtype
456 && (TypeParent(intype) != TypeParent(outtype)))
461 if (intype != outtype)
470 HandleError HandleSystem::GetHandle(Handle_t handle,
471 IdentityToken_t *ident,
472 QHandle **in_pHandle,
473 unsigned int *in_index,
476 unsigned int serial = (handle >> 16);
477 unsigned int index = (handle & HANDLESYS_HANDLE_MASK);
479 if (index == 0 || index > m_HandleTail || index > HANDLESYS_MAX_HANDLES)
481 return HandleError_Index;
484 QHandle *pHandle = &m_Handles[index];
487 || (pHandle->set == HandleSet_Freed && !ignoreFree))
489 return HandleError_Freed;
490 } else if (pHandle->set == HandleSet_Identity
491 && ident != g_ShareSys.GetIdentRoot())
493 /* Only IdentityHandle() can read this! */
494 return HandleError_Identity;
496 if (pHandle->serial != serial)
498 return HandleError_Changed;
501 *in_pHandle = pHandle;
504 return HandleError_None;
507 bool HandleSystem::CheckAccess(QHandle *pHandle, HandleAccessRight right, const HandleSecurity *pSecurity)
509 QHandleType *pType = &m_Types[pHandle->type];
512 if (pHandle->access_special)
514 access = pHandle->sec.access[right];
516 access = pType->hndlSec.access[right];
519 /* Check if the type's identity matches */
520 if (access & HANDLE_RESTRICT_IDENTITY)
522 IdentityToken_t *owner = pType->typeSec.ident;
524 || (!pSecurity || pSecurity->pIdentity != owner))
530 /* Check if the owner is allowed */
531 if (access & HANDLE_RESTRICT_OWNER)
533 IdentityToken_t *owner = pHandle->owner;
535 && (!pSecurity || pSecurity->pOwner != owner))
544 HandleError HandleSystem::CloneHandle(QHandle *pHandle, unsigned int index, Handle_t *newhandle, IdentityToken_t *newOwner)
546 /* Get a new Handle ID */
547 unsigned int new_index;
552 if ((err=MakePrimHandle(pHandle->type, &pNewHandle, &new_index, &new_handle, newOwner)) != HandleError_None)
557 /* Assign permissions from parent */
558 if (pHandle->access_special)
560 pNewHandle->access_special = true;
561 pNewHandle->sec = pHandle->sec;
564 pNewHandle->clone = index;
565 pNewHandle->object = NULL;
568 *newhandle = new_handle;
570 return HandleError_None;
573 HandleError HandleSystem::CloneHandle(Handle_t handle, Handle_t *newhandle, IdentityToken_t *newOwner, const HandleSecurity *pSecurity)
578 IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
580 if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
585 /* Identities cannot be cloned */
586 if (pHandle->set == HandleSet_Identity)
588 return HandleError_Identity;
591 /* Check if the handle can be cloned */
592 if (!CheckAccess(pHandle, HandleAccess_Clone, pSecurity))
594 return HandleError_Access;
597 /* Make sure we're not cloning a clone */
600 QHandle *pParent = &m_Handles[pHandle->clone];
601 return CloneHandle(pParent, pHandle->clone, newhandle, newOwner);
604 return CloneHandle(pHandle, index, newhandle, newOwner);
607 Handle_t HandleSystem::FastCloneHandle(QHandle *pHandle, unsigned int index)
610 return FastCloneHandle(&m_Handles[pHandle->clone], pHandle->clone);
613 if (CloneHandle(pHandle, index, &hndl, g_pCoreIdent) != HandleError_None)
619 Handle_t HandleSystem::FastCloneHandle(Handle_t hndl)
624 GetHandleUnchecked(hndl, pHandle, index);
626 return FastCloneHandle(pHandle, index);
629 void HandleSystem::GetHandleUnchecked(Handle_t hndl, QHandle *& pHandle, unsigned int &index)
632 unsigned int serial = (hndl >> 16);
634 index = (hndl & HANDLESYS_HANDLE_MASK);
636 assert(index != 0 && index <= m_HandleTail && index < HANDLESYS_MAX_HANDLES);
638 pHandle = &m_Handles[index];
640 assert(pHandle->set && pHandle->set != HandleSet_Freed);
641 assert(pHandle->serial == serial);
644 HandleError HandleSystem::FreeHandle(QHandle *pHandle, unsigned int index)
646 if (pHandle->is_destroying)
648 /* Someone tried to free this recursively.
649 * We'll just ignore this safely.
651 return HandleError_None;
654 QHandleType *pType = &m_Types[pHandle->type];
658 /* If we're a clone, decrease the parent reference count */
662 /* Note that if we ever have per-handle security, we would need to re-check
663 * the access on this Handle. */
664 master = pHandle->clone;
665 pMaster = &m_Handles[master];
667 /* Release the clone now */
668 pHandle->is_destroying = true;
669 ReleasePrimHandle(index);
671 /* Decrement the master's reference count */
672 if (--pMaster->refcount == 0)
674 /* Type should be the same but do this anyway... */
675 pType = &m_Types[pMaster->type];
676 pMaster->is_destroying = true;
679 pType->dispatch->OnHandleDestroy(pMaster->type, pMaster->object);
681 ReleasePrimHandle(master);
683 } else if (pHandle->set == HandleSet_Identity) {
684 /* If we're an identity, skip all this stuff!
685 * NOTE: SHARESYS DOES NOT CARE ABOUT THE DESTRUCTOR
687 pHandle->is_destroying = true;
688 ReleasePrimHandle(index);
690 /* Decrement, free if necessary */
691 if (--pHandle->refcount == 0)
693 pHandle->is_destroying = true;
696 pType->dispatch->OnHandleDestroy(pHandle->type, pHandle->object);
698 ReleasePrimHandle(index);
700 /* We must be cloned, so mark ourselves as freed */
701 pHandle->set = HandleSet_Freed;
702 /* Now, unlink us, so we're not being tracked by the owner */
705 UnlinkHandleFromOwner(pHandle, index);
710 return HandleError_None;
713 HandleError HandleSystem::FreeHandle(Handle_t handle, const HandleSecurity *pSecurity)
718 IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
720 if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
725 if (!CheckAccess(pHandle, HandleAccess_Delete, pSecurity))
727 return HandleError_Access;
730 return FreeHandle(pHandle, index);
733 HandleError HandleSystem::ReadHandle(Handle_t handle, HandleType_t type, const HandleSecurity *pSecurity, void **object)
738 IdentityToken_t *ident = pSecurity ? pSecurity->pIdentity : NULL;
740 if ((err=GetHandle(handle, ident, &pHandle, &index)) != HandleError_None)
745 if (!CheckAccess(pHandle, HandleAccess_Read, pSecurity))
747 return HandleError_Access;
750 /* Check the type inheritance */
751 if (pHandle->type & HANDLESYS_SUBTYPE_MASK)
753 if (pHandle->type != type
754 && (TypeParent(pHandle->type) != TypeParent(type)))
756 return HandleError_Type;
759 if (pHandle->type != type)
761 return HandleError_Type;
767 /* if we're a clone, the rules change - object is ONLY in our reference */
770 pHandle = &m_Handles[pHandle->clone];
772 *object = pHandle->object;
775 return HandleError_None;
778 void HandleSystem::UnlinkHandleFromOwner(QHandle *pHandle, unsigned int index)
780 /* Unlink us if necessary */
781 unsigned int ident_index;
782 if (IdentityHandle(pHandle->owner, &ident_index) != HandleError_None)
785 assert(pHandle->owner == 0);
789 pHandle->owner = NULL;
791 /* Note that since 0 is an invalid handle, if any of these links are 0,
792 * the data can still be set.
794 QHandle *pIdentity = &m_Handles[ident_index];
796 /* Unlink case: We're the head AND tail node */
797 if (index == pIdentity->ch_prev && index == pIdentity->ch_next)
799 pIdentity->ch_prev = 0;
800 pIdentity->ch_next = 0;
802 /* Unlink case: We're the head node */
803 else if (index == pIdentity->ch_prev) {
804 /* Link us to the next in the chain */
805 pIdentity->ch_prev = pHandle->ch_next;
806 /* Patch up the previous link */
807 m_Handles[pHandle->ch_next].ch_prev = 0;
809 /* Unlink case: We're the tail node */
810 else if (index == pIdentity->ch_next) {
811 /* Link us to the previous in the chain */
812 pIdentity->ch_next = pHandle->ch_prev;
813 /* Patch up the next link */
814 m_Handles[pHandle->ch_prev].ch_next = 0;
816 /* Unlink case: We're in the middle! */
818 /* Patch the forward reference */
819 m_Handles[pHandle->ch_next].ch_prev = pHandle->ch_prev;
820 /* Patch the backward reference */
821 m_Handles[pHandle->ch_prev].ch_next = pHandle->ch_next;
824 /* Lastly, decrease the reference count */
825 pIdentity->refcount--;
828 void HandleSystem::ReleasePrimHandle(unsigned int index)
830 QHandle *pHandle = &m_Handles[index];
831 HandleSet set = pHandle->set;
833 if (pHandle->owner && (set != HandleSet_Identity))
835 UnlinkHandleFromOwner(pHandle, index);
838 /* Were we an identity ourself? */
840 if (set == HandleSet_Identity)
842 /* Extra work to do. We need to find everything connected to this identity and release it. */
843 unsigned int ch_index;
845 unsigned int old_index = 0;
847 while ((ch_index = pHandle->ch_next) != 0)
849 pLocal = &m_Handles[ch_index];
851 assert(old_index != ch_index);
852 assert(pLocal->set == HandleSet_Used);
853 old_index = ch_index;
855 FreeHandle(pLocal, ch_index);
859 pHandle->set = HandleSet_None;
860 m_Types[pHandle->type].opened--;
861 m_Handles[++m_FreeHandles].freeID = index;
864 bool HandleSystem::RemoveType(HandleType_t type, IdentityToken_t *ident)
866 if (type == 0 || type >= HANDLESYS_TYPEARRAY_SIZE)
871 QHandleType *pType = &m_Types[type];
873 if (pType->typeSec.ident
874 && pType->typeSec.ident != ident)
879 if (pType->dispatch == NULL)
884 /* Remove children if we have to */
885 if (!(type & HANDLESYS_SUBTYPE_MASK))
887 QHandleType *childType;
888 for (unsigned int i=1; i<=HANDLESYS_MAX_SUBTYPES; i++)
890 childType = &m_Types[type + i];
891 if (childType->dispatch)
893 RemoveType(type + i, childType->typeSec.ident);
896 /* Link us into the free chain */
897 m_Types[++m_FreeTypes].freeID = type;
900 /* Make sure nothing is using this type. */
904 for (unsigned int i=1; i<=m_HandleTail; i++)
906 pHandle = &m_Handles[i];
907 if (!pHandle->set || pHandle->type != type)
912 FreeHandle(pHandle, i);
914 if (pType->opened == 0)
921 /* Invalidate the type now */
922 pType->dispatch = NULL;
924 /* Remove it from the type cache. */
926 m_TypeLookup.remove(pType->name->chars());
931 bool HandleSystem::InitAccessDefaults(TypeAccess *pTypeAccess, HandleAccess *pHandleAccess)
935 if (pTypeAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
939 pTypeAccess->access[HTypeAccess_Create] = false;
940 pTypeAccess->access[HTypeAccess_Inherit] = false;
941 pTypeAccess->ident = NULL;
946 if (pHandleAccess->hsVersion > SMINTERFACE_HANDLESYSTEM_VERSION)
951 pHandleAccess->access[HandleAccess_Clone] = 0;
952 pHandleAccess->access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER;
953 pHandleAccess->access[HandleAccess_Read] = HANDLE_RESTRICT_IDENTITY;
959 #define HANDLE_LOG_VERY_BAD(message, ...) \
960 smcore.LogFatal(message, ##__VA_ARGS__); \
961 smcore.LogError(message, ##__VA_ARGS__);
963 bool HandleSystem::TryAndFreeSomeHandles()
965 IPlugin *highest_owner = NULL;
966 unsigned int highest_handle_count = 0;
968 /* Search all plugins */
969 for (IPluginIterator *pl_iter = g_PluginSys.GetPluginIterator(); pl_iter->MorePlugins(); pl_iter->NextPlugin())
971 IPlugin *plugin = pl_iter->GetPlugin();
972 IdentityToken_t *identity = plugin->GetIdentity();
973 unsigned int handle_count = 0;
975 if (identity == NULL)
980 /* Search all handles */
981 for (unsigned int i = 1; i <= m_HandleTail; i++)
983 if (m_Handles[i].set != HandleSet_Used)
987 if (m_Handles[i].owner == identity)
993 if (handle_count > highest_handle_count)
995 highest_owner = plugin;
996 highest_handle_count = handle_count;
1000 if (highest_owner == NULL || highest_handle_count == 0)
1005 HANDLE_LOG_VERY_BAD("[SM] MEMORY LEAK DETECTED IN PLUGIN (file \"%s\")", highest_owner->GetFilename());
1006 HANDLE_LOG_VERY_BAD("[SM] Unloading plugin to free %d handles.", highest_handle_count);
1007 HANDLE_LOG_VERY_BAD("[SM] Contact the author(s) of this plugin to correct this error.", highest_handle_count);
1008 HANDLE_LOG_VERY_BAD("--------------------------------------------------------------------------");
1010 const IdentityToken_t *pIdentity = highest_owner->GetIdentity();
1011 unsigned int total = 0, highest_index = 0, total_size = 0, size;
1012 unsigned int * pCount = new unsigned int[HANDLESYS_TYPEARRAY_SIZE+1];
1013 memset(pCount, 0, ((HANDLESYS_TYPEARRAY_SIZE + 1) * sizeof(unsigned int)));
1015 for (unsigned int i = 1; i <= m_HandleTail; ++i)
1017 const QHandle &Handle = m_Handles[i];
1018 if (Handle.set != HandleSet_Used || Handle.owner != pIdentity)
1023 ++pCount[Handle.type];
1026 if (Handle.type >= highest_index)
1028 highest_index = ((Handle.type) + 1);
1031 if (Handle.clone != 0)
1036 if (m_Types[Handle.type].dispatch->GetHandleApproxSize(Handle.type, Handle.object, &size))
1042 const char * pTypeName = NULL;
1044 for (unsigned int i = 0; i < highest_index; ++i)
1048 continue; /* We may have gaps, it's fine. */
1051 if (m_Types[i].name)
1052 pTypeName = m_Types[i].name->chars();
1056 HANDLE_LOG_VERY_BAD("Type\t%-20.20s|\tCount\t%u", pTypeName, pCount[i]);
1059 HANDLE_LOG_VERY_BAD("-- Approximately %d bytes of memory are in use by (%u) Handles.\n", total_size, total);
1062 highest_owner->GetBaseContext()->ThrowNativeErrorEx(SP_ERROR_MEMACCESS, "Memory leak");
1064 return scripts->UnloadPlugin(highest_owner);
1067 void HandleSystem::Dump(HANDLE_REPORTER rep)
1069 unsigned int total_size = 0;
1070 rep("%-10.10s\t%-20.20s\t%-20.20s\t%-10.10s", "Handle", "Owner", "Type", "Memory");
1071 rep("--------------------------------------------------------------------------");
1072 for (unsigned int i = 1; i <= m_HandleTail; i++)
1074 if (m_Handles[i].set != HandleSet_Used)
1079 unsigned int index = (m_Handles[i].serial << 16) | i;
1080 /* Determine the owner */
1081 const char *owner = "UNKNOWN";
1082 if (m_Handles[i].owner)
1084 IdentityToken_t *pOwner = m_Handles[i].owner;
1085 if (pOwner == g_pCoreIdent)
1089 else if (pOwner == scripts->GetIdentity())
1091 owner = "PLUGINSYS";
1095 IExtension *ext = g_Extensions.GetExtensionFromIdent(pOwner);
1098 owner = ext->GetFilename();
1102 SMPlugin *pPlugin = scripts->FindPluginByIdentity(pOwner);
1105 owner = pPlugin->GetFilename();
1114 const char *type = "ANON";
1115 QHandleType *pType = &m_Types[m_Handles[i].type];
1116 unsigned int size = 0;
1117 unsigned int parentIdx;
1120 type = pType->name->chars();
1122 if ((parentIdx = m_Handles[i].clone) != 0)
1124 if (m_Handles[parentIdx].refcount > 0)
1131 bresult = pType->dispatch->GetHandleApproxSize(m_Handles[parentIdx].type, m_Handles[parentIdx].object, &size);
1136 bresult = pType->dispatch->GetHandleApproxSize(m_Handles[i].type, m_Handles[i].object, &size);
1139 if (pType->dispatch->GetDispatchVersion() < HANDLESYS_MEMUSAGE_MIN_VERSION
1142 rep("0x%08x\t%-20.20s\t%-20.20s\t%-10.10s", index, owner, type, "-1");
1147 smcore.Format(buffer, sizeof(buffer), "%d", size);
1148 rep("0x%08x\t%-20.20s\t%-20.20s\t%-10.10s", index, owner, type, buffer);
1152 rep("-- Approximately %d bytes of memory are in use by Handles.\n", total_size);