“””
Write a memory allocation function (and corresponding free function)
that returns a block of memory aligned to the specified alignment value.
You may assume that the alignment is a power of 2 (1, 2, 4, 8, 16, …).
“””
这道谷歌底层系统题要求你编写一个支持任意 2 的幂次对齐的内存分配函数,同时还需要实现对应的 free。由于普通 malloc 并不能保证任意对齐,你通常要多申请一些字节,然后把返回指针往上调整到满足对齐的地址。同时要在对齐后的地址之前存一段元数据(通常是原始的 malloc 返回值),这样 free 时才能正确还原并释放原始内存。该题考察候选人对指针运算、内存布局、对齐规则以及系统级编程的理解。
正文完