Search code examples
cmallocglibc

What abbr is "bk" member within malloc.c?


My question is about glibc. What is the name of the bk member an abbreviation for within malloc_chunk?

struct malloc_chunk {
  INTERNAL_SIZE_T      mchunk_prev_size;  /* Size of previous chunk (if free).  */
  INTERNAL_SIZE_T      mchunk_size;       /* Size in bytes, including overhead. */
  struct malloc_chunk* fd;         /* double links -- used only if free. */
  struct malloc_chunk* bk;
  /* Only used for large blocks: pointer to next larger size.  */
  struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
  struct malloc_chunk* bk_nextsize;
};

Refer to: https://code.woboq.org/userspace/glibc/malloc/malloc.c.html#malloc_chunk


Solution

  • The fd and bk members are the forward and backward pointers in the doubly-linked list.

    In other words, fd is the next pointer and bk is the previous pointer, as illustrated in the following three-element list:

    forward links -->
                           _(fd)_        _(fd)_
                          /      \      /      \      _(fd)-> NULL
                         /        V    /        V    /
          head ---> item1         item2         item3
                   /    ^        /    ^        /
      NULL <-(bk)_/      \_(bk)_/      \_(bk)_/
    
    <-- backward links
    

    Note that this particular example has a head and tail, with end element pointing to NULL. That's probably not the case for the circular list maintained by glibc (since it's a circular list), it's just used to show one method of implementation.


    In fact, the link you provide in your question states this in no uncertain terms, starting fifty-odd lines beyond the structure definition (slightly paraphrased):

    Free chunks are stored in circular
        doubly-linked lists, and look
        like this:
    +------------------------------------------+
    | Size of previous chunk, if unallocated   | <- mchunk_prev_size
    +------------------------------------------+
    | Size of chunk, in bytes (and some flags) | <- mchunk_size
    +------------------------------------------+
    | Forward pointer to next chunk in list    | <- fd
    +------------------------------------------+
    | Back pointer to previous chunk in list   | <- bk
    +------------------------------------------+
    :               <other stuff>              :