• Allocator 之问


    1. 为什么需要Allocator

    在最开始,Allocator被设计用来为不同的内存模型提供一个抽象机制(早期的16位操作系统的指针类型有near,far等若干种类型,但是Allocator并没能解决这个问题,为什么?)Nowadays, allocators serve as an abstraction to translate the need to use memory into a raw call for memory.

    Thus, allocators simply separate the implementation of containers, which need to allocate memory dynamically, from the details of the underlying physical memory management. Thus, you can simply apply different memory models such as shared memory, garbage collections, and so forth to your containers without any hassle because allocators provide a common interface.

    在container的实现中,经常需要动态分配内存,Allocator将‘容器动态分配内存的需求’与底层物理内存的管理方式分离开。因此,我们可以使用不同的内存模型,比如shared memory或者GC。[1] is a Allocator which is based on shared memory. Allocator所提供的common interface为隐藏各种内存管理的细节提供了可能。

    2. 在Allocator的设计中,为什么allocate和construct是分开的, 而new则是既allocate又construct

    allocate和construct分开的好处是当有一段内存会反复使用时,只需要每次使用前construct一下就可以了,不需要每次都去allocate memory,虽然OS有可能将allocate memory的cost降得很低(比如需要多分配一些,或者有一些cache),但是也并不是所有的OS在任何时候都能做到这一点,作为一个通用的接口而言,还是应该分开的。

    new既做了allocate又做了construct,好处就是比较简单,需要用什么对象,new一下就可以了,只是在有些时候效率不高而已。

    3.

    [1] A C++ POOLED, SHARED MEMORY ALLOCATOR FOR THE STANDARD TEMPLATE LIBRARY

  • 相关阅读:
    Build 2019 彩蛋
    崂山
    Win10 iot 修改日期时间
    《 结网:改变世界的互联网产品经理 》
    <[你在荒废时间的时候别人都在拼命!]>
    《时间的玫瑰》阅读笔记
    翻石头价值投资手册-科技行业
    No module named flask.ext.sqlalchemy.SQLALchemy
    《寻找伟大的企业》
    <《基金经理投资笔记丛书4-1:投资是一种生活方式》>
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2309523.html
Copyright © 2020-2023  润新知