Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

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

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.  

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)

Share

About the Author

goecerfun

China China
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 [modified] PinmemberAescleal30-May-12 8:47 
GeneralRe: My vote of 3 Pinmembergoecerfun31-May-12 19:20 
Question[My vote of 2] Hard to understand PinmemberHamidYaseen29-May-12 23:29 
I am beginer to C++, and this article is hard to understand for me...

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 28 May 2012
Article Copyright 2012 by goecerfun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid