简体   繁体   English

GCC如何实现C ++标准分配器?

[英]How does GCC implement the C++ standard allocator?

I am not familiar with tracking down the source codes to figure out the GCC STL implementation (libstdc++) of the C++ standard allocator, and I cannot find any brief explanation, documentation and technical reports that describe what the memory model that GCC selects. 我不熟悉跟踪源代码以找出C ++标准分配器的GCC STL实现(libstdc ++),并且找不到描述GCC选择的内存模型的任何简要说明,文档和技术报告。

I guess that GCC uses several fixed-size buckets for storing small objects of the same size in bytes and allocate large memory space ad hoc for large objects over the specific size. 我猜想,GCC使用几个固定大小的存储桶来存储相同大小的小对象(以字节为单位),并为特定大小的大对象临时分配大的存储空间。

What is the specific memory model that GCC selects for the C++ standard allocator? GCC为C ++标准分配器选择的特定内存模型是什么?

I cannot find any brief explanation, documentation and technical reports that describe what the memory model that GCC selects 我找不到任何简短的说明,文档和技术报告来描述GCC选择的内存模型

Read the code. 阅读代码。 It's open source, and it's included with the compiler as plain text. 它是开源的,以纯文本形式包含在编译器中。

If you're not sure where your standard headers are, you can do one of two things: 如果不确定标准标头在哪里 ,可以执行以下两项操作之一:

  1. learn how to ask the compiler as in this question 学习如何在这个问题中询问编译器

  2. trick the compiler into telling you, eg. 欺骗编译器告诉您,例如。 by trying to do something std::allocator cannot 通过尝试做某事std::allocator无法

     #include <memory> int main() { std::allocator<int> a; a.allocate(&a); return 0; } 

    gives

     error ... In file included from \\ /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33:0, 

When you find out that std::allocator just uses the heap to make all those decisions, you can look at the glibc source for malloc . 当您发现std::allocator仅使用堆来做出所有这些决定时,您可以查看glibc源代码中的malloc

std::allocator just uses operator new and operator delete , and those in turn simply wrap malloc and free . std::allocator只使用operator new operator delete operator newoperator delete ,而依次使用mallocfree包装。

Thus the implementation is delegated to the C library, whichever happens to be in use. 因此,将实现委派给C库,无论碰巧正在使用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM