简单的c malloc

发布于 2024-09-24 12:04:20 字数 446 浏览 1 评论 0原文

虽然 C/C++ 有许多不同的复杂的 malloc / free 实现,但我正在寻找一种非常简单且(特别是)小型的、可以在固定的环境下工作的实现。 -size 缓冲区并支持realloc。不需要线程安全等,而且我的对象很小并且大小变化不大。您可以推荐任何实施吗?

编辑

我将在接收器处使用该通信缓冲区的实现来传输可变大小的对象(接收器未知)。分配的对象不会存活很长时间,但可能有多个对象同时使用。

由于每个人似乎都推荐标准 malloc,我也许应该重新表述我的问题。我需要的是在缓冲区之上的 malloc 的“最简单”实现,我可以开始根据自己的需求进行优化。也许最初的问题不清楚,因为我不是在寻找优化的 malloc,只是为了一个简单的。我不想从 glibc-malloc 开始并扩展它,而是想从一个轻量级的开始。

While there are lots of different sophisticated implementations of malloc / free for C/C++, I'm looking for a really simple and (especially) small one that works on a fixed-size buffer and supports realloc. Thread-safety etc. are not needed and my objects are small and do not vary much in size. Is there any implementation that you could recommend?

EDIT:

I'll use that implementation for a communication buffer at the receiver to transport objects with variable size (unknown to the receiver). The allocated objects won't live long, but there are possibly several objects used at the same time.

As everyone seems to recommend the standard malloc, I should perhaps reformulate my question. What I need is the "simplest" implementation of malloc on top of a buffer that I can start to optimize for my own needs. Perhaps the original question was unclear because I'm not looking for an optimized malloc, only for a simple one. I don't want to start with a glibc-malloc and extend it, but with a light-weight one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

宛菡 2024-10-01 12:04:20

克宁汉和Ritchie 似乎在他们的 C 书中提供了一个小的 malloc / free - 这正是我正在寻找的 (此处重新实现)。我只会添加一个简单的重新分配。

我仍然很高兴获得与此一样简单和简洁的其他实现的建议(例如,使用双向链表)。

Kerninghan & Ritchie seem to have provided a small malloc / free in their C book - that's exactly what I was looking for (reimplementation found here). I'll only add a simple realloc.

I'd still be glad about suggestions for other implementations that are as simple and concise as this one (for example, using doubly-linked lists).

吻泪 2024-10-01 12:04:20

我推荐与编译器捆绑在一起的标准库附带的一个。

还应该注意的是,没有合法的方法来重新定义 malloc/free

I recommend the one that came with standard library bundled with your compiler.

One should also note there is no legal way to redefine malloc/free

最近可好 2024-10-01 12:04:20

编译器附带的 malloc/free/realloc 几乎肯定比您要插入的某些函数更好。

这是可能的改进固定大小对象的功能,但这通常不涉及尝试替换 malloc ,而是用 内存池。通常,您可以使用malloc来获取大块内存,您可以将其划分为适当大小的离散块,并管理这些块。

The malloc/free/realloc that come with your compiler are almost certainly better than some functions you're going to plug in.

It is possible to improve things for fixed-size objects, but that usually doesn't involve trying to replace the malloc but rather supplementing it with memory pools. Typically, you would use malloc to get a large chunk of memory that you can divide into discrete blocks of the appropriate size, and manage those blocks.

回眸一笑 2024-10-01 12:04:20

There's a relatively simple memory pool implementation in CCAN:

http://ccodearchive.net/info/antithread/alloc.html

This looks like fits your bill. Sure, alloc.c is 1230 lines, but a good chunk of that is test code and list manipulation. It's a bit more complex than the code you implemented, but decent memory allocation is complicated.

锦欢 2024-10-01 12:04:20

在我看来,你正在寻找一个内存池。 Apache 运行时库 有一个非常好的库,而且它也是跨平台的。

它可能不完全是轻量级的,但源代码是开放的,您可以对其进行修改。

It sounds to me that you are looking for a memory pool. The Apache Runtime library has a pretty good one, and it is cross-platform too.

It may not be entirely light-weight, but the source is open and you can modify it.

じ违心 2024-10-01 12:04:20

我通常不会用分配函数重新发明轮子,除非我的内存使用模式不受 malloc/etc 的支持。 内存可以划分为一个或多个预分配区域,每个预分配区域包含一个或两个 LIFO 堆(释放任何对象会释放同一堆中在其之后分配的所有对象)。在后一种情况的常见版本中,只有在释放任何内容时,所有内容都被释放;在这种情况下,malloc() 可以有效地重写为:

char *malloc_ptr;
void *malloc(int size)
{
  void *ret;
  ret = (void*)malloc_ptr;
  malloc_ptr += size;
  return ret;
}

每个分配的对象零字节的开销。自定义内存管理器用于 malloc() 不足的场景的一个示例是可变长度测试记录生成可变长度结果记录(可能更长或更短)的应用程序;该应用程序需要支持获取结果并在批次中添加更多测试。测试存储在从缓冲区底部开始的递增地址处,而结果存储在从顶部开始递减的地址处。作为一项后台任务,当前测试之后的测试将被复制到缓冲区的开头(由于只有一个指针用于读取测试进行处理,因此复制逻辑将根据需要更新该指针)。如果应用程序使用 malloc/free,测试和结果的交错分配可能会产生内存碎片,但使用的系统不存在这种风险。

I would generally not reinvent the wheel with allocation functions unless my memory-usage pattern either is not supported by malloc/etc. or memory can be partitioned into one or more pre-allocated zones, each containing one or two LIFO heaps (freeing any object releases all objects in the same heap that were allocated after it). In a common version of the latter scenario, the only time anything is freed, everything is freed; in such a case, malloc() may be usefully rewritten as:

char *malloc_ptr;
void *malloc(int size)
{
  void *ret;
  ret = (void*)malloc_ptr;
  malloc_ptr += size;
  return ret;
}

Zero bytes of overhead per allocated object. An example of a scenario where a custom memory manager was used for a scenario where malloc() was insufficient was an application where variable-length test records produced variable-length result records (which could be longer or shorter); the application needed to support fetching results and adding more tests mid-batch. Tests were stored at increasing addresses starting at the bottom of the buffer, while results were stored at decreasing addresses starting at the top. As a background task, tests after the current one would be copied to the start of the buffer (since there was only one pointer that was used to read tests for processing, the copy logic would update that pointer as required). Had the application used malloc/free, it's possible that the interleaving of allocations for tests and results could have fragmented memory, but with the system used there was no such risk.

她如夕阳 2024-10-01 12:04:20

呼应首先测量的建议,并且仅在性能糟糕时才进行专门研究 - 应该很容易抽象您的 malloc/free/reallocs,以便替换很简单。

鉴于专业平台,我无法评论运行时的有效性。如果您确实调查自己的对象池(请参阅其他答案)或小对象分配 Loki这个值得一看。第二个链接也对这个问题有一些有趣的评论。

Echoing advice to measure first and only specialize if performance sucks - should be easy to abstract your malloc/free/reallocs such that replacement is straightforward.

Given the specialized platform I can't comment on effectiveness of the runtimes. If you do investigate your own then object pooling (see other answers) or small object allocation a la Loki or this is worth a look. The second link has some interesting commentary on the issue as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文