Custom memory manager

The default new and delete operators are time consuming, since they implement memory operations for a multithreaded environment. Mine is a single threaded, and I want to optimize new and delete operators by customizing them. To start with, when I write new operator, it is going to have to assign raw memory by calling operator new. Isn't it going to have the same problem of multithreading? I mean, the compiler could have implemented operator new for a multithreaded environment. Anybody dealt with this issue before? How could I optimize memory manager to not care about multithreaded access to allocations/deallocations?

Thanks!
Last edited on
Have you actually measured how much time is being "wasted" on thread-safety you supposedly don't need?
I would be surprised if this were possible on a modern OS.

-- disclaimer: I'm kind of talking out of my ass here, since I don't really know for sure how it all works, but this is what I figure in my mind --


It's not really a language/implementation thing, since the OS is reponsible for managing memory allocation and distribution. Since the OS is a multithreading environment (even if your particular program isn't), the allocation still needs to be threadsafe.

Unless you're writing your own OS or you're sure your computer will be the only thing allocating memory on the whole computer, then there's no way to remove the threadsafety.

ALTHOUGH

You could do something like a memory pool. Have your program allocate a big chunk of memory upfront, and then give out portions of it to the rest of the program as it needs it.

Whether or not that would be faster though....
Disch:
"You could do something like a memory pool. Have your program allocate a big chunk of memory upfront, and then give out portions of it to the rest of the program as it needs it."

- That's what I'm trying to do. Thanks! I would still like to know if "operator new" is threadsafe.

helios:
Not really sure. Will let you know as soon as I get to it.
Last edited on
A non-reentrant new would be... catastrophic. Just imagine what could happen if there was a type A that dynamically allocated instances of itself.
Last edited on
The default memory manager is or can be made thread-safe.

I cannot speak for other compilers, but at least for GCC, the default memory
manager is NOT threadsafe unless you specifically define _REENTRANT or
_THREAD_SAFE (not sure if the middle underscore should be there), so you
should not be paying a threading penalty if not using threads.
Topic archived. No new replies allowed.