(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43645984

这个Hacker News帖子讨论了区域分配器(arena allocators),一种内存管理技术。原文重点介绍了常见的错误和有用的应用。 Zambyte 指出,在Zig和Odin等语言中,显式分配器的采用率越来越高。codr7 提倡可组合的分配器,其中可以根据需要插入自由列表等各个特性。 o11c 对文章中区域分配器无法释放单个部分的说法提出了质疑,并以GNU Obstack为例,它具有“释放此点之后的所有内容”的操作。grandemire对此提出异议,认为这取决于对区域的定义。o11c 反驳说,文中描述的多块区域结构类似于Obstack,后者实现了上述释放操作。 cogman10 指出了分配器速度和碎片最小化之间的权衡,解释了为什么碰撞分配器(bump allocators)很快,但也有局限性。tonyedgecombe 将其与PostScript的保存和恢复操作符进行了类比。

相关文章
  • 关于竞技场分配器的错误和巧妙用法 2025-04-14
  • (评论) 2025-03-17
  • (评论) 2025-03-24
  • (评论) 2023-11-30
  • (评论) 2024-09-02

  • 原文
    Hacker News new | past | comments | ask | show | jobs | submit login
    Mistakes and cool things to do with arena allocators (zylinski.se)
    63 points by skeptrune 7 hours ago | hide | past | favorite | 7 comments










    Cool read! I have been getting my toes wet with the idea of explicit allocators in Zig. I haven't done anything with Odin yet, but it's cool to see that explicit allocators with off-the-shelf standard library implementations is making it's way into more languages.


    I like composable single feature allocators, a free list is just another kind of allocator that may be plugged in at any point.

    https://github.com/codr7/hacktical-c/tree/main/malloc1

    https://github.com/codr7/hacktical-c/tree/main/malloc2



    > You can only deallocate everything in the arena, not individual parts of it.

    Untrue; this excludes the middle. GNU Obstack is a widely-used arena allocator with a "free everything allocated after this point" operation, among others.



    This is arguing about the definition of an Arena.

    It’s like if I say a stack only has push and pop, and you tell me you can search though and find another element.



    It's really not. The multi-chunk arena it describes at the end of the article is exactly the same structure as GNU obstack, except that obstack also bothers to implement the free-to-mark operation.

    It's trivial to provide the operation for any arena, finite (just set the end pointer to the given value) or chained (a little more work since you have to free chunks until the former succeeds). The operation is O(1) if you're within the last chunk or two (and if you're tuning your chunk size well, you usually will be), O(log n) if your chunks increase in size as you go, or O(n) if all your chunks are the same size (not necessarily a bad idea for some use cases), where n is the number of chunks you're freeing (much smaller than the number of bump allocations).



    It'll depend on the allocator. Generally speaking, the better an allocator is at minimizing fragmentation, the slower it's going to be with new allocations.

    That's why bump allocators are wicked fast, but make the trade-off the article mentions.



    PostScript does something similar with its save and restore operators.






    Join us for AI Startup School this June 16-17 in San Francisco!


    Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



    Search:
    联系我们 contact @ memedata.com