展示 HN:我用 C 写了一个极简的内存分配器
Show HN: I wrote a minimal memory allocator in C

原始链接: https://github.com/t9nzin/memory

本项目实现了一个自定义的C内存分配器,从头开始实现了`malloc`、`calloc`、`realloc`和`free`。它利用`sbrk`进行较小的分配,利用`mmap`进行较大的分配,旨在通过分块和合并空闲块来减少碎片。 该分配器专为符合POSIX标准(Linux、macOS)的系统设计,并使用`Makefile`构建,包含测试和基准测试。一个示例程序展示了它的用法,包括分配、调整大小和释放内存。 **重要限制:** 该分配器**不是线程安全的**,并发访问会导致未定义行为。虽然在macOS上可用,但`sbrk`在该平台已被弃用。它也缺乏碎片整理功能。 详细文档可在配套博客文章中找到,该项目采用MIT许可,欢迎贡献。作者感谢Dan Luu的malloc教程作为关键参考。

## 最小C内存分配器 - 摘要 一位开发者分享了一个用C编写的“玩具”级最小内存分配器,并附带一篇博客文章(约20分钟阅读量)解释了实现细节(可在GitHub上找到:[github.com/t9nzin](https://github.com/t9nzin))。该项目旨在演示内存分配原理,但并非线程安全的。 讨论集中在博客对内存作为简单字节数组的解释是否准确,评论者指出缓存行和内存组对性能的重要性。有人对使用`sbrk()`表示担忧,因为它在碎片化和潜在失败方面存在局限性,并建议使用`mmap()`作为更好的替代方案。 许多评论者提出了改进建议,包括对局部变量使用`static`,为清晰起见添加`const`,以及改进`realloc()`函数。 该项目引发了关于重新发明内存分配的价值的争论,一些人提倡新的API设计,而另一些人则质疑自定义头文件模仿C标准库的必要性。
相关文章

原文

A custom implementation of malloc, calloc, realloc, and free in C.

This project implements a memory allocator from scratch using sbrk for small allocations and mmap for large allocations. It includes optimizations like block splitting to reduce fragmentation and coalescing to merge adjacent free blocks. Please note that this allocator is not thread-safe. Concurrent calls to malloc/free/realloc will cause undefined behavior. I've also written a blog post (~20 minute read) explaining step by step the process behind writing this memory allocator project, if that's of interest, you can read it here!

  • GCC compiler
  • Make
  • POSIX-compliant system (Linux, macOS) because we're using sbrk() and mmap() <- won't work on Windows
make           # build everything
make tests     # run tests
make bench     # run benchmark

Using the library in your own code

  1. Build the static library:

  2. Include the header in your C file:

  3. Compile your program with the library:

    gcc -I./include examples/my_program.c -L./build -lallocator -o my_program
  4. Run the compiled program

#include <stdio.h>
#include "allocator.h"

int main() {
    int *arr = malloc(10 * sizeof(int));
    if (arr == NULL) {
        return 1;
    }
    
    for (int i = 0; i < 10; i++) {
        arr[i] = i;
    }
    
    arr = realloc(arr, 20 * sizeof(int));
    
    free(arr);
    return 0;
}
.
├── Makefile
├── README.md
├── examples
│   └── my_program.c        # an example program using implemented malloc()
├── include
│   └── allocator.h         # header file w/ function declarations
├── src
│   └── allocator.c         # allocator
└── tests
    ├── benchmark.c         # performance benchmarks
    ├── test_basic.c        # basic functionality tests
    └── test_edge_cases.c   # edge cases and stress tests 
  • Not thread-safe (no mutex protection)
  • fyi: sbrk is deprecated on macOS but still functional
  • No defragmentation or compaction

MIT License :D

Contributions are welcome. Please open an issue or submit a pull request.

@t9nzin

I credit Dan Luu for his fantastic malloc() tutorial which I greatly enjoyed reading and served as a helpful reference for this project. If you would like to take a look at his tutorial (which I highly recommend), you can find it here.

I'd also like to thank Joshua Zhou and Abdul Fatir for reading over and giving me great feedback on the accompanying blog post for this project.

联系我们 contact @ memedata.com