简体   繁体   中英

Multiple arguments to C++ placement new 'constructor'

In order to do some custom memory tracking (leak prevention, corruption detection), I'm having to use placement new to create C++ objects, which works fine - but I'm struggling to figure out how I can pass arguments to the constructor, since it's called from a macro (so the file + line can be provided automatically).

The function:

template <typename T>
T*
cpp_new(
    const char *file,
    size_t line
)
{
    T   *n = (T*)tracked_allocate(&memory_context, sizeof(T), file, line);

    if ( n )
    {
        construct(n);
    }
    else
    {
        throw std::bad_alloc();
    }

    return n;
}

This is called via the macro:

#define new_object(type)    cpp_new<type>(__FILE__, __LINE__)

Placemented new:

template <typename T>
void
construct(
    T *obj
)
{
    obj = new (obj) T;
}

The va_list macros would cover the expansion for the variable number of arguments, only I don't want to supply the number of arguments the constructor has, removing va_arg(), and can't use va_start(), since it expects a format.

This went a little over my head: http://www.drdobbs.com/cpp/calling-constructors-with-placement-new/232901023?pgno=2

Is there any way I can use __VA_ARGS__ from new_object and pass them into the construct function? Each object only has one constructor, but there are many different types of objects taking different parameters, so I want to remove as much manual maintenance as possible.

Or is there just a better way in general of doing what I'm attempting!

You should not deal with the construct-if-allocation-succeeded. That's the job of a new -expression. It does that correctly, as a transaction-like operation: either all succeeds, or cleanup is performed before the exception is propagated.

So, relieved of that responsibility, the job of your macro is to do that which only macros can do, namely picking up the filename and line.

Those items can/should be passed to allocator function, which then technically is a "placement new", although here it will not construct in-place : it's a placement new merely because it has extra, user-defined arguments, like this:

bool hopefully( bool const c ) { return c; }

template< class X >
bool throw_( X const& x ) { throw x; }

void* operator new( size_t const size, char const* const filename, int const linenum )
{
    void* const p = tracked_allocate( &memory_context, size, filename, linenum );
    hopefully( p != 0 )
        || throw_( std::bad_alloc() )
    return p;
}

You need to define a corresponding placement deallocation function, or else a new expression will fail to deallocate when a constructor throws:

void operator delete( void* const p )
{
    // Your custom deallocation.
}

void operator delete( void* const p, char const*, int )
{
    ::operator delete( p );
}

Now your macro just needs to provide the relevant placement arguments, like this:

#define SOURCE_LINE_INFO __FILE__, __LINE__

Then you can just say, like,

new (SOURCE_LINE_INFO) MyType( arg1, arg2, arg3 )

For a more reusable solution consider defining a struct to hold the filename and line number. Then the macro reduces to constructing an instance of that type, and can be used more generally. In particular, it can then be used for logging calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM