Ideas from a smart pointer. Support data-alignment (part 1)
embedded ref-counted resource management
Introduction
A different and easy way to achieve the functions like the boost::shared_ptr. It supports data-alignment and other custom actions beyond boost::shared_ptr
Ref-counted management and custom alignment support are introduced in this section
Background
Maximum efficiency and minimal resource consumption by the reference counter directly embedded in the object heapThe Solution
Memory distribution for single object
object |
blank area to ensure ref-counter alignment |
ref-counter |
Memory distribution for array objects
object1 |
object2 |
object3... |
blank area to ensure ref-counter alignment |
ref-counter |
We directly use the overloaded new operator to embed the reference counter in the object heap during allocation. Reference counter and object(s) share the same heap, which will greatly enhance efficiency and reduce resource consumption. Think about boost::make_share
Some key code
inline size_t GetTypeCombineOffset(size_t size, size_t align)
{
size_t remain = size%align;
if(0 == remain)
return size;
else
return size+align-remain;
}
Parameters
size: the size of memory in bytes to be allocated which doesn't consider the reference counter space.
align: the alignment of the reference counter in bytes
Return value
The ref-counter's address offset relative to the heap start point
Remark
The operator new will call this function to determine the allocated heap size and the location of the embedded reference counter
inline void* operator new(size_t size, long*& ref, void* (*allocator)(size_t) = operator new, void (*cleaner)(void*)=operator delete)
{
size_t refOffset = GetTypeCombineOffset(size, __alignof(long));
char* head = reinterpret_cast<char*>(allocator(refOffset+sizeof(long)));
ref = reinterpret_cast<long*>(head+refOffset);
*ref = 1;
return head;
}
inline void* operator new(size_t size, long*& ref, size_t alignement, void* (*allocator)(size_t, size_t) = _aligned_malloc, void (*cleaner)(void*)=_aligned_free)
{
size_t refOffset = GetTypeCombineOffset(size, __alignof(long));
char* head = reinterpret_cast<char*>(allocator(refOffset+sizeof(long), alignement));
ref = reinterpret_cast<long*>(head+refOffset);
*ref = 1;
return head;
}</long*>
Test code
Ref-counted management without explicit free or delete
#include "stdafx.h"
struct A
{
A()
{
printf("A()\n");
}
virtual ~A()
{
printf("~A()\n");
}
};
struct B: A
{
B()
{
printf("B()\n");
}
virtual ~B()
{
printf("~B()\n");
}
};
Single<A*>::Ref g_pA;
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); //auto detect memory leak
Single<A*>::Ref pB = SingleAlloc<B>(); //ref new B
g_pA = pB; //auto InterlockedIncrement(ref-counter)
Array<A*>::Ref pAs = ArrayAlloc<A>(4); //ref new A[4]
SingleAligned<A*>::Ref pA_aligned = SingleAllocAligned<A>(16); //16 bytes aligned
ArrayAligned<A*>::Ref pAs_aligned = ArrayAllocAligned<B>(5, 16); //16 bytes aligned array(5 elements)
return 0;
}
Points of Interest
For prototype with no destructor, such as char, short, int, float..., we can further optimize to make sizeof (smart pointer) == sizeof (raw pointer)We will do the corresponding test in the part 2.