再入门一下linux堆
date
Mar 3, 2020
URL
slug
heap-start
status
Published
tags
二进制
summary
老文章迁移
type
Post
关于堆的常识性内容
在程序运行过程中,堆可以提供动态分配的内存,允许程序申请大小未知的内存。堆其实就是程序虚拟地址空间的一块连续的线性区域,它由低地址向高地址方向增长。我们一般称管理堆的那部分程序为堆管理器。
Linux的堆是glibc集成ptmalloc2实现的,主要是通过 malloc/free 函数来分配和释放内存块
malloc
malloc(size_t n) size_t 无符号数
动态分配内存,用多少分配多少,不会浪费资源。
free
free 函数会释放由 p 所指向的内存块 这个内存块有可能是通过 malloc 函数得到的,也有可能是通过相关的函数 realloc 得到的。
此外,该函数也同样对异常情况进行了处理
- 当 p 为空指针时,函数不执行任何操作。
- 当 p 已经被释放之后,再次释放会出现乱七八糟的效果,这其实就是
double free
。
- 除了被禁用 (mallopt) 的情况下,当释放很大的内存空间时,程序会将这些内存空间还给系统,以便于减小程序所使用的内存空间。
malloc 和 free并不是最底层与系统交互的函数
这些函数背后的系统调用主要是 (s)brk 函数以及 mmap, munmap 函数。
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F3ce586f3-1b26-4e04-b09f-a43864e46771%2FUntitled.png?table=block&id=72367a5c-493c-4633-95f1-c01eccc2363c&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F11f5dedb-ce06-4bc2-934e-5fe2e0f7cf01%2FUntitled.png?table=block&id=93cd3f69-6d38-43e4-9ec7-06a81d342d03&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fd7dadfb3-2869-4cfe-8043-ad468429f5e0%2FUntitled.png?table=block&id=6d67f978-6b3c-4ba5-8d50-656638af89ba&cache=v2)
prev_size 如果连续的上一个chunk为free状态,则prev_size 存有上一块chunk的size 包括header
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F605be5c0-6418-41c1-8505-e0bd0f683918%2FUntitled.png?table=block&id=f020eca6-8f48-4102-81c0-9c8289548e13&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ff0967937-284d-4592-a23c-5d127550d510%2FUntitled.png?table=block&id=b5b9019f-2960-4688-97ee-02693b6588ef&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fe3733504-b6b6-467f-b63e-4db0f5225b98%2FUntitled.png?table=block&id=c564d2e5-4fd5-46d9-a7aa-cd5bf70218d0&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F886f25fe-0604-42b8-a4e3-7b0eeedf51c3%2FUntitled.png?table=block&id=d2245ff4-f603-4656-acc9-46bb1180a9ca&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F3e345f50-6d36-4937-a1a5-7ce2f73f4687%2FUntitled.png?table=block&id=ea6bd878-8501-484e-a06d-e0228af5c849&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4b907045-99ec-4423-b789-5b01f8626998%2FUntitled.png?table=block&id=e9826e09-c3e1-4197-81db-661ac1e51e73&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fe00b0706-2567-49b7-a52d-42ddf2944eb5%2FUntitled.png?table=block&id=82c92a98-59bb-467f-997f-4781a625916c&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fda3ae0de-b757-4e47-a045-1efe3b9057b6%2FUntitled.png?table=block&id=6df5f194-54be-494e-9370-0afa57b73d55&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F11906133-3fca-464a-ab48-0dca1c3b3f33%2FUntitled.png?table=block&id=0f4a0dec-7d6c-4284-a8a8-1e3023350357&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F855645f0-8dce-4a70-bb06-55a13fbd8c92%2FUntitled.png?table=block&id=b03fb1e3-d1ab-4325-9ae2-07a276e2bb7c&cache=v2)