Is it a good idea to warp the new and delete with a macro?

In many famous libraries and frameworks, they encapsulted their memory alloctor and dealloctor, and replace the original c++ new and delete operator with macros. As I am a game developer, I find the Ogre use OGRE_NEW and OGRE_DELETE, and the gamebryo use EE_NEW and EE_DELETE etc.
When I try to begin with my own framwork, I want to do the same thing, because use macro you can log the location of the memeory in codes, and will not conflict with some other libraries which replace the globe new and delete such as MFC.
But soon I met more and more problem. When I use it with boost, it become more syntex burden for write codes. For example if I want to use it with boost.shared_ptr, I need build my own allocator and deletor, at last I put all the detail into a template function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    template<typename T>
    class MemoryDeleter
    {
    public:
        void operator()(T* p)
        {
            MY_DELETE(p);
        }
    };

    template<typename T>
    class MemoryArrayDeleter
    {
    public:
        void operator()(T* p)
        {
            MY_DELETE_ARRAY(p);
        }
    };

    template<typename T>
    inline shared_ptr<T> make_shared_array(int count)
    {
        return shared_ptr<T>(MY_NEW T[count], MemoryArrayDeleter<T>(), MYSTLAllocator<shared_ptr<T> >());
    }

    template<typename T>
    inline shared_ptr<T> make_shared_ptr()
    {
        return shared_ptr<T>(MY_NEW T, MemoryDeleter<T>(), MYSTLAllocator<shared_ptr<T> >());
    }

    template<typename T, typename P1>
    inline shared_ptr<T> make_shared_ptr(P1 p1)
    {
        return shared_ptr<T>(MY_NEW T(p1), MemoryDeleter<T>(), STLAllocator<shared_ptr<T> >());
    }


The problem is I am not use c++0x, so there is no variadic templates to deal with multi-params, I need write them all, and there is no right value reference so can not implement perfect forward.
Then I choose this solution:
1
2
3
4
5
    template<typename T>
    inline shared_ptr<T> make_shared_ptr(T* p)
    {
        return shared_ptr<T>(p, MemoryDeleter<T>(), STLAllocator<shared_ptr<T> >());
    }

The origin idea is to encapsulate the memory allocator and deallocator into one place, but this divide them. The creating of the object is come into user's codes. If they place a point whitch is not created using MY_NEW, there must be a crush or more evil.

And there still some circumstances I have totally no idea how to deal with it. For I replace all the STL container allocator into my type(For I am not using c++0x, there is not template typedef too, so I use inherit). But when I use boost.asio and boost.filesystem, they use std::string directly both the internal implement and the interface params. Yes I can translate MyString into std::string but thay mean heap allocator and string copy...

There last thing is boost.function. The boost function will use heap allocator when the functor is big enough. But I can not find a way to replace it allocator.

So after I beared all of this for a time, I want to ask is it a good way to do so, should I just replace new and delete operator in class level and use new and delete directly(which is neither work with native type, nor for the STL container and other class whitch not under my control), or to replace the globle new and delete directly?

How you think about this, Thank you!
Topic archived. No new replies allowed.