Skip to content

Commit

Permalink
修复内存对齐分配的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Nov 9, 2024
1 parent bda8f77 commit a97b0f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/testapp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ int main(int argc, char **argv) {
for (int i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
printf("%p\n", aligned_alloc(4096, 114));
return 0;
}
4 changes: 2 additions & 2 deletions src/libc-base/alloc/freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ finline void *freelists_match(freelists_t lists, size_t size) {
finline void *freelist_aligned_match(freelist_t *list_p, size_t size, size_t align) {
for (freelist_t list = *list_p; list != null; list = list->next) {
ssize_t tgt_size = aligned_size_of(list, align);
if (tgt_size >= size) {
if (tgt_size >= (ssize_t)size) {
*list_p = freelist_detach(*list_p, list);
return list;
}
Expand All @@ -130,7 +130,7 @@ finline void *freelists_aligned_match(freelists_t lists, size_t size, size_t ali
for (; id < FREELIST_NUM; id++) {
for (freelist_t list = lists[id]; list != null; list = list->next) {
ssize_t tgt_size = aligned_size_of(list, align);
if (tgt_size >= size) return freelists_detach(lists, id, list);
if (tgt_size >= (ssize_t)size) return freelists_detach(lists, id, list);
}
}
return null;
Expand Down
11 changes: 6 additions & 5 deletions src/libc-base/alloc/mman.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dlexport void mman_setcb(mman_t man, cb_reqmem_t reqmem, cb_delmem_t delmem) {
#define RESERVED_SIZE (PADDING(sizeof(struct mman_pool)) + 6 * sizeof(size_t))

static bool mman_reqmem(mman_t man, size_t size) {
size += sizeof(struct mman_pool) + 4 * sizeof(size_t);
if (man->cb_reqmem == null) return false;
if (size > SIZE_2M) return false;
#if !ALLOC_FORCE_2M_PAGE
Expand Down Expand Up @@ -161,7 +162,7 @@ dlexport void *mman_aligned_alloc(mman_t man, size_t size, size_t align) {
if (align < 2 * sizeof(size_t)) return mman_alloc(man, size); // 对齐小于 2 倍指针大小
size = size == 0 ? 2 * sizeof(size_t) : PADDING(size); // 保证最小分配 2 个字长且对齐到 2 倍字长

if (size >= ALLOC_LARGE_BLK_SIZE) {
if (size >= ALLOC_LARGE_BLK_SIZE || align >= ALLOC_LARGE_BLK_SIZE) {
void *ptr = large_blk_alloc(size, man->large, man->cb_reqmem, man->cb_delmem);
if ((size_t)ptr % align != 0) { // TODO 让这种情况永远不会出现
large_blk_free(man->large, ptr, man->cb_delmem);
Expand All @@ -175,15 +176,15 @@ dlexport void *mman_aligned_alloc(mman_t man, size_t size, size_t align) {
?: freelist_aligned_match(&man->large_blk, size, align);

if (ptr == null) { // 不足就分配
if (!mman_reqmem(man, size)) return null;
if (!mman_reqmem(man, size + align)) return null;
ptr = freelist_aligned_match(&man->large_blk, size, align);
if (ptr == null) return null; // TODO 让这种情况永远不会出现
}

size_t offset = (size_t)ptr % align;
size_t offset = PADDING_UP(ptr, align) - (size_t)ptr;
if (offset > 0) {
void *new_ptr = blk_split(ptr, offset - 2 * sizeof(size_t));
do_free(man, new_ptr);
do_free(man, ptr);
ptr = new_ptr;
}

try_split_and_free(man, ptr, size);
Expand Down
8 changes: 4 additions & 4 deletions src/libc-base/alloc/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ dlexport void *mpool_aligned_alloc(mpool_t pool, size_t size, size_t align) {
?: freelist_aligned_match(&pool->large_blk, size, align);

if (ptr == null) { // 不足就分配
if (!mpool_reqmem(pool, size)) return null;
if (!mpool_reqmem(pool, size + align)) return null;
ptr = freelist_aligned_match(&pool->large_blk, size, align);
if (ptr == null) return null; // TODO 让这种情况永远不会出现
}

size_t offset = (size_t)ptr % align;
size_t offset = PADDING_UP(ptr, align) - (size_t)ptr;
if (offset > 0) {
void *new_ptr = blk_split(ptr, offset - 2 * sizeof(size_t));
do_free(pool, new_ptr);
do_free(pool, ptr);
ptr = new_ptr;
}

try_split_and_free(pool, ptr, size);
Expand Down

0 comments on commit a97b0f0

Please sign in to comment.