Why does malloc (1) give more than one page size?

I tried in my machine using sbrk (1), and then deliberately disabled the size of the test page, which is 4096 bytes. But when I call malloc (1), I get SEGV after accessing 135152 bytes, which is more than one page size. I know that malloc is a library function and it is implementation dependent, but given that it calls sbrk in the end why it gives more than one page size. Can someone tell me about their inner work?

My operating system is ubuntu 14.04 and my architecture is x86

Update: now I wonder if this is because malloc returns the address in a free list that is large enough to hold my data. But this address may be in the middle of the heap so that I can write until the upper limit of the heap is reached.

+4
source share
2 answers

malloc() UNIX sbrk()/brk(). mmap() sbrk(). glibc malloc() (, , Ubuntu 14.04) sbrk() mmap(), , , , glibc .

glibc sbrk() mmap(). M_MMAP_THRESHOLD. 128K. , 135152 , ~ 128K. , 1 , 128 . , segfault , .

M_MAP_THRESHOLD mallopt(), .

M_MMAP_THRESHOLD

, ( ) M_MMAP_THRESHOLD, mmap (2) sbrk (2).

mmap (2) : . ( , .) , mmap (2): ; mmap (2) ; ​​ , (2). 128 * 1024 M_MMAP_THRESHOLD.

0. DEFAULT_MMAP_THRESHOLD_MAX: 512 * 1024 32- 4 * 1024 * 1024 * sizeof (long) 64- .

. glibc mmap. 128 * 1024, DEFAULT_MMAP_THRESHOLD_MAX , . mmap , mmap. mmap , - M_TRIM_THRESHOLD, M_TOP_PAD, M_MMAP_THRESHOLD M_MMAP_MAX .

, :

#include<malloc.h>

mallopt(M_MMAP_THRESHOLD, 0);

malloc(), , , . - , C , undefined , . - ; -)

+6

malloc . malloc , . .

:

, - , , brk() sbrk(). , brk(), , , . , malloc(). malloc() brk(). .

, malloc mmap brk/sbrk , .

+3

Source: https://habr.com/ru/post/1619693/


All Articles