问题描述
我最近看到以下职位:
一个内存分配器不低于malloc的较低水平。 (默认
通常分配直接或间接)调用malloc
这是分配器只允许你指定不同的分配
策略。例如,你可以使用它调用malloc分配器
一旦检索一个大的内存池,然后为后续
分配请求,它只是返回此池的一小块。
An allocator just allows you to specify different allocation strategies. For example, you might use an allocator which calls malloc once to retrieve a large pool of memory, and then for subsequent allocation requests, it just returns a small chunk of this pool.
或者你可以使用它作为一个钩子,让你执行一些附加
每次内存分配或释放任务。
Or you may use it as a hook to allow you to perform some additional task every time memory is allocated or freed.
关于你的第二个问题,是的malloc你可以去无最低
失去了便携性。 的malloc通常使用一些实施
OS专用内存分配函数,所以这将是较低水平
仍然。但是,这无关你的主要问题,因为C ++
分配器是一个更高层次的抽象。
As to your second question, malloc is the lowest you can go without losing portability. malloc is typically implemented using some OS-specific memory allocation function, so that would be lower level still. But that's unrelated to your main question, since C++ allocators are a higher-level abstraction.
来自:
我的问题是 - 如何在以下操作系统实现的malloc?
My question is- how is malloc implemented in the following Operating systems?
- 对于Windows
- 用于Linux
什么是被称为操作系统特定的函数/的malloc()的实现?
what are the OS-specific functions which are called/implementations of malloc()?
推荐答案
在Windows中,在最新版本的MSVC,的malloc(和C ++ 新
,因为它是使用实施对于实际的内存分配部分相同的基本面新
)调用<$c$c>HeapAlloc()$c$c>.在其他的版本,比如G ++ MinGW的,C运行时是较旧的版本,这并不完全是直接打电话到 HeapAlloc
,但在它的基础,它仍然去 HeapAlloc
- 找不同的东西,我们需要回去到Windows pre-95,它确实有一个的GlobalAlloc
和 LocalAlloc
的功能集 - 但我不认为人们使用16位编译器这些日子 - 至少不适用于Windows编程
In Windows, in recent versions of MSVC, malloc (and C++ new
, as it is implemented using the same fundamentals for the actual memory allocation part of new
) calls HeapAlloc()
. In other versions, such as g++ mingw, the C runtime is an older version, which doesn't call quite as directly to HeapAlloc
, but at the base of it, it still goes to HeapAlloc
- to find something different, we need to go back to Windows pre-95, which did have a GlobalAlloc
and LocalAlloc
set of functions - but I don't think people use 16-bit compilers these days - at least not for Windows programming.
在Linux上,如果您使用的是的glibc
,这取决于分配的大小是否它调用的或的 - MMAP
(用 MAP_ANONYMOUS
中的标志),用于分配的较大(超过阈值,我相信这是在2MB典型的实现)
In Linux, if you are using glibc
, it depends on the size of the allocation whether it calls sbrk
or mmap
- mmap
(with MAP_ANONYMOUS
in the flags) is used for larger allocations (over a threshold, which I believe is 2MB in the typical implementation)
这篇关于什么是由malloc的取得了Windows和Linux的本机OS /系统调用()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!