Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / C++
Article

Ideas from a smart pointer. Support data-alignment (part 1)

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
28 May 2012CPOL1 min read 16.5K   96   3   3
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 heap

The 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

C++
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

C++
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

C++
#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.  

History

Created by Wei, Bing in 2006

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Aescleal30-May-12 8:47
Aescleal30-May-12 8:47 
GeneralRe: My vote of 3 Pin
goecerfun31-May-12 19:20
goecerfun31-May-12 19:20 
Question[My vote of 2] Hard to understand Pin
HamidYaseen29-May-12 23:29
professionalHamidYaseen29-May-12 23:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.