Structured Data 'New' and 'Delete'
Posted: Sat Mar 10, 2012 11:59 am
Hey all,
Find below some code that I use to create an allocator for structured data. It's a very simple concept, yet I've always found it useful, and with the latest revision being completed, I thought maybe someone here may also find it useful.
What does it do?
Very simply, say you want to have a more object-orientated approach to handling data, and wanted something a bit more than just AllocateMemory(SizeOf(...))... By using the macro 'mfwCreateAllocator(Structure)' it will generate code to create a constructor 'Structure_New()', and a destructor 'Structure_Delete(*Address)'... Not only that, you can optionally create procedures that will be called upon construction and destruction of the object.... Not only that!, you can also utilize LinkedLists to ensure all data allocated gets destroyed on application end by simply setting a constant.
The Code (Allocator.pbi)
Let me know what you think, and I hope someone else can find this useful.
Thanks!
EDIT: Changed the title of the thread to avoid confusion.
Find below some code that I use to create an allocator for structured data. It's a very simple concept, yet I've always found it useful, and with the latest revision being completed, I thought maybe someone here may also find it useful.
What does it do?
Very simply, say you want to have a more object-orientated approach to handling data, and wanted something a bit more than just AllocateMemory(SizeOf(...))... By using the macro 'mfwCreateAllocator(Structure)' it will generate code to create a constructor 'Structure_New()', and a destructor 'Structure_Delete(*Address)'... Not only that, you can optionally create procedures that will be called upon construction and destruction of the object.... Not only that!, you can also utilize LinkedLists to ensure all data allocated gets destroyed on application end by simply setting a constant.
The Code (Allocator.pbi)
Code: Select all
;------------------------------------------------------------------------------------------
; Title: Allocator Library
; Description: Macro to generate memory allocators for structured data object.
; Author(s): Michael R. King (mrking2910@gmail.com)
; Revision: 10 March 2012
; Notes: Part of the Mastiff Framework
;------------------------------------------------------------------------------------------
; - Allocation Mode -
CompilerIf Defined(MFW_ALLOC_USELIST, #PB_Constant) = #False
#MFW_ALLOC_USELIST = #True ; Set as True to enable allocations to be done using a LinkedList. (Prevent Memory Leaks)
CompilerEndIf
; - Allocator Macro -
Macro mfwCreateAllocator(Struct)
CompilerIf #MFW_ALLOC_USELIST = #True
Global NewList __mfwAllocator_#Struct.Struct()
CompilerEndIf
Procedure.i Struct#_New()
Protected *obj.Struct
CompilerIf #MFW_ALLOC_USELIST = #True
*obj = AddElement(__mfwAllocator_#Struct())
CompilerElse
*obj = AllocateMemory(SizeOf(Struct))
InitializeStructure(*obj, Struct)
CompilerEndIf
CompilerIf Defined(Struct#_Constructor, #PB_Procedure)
Struct#_Constructor(*obj)
CompilerEndIf
ProcedureReturn *obj
EndProcedure
Procedure.a Struct#_Delete(*obj)
If *obj
CompilerIf Defined(Struct#_Destructor, #PB_Procedure)
Struct#_Destructor(*obj)
CompilerEndIf
CompilerIf #MFW_ALLOC_USELIST = #True
ForEach __mfwAllocator_#Struct()
If @__mfwAllocator_#Struct() = *obj
DeleteElement(__mfwAllocator_#Struct())
ProcedureReturn #True
EndIf
Next
CompilerElse
FreeMemory(*obj)
ProcedureReturn #True
CompilerEndIf
EndIf
ProcedureReturn #False
EndProcedure
EndMacro
; ----------------
; - Example Code -
; ----------------
; - Structure Definition -
Structure MyData
SomeData.l
EndStructure
; - Constructor (Will be called upon allocating the object) -
Procedure MyData_Constructor(*this.MyData)
Debug "Object has been allocated."
EndProcedure
; - Destructor (Will be called upon destroying the object) -
Procedure MyData_Destructor(*this.MyData)
Debug "Object has been destroyed."
Debug "SomeData = " + Str(*this\SomeData)
EndProcedure
; - Create Allocator -
mfwCreateAllocator(MyData)
; - Create Object -
Define.MyData *Object = MyData_New()
; - Assign Data -
*Object\SomeData = 1234
; - Destroy Object -
MyData_Delete(*Object)
Thanks!
EDIT: Changed the title of the thread to avoid confusion.