zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Java反编译插件Jad及eclipse编译插件JadClipse综合使用
    Java IO示例总结
    java Statement与preparedStatement的区别
    Android TabHost 文字及图片的设置
    Android EditText属性
    ubuntu 软件安装及卸载
    Android 设置控件不可见且不占用空间
    Ubuntu 11 安装后要做的20件事情
    Ubuntu Samba安装与创建目录
    Android Service
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2309523.html
Copyright © 2011-2022 走看看