Operators new and delete

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

Operators new and delete

Post by 8infy »

Hi guys, just implemented my first kernel heap allocator and I'm kinda confused about the C++ operators...

Why does this call the delete with size_t?
Image

I did read this page https://en.cppreference.com/w/cpp/memor ... tor_delete but it says that they're called
only if user defined implementation is provided, but if I don't provide them GCC starts complaining and it results in an undefined reference...
(I'm using C++17)

Thanks :)
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Operators new and delete

Post by nexos »

I am not a C++ expert, I mainly know C, but in the picture, it says there are 4 overloads. That tells me that is one way to define it. You could also try using a lower standard and see if that helps.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Operators new and delete

Post by kzinti »

It is rather trivial to implement new/delete. Here is what I use (I didn't implement all overloads as I didn't need them):

Code: Select all

inline void* operator new(size_t, void* p)      { return p; }
inline void* operator new[](size_t, void* p)    { return p; }
inline void* operator new(size_t size)          { return ::malloc(size); }

inline void operator delete(void* p)            { ::free(p); }
inline void operator delete(void* p, size_t)    { ::free(p); }
Post Reply