sbrk brk 函数区分

时间:2022-12-28 01:15:49

1. brk:是系统调用接口:

内核的syscall_table.s 中定义:
    ENTRY_SAME(brk)

例如:

在malloc函数实现中有调用brk

malloc 函数调用链中:

malloc-->expand_heap-->__expand_heap

即在__expand_heap调用系统brk函数:

 __syscall(SYS_brk, 0);

注意:malloc中,当if (n > MMAP_THRESHOLD) 一般是128K,使用mmap分配内存了,否则使用brk分配内存。

2. sbrk:是glibc封装的一个应用函数,sbrk其实是调用系统接口brk:

glibc的sbrk.c文件中:

void *sbrk(intptr_t inc)

{

    if (inc) return (void *)__syscall_ret(-ENOMEM);

    return (void *)__syscall(SYS_brk, 0);

}

3. 附上__expand_heap和__simple_malloc简短代码实现:

/* Expand the heap in-place if brk can be used, or otherwise via mmap,

 * using an exponential lower bound on growth by mmap to make

 * fragmentation asymptotically irrelevant. The size argument is both

 * an input and an output, since the caller needs to know the size

 * allocated, which will be larger than requested due to page alignment

 * and mmap minimum size rules. The caller is responsible for locking

 * to prevent concurrent calls. */

static void *__expand_heap(size_t *pn)

{

    static uintptr_t brk;

    static unsigned mmap_step;

    size_t n = *pn;

    if (n > SIZE_MAX/2 - PAGE_SIZE) {

        errno = ENOMEM;

        return 0;

    }

    n += -n & PAGE_SIZE-1;

    if (!brk) {

        brk = __syscall(SYS_brk, 0);

        brk += -brk & PAGE_SIZE-1;

    }

    if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)

        && __syscall(SYS_brk, brk+n)==brk+n) {

        *pn = n;

        brk += n;

        return (void *)(brk-n);

    }

    size_t min = (size_t)PAGE_SIZE << mmap_step/2;

    if (n < min) n = min;

    void *area = __mmap(0, n, PROT_READ|PROT_WRITE,

        MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

    if (area == MAP_FAILED) return 0;

    *pn = n;

    mmap_step++;

    return area;

}

-----------------------------------------------------------------------------

static void *__simple_malloc(size_t n)

{

    static uintptr_t brk, cur, end;

    static unsigned mmap_step;

    size_t align=1;

    void *p;

    if (n > SIZE_MAX/2) {

        errno = ENOMEM;

        return 0;

    }

    if (!n) n++;

    while (align<n && align<ALIGN)

        align += align;

    LOCK(lock);

    cur += -cur & align-1;

    if (n > end-cur) {

        size_t req = n - (end-cur) + PAGE_SIZE-1 & -PAGE_SIZE;

        if (!cur) {

            brk = __syscall(SYS_brk, 0);

            brk += -brk & PAGE_SIZE-1;

            cur = end = brk;

        }

        if (brk == end && req < SIZE_MAX-brk

            && !traverses_stack_p(brk, brk+req)

            && __syscall(SYS_brk, brk+req)==brk+req) {

            brk = end += req;

        } else {

            int new_area = 0;

            req = n + PAGE_SIZE-1 & -PAGE_SIZE;

            /* Only make a new area rather than individual mmap

             * if wasted space would be over 1/8 of the map. */

            if (req-n > req/8) {

                /* Geometric area size growth up to 64 pages,

                 * bounding waste by 1/8 of the area. */

                size_t min = PAGE_SIZE<<(mmap_step/2);

                if (min-n > end-cur) {

                    if (req < min) {

                        req = min;

                        if (mmap_step < 12)

                            mmap_step++;

                    }

                    new_area = 1;

                }

            }

            void *mem = __mmap(0, req, PROT_READ|PROT_WRITE,

                MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

            if (mem == MAP_FAILED || !new_area) {

                UNLOCK(lock);

                return mem==MAP_FAILED ? 0 : mem;

            }

            cur = (uintptr_t)mem;

            end = cur + req;

        }

    }

    p = (void *)cur;

    cur += n;

    UNLOCK(lock);

    return p;

}