《Glibc内存管理》笔记DAY3

边界标记法

/* conversion from malloc headers to user pointers, and back */
#define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ))
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
  • chunk2mem(p):根据 chunk 地址获得返回给用户的内存地址
  • mem2chunk(mem):根据 mem 地址得到 chunk 地址
/* The smallest possible chunk */
#define MIN_CHUNK_SIZE (offsetof(struct malloc_chunk, fd_nextsize))
  • fd_nextsize 指针距离 malloc_chunk 结构体开头的字节偏移量,也就是最小的chunk的大小。(32 位平台上位 16 字节,64 位平台为 24 字节或是 32 字节。)
/* The smallest size we can malloc is an aligned minimal chunk */
#define MINSIZE 
 (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))

  • 这个式子的含义是求 MIN_CHUNK_SIZE 在 MALLOC_ALIGN_MASK + 1 的最小上界,MALLOC_ALIGN + 1 等于 MALLOC_ALIGNMENT ,所以这个式子的意思便是求满足字节对齐最小的可 malloc 分配的 chunk 。
/* Check if m has acceptable alignment */
#define aligned_OK(m) (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0)
#define misaligned_chunk(p) 
 ((uintptr_t)(MALLOC_ALIGNMENT == 2 * SIZE_SZ ? (p) : chunk2mem (p)) 
 & MALLOC_ALIGN_MASK)
  • intptr_t:其长度总是所在平台的位数,用来存放地址。
  • uintptr_t 是intptr_t 的无符号版本。
  • aligned_OK(m):m判断是否对齐
  • misaligned_chunk(p):实现p的对齐
/*
 Check if a request is so large that it would wrap around zero when
 padded and aligned. To simplify some other code, the bound is made
 low enough so that adding MINSIZE will also not wrap around zero.
*/
#define REQUEST_OUT_OF_RANGE(req) 
 ((unsigned long)(req) >= 
 (unsigned long)(INTERNAL_SIZE_T)(-2 * MINSIZE))

/* pad request bytes into a usable size -- internal version */
#define request2size(req) 
 (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? 
 MINSIZE : 
 ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)

/* Same, except also perform argument check */
#define checked_request2size(req, sz) 
 if (REQUEST_OUT_OF_RANGE(req)) { 
 MALLOC_FAILURE_ACTION; 
 return 0; 
 } 
 (sz) = request2size(req);
  • REQUEST_OUT_OF_RANGE(req):如果申请大小加上两个最小块大小大于等于 unsigned long 的长度则置 1,否则置 0。
  • request2size(req):将用户请求的大小转换成实际分配的大小,SIZE_SZ是下一个chunk的prev_size域的空间复用。
  • checked_request2size(req, sz):判断申请大小是否溢出,没有溢出的情况下sz记录实际分配的大小。
/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
#define PREV_INUSE 0x1

/* extract inuse bit of previous chunk */
#define prev_inuse(p) ((p)->size & PREV_INUSE)

/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
#define IS_MMAPPED 0x2

/* check for mmap()'ed chunk */
#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)

/* size field is or'ed with NON_MAIN_ARENA if the chunk was obtained
 from a non-main arena. This is only set immediately before handing
 the chunk to the user, if necessary. */
#define NON_MAIN_ARENA 0x4

/* check for chunk from non-main arena */
#define chunk_non_main_arena(p) ((p)->size & NON_MAIN_ARENA)
  • prev_inuse(p):为 0 则表示前一个 chunk 为空闲,为 1 表示前一个 chunk 正在使用。
  • chunk_is_mmapped(p):为 1 表示该 chunk 是从 mmap 映射区域分配的,否则是从 heap 区域分配的。
  • chunk_non_main_arena(p):为 1 表示该 chunk 属于非分配区,为 0 表示该 chunk 属于主分配区。
/*
 Bits to mask off when extracting size
 Note: IS_MMAPPED is intentionally not masked off from size field in
 macros for which mmapped chunks should never be seen. This should
 cause helpful core dumps to occur if it is tried by accident by
 people extending or adapting this malloc.
*/
#define SIZE_BITS (PREV_INUSE|IS_MMAPPED|NON_MAIN_ARENA)

/* Get size, ignoring use bits */
#define chunksize(p) ((p)->size & ~(SIZE_BITS))

/* Ptr to next physical malloc_chunk. */
#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~SIZE_BITS) ))

/* Ptr to previous physical malloc_chunk */
#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))

/* Treat space at ptr + offset as a chunk */
#define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
  • chunksize(p):获取size域低3位的值。
  • next_chunk(p):获取下一个chunk的地址。
  • prev_chunk(p):获取上一个chunk的地址。
  • chunk_at_offset(p, s):将 p + s 的地址强制看作一个chunk。
/* extract p's inuse bit */
#define inuse(p)
((((mchunkptr)(((char*)(p))+((p)->size & ~SIZE_BITS)))->size) & PREV_INUSE)

/* set/clear chunk as being inuse without otherwise disturbing */
#define set_inuse(p)
((mchunkptr)(((char*)(p)) + ((p)->size & ~SIZE_BITS)))->size |= PREV_INUSE

#define clear_inuse(p)
((mchunkptr)(((char*)(p)) + ((p)->size & ~SIZE_BITS)))->size &= ~(PREV_INUSE)
  • inuse(p):获取当前 chunk 是否使用的标志位,为 1 正在使用,为 0 chunk 空闲。
  • set_inuse(p):将当前 chunk 的是否使用标志位置 1。
  • clear_inuse(p):将当前 chunk 的是否使用标志位置 0。
/* check/set/clear inuse bits in known places */
#define inuse_bit_at_offset(p, s)
(((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)

#define set_inuse_bit_at_offset(p, s)
(((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)

#define clear_inuse_bit_at_offset(p, s)
(((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
  • inuse_bit_at_offset(p, s):获取指定 chunk 的 size 域的使用标志位
  • set_inuse_bit_at_offset(p, s):将指定 chunk 的 size 域的使用标志位置 1。
  • clear_inuse_bit_at_offset(p, s):将指定 chunk 的 size 域的使用标志位置 0。
/* Set size at head, without disturbing its use bit */
#define set_head_size(p, s) ((p)->size = (((p)->size & SIZE_BITS) | (s)))

/* Set size/use field */
#define set_head(p, s) ((p)->size = (s))

/* Set size at footer (only when chunk is not in use) */
#define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s)) 
  • set_head_size(p, s):设置当前 chunk p 的 size 域并保留 size 域的控制信息。
  • set_head(p, s):设置当前 chunk p 的 size 域并忽略已有的 size 域控制信息。
  • set_foot(p, s):设置当前 chunk p 的下一个 chunk 的 prev_size 为 s,s 为当前 chunk 的 size,只有当 chunk p 为空闲时才能使用这个宏,当前 chunk 的 foot 的内存空间存在于下一个 chunk,即下一个 chunk 的 prev_size。

内容来源

《Glibc内存管理》

原文地址:https://www.cnblogs.com/luoleqi/p/11973523.html