0

I found this construction reading someone's else C++ code:

_worklist = new (_arena) Block_List();

I'm a little rusty on my C++, can someone explain what is going on here? I don't understand what this code is doing.

EDIT:

_arena is a field of type Arena.

EDIT 2:

And here is the code of Block_List

Edit 3: My bad, I now understand should have provide more context, here is the whole code: http://hg.openjdk.java.net/hsx/hsx24/hotspot/file/ed3ac73a70ab/src/share/vm/opto/live.cpp#l52

El Marce
  • 3,144
  • 1
  • 26
  • 40

1 Answers1

2

Placement syntax is used to call a custom operator new that takes additional arguments. In this case, Block_List is derived from ResourceObj, and ResourceObj contains the following function:

void* operator new(size_t size, Arena *arena) {
    address res = (address)arena->Amalloc(size);
    DEBUG_ONLY(set_allocation_type(res, ARENA);)
    return res;
}

The expression new (_arena) Block_List(); will call this operator new, passing in the required size and the value that was passed in. The function returns the address to use for the object.

interjay
  • 107,303
  • 21
  • 270
  • 254
  • Your answer is entirely correct, but it might be helpful to distinguish between: "placement new" (where the object is created in a pre-allocated buffer without any need for a custom `operator new`); "a placement new expression" (where a custom `operator new` is called to allocate memory). What we have here is the latter. – Martin Bonner supports Monica Mar 07 '16 at 11:44
  • @MartinBonner Those are not official or accurate terms. The standard defines a placement syntax which includes the parentheses after `new`, and a placement `operator new` which takes extra arguments. There are also predefined placement `operator new` versions which take arguments of type `void*` (to set the address) or `nothrow_t` (to avoid `bad_alloc` exceptions), but they are merely specific instances of the placement new operator. – interjay Mar 07 '16 at 11:58
  • Thank you. I hadn't realized that. – Martin Bonner supports Monica Mar 07 '16 at 14:00