简体   繁体   English

我如何使用microsoft的C ++分配器

[英]How do I use microsoft's C++ allocators

I see on http://msdn.microsoft.com/en-us/library/ee292117.aspx and http://msdn.microsoft.com/en-us/library/ee292134.aspx that Microsoft provides macros and classes for specialized allocators, but I'm not sure what each of the caching strategies are, how to use them. 我在http://msdn.microsoft.com/en-us/library/ee292117.aspxhttp://msdn.microsoft.com/en-us/library/ee292134.aspx上看到Microsoft为专门提供的宏和类分配器,但我不确定每个缓存策略是什么,如何使用它们。 Can someone explain when to use each of these parts? 有人可以解释何时使用这些部件?

  • Cache Templates 缓存模板
    • cache_freelist - The cache_freelist template class maintains a free list of memory blocks of size Sz . cache_freelist - cache_freelist模板类维护一个大小为Sz的内存块的空闲列表。 When the free list is full it uses operator delete to deallocate memory blocks. 当空闲列表已满时,它使用operator delete来释放内存块。 When the free list is empty it uses operator new to allocate new memory blocks. 当空闲列表为空时,它使用operator new来分配新的内存块。 The maximum size of the free list is determined by the class max class passed in the Max parameter. 空闲列表的最大大小由Max参数中传递的类max class确定。 Each memory block holds Sz bytes of usable memory and the data that operator new and operator delete require. 每个存储器块保存Sz字节的可用存储器以及operator newoperator delete所需的数据。
    • cache_suballoc - The cache_suballoc template class stores deallocated memory blocks in a free list with unbounded length, using freelist<sizeof(Type), max_unbounded> , and suballocates memory blocks from a larger chunk allocated with operator new when the free list is empty. cache_suballoc - cache_suballoc模板类将释放的内存块存储在具有无限长度的空闲列表中,使用freelist<sizeof(Type), max_unbounded> ,并在空闲列表为空时从使用operator new分配的较大块freelist<sizeof(Type), max_unbounded>分配内存块。 Each chunk holds Sz * Nelts bytes of usable memory and the data that operator new and operator delete require. 每个块保存可用内存的Sz * Nelts字节以及operator newoperator delete所需的数据。 Allocated chunks are never freed. 从未释放分配的块。
    • cache_chunklist - This template class uses operator new to allocate chunks of raw memory, suballocating blocks to allocate storage for a memory block when needed; cache_chunklist - 此模板类使用operator new来分配原始内存块,子分配块以在需要时为内存块分配存储空间; it stores deallocated memory blocks in a separate free list for each chunk, and uses operator delete to deallocate a chunk when none of its memory blocks is in use. 它将释放的内存块存储在每个块的单独空闲列表中,并使用operator delete在没有任何内存块正在使用时释放块。 Each memory block holds Sz bytes of usable memory and a pointer to the chunk that it belongs to. 每个内存块保存Sz字节的可用内存和指向它所属的块的指针。 Each chunk holds Nelts memory blocks, three pointers, an int and the data that operator new and operator delete require. 每个块都包含Nelts内存块,三个指针,一个int以及operator newoperator delete所需的数据。

I've written several allocators myself, but this documentation is just... confusing. 我自己写了几个分配器,但是这个文档只是......令人困惑。

Wow, they really garbled the documentation, didn't they? 哇,他们真的乱了文档,不是吗? (I wrote that code when I was at Dinkumware) (当我在Dinkumware时,我写了那段代码)

The allocators here are policy-based: you can specify a caching policy and a synchronization policy. 这里的分配器是基于策略的:您可以指定缓存策略和同步策略。

The basic idea is to make it easier to write allocators that use internal caches and can be synchronized across threads. 基本思想是使编写使用内部缓存的分配器更容易,并且可以跨线程同步。 There are half a dozen predefined allocators; 有六个预定义的分配器; their names all begin with allocator_ . 他们的名字都以allocator_开头。 Use them if they fit your needs. 如果符合您的需要,请使用它们。 In the MSDN stuff, look at the first paragraph of the description of the particular allocator; 在MSDN中,查看特定分配器描述的第一段; don't read the "Remarks", where they talk about ALLOCATOR_DECL ;. 不要阅读“备注”,他们在那里谈论ALLOCATOR_DECL ;

You can also use that code to create your own allocators that use the pre-defined caching strategies (templates whose names begin with cache_ ) and synchronization strategies (templates whose names begin with sync_ ), or that use your own caching template and synchronization template. 您还可以使用该代码创建自己的分配器,这些分配器使用预定义的缓存策略(名称以cache_开头的模板)和同步策略(名称以sync_开头的模板),或者使用您自己的缓存模板和同步模板。 Getting a usable allocator from these pieces is a bit tedious, so the header provides ALLOCATOR_DECL as a convenient way to produce all the necessary boilerplate without having to write it yourself. 从这些部分获取可用的分配器有点单调乏味,因此标题提供了ALLOCATOR_DECL作为生成所有必需样板的便捷方式,而无需自己编写。 ALLOCATOR_DECL takes three arguments: the name of the cache template to use, the name of the synchronization template to use, and the name of the allocator that you're creating. ALLOCATOR_DECL有三个参数:要使用的缓存模板的名称,要使用的同步模板的名称以及您正在创建的分配器的名称。 So instead of writing 所以不要写作

template <class T> class name
    : public Dinkum::allocators::allocator_base<T, sync<cache > >
    {
    public:
        name() {}
        template <class U> name(const name<U>&) {}
        template <class U> name& operator = (const name<U>&)
            {return *this; }
        template <class U> struct rebind
            {    /* convert a name<T> to a name<U> */
            typedef name<U> other;
            };
    };

you would write ALLOCATOR_DECL(cache, sync, name); 你会写ALLOCATOR_DECL(cache, sync, name); . allocator_base does the heavy lifting; allocator_base完成了繁重的工作; the allocator itself has to be a derived type, so that it can handle rebind correctly. 分配器本身必须是派生类型,以便它可以正确处理rebind (The Dinkum in that code comes from the Dinkumware documentation; I don't know what namespace the Microsoft stuff puts these names in, but presumably the macro puts the right name there). (该代码中的Dinkum来自Dinkumware文档;我不知道微软的东西将这些名称放在哪个命名空间中,但可能是宏在那里放了正确的名称)。

For the cache templates: 对于缓存模板:

  • cache_freelist maintains a linked list of node-sized blocks; cache_freelist维护节点大小的块的链表; the maximum size of the list is set by the template parameter Sz , and node allocation is managed with operator new and operator delete . 列表的最大大小由模板参数Sz ,节点分配由operator newoperator delete
  • cache_suballoc adds another layer that manages a block of Nelts node blocks, all allocated as a single blob; cache_suballoc添加另一个层来管理一个Nelts节点块,所有这些层都被分配为一个blob; allocation of a new element looks first to the free list, and if there is nothing free, allocates a new blob. 新元素的分配首先查看空闲列表,如果没有空闲,则分配新的blob。 Memory blocks are not deleted until the allocator is destroyed. 在销毁分配器之前,不会删除内存块。
  • cache_chunklist allocates memory in blobs (like cache_suballoc ), but doesn't use a common free list; cache_chunklist在blob中分配内存(如cache_suballoc ),但不使用公共空闲列表; when a block is released it goes back into the free list for its blob. 当一个块被释放时,它会回到它的blob的空闲列表中。 When all of a blobs blocks have been released, the blob itself is deleted. 释放所有blob块后,将删除blob本身。

The first subquestion is easy. 第一个问题很简单。 When to use them? 什么时候使用它们? When profiling shows an issue with the default allocator. 分析显示默认分配器的问题。 That alone makes the question irrelevant to most developers. 仅这一点就使问题与大多数开发人员无关。

Some of the macros use the verb "yields", which loosely means "eventually expands to, probably through multiple steps". 一些宏使用动词“yield”,松散意味着“最终扩展到,可能通过多个步骤”。

The cache templates save deallocated blocks for later re-allocation. 缓存模板保存解除分配的块以供以后重新分配。 You'd pick the cache which works best for your allocation pattern, the one which has the best tradeoff between reuse % and cache size (see profiling above). 您将选择最适合您的分配模式的缓存,即在重用%和缓存大小之间具有最佳权衡的缓存(请参阅上面的分析)。

Some cache templates have a finite number of freed blocks, implemented via freelist<> . 一些缓存模板具有有限数量的释放块,通过freelist<> The Max classes determine how long such a freelist can become. Max类确定这样的freelist可以变成多长时间。

The allocators finally are pre-composed combinations of the above. 分配器最终是预先组合的上述组合。

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

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