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

Windows heap_allocator doesn't resize properly #4880

Open
cmpowys opened this issue Feb 25, 2025 · 2 comments
Open

Windows heap_allocator doesn't resize properly #4880

cmpowys opened this issue Feb 25, 2025 · 2 comments

Comments

@cmpowys
Copy link

cmpowys commented Feb 25, 2025

Context

Odin: dev-2025-02-nightly:748a771
OS: Windows 11 Professional (version: 24H2), build 26100.3194
CPU: 13th Gen Intel(R) Core(TM) i5-1335U
RAM: 16107 MiB
Backend: LLVM 18.1.8

Expected Behavior

Calling the heap_allocator proc with .Resize doesn't copy over old data.

Current Behavior

Old data is not copied over after .Resize call.

Failure Information (for bugs)

Steps to Reproduce

The following code reproduces the issue for me. I am assuming test^ == 5 after the realloc call.

context.allocator = runtime.heap_allocator();
bytes, allocator_error := runtime.heap_allocator_proc(context.allocator.data, .Alloc, 64, align_of(rawptr), nil, 0);
assert(allocator_error == runtime.Allocator_Error.None && bytes != nil);
test := cast(^u64)raw_data(bytes);
test^ = 5;
test2 := test;
assert(test^ == 5);
bytes, allocator_error = runtime.heap_allocator_proc(context.allocator.data, .Resize, auto_cast 256, align_of(rawptr)*2, raw_data(bytes), 0);
assert(allocator_error == runtime.Allocator_Error.None && bytes != nil);
test = cast(^u64)raw_data(bytes);
assert(test2 != test);
assert(test^ == 5); // code fails here

Failure Logs

.... main.odin(193:2) runtime assertion: test^ == 5

@cmpowys
Copy link
Author

cmpowys commented Feb 25, 2025

I am not sure if the following is correct in https://github.com/odin-lang/Odin/blob/master/base/runtime/heap_allocator.odin
I would think if force_copy = true, we resize instead of alloc.

force_copy := old_ptr != nil && alignment > align_of(rawptr)

if old_ptr != nil && !force_copy {
original_old_ptr := ([^]rawptr)(old_ptr)[-1]
allocated_mem = heap_resize(original_old_ptr, space)
} else {
allocated_mem = heap_alloc(space, zero_memory)
}

@blob1807
Copy link
Contributor

blob1807 commented Feb 26, 2025

When you're resizing you're telling it the old size is 0 not 64. So it thinks there's nothing to copy over.

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

2 participants