Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 753277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:52:42+00:00 2026-05-14T14:52:42+00:00

In glibc malloc.c or dlmalloc It said repositioning tricks As in blew, and use

  • 0

In glibc malloc.c or dlmalloc It said “repositioning tricks“As in blew, and use this trick in bin_at.

bins is a array,the space is allocated when av(struct malloc_state) is allocated.doesn’t it? the sizeof(bin[i]) is less then sizeof(struct malloc_chunk*)?

When bin_at(M,1)(which is used as unsorted_chunks) is called,the result is: bin[0] – offsetof (struct malloc_chunk, fd) bin[0] – 8 is right?

Who can describe this trick for me? I can’t understand the bin_at macro.why they get the bins address use this method?how it works?

Very thanks,and sorry for my poor English.

/*
     To simplify use in double-linked lists, each bin header acts
    as a malloc_chunk. This avoids special-casing for headers.
    But to conserve space and improve locality, we allocate
    only the fd/bk pointers of bins, and then use repositioning tricks
    to treat these as the fields of a malloc_chunk*.
*/

typedef struct malloc_chunk* mbinptr;

/* addressing -- note that bin_at(0) does not exist */
#define bin_at(m, i) \
  (mbinptr) (((char *) &((m)->bins[((i) - 1) * 2]))               \
         - offsetof (struct malloc_chunk, fd))

The malloc_chunk struct like this:

struct malloc_chunk {

  INTERNAL_SIZE_T      prev_size;  /* Size of previous chunk (if free).  */
  INTERNAL_SIZE_T      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;
};

And the bin type like this:

typedef struct malloc_chunk* mbinptr;

struct malloc_state {
  /* Serialize access.  */
  mutex_t mutex;

  /* Flags (formerly in max_fast).  */
  int flags;

#if THREAD_STATS
  /* Statistics for locking.  Only used if THREAD_STATS is defined.  */
  long stat_lock_direct, stat_lock_loop, stat_lock_wait;
#endif

  /* Fastbins */
  mfastbinptr      fastbinsY[NFASTBINS];

  /* Base of the topmost chunk -- not otherwise kept in a bin */
  mchunkptr        top;

  /* The remainder from the most recent split of a small request */
  mchunkptr        last_remainder;

  /* Normal bins packed as described above */
  mchunkptr        bins[NBINS * 2 - 2];

  /* Bitmap of bins */
  unsigned int     binmap[BINMAPSIZE];

  /* Linked list */
  struct malloc_state *next;

#ifdef PER_THREAD
  /* Linked list for free arenas.  */
  struct malloc_state *next_free;
#endif

  /* Memory allocated from the system in this arena.  */
  INTERNAL_SIZE_T system_mem;
  INTERNAL_SIZE_T max_system_mem;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:52:43+00:00Added an answer on May 14, 2026 at 2:52 pm

    Presumably struct malloc_chunk looks something like:

    struct malloc_chunk {
        /* ... fields here ... */
        struct malloc_chunk *fd;
        struct malloc_chunk *bk;
        /* ... more fields here ... */
    };
    

    …and the ->bins type looks like:

    struct {
        struct malloc_chunk *fd;
        struct malloc_chunk *bk;
    };
    

    The bin_at macro makes a pointer to the latter structure into a fake pointer to the former structure, for the purpose of accessing the fd and bk members only (since they’re the only ones that exist in the smaller one). ie bin_at(m, i)->fd and bin_at(m, i)->bk are the same as m->bins[(i - 1) * 2].fd and m->bins[(i - 1) * 2].bk, but bin_at can be used in places that expect a struct malloc_chunk * (as long as they only use the fd and bk members).

    It’s a bit of a hack. I wouldn’t do this in your own code – remember Kernighan’s advice about writing code as cleverly as possible:

    “Debugging is twice as hard as writing
    the code in the first place.
    Therefore, if you write the code as
    cleverly as possible, you are, by
    definition, not smart enough to debug
    it.” – Brian W. Kernighan


    OK, so ->bins isn’t an array of structs at all – it’s an array of struct malloc_chunk *.

    Notice that ->bins[(i - 1) * 2] refers to the i-th pair of struct malloc_chunk * pointers in the ->bins array. This pair is equivalent to the fd and bk pair of pointers in a struct malloc_chunk, with the first (->bins[(i - 1) * 2]) being equivalent to fd (they could have instead made ->bins an array of the smaller struct I suggested above; it would be functionally equivalent and probably clearer).

    The bin_at macro lets the code insert one of those pairs of pointers that are in the ->bins array into a linked list of struct malloc_chunk structs – without allocating an entire struct malloc_chunk. This is the space saving they are talking about.

    The bin_at macro takes a index into the bins array, then does “if this pointer was actually the fd value in a struct malloc_chunk, then calculate a pointer to where that struct malloc_chunk would be”. It does this by subtracting the offset of the fd member within a struct malloc_chunk from the address of the item in the bins array.

    It doesn’t really “locate the bins[i]” – that’s straightforward (&bins[i]). It actually locates the imaginary struct malloc_chunk that bins[i] is the fd member of.

    Sorry, it’s complicated to explain because it’s a complicated concept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

* glibc detected * malloc(): memory corruption (fast): *** This is the error I
Possible Duplicate: Malloc thread-safe? I heard that glibc malloc() was not thread safe, since
I'm getting *** glibc detected *** (/my/program/...): malloc(): memory corruption: 0xf28000fa *** I've run
I would like to use OpenBSD's implementation of malloc, realloc and free on my
Have run into a bug with glibc's malloc(): http://sourceware.org/bugzilla/show_bug.cgi?id=4349 and am thinking a work
We have a new application that requires glibc 2.4 (from gcc 4.1). The machine
I know of the following: The venerable getopt(3) The extended getopt_long glibc's argp parser
Notice these two RedHat Linux system configuration settings: $ getconf GNU_LIBC_VERSION glibc 2.3.4 $
Per man pages, snprintf is returning number of bytes written from glibc version 2.2
A call to clear on a QByteArray generates the following exception: * glibc detected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.