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

Segmentation fault when optimized #3

Open
yhyadev opened this issue May 17, 2024 · 2 comments
Open

Segmentation fault when optimized #3

yhyadev opened this issue May 17, 2024 · 2 comments

Comments

@yhyadev
Copy link

yhyadev commented May 17, 2024

I had some issues lately when I optimize the code with gcc -O2 or any level above 0, It gives me segfault at arena_da_append because of accessing the memory got from arena_alloc, the weird thing is that it first made it through 4 or more allocations

@drewjohnson2
Copy link

@yhyadev It's hard to tell exactly what's happening, but based off of what you're describing it sounds like a memory alignment issue. I've experienced something similar when allocating large numbers of strings to a similar type of arena allocator.

Something you can try is getting the alignment of your type with _Alignof() and doing something like the following:

void * foo_alloc(Arena *arena, size_t size, size_t alignment -- *your alignment you got with _Alignof()*)
{
    size = (size + alignment - 1) & ~(alignment - 1);

    ...
       allocation related code
    ...

    uintptr_t current = (uintptr_t)arena->region->data + arena->region->size;
    uintptr_t aligned = (current + alignment - 1) & ~(alignment - 1);

    arena->region->size = aligned - (uintptr_t)arena->region->data + size;

    return (void *)aligned;
}

I'm working from memory here, so you may have to tweak some things, but it could be worth a shot to see if that's your issue.

Sorry if this doesn't help.

@yhyadev
Copy link
Author

yhyadev commented Oct 29, 2024

Sorry if this doesn't help.

I stopped already working with C, and removed arena.h from the project as I didn't need it, It would be better for someone else that got a similar issue to try this solution

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