Difference between brk (), sbrk () and realloc () functions

As I know, the brk (), sbrk () functions are used to reallocate memory. But how do they differ from the realloc () function ?. Present me coding examples.

+5
source share
2 answers

At the OS level (at least in the Unix model), your program has one large area of ​​memory for program text, initialized and uninitialized data, and a heap for dynamically distributed data. (The stack is separate.) You can adjust the size of this region using brk and sbrk , but you cannot change it, and it is always contiguous. The vast majority of programs that deal with dynamic memory allocation require something more flexible.

malloc , free and realloc are C library functions that give you something more flexible. Under them, they get memory from the OS, calling brk and / or sbrk , but then they do additional processing to allocate (a) any number of fragments (b) of different sizes and which you can (c) individually return to the pool when you are done with them, and by the way (d) resize.

But when you return memory to the pool using free , it usually just returns to the pool, which future calls to malloc your program will draw; memory, as a rule, does not return back to the OS.

(Sorry for the lack of sample code, I don't have time for this just now.)

+4
source

brk and sbrk are system calls (implemented in the kernel), and malloc , free , realloc are library functions in user space. So the malloc functions, etc. Use brk and sbrk internally, but provide additional functionality (see man (2) for more information on brk and man (3) for more information on malloc .).

brk tells the kernel how much memory your program wants to use by pointing to the kernel a pointer to the largest area of ​​virtual memory that your program can use. But you only have one big chunk of memory.

malloc will help you split this huge block of memory into smaller parts.

The sample code here does not make much sense, because brk and malloc work at different levels. But you might think how to implement a very simple (and not thread safe) version of malloc and free and where you would use brk there:

  • The basic data structure for our primitive malloc is a linked list.
  • Each list item in the list contains:
    • Block size
    • Pointer to the next item
    • Flag if block is used
    • Flag if this is the last element
    • Array of bytes with a given size
  • In each call, malloc scans the list and checks each block.
    • If the block is marked as "not used"
    • If the size field of the list item does not exceed the size of the required
  • If malloc finds such a block, it will be:
    • Mark used list item
    • Adjust the size field in the list item
    • If there is enough space, add a list item after the item, pointing to the next item in the list (if applicable)
    • Returns a pointer to a byte array of the found list item
  • If malloc does not find such a list item, it will be
    • Call brk to increase the useful memory received from the kernel.
    • Add a new item to the end of the list, set the size to the desired size
    • Mark this item as used and as the last item
    • Returns a pointer to a byte array of the newly created list entry

And as @BasileStarynkevitch noted in his comment, as an alternative to brk you can also use mmap (with fd=-1 and flags=MAP_PRIVATE|MAP_ANONYMOUS ) to reserve one block of memory supported by the swap file. For more information on mmap, see man (2) .

+3
source

All Articles