malloc 实现?
我正在尝试为 C 实现 malloc
和 free
,但我不确定如何重用内存。我目前有一个如下所示的 struct
:
typedef struct _mem_dictionary {
void *addr;
size_t size;
int freed;
} mem_dictionary;
我的 malloc
如下所示:
void *malloc(size_t size) {
void *return_ptr = sbrk(size);
if (dictionary == NULL)
dictionary = sbrk(1024 * sizeof(mem_dictionary));
dictionary[dictionary_ct].addr = return_ptr;
dictionary[dictionary_ct].size = size;
dictionary[dictionary_ct].freed = 1;
dictionary_ct++;
return return_ptr;
}
当我释放内存时,我会将地址标记为 0
(这表明它是免费的)。在我的 malloc
中,我将使用 for 循环来查找数组中等于 0
的任何值,然后将内存分配给该地址。我有点困惑如何实现这个。
I'm trying to implement malloc
and free
for C, and I am not sure how to reuse memory. I currently have a struct
that looks like this:
typedef struct _mem_dictionary {
void *addr;
size_t size;
int freed;
} mem_dictionary;
My malloc
looks like this:
void *malloc(size_t size) {
void *return_ptr = sbrk(size);
if (dictionary == NULL)
dictionary = sbrk(1024 * sizeof(mem_dictionary));
dictionary[dictionary_ct].addr = return_ptr;
dictionary[dictionary_ct].size = size;
dictionary[dictionary_ct].freed = 1;
dictionary_ct++;
return return_ptr;
}
When I free memory, I would just mark the address as 0
(that would indicate that it is free). In my malloc
, I would then use a for loop to look for any value in the array to equal 0
and then allocate memory to that address. I'm kind of confused how to implement this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是保留空闲块的链表。在
malloc
中,如果列表不为空,则搜索足够大的块来满足请求并返回它。如果列表为空或者找不到这样的块,则调用 sbrk 从操作系统分配一些内存。在free
中,您只需将内存块添加到空闲块列表中即可。作为奖励,您可以尝试合并连续的释放块,并且可以更改选择要返回的块的策略(首先适合,最佳适合,...)。如果块大于请求,您还可以选择拆分块。一些示例实现(它未经测试,显然不是线程安全的,使用风险自负):
注意:
(n +align_to - 1) & ~ (align_to - 1)
是将n
舍入到大于n
的align_to
的最接近倍数的技巧。仅当align_to
为 2 的幂且取决于数字的二进制表示形式时,此功能才有效。当
align_to
是2的幂时,它只有一个位集,因此align_to - 1
具有所有最低位集(即align_to 的形式为 000...010...0,
将把align_to - 1
的形式为000...001...1
)。这意味着~ (align_to - 1)
已设置所有高位,而未设置低位(即,其形式为111...110...0
代码>)。所以 x & ~ (align_to - 1)x
的所有低位设置为零,并将其向下舍入到align_to
最接近的倍数。最后,将
align_to - 1
添加到size
确保我们向上舍入到align_to
最接近的倍数(除非size
> 已经是align_to
的倍数,在这种情况下我们想要获取size
)。The easiest way to do it is to keep a linked list of free block. In
malloc
, if the list is not empty, you search for a block large enough to satisfy the request and return it. If the list is empty or if no such block can be found, you callsbrk
to allocate some memory from the operating system. infree
, you simply add the memory chunk to the list of free block. As bonus, you can try to merge contiguous freed block, and you can change the policy for choosing the block to return (first fit, best fit, ...). You can also choose to split the block if it is larger than the request.Some sample implementation (it is not tested, and is obviously not thread-safe, use at your own risk):
Note:
(n + align_to - 1) & ~ (align_to - 1)
is a trick to roundn
to the nearest multiple ofalign_to
that is larger thann
. This only works whenalign_to
is a power of two and depends on the binary representation of numbers.When
align_to
is a power of two, it only has one bit set, and thusalign_to - 1
has all the lowest bit sets (ie.align_to
is of the form 000...010...0, andalign_to - 1
is of the form000...001...1
). This means that~ (align_to - 1)
has all the high bit set, and the low bit unset (ie. it is of the form111...110...0
). Sox & ~ (align_to - 1)
will set to zero all the low bits ofx
and round it down to the nearest multiple ofalign_to
.Finally, adding
align_to - 1
tosize
ensure that we round-up to the nearest multiple ofalign_to
(unlesssize
is already a multiple ofalign_to
in which case we want to getsize
).您不想将字典条目的
size
字段设置为零 - 您将需要该信息来重新使用。相反,仅在释放块时设置freed=1
。您无法合并相邻的块,因为可能存在对 sbrk() 的干预调用,因此这使这变得更容易。您只需要一个
for
循环来搜索足够大的已释放块:这不是一个很好的
malloc()
实现。事实上,大多数malloc
/free
实现都会为 malloc 返回的每个块分配一个小标头。例如,标头可能从比返回的指针少八 (8) 个字节的地址开始。在这些字节中,您可以存储指向拥有该块的mem_dictionary
条目的指针。这避免了free
中的O(N)操作。您可以通过实现已释放块的优先级队列来避免malloc()
中的 O(N)。考虑使用二项式堆,以块大小作为索引。You don't want to set the
size
field of the dictionary entry to zero -- you will need that information for re-use. Instead, setfreed=1
only when the block is freed.You cannot coalesce adjacent blocks because there may have been intervening calls to
sbrk()
, so that makes this easier. You just need afor
loop which searches for a large enough freed block:This is not a great
malloc()
implementation. In fact, mostmalloc
/free
implementations will allocate a small header for each block returned by malloc. The header might start at the address eight (8) bytes less than the returned pointer, for example. In those bytes you can store a pointer to themem_dictionary
entry owning the block. This avoids the O(N) operation infree
. You can avoid the O(N) inmalloc()
by implementing a priority queue of freed blocks. Consider using a binomial heap, with block size as the index.我从 Sylvain 的回复中借用了代码。他似乎错过了计算 free_block* ini 计算开销的大小。
总的来说,代码的工作原理是将此 free_block 作为标头添加到分配的内存中。
1. 当用户调用 malloc 时,malloc 返回有效负载的地址,就在该标头之后。
2. 当调用 free 时,计算块头的起始地址(通过从块地址中减去头大小)并将其添加到空闲块池中。
I am borrowing code from Sylvain's response. He seems to have missed calculating the size of the free_block* ini calculating the overhead.
In overall the code works by prepending this free_block as a header to the allocated memory.
1. When user calls malloc, malloc returns the address of the payload, right after this header.
2. when free is called, the address of the starting of the header for the block is calculated (by subtracting the header size from the block address) and that is added to the free block pool.