展示 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教程作为关键参考。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 展示 HN: 我用 C 写了一个极简的内存分配器 (github.com/t9nzin) 12 分,作者 t9nzin 1 小时前 | 隐藏 | 过去 | 收藏 | 讨论 一个有趣的玩具内存分配器(非线程安全,未来待办事项)。我还想解释我的方法,所以我写了一篇教程博客文章(约 20 分钟阅读),其中涵盖了代码,你可以在 README 中找到链接。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系方式 搜索:
相关文章

原文

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