Skip to content

Instantly share code, notes, and snippets.

@od0x0
Created June 27, 2010 22:01
Show Gist options
  • Save od0x0/455203 to your computer and use it in GitHub Desktop.
Save od0x0/455203 to your computer and use it in GitHub Desktop.
Allocator: cover{
mallocPtr: Func(SizeT)->Pointer
callocPtr: Func(Int,SizeT)->Pointer
reallocPtr: Func(Pointer,SizeT)->Pointer
freePtr: Func(Pointer)
new: static func()->This{
allocator: This
allocator mallocPtr=null
allocator callocPtr=null
allocator freePtr=null
allocator reallocPtr=null
return allocator
}
//Returns the std-c allocator (malloc/free)
cAllocator: static func->This{
allocator: This
allocator mallocPtr=malloc as Pointer
allocator callocPtr=calloc as Pointer
allocator freePtr=free as Pointer
allocator reallocPtr=realloc as Pointer
return allocator
}
gcAllocator: static func->This{
allocator: This
allocator mallocPtr=gc_malloc as Pointer
allocator callocPtr=gc_calloc as Pointer
allocator freePtr=gc_free as Pointer
allocator reallocPtr=gc_realloc as Pointer
return allocator
}
new: static func ~withReallocatorAndFree(.reallocPtr, .freePtr)->This{
allocator: This
allocator mallocPtr=null
allocator callocPtr=null
allocator freePtr= freePtr
allocator reallocPtr= reallocPtr
return allocator
}
isSane: func->Bool{
//Bug with rock
callocPtr:=this callocPtr
mallocPtr:=this mallocPtr
reallocPtr:=this reallocPtr
freePtr:=this freePtr
return mallocPtr || callocPtr || freePtr || reallocPtr
}
sanityCheck: func@{
//This is incase the user has a null allocator (i.e. backwards compatibility to code that is unaware of the Allocator system, and thus the allocator is set to all null because it was merely included into a class by inheritance)
//Grr, going to need to explain this one :x
if(isSane()==false) this=This gcAllocator()
}
//These wrap the pointers and also implement behavior if a pointer is absent
//So, if you are only given a malloc, it will derive calloc and realloc for you, and so forth
allocate: func(size:SizeT)->Pointer{
sanityCheck()
//Bug with rock
callocPtr:=this callocPtr
mallocPtr:=this mallocPtr
reallocPtr:=this reallocPtr
if(mallocPtr==null){
if(callocPtr) return callocPtr(1,size)
if(reallocPtr) return reallocPtr(null,size)
//We're screwed
return null
}
return mallocPtr(size)
}
allocateZeroed: func(count:Int,size:SizeT)->Pointer{
sanityCheck()
//Bug with rock
callocPtr:=this callocPtr
if(callocPtr==null){
pointer:=allocate(count*size)
memset(pointer,0,count*size)
return pointer
}
return callocPtr(count,size)
}
reallocate: func(old:Pointer,size:SizeT)->Pointer{
sanityCheck()
//Bug with rock
reallocPtr:=this reallocPtr
if(reallocPtr==null){
newPointer:=allocate(size)
if(newPointer==null) return null
if(old==null) return newPointer
//bad access if you reallocate smaller than the original size
//Unfortunately, there is no way to portably get the size of the old allocated block of memory
memcpy(newPointer,old,size)
return newPointer
}
return reallocPtr(old,size)
}
free: func(old:Pointer){
//Bug with rock
freePtr:=this freePtr
if(freePtr==null) return//We don't always have to have a free-er
return freePtr(old)
}
/*cloneClass: func<T>(aClass:T)->T{//Could never get this working :x
newClass:T
newClass=allocate(T size)
memcpy(newClass, aClass,T size)
newClass as Class setAllocator(this)
newClass as Class super=aClass
//newClass as Class __retain__()
return newClass
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment