相关代码是这样的:
typedef unsigned long int chunk_head;
typedef struct malloc_chunk
{
// Contains the size of the data in the chunk and the flag byte.
chunk_head head;
// Deliberately left unsized to allow overflow.
// Contains the payload of the chunk.
unsigned int data[];
};
举个例子,“get”宏是这样的:
//Get the size of the data contained within the chunk.
#define GET_CHUNK_SIZE(chunk) ((chunk.head) & 0xFFFFFF)
我使用标志位的高位字节——“inuse”和“can be coalesced”,以及我发现的任何其他字节都是有用的。
现在我已经完成了提供背景信息,正如我在标题中所述,我需要能够将低 3 个字节更改为块的大小。我最初的直觉是按位 AND 标头与大小,因为它会正确对齐,但后来我意识到它也可能覆盖标志字节,因为它会自动添加零,直到它的大小与 long 匹配。我什至不确定您是否可以按位 AND 一个 int 和一个 long。无论如何,非常感谢帮助。