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

implicit declaration of aligned_alloc #19

Open
zlago opened this issue Dec 25, 2024 · 11 comments
Open

implicit declaration of aligned_alloc #19

zlago opened this issue Dec 25, 2024 · 11 comments

Comments

@zlago
Copy link

zlago commented Dec 25, 2024

when compiling libplum for windows, aligned_alloc is not declared
x86_64-w64-mingw32-cc (GCC) 14.2.0 treats implicit declaration as an error
aligned_alloc appears to be declared when compiling for linux

@aaaaaa123456789
Copy link
Owner

Very odd, as this is a function required by the standard. Are you sure you're using the right system headers?
It should be fairly easy to implement if it's missing from the system, but it shouldn't be...

@zlago
Copy link
Author

zlago commented Dec 25, 2024

as it turns out mingw stdlib.h does not seem to have aligned_alloc, to make things more fun _aligned_malloc requires the use of _aligned_free so i cant just use a macro as a hack workaround

@aaaaaa123456789
Copy link
Owner

Maybe build it as a DLL using a real (and static) libc and link to it?

@zlago
Copy link
Author

zlago commented Dec 25, 2024

im not entirely sure what you want me to do, putting a declaration of aligned_alloc in build/libplum.c seems to just sort of work

@aaaaaa123456789
Copy link
Owner

If simply adding void * aligned_alloc(size_t, size_t); somewhere in the file makes everything work, that means that the function is available in the standard library, but not declared by your compiler's headers. In that case: have you messed around with the headers (or specifically stdlib.h) in any way?

@zlago
Copy link
Author

zlago commented Dec 26, 2024

i have not modified the mingw headers, it seems like they just dont declare aligned_alloc. despite that, aligned_alloc appears to work, though i have only tested that it doesnt segfault.
searching the internet implies that aligned_alloc does not exist on windows, i have no idea why this works.

@aaaaaa123456789
Copy link
Owner

You can always try a simple program like this one:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdalign.h>

void * aligned_alloc(size_t, size_t);

int main (void) {
  char (* buf)[alignof(max_align_t)] = aligned_alloc(4 * sizeof *buf, 4 * sizeof *buf);
  printf("buf: %p\n", buf);
  if (buf) {
    for (size_t p = 0; p < 4; p ++) {
      memset(buf[p], 'A' + p, sizeof *buf - 1);
      buf[p][sizeof *buf - 1] = 0;
    }
    for (size_t p = 0; p < 4; p ++) puts(buf[p]);
  }
  return 0;
}

If this program works (as in, it prints reasonable stuff), then aligned_alloc works just fine and you should file a bug against MinGW. If it doesn't, then your declaration isn't doing anything, and there's probably something else going on.

@zlago
Copy link
Author

zlago commented Dec 26, 2024

undefined reference to aligned_alloc

so, looking at the only instance of aligned_alloc in the code of libplum suggest that it gets optimized out during compilation, meaning it doesnt need to actually exist in the c runtime if the alignment provided by malloc is sufficient, but recent gcc requires the function to have a prototype otherwise it refuses to compile it

@aaaaaa123456789
Copy link
Owner

That's exactly what I was thinking. I wonder if there is a good solution to this.

@zlago
Copy link
Author

zlago commented Dec 26, 2024

is there anything wrong with a declaration for aligned_alloc that may fail at link time, with a comment explaining what its for? the alternative is a compile-time flag to make the function fail at runtime

@aaaaaa123456789
Copy link
Owner

It's not a general solution. It only makes sense in the nonconformant nightmare of Windows.

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