gpt4 book ai didi

c - dlmalloc 中的 bin_at

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:35 25 4
gpt4 key购买 nike

在 glibc malloc.c 或 dlmalloc 中说“repositioning tricks”如 blew,并在 bin_at 中使用此技巧。

bins是一个数组,空间是在分配av(struct malloc_state)时分配的,不是吗? sizeof(bin[i]) 小于 sizeof(struct malloc_chunk*)?

调用bin_at(M,1)(用作unsorted_chunks)时,结果是:bin[0] - offsetof (struct malloc_chunk, fd) bin[0] - 8 对吗?

谁能为我描述一下这个技巧?我无法理解 bin_at 宏。为什么他们使用这种方法获取 bins 地址?它是如何工作的?

非常感谢,抱歉我的英语不好。

/*
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))

像这样的 malloc_chunk 结构:

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;
};

像这样的 bin 类型:

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;
};

最佳答案

大概是struct malloc_chunk看起来像:

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

...和 ​​->bins类型看起来像:

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

bin_at宏将指向后一个结构的指针变成指向前一个结构的伪指针,目的是访问 fd。和 bk仅限成员(因为它们是较小的成员中唯一存在的成员)。即bin_at(m, i)->fdbin_at(m, i)->bkm->bins[(i - 1) * 2].fd 相同和 m->bins[(i - 1) * 2].bk ,但是bin_at可以用在需要 struct malloc_chunk * 的地方(只要他们只使用 fdbk 成员)。

有点乱。我不会在您自己的代码中这样做 - 请记住 Kernighan 关于尽可能聪明地编写代码的建议:

"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


好的,所以 ->bins根本不是结构数组 - 它是 struct malloc_chunk * 的数组.

注意 ->bins[(i - 1) * 2]指的是 struct malloc_chunk * 的第 i 个 ->bins 中的指针大批。这对相当于 fdbk struct malloc_chunk 中的一对指针, 第一个 ( ->bins[(i - 1) * 2] ) 相当于 fd (他们本可以将 ->bins 设为我在上面建议的较小结构的数组;它在功能上是等效的,而且可能更清晰)。

bin_at宏让代码插入 ->bins 中的一对指针。数组转换为 struct malloc_chunk 的链表结构 - 不分配整个 struct malloc_chunk .这就是他们所说的节省空间。

bin_at宏将索引放入 bins数组,然后执行“如果此指针实际上是 fd 中的 struct malloc_chunk 值,则计算指向 struct malloc_chunk 所在位置的指针”。它通过减去 fd 的偏移量来实现。 struct malloc_chunk 中的成员来自 bins 中项目的地址数组。

它并没有真正“找到 bins[i]”——这很简单 (&bins[i])。它实际上定位了虚构的 struct malloc_chunkbins[i]fd的成员。

抱歉,解释起来很复杂,因为这是一个复杂的概念。

关于c - dlmalloc 中的 bin_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2731806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com