1) Is MMGR thread safe?
2) I was hoping someone could help me understand some code. I am looking at something where a macro is used, but I don’t understand the macro. I know it contains a function call and an if check, however, the function is a void function. How does wrapping “(m_setOwner (FILE,_LINE_,FUNCTION),false)” ever change return types?
#define someMacro (m_setOwner(__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new ...
void m_setOwner(const char *file, const unsigned int line, const char *func);
3) What is the point of the reservoir?
4) On line 770 (“void *operator new(size_t reportedSize)” there is the line
“// ANSI says: allocation requests of 0 bytes will still return a valid value”
Who/what is ANSI in this context? Do they mean the standards?
5) This is more of C++ standards, but where does “reportedSize” come from for “void *operator new(size_t reportedSize)”?
6) Is this the code that is actually doing the allocation needed?
“au->actualAddress = malloc(au->actualSize);”
1) The C++03 standard does not mention threads. However, in all platforms with thread support I know of, the default memory allocator (
newanddelete) is thread safe.Edit: In general, if things are not marked as thread-safe, you should assume they aren’t, especially when there implicit global data (such as heap management structures in a memory manager). I’ve read some comments on another forum about this MMGR library not being thread safe.
2) The comma operator in the macro discards the result on the left, so the result of the
(m_setOwner(...), false)expression is alwaysfalse.Edit: This syntax is used in MMGR to log the memory allocation before proceeding to the real allocation. The comma operator is used so that the
newmacro syntax is unchanged. Pre-processor macros are a simple text-based find-and-replace mechanism. Any use ofnewin your code will compile with or without this MMGR library. Just that, when using MMGR, the memory allocation will be logged, which is useful for debugging!3) What “reservoir”? Are you referring to the heap? Where did you get this term from?
Edit: The memory manager at the application level is just a front-end to the memory manager at the system level. Hence, it must ask the system to allocate large pages of memory. The reservoir, in this case, seems to be the name for the mechanism that pre-allocates some of those large pages such that the next few allocations are guaranteed to succeed. This is mainly an optimization, as you amortize the cost of a single (expensive) system-level allocation over several application-level allocations.
4) Yes, “ANSI”, in this context, refers to the the C++03 standard. The proper way to refer to it now is to use the ISO standard number. Feel free to Google it.
5) The reported size is set by the compiler. When you write something like
X* x = new X(...);the compiler logically “rewrites” this to the equivalent form:The first line allocates enough memory (
sizeof(X)is the value passed as thereportedSizeargument tooperator new). The second line invokes the constructor of theXclass to create an object in the allocated slot of memory.6) See #5. Yes, can think of it in these temrs, although your platform will likely not call
malloc()in theoperator newin “release” mode.