Files CCore/inc/MemAllocGuard.h CCore/src/MemAllocGuard.cpp
MemAllocGuard is a Scope Lock Type for the memory allocation.
class MemAllocGuard : NoCopy
{
void *mem;
public:
explicit MemAllocGuard(ulen len); // allocate len bytes
MemAllocGuard(ulen count,ulen size_of,ulen extra=0); // allocate count*size_of+extra bytes,
// overflow check is performed
~MemAllocGuard();
operator void * () const { return mem; } // memory block
void * disarm() { return Replace_null(mem); } // take the ownership on the block
};
You can use this class to allocate and keep alive a memory block for the duration of a scope. MemAllocGuard destructor releases the block, unless the method disarm() is called.
MemAllocGuardOf is a template variant. You can customize the methods of allocating/deallocating the memory.
template <class Algo>
class MemAllocGuardOf : NoCopy
{
void *mem;
public:
explicit MemAllocGuardOf(ulen len);
MemAllocGuardOf(ulen count,ulen size_of,ulen extra=0);
~MemAllocGuardOf();
operator void * () const { return mem; }
void * disarm() { return Replace_null(mem); }
};
The Algorithm Package Algo must provide two functions:
struct Algo { static void * MemAlloc(ulen len); static void MemFree(void *mem); };