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

  • 相关阅读:
    山东省第三届ACM省赛The Best Seat in ACM Contest
    Rescue The Princess(2013年山东省第四届ACM大学生程序设计竞赛A题)
    自定义颜色(UIColor)
    调用主程序的委托
    设置View的背景颜色
    基本控件_UIImageView
    CGRect
    屏幕尺寸
    UIScrollView 原理
    设置将警告当做Error来对待
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2309523.html
Copyright © 2011-2022 走看看