I have been taking a look at MMGR for a memory allocation checker and I have a few questions that I don't see anywhere else on the internet.
1) There is a "reported size" and an "actual size". I understand what the "reported size" is, with it being the size that new receives, however, what is "actual size"? Why is there a difference?
2) Is the logging exactly safe? I see that 开发者_StackOverflow社区the logging happens at the end of a static deallocation of a class, however, could this give a false positive of memory leaks? 2A) Just to be sure, static deallocations always happen last, right?
3) Is this code thread safe? If not, how can it become thread safe?
I'm afraid I do not have any experience with MMGR, so I can only provide an answer for your first question.
The reason some memory debuggers mention a reported (or requested) and an actual allocation size has to do with the way memory allocators work. They rarely allocate exactly the requested size - usually they reserve a bit more. There are various possible reasons:
Alignment issues: I don't think that there is an allocator that will allocate e.g. exactly 3 bytes on a modern 32-bit (or more) system. The value will be rounded to at least the next multiple of the word-size or 4, depending on the architecture.
Management issues: some allocators will only handle allocations sized to powers of two. So you get blocks of 4, 8, 16, 32, 64, 128, 256 etc bytes. This is rare in userspace allocators, but common in kernel-space ones.
Providence: Some allocators will allocate a bit more each time in anticipation of a possible reallocation.
In any case, the reserved memory will often be somewhat more than the amount requested, hence the existence of two numbers.
精彩评论