Remove dependency on C++ headers? #484

Closed
opened 2016-01-14 15:47:27 +00:00 by yngccc · 9 comments
yngccc commented 2016-01-14 15:47:27 +00:00 (Migrated from github.com)

Failed to compile with -nostdinc++ because #include <new>, how about replace all replacement news with memcpy?

Failed to compile with `-nostdinc++` because `#include <new>`, how about replace all replacement news with memcpy?
ocornut commented 2016-01-14 15:52:38 +00:00 (Migrated from github.com)

There are 4 calls to placement new, Not sure how you would do that tho. Have a static instance filled with all default values and memcpy? Sounds rather awkward and unpleasant to maintain.
Also not sure of the benefits. I'd rather avoid the additional maintenance.

There are 4 calls to placement new, Not sure how you would do that tho. Have a static instance filled with all default values and memcpy? Sounds rather awkward and unpleasant to maintain. Also not sure of the benefits. I'd rather avoid the additional maintenance.
yngccc commented 2016-01-14 16:08:02 +00:00 (Migrated from github.com)

hmm, how about add a member function that mirror the constructor, call that on the pointer return from malloc instead of replacement new?

hmm, how about add a member function that mirror the constructor, call that on the pointer return from malloc instead of replacement new?
ocornut commented 2016-01-14 16:13:02 +00:00 (Migrated from github.com)

I'm not sure it is possible with the syntax of the call to new. If you can find a way to do it feel free to try.

I'm not sure it is possible with the syntax of the call to new. If you can find a way to do it feel free to try.
nem0 commented 2016-01-14 16:19:51 +00:00 (Migrated from github.com)

it is possible to have placement new without including new:

struct NewPlaceholder {};
inline void* operator new(size_t, NewPlaceholder, void* where)
{
    return where;
}
#define NEW_MACRO(allocator, ...) new (NewPlaceholder(), (allocator).allocate(sizeof(__VA_ARGS__))) __VA_ARGS__

it is possible to have placement new without including new: ``` struct NewPlaceholder {}; inline void* operator new(size_t, NewPlaceholder, void* where) { return where; } #define NEW_MACRO(allocator, ...) new (NewPlaceholder(), (allocator).allocate(sizeof(__VA_ARGS__))) __VA_ARGS__ ```
ocornut commented 2016-01-25 10:18:43 +00:00 (Migrated from github.com)

@nem0 could you clarify the code above? Not sure to understand how it would work.
It may also possibly help with https://github.com/ocornut/imgui/issues/504

@nem0 could you clarify the code above? Not sure to understand how it would work. It may also possibly help with https://github.com/ocornut/imgui/issues/504
nem0 commented 2016-01-25 11:09:43 +00:00 (Migrated from github.com)

I also wanted to get rid of new include in my engine, you can see it here https://github.com/nem0/LumixEngine/blob/master/src/engine/core/iallocator.h

The NewPlaceholder is there just because it is not possible to redefine the default placement new (at least according to standard), however it is legal to define your own.

VA_ARGS is there because of templates:

NEW_MACRO(allocator, T<int, int>)(arg1, arg2)

Usage should be something along these lines:

DefaultAllocator allocator;
...
T* value = NEW_MACRO(allocator, T)(arg);

or without allocator:

#define NEW_MACRO2(allocator, ptr) new (NewPlaceholder(), ptr) 
...
char buf[64]
T* value = NEW_MACRO2(buf) T(arg);

Note: all the examples are just from my head, there might be some errors in them.

I also wanted to get rid of new include in my engine, you can see it here https://github.com/nem0/LumixEngine/blob/master/src/engine/core/iallocator.h The NewPlaceholder is there just because it is not possible to redefine the default placement new (at least according to standard), however it is [legal to define your own](http://en.cppreference.com/w/cpp/memory/new/operator_new). **VA_ARGS** is there because of templates: ``` NEW_MACRO(allocator, T<int, int>)(arg1, arg2) ``` Usage should be something along these lines: ``` DefaultAllocator allocator; ... T* value = NEW_MACRO(allocator, T)(arg); ``` or without allocator: ``` #define NEW_MACRO2(allocator, ptr) new (NewPlaceholder(), ptr) ... char buf[64] T* value = NEW_MACRO2(buf) T(arg); ``` Note: all the examples are just from my head, there might be some errors in them.
ocornut commented 2016-01-25 11:13:10 +00:00 (Migrated from github.com)

Thanks! I understand and will experiment with that.

Thanks! I understand and will experiment with that.
ocornut commented 2016-01-25 21:27:58 +00:00 (Migrated from github.com)

@yngccc I have removed the dependency on the <new> header now.
Let us know if that works better for you. Thanks Mikulas!

@yngccc I have removed the dependency on the `<new>` header now. Let us know if that works better for you. Thanks Mikulas!
yngccc commented 2016-01-28 15:21:47 +00:00 (Migrated from github.com)

@ocornut This is great, thanks!

@ocornut This is great, thanks!
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#484
No description provided.