Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

内存分配错误 #267

Open
vkensou opened this issue Oct 13, 2024 · 5 comments
Open

内存分配错误 #267

vkensou opened this issue Oct 13, 2024 · 5 comments

Comments

@vkensou
Copy link

vkensou commented Oct 13, 2024

描述问题

调用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
Copy link

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
Copy link
Author

vkensou commented Oct 13, 2024

看了代码

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
Copy link
Member

waruqi commented Oct 13, 2024

搞这么大对齐干嘛。。

@Issues-translate-bot
Copy link

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


Why do we have such a big alignment? .

@vkensou
Copy link
Author

vkensou commented Oct 13, 2024

搞这么大对齐干嘛。。

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@waruqi @vkensou @Issues-translate-bot and others