tboox / tbox

🎁 A glib-like multi-platform c library
https://docs.tboox.org
Apache License 2.0
4.88k stars 723 forks source link

内存分配错误 #267

Open vkensou opened 1 month ago

vkensou commented 1 month ago

描述问题

调用tb_align_malloc(2104, 512); 会有断言错误:

期待的结果

正常分配内存

错误信息

[tbox]: [allocator]: [assert]: at tb_assert_impl(): 300, C:\Users\user\AppData\Local\.xmake\cache\packages\2410\t\tbox\v1.7.6\source\tbox\src\tbox\prefix\assert.h: expr[!((tb_size_t)data & (align - 1))] at tb_allocator_align_malloc_(): 373, src\tbox\memory\allocator.c

相关环境

windows msvc 但我感觉跟系统和编译器关系不大

xmake.lua

add_requires("tbox v1.7.6", {configs = {debug = is_mode("debug")}})

target("main")
    set_kind("binary")
    add_packages("tbox")
    add_files("main.cpp")

main.cpp

#include "tbox/tbox.h"

int main()
{
    tb_init(tb_null, tb_null);
    tb_align_malloc(2104, 512);
}
Issues-translate-bot commented 1 month ago

Bot detected the issue body's language is not English, translate it automatically.


Title: Memory allocation error

Describe the problem

Calling tb_align_malloc(2104, 512); will cause an assertion error:

Expected results

Allocate memory normally

error message

[tbox]: [allocator]: [assert]: at tb_assert_impl(): 300, C:\Users\user\AppData\Local\.xmake\cache\packages\2410\t\tbox\v1.7.6\source\tbox \src\tbox\prefix\assert.h: expr[!((tb_size_t)data & (align - 1))] at tb_allocator_align_malloc_(): 373, src\tbox\memory\allocator.c

Related environment

windows msvc but I feel it has little to do with the system and compiler

xmake.lua

add_requires("tbox v1.7.6", {configs = {debug = is_mode("debug")}})

target("main")
    set_kind("binary")
    add_packages("tbox")
    add_files("main.cpp")

main.cpp

#include "tbox/tbox.h"

int main()
{
    tb_init(tb_null, tb_null);
    tb_align_malloc(2104, 512);
}
vkensou commented 1 month ago

看了代码

tb_pointer_t tb_allocator_align_malloc_(tb_allocator_ref_t allocator, tb_size_t size, tb_size_t align __tb_debug_decl__)
{
// ...
tb_byte_t diff = (tb_byte_t)((~(tb_long_t)data) & (align - 1)) + 1;
// ...
}

是因为这句,计算diff的时候转到u8了,对齐到512的话超出范围了

waruqi commented 1 month ago

搞这么大对齐干嘛。。

Issues-translate-bot commented 1 month ago

Bot detected the issue body's language is not English, translate it automatically.


Why do we have such a big alignment? .

vkensou commented 1 month ago

搞这么大对齐干嘛。。

我想用tbox作为c++的memory_resource的上游,然后vector resize几次以后就这样的。。。