Is the initial call to sbrk (0) on Linux to always return a value aligned to 8 bytes (or 4 in the case of 32-bit systems)

I studied the malloc implementation defined here:

http://www.inf.udec.cl/~leo/Malloc_tutorial.pdf .

The author creates a metadata structure that is naturally aligned on a 4 byte border, and then aligns the request for x bytes to a 4 byte border after the metadata structure, which effectively acts as a header for the block. The pdf states that since the metadata and the request are now aligned, the resulting data will be completely aligned. The result is obtained if the first call to sbrk () returns the base address for the heap that is aligned with a 4-byte boundary. Does sbrk () always return a 4-byte (or 8-byte in the case of 64-bit systems) address aligned on the initial call?

+4
source share
1 answer

brk sbrk , - . Mac OS X (, , BSD-) / , Linux , :

#include <unistd.h>
#include <stdio.h>

int main() {
        void *p;
        p = sbrk(0);
        printf("Initial brk: %p\n", p);
        p = sbrk(1); // Increase the brk (returns OLD brk!)
        p = sbrk(0); // Get the new brk
        printf("New brk: %p\n", p);

        return 0;
}

:

Initial brk: 0x602000
New brk: 0x602001

. man- Linux :

brk() sbrk() , ( ). ; .

BSS. - , , , .

, , modulo ( getpagesize).


:. . man- , brk sbrk sys_brk. mm/mmap.c ( mm/nommu.c , ). brk mm/mmap.c :

newbrk = PAGE_ALIGN(brk);

( "brk" , .) , ​​ ... : , , , , brk, - :

mm->brk = brk;

, , . 3.17.5 2.4.37, .

fs/binfmt_elf.c ( ELF) set_brk, "brk" (mm->start_brk). . fs/binfmt_aout.c, a.out fs/binfmt_som.c, SOM HP-UX ( ). fs/binfmt_flat.c, brk, ; . , , . , ELF, "" .

Glibc sys_brk sbrk. , glibc brk - , sys_brk __curbrk, sbrk .

+5

All Articles