/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "utilities/HashTable.h" #include "utilities/ReferenceCount.h" #include "stemobject/StemObject.h" #ifdef DEBUG #include #endif struct ReferenceCountEntry { ReferenceType type; unsigned int referenceCount; }; static HashTable * zeroCallbackTable; static HashTable * referenceTable; void registerReferenceType(ReferenceType type, referenceCountZeroCallback zeroCallback) { if (zeroCallbackTable == NULL) { zeroCallbackTable = HashTable_create(sizeof(referenceCountZeroCallback)); } HashTable_set(zeroCallbackTable, HashTable_uint32Key(type), &zeroCallback); } void incrementReferenceCount(void * object, ReferenceType type) { if (referenceTable == NULL) { referenceTable = HashTable_create(sizeof(struct ReferenceCountEntry)); } struct ReferenceCountEntry * entry = HashTable_get(referenceTable, HashTable_pointerKey(object)); if (entry == NULL) { struct ReferenceCountEntry newEntry = {type, 1}; HashTable_set(referenceTable, HashTable_pointerKey(object), &newEntry); } else { #ifdef DEBUG if (entry->type != type) { fprintf(stderr, "Warning: incrementReferenceCount called on object %p, which had previously been incremented with type %u, using type %u\n", object, entry->type, type); } #endif entry->referenceCount += 1; } } void decrementReferenceCount(void * object) { struct ReferenceCountEntry * entry = NULL; if (referenceTable != NULL) { entry = HashTable_get(referenceTable, HashTable_pointerKey(object)); } if (entry != NULL) { entry->referenceCount -= 1; if (entry->referenceCount == 0) { referenceCountZeroCallback zeroCallback = NULL; if (zeroCallbackTable != NULL) { referenceCountZeroCallback * zeroCallbackEntry = HashTable_get(zeroCallbackTable, HashTable_uint32Key(entry->type)); if (zeroCallbackEntry != NULL) { zeroCallback = *zeroCallbackEntry; } } if (zeroCallback != NULL) { zeroCallback(object); #ifdef DEBUG } else { fprintf(stderr, "Warning: Dereferenced object %p of type %u has no zero callback registered; memory is leaking\n", object, entry->type); #endif } HashTable_delete(referenceTable, HashTable_pointerKey(object)); } #ifdef DEBUG } else { fprintf(stderr, "Warning: decrementReferenceCount underflow with object %p\n", object); #endif } } void zeroCallback_free(void * object) { free(object); } void zeroCallback_StemObject_dispose(void * objectUntyped) { StemObject * object = objectUntyped; call_virtual(dispose, object); }