New vs. Malloc,当重载New时 [英] New vs. Malloc, when overloading New
问题描述
我重载新
和删除
以实现我自己的小对象/线程安全分配器。 / p>
问题是,当我重载 new
时,我不能使用 new
不破坏通用因果关系或至少编译器。大多数例子中,我发现 new
是重载的,使用 Malloc()
做实际分配。但是从我对C ++的理解来看, Malloc()
根本没有用例。
与此类似的答案,一些在SO之外的侵权行为较少:我在什么情况下使用malloc vs new?
我的问题是,当重载operator new时,如何分配实际内存 c>
(这不是好奇心) ,
Short答案:如果您不想要现有的 malloc
,您需要实现自己的堆管理器。
一个堆管理器,例如Linux中的glibc中的 malloc
,Windows中的 HeapAlloc
级别算法。首先,请记住,堆被优化用于分配小尺寸的对象,如4〜512字节。
如何实现自己的堆管理器?至少,您必须调用在您的进程中分配内存块的系统API。对于Windows,有 VirtualAlloc
,对于Linux有 sbrk
。这些API分配大量内存,但大小必须是 page 大小的倍数。通常,x86和Windows / Linux中的页面大小为4KB。
获取一大块页面后,您需要实现自己的算法,内存转换为更小的请求。一个经典的(仍然很实用的)实现和算法是 dlmalloc
: http://g.oswego.edu/dl/html/malloc.html
要实现,您需要有多个数据保存结构和一些优化策略。例如,对于像16,20,36,256字节这样的小对象,堆管理器维护每个大小的块的列表。所以,有一个列表的列表。如果请求的大小大于页面大小,则只需调用 VirtualAlloc
或 sbrk
。然而,高效的实施是非常具有挑战性的。您不仅必须考虑速度和空间开销,还必须考虑缓存位置和碎片。
如果您对为多线程环境优化的堆管理器感兴趣,请查看 tcmalloc
: http:// goog-perftools。 sourceforge.net/doc/tcmalloc.html
I'm overloading new
and delete
to implement my own small-objects/thread-safe allocator.
The problem is that when I am overloading new
, I cannot use new
without breaking universal causality or at least the compiler. Most examples I found where new
is overloaded, use Malloc()
to do the actual allocation. But from what I understood of C++, there is no use-case for Malloc()
at all.
Multiple answers similar to this one, some with less tort outside of SO: In what cases do I use malloc vs new?
My question, is how do I allocate the actual memory when overloading operator new
without using Malloc()
?
(This is out of curiosity more than anything, try not to take the reasoning behind the overload too seriously; I have a seperate question out on that anywho!)
Short answer: if you don't want existing malloc
, you need to implement your own heap manager.
A heap manager, for example malloc
in glibc of Linux, HeapAlloc
in Windows, is a user-level algorithm. First, keep in mind that heap is optimized for allocating small sizes of objects like 4~512 bytes.
How to implement your own heap manager? At least, you must call a system API that allocates a memory chunk in your process. There are VirtualAlloc
for Windows and sbrk
for Linux. These APIs allocate a large chunk of memory, but the size must be multiple of page size. Typically, the size of page in x86 and Windows/Linux is 4KB.
After obtaining a chunk of page, you need to implement your own algorithms how to chop down this big memory into smaller requests. A classic (still very practical) implementation and algorithm is dlmalloc
: http://g.oswego.edu/dl/html/malloc.html
To implement, you need to have several data structures for book-keeping and a number of policies for optimization. For example, for small objects like 16, 20, 36, 256 bytes, a heap manager maintains a list of blocks of each size. So, there are a list of lists. If requested size is bigger than a page size, then it just call VirtualAlloc
or sbrk
. However, an efficient implementation is very challenging. You must consider not only speed and space overhead, but also cache locality and fragmentation.
If you are interested in heap managers optimized for multithreaded environment, take a look a tcmalloc
: http://goog-perftools.sourceforge.net/doc/tcmalloc.html
这篇关于New vs. Malloc,当重载New时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!