#Custom allocator for C++ code?

1 messages · Page 1 of 1 (latest)

olive spruce
#

Hello, I saw that it was possible to replace malloc to use a custom zig function that internally would use a Zig allocator. Is the same thing possible for C++'s new?

#

Nevermind, I thought about how it's linked in my case and it doesn't seem like i could do that...

signal basalt
#

you would have to override new/delete in C++ to call Zig functions through the C abi

#

although in all the major impls of the c++ standard library I'm aware of they just call malloc/free

#

however it could be useful to get the alignment from C++ and pass that to the Zig allocator

olive spruce
#

Yeah, that's what I thought

#

sadly the library i use is precompiled so I'd need to recompile it with my change for that to be possible

signal basalt
#

you can just link to an overridden new

#

that's kind of the point of being able to override new in C++

olive spruce
#

ahhhhhhhh

#

ohhhhhhhh

signal basalt
#

In C terms, new is just a weirdly named function symbol that is provided by the c++ standard library by default but can be overridden with a stronger definition

olive spruce
#

It's confusing to think about in my case because i use a lib that is a C bindings to a C++ library

#

and this library is provided in the form of a dll and a static lib, for use in C, so that you don't have to link libCPP...

#

i wonder if that would be possible in that case...

#

I guess it's not really possible for you to answer that, at this point im just thinking out loud lol

signal basalt
#

all you are doing is adding another static lib to the link command containing the functions

olive spruce
#

but thats interesting, thanks for the info !

olive spruce
signal basalt
#

that would be an unimaginely insane and unusable library

#

because that dll would override every other dll in the entire program

olive spruce
#

ahhh makes sense

#

i got a question though

#

then im guesing the new should be in the static part of the library right? since im not linking against libcpp

signal basalt
#

it should never ever be "in" any library

olive spruce
#

what provides the impl then?

signal basalt
#

it should be the executable's choice, so it should be in the executable

olive spruce
#

but i'm not linking against LibCPP, yet it works?

signal basalt
#

what would that affect?

olive spruce
#

sorry if im being unclear, my knowledge of that subject is fairly limited....

signal basalt
#

I think your best bet is trying it and seeing if it works

olive spruce
#

okay i just want to ask in the general case

#

without overriding new

signal basalt
#

ask what?

olive spruce
#

hold on, writing the question in a way that makes sense lol

#

how does the library i'm using find the implementation of new in the default scenario, with no override?

#

im guessing it was compiled in the static library, right?

signal basalt
#

it says "I use new" and the loader goes and finds out who defines new

olive spruce
#

so new is provided by the system in the form of a dynamic library that the executable asks for?

signal basalt
#

well I don't really know what nonsense windows does

olive spruce
#

okay i just wanted to connect it to things i understand

#

okay!

#

thanks for helping me process this, i will try

signal basalt
#

it's possible that it doesn't work on windows, but everything I said is true everywhere else