Click here to Skip to main content
13,558,426 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 28 May 2012
Licenced CPOL

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

, 28 May 2012
Rate this:
Please Sign up or sign in to vote.
embedded ref-counted resource management


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


Maximum efficiency and minimal resource consumption by the reference counter directly embedded in the object heap

The Solution

Memory distribution for single object


blank area to ensure ref-counter alignment


Memory distribution for array objects




blank area to ensure ref-counter alignment


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;
       return size+align-remain;

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

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;

Test code
Ref-counted management without explicit free or delete

#include "stdafx.h" 
struct A 
    virtual ~A()
struct B: A
    virtual ~B() 
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.  


Created by Wei, Bing in 2006


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


About the Author

China China
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Aescleal30-May-12 8:47
memberAescleal30-May-12 8:47 
GeneralRe: My vote of 3 Pin
goecerfun31-May-12 19:20
membergoecerfun31-May-12 19:20 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 28 May 2012
Article Copyright 2012 by goecerfun
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid