C++ 与记忆相关的术语 arena 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12825148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:39:33  来源:igfitidea点击:

What is the meaning of the term arena in relation to memory?

c++cmemory-managementterminology

提问by Nocturno

I'm reading a book on memory as a programming concept. In one of the later chapters, the author makes heavy use of the word arena, but never defines it. I've searched for the meaning of the word and how it relates to memory, and found nothing. Here are a few contexts in which the author uses the term:

我正在阅读一本关于内存作为编程概念的书。在后面的一章中,作者大量使用了arena一词,但从未定义它。我搜索了这个词的含义以及它与记忆的关系,但一无所获。以下是作者使用该术语的一些上下文:

"The next example of serialization incorporates a strategy called memory allocation from a specific arena."

"...this is useful when dealing with memory leaks or when allocating from a specific arena."

"...if we want to deallocate the memory then we will deallocate the whole arena."

“序列化的下一个示例结合了一种称为来自特定领域的内存分配的策略。”

“...这在处理内存泄漏或从特定领域分配时很有用。”

“……如果我们想释放内存,那么我们将释放整个竞技场。”

The author uses the term over 100 times in one chapter. The only definition in the glossary is:

作者在一章中使用了 100 多次这个词。词汇表中的唯一定义是:

allocation from arena- Technique of allocating an arena first and then managing the allocation/deallocation within the arena by the program itself (rather then by the process memory manager); used for compaction and serialization of complex data structures and objects, or for managing memory in safety-critical and /or fault-tolerant systems.

从 arena 分配- 首先分配一个 arena,然后由程序本身(而不是进程内存管理器)管理 arena 内的分配/释放的技术;用于复杂数据结构和对象的压缩和序列化,或用于管理安全关键和/或容错系统中的内存。

Can anyone define arenafor me given these contexts?

鉴于这些情况,有人可以为我定义竞技场吗?

回答by Kerrek SB

An arena is just a large, contiguous piece of memory that you allocate once and then use to manage memory manually by handing out parts of that memory. For example:

一个 arena 只是一块大的、连续的内存,你分配一次,然后通过分发部分内存来手动管理内存。例如:

char * arena = malloc(HUGE_NUMBER);

unsigned int current = 0;

void * my_malloc(size_t n) { current += n; return arena + current - n; }

The point is that you get full control over how the memory allocation works. The only thing outside your control is the single library call for the initial allocation.

关键是您可以完全控制内存分配的工作方式。唯一不受您控制的是初始分配的单个库调用。

One popular use case is where each arena is only used to allocate memory blocks of one single, fixed size. In that case, you can write very efficient reclamation algorithms. Another use case is to have one arena per "task", and when you're done with the task, you can free the entire arena in one go and don't need to worry about tracking individual deallocations.

一个流行的用例是每个 arena 仅用于分配一个单一、固定大小的内存块。在这种情况下,您可以编写非常有效的回收算法。另一个用例是每个“任务”有一个竞技场,当您完成任务时,您可以一次性释放整个竞技场,而无需担心跟踪单个释放。

Each of those techniques is very specialized and generally only comes in handy if you know exactly what you're doing and why the normal library allocation is not good enough. Note that a good memory allocator will already do lots of magic itself, and you need a decent amount of evidence that that's not good enough before you start handling memory yourself.

这些技术中的每一种都非常专业,通常只有在您确切地知道自己在做什么以及为什么正常的库分配不够好时才会派上用场。请注意,一个好的内存分配器本身已经可以发挥很多作用,在您开始自己处理内存之前,您需要大量证据证明这还不够好。

回答by Mike

I'll go with this oneas a possible answer.

我会用这个作为可能的答案。

?Memory Arena (also known as break space)--the area where dynamic runtime memory is stored. The memory arena consists of the heap and unused memory. The heap is where all user-allocated memory is located. The heap grows up from a lower memory address to a higher memory address.

?Memory Arena (also known as break space)--the area where dynamic runtime memory is stored. The memory arena consists of the heap and unused memory. The heap is where all user-allocated memory is located. The heap grows up from a lower memory address to a higher memory address.

I'll add Wikipedia's synonyms: region, zone, arena, area, or memory context.

我将添加维基百科的同义词:region、zone、arena、area 或 memory context。

Basically it's memory you get from the OS, and divvy out, then can be freed all at once. The advantage to this is that repeated small calls to malloc()could be costly (Every memory allocation has a performance cost: the time it takes to allocate the memory in your program's logical address space and the time it takes to assign that address space to physical memory) where as if you know a ball park you can get yourself a big chunk of memory then hand it out to your variables as/how you need it.

基本上它是您从操作系统中获得的内存,然后分配出去,然后可以一次全部释放。这样做的好处是重复小调用malloc()可能会很昂贵(每个内存分配都有性能成本:在程序的逻辑地址空间中分配内存所需的时间以及将该地址空间分配给物理内存所需的时间)就像你知道一个棒球场一样,你可以让自己获得一大块内存,然后根据你的需要/如何将它交给你的变量。

回答by Adam Rosenfield

Think of it as a synonym for 'heap'. Ordinarily, your process only has one heap/arena, and all memory allocation happens from there.

将其视为“堆”的同义词。通常,您的进程只有一个堆/竞技场,并且所有内存分配都发生在那里。

But, sometimes you have a situation where you would to group a series of allocations together (e.g. for performance, to avoid fragmentation, etc.). In that case, it's better to allocate a new heap/arena, and then for any allocation, you can decide which heap to allocate from.

但是,有时您会遇到将一系列分配组合在一起的情况(例如,为了性能、避免碎片等)。在这种情况下,最好分配一个新的堆/竞技场,然后对于任何分配,您可以决定从哪个堆分配。

For example, you might have a particle system where lots of objects of the same size are being frequently allocated and deallocated. To avoid fragmenting memory, you could allocate each particle from a heap which is only used for those particles, and all other allocations would come from the default heap.

例如,您可能有一个粒子系统,其中许多相同大小的对象经常被分配和释放。为避免内存碎片化,您可以从仅用于这些粒子的堆中分配每个粒子,所有其他分配将来自默认堆。

回答by Rahul Tripathi

From http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html:

来自http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html

The libc.so.x shared library contains the glibc component and the heap code resides inside it. The current implementation of the heap uses multiple independent sub-heaps called arenas. Each arena has its own mutex for concurrency protection. Thus if there are sufficient arenas within a process' heap, and a mechanism to distribute the threads' heap accesses evenly between them, then the potential for contention for the mutexes should be minimal. It turns out that this works well for allocations. In malloc(), a test is made to see if the mutex for current target arena for the current thread is free (trylock). If so then the arena is now locked and the allocation proceeds. If the mutex is busy then each remaining arena is tried in turn and used if the mutex is not busy. In the event that no arena can be locked without blocking, a fresh new arena is created. This arena by definition is not already locked, so the allocation can now proceed without blocking. Lastly, the ID of the arena last used by a thread is retained in thread local storage, and subsequently used as the first arena to try when malloc() is next called by that thread. Therefore all calls to malloc() will proceed without blocking.

libc.so.x 共享库包含 glibc 组件,堆代码位于其中。堆的当前实现使用称为 arenas 的多个独立子堆。每个 arena 都有自己的互斥锁用于并发保护。因此,如果在进程的堆中有足够的区域,并且有一种机制可以在它们之间均匀地分配线程的堆访问,那么互斥体争用的可能性应该是最小的。事实证明,这适用于分配。在 malloc() 中,进行测试以查看当前线程的当前目标 arena 的互斥锁是否空闲(trylock)。如果是,那么竞技场现在被锁定,分配继续进行。如果互斥锁很忙,那么每个剩余的 arena 都会依次尝试并在互斥锁不忙时使用。如果没有任何竞技场可以在不阻塞的情况下被锁定,则会创建一个新的竞技场。根据定义,这个 arena 尚未锁定,因此分配现在可以在不阻塞的情况下进行。最后,线程最后使用的 arena 的 ID 保留在线程本地存储中,随后用作该线程下一次调用 malloc() 时要尝试的第一个 arena。因此,所有对 malloc() 的调用都将无阻塞地进行。

You can also refer to this link:

你也可以参考这个链接:

http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf

http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf