import
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
#include <pool.h>
|
||||
#include "os_macros.h"
|
||||
#include "os_align.h"
|
||||
|
||||
void pool_add_block(pool_t* self, void* block, os_size_t block_size){
|
||||
// 对齐
|
||||
os_uintptr_t init_start = (os_uintptr_t)block; // 初始的位置
|
||||
os_uintptr_t start = OS_ALIGN_UP(init_start, OS_ALIGN_SIZE); // 向上对齐后的位置
|
||||
block_size -= (start - init_start); // 实际使用的块大小
|
||||
|
||||
OS_ASSERT(block_size > self->obj_size);
|
||||
|
||||
// 将内存块拆成 obj_size 大小
|
||||
os_size_t count = block_size/self->obj_size;
|
||||
uint8_t * last = &(((uint8_t*)start)[(count - 1) * self->obj_size]);
|
||||
for(uint8_t * p = (uint8_t*)start; p<last; p+=self->obj_size){
|
||||
((pool_link_t*)p)->next = (pool_link_t*)(p+self->obj_size);
|
||||
}
|
||||
((pool_link_t*)last)->next = 0;
|
||||
self->free_list_p = (pool_link_t*)start;
|
||||
self->capacity += count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user