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

Unable to build master branch on Mac OS due to use of malloc and memalign() #405

Closed
RulerOf opened this issue Jul 12, 2022 · 4 comments
Closed

Comments

@RulerOf
Copy link

RulerOf commented Jul 12, 2022

Attempting to build on Mac OS fails because we don't have malloc.h on the platform:

andrewbobulsky ~/Projects/personal/build-stratagus
% CXX=$(brew --prefix)/bin/g++-11 CC=$(brew --prefix)/bin/gcc-11 cmake ../stratagus -DENABLE_DEV=ON
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
<snip>
==================================
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/andrewbobulsky/Projects/personal/build-stratagus
6s andrewbobulsky ~/Projects/personal/build-stratagus
% make -j$(nproc)
[  0%] Building CXX object CMakeFiles/genversion.dir/tools/genversion.cpp.o
<snip>
[ 82%] Building CXX object CMakeFiles/stratagus.dir/src/ui/button_checks.cpp.o
/Users/andrewbobulsky/Projects/personal/stratagus/src/stratagus/util.cpp:44:10: fatal error: malloc.h: No such file or directory
   44 | #include <malloc.h>
      |          ^~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/stratagus.dir/src/stratagus/util.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/stratagus.dir/all] Error 2
make: *** [all] Error 2

I can use #ifndef to avoid loading malloc.h and letting stdlib.h do the job:
image

...but we fail later because memalign() is not supported:

[ 83%] Building CXX object CMakeFiles/stratagus.dir/src/ui/icons.cpp.o
/Users/andrewbobulsky/Projects/personal/stratagus/src/stratagus/util.cpp: In function 'void* aligned_malloc(size_t, size_t)':
/Users/andrewbobulsky/Projects/personal/stratagus/src/stratagus/util.cpp:640:16: error: 'memalign' was not declared in this scope
  640 |         return memalign(alignment, size);
      |                ^~~~~~~~
[ 84%] Building CXX object CMakeFiles/stratagus.dir/src/ui/interface.cpp.o
[ 84%] Building CXX object CMakeFiles/stratagus.dir/src/ui/mainscr.cpp.o
make[2]: *** [CMakeFiles/stratagus.dir/src/stratagus/util.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
[ 85%] Building CXX object CMakeFiles/stratagus.dir/src/ui/uibuttons_proc.cpp.o
make[1]: *** [CMakeFiles/stratagus.dir/all] Error 2
make: *** [all] Error 2

I did try briefly to comprehend what the function is doing but I'm not familiar enough with CPU plumbing to resolve the problem.

Stackoverflow suggests the use of posix_memalign(). It seemed reasonable to stuff it in here

void *aligned_malloc(size_t alignment, size_t size)
{
#ifdef WIN32
return _aligned_malloc(size, alignment);
#elif _ISOC11_SOURCE
return aligned_alloc(alignment, size);
#else
return memalign(alignment, size);
#endif
}

and my wildest guess is something like this might do the trick:

void *aligned_malloc(size_t alignment, size_t size)
{
#ifdef WIN32
	return _aligned_malloc(size, alignment);
#elif _ISOC11_SOURCE
	return aligned_alloc(alignment, size);
#elif __APPLE__
	return posix_memalign(?????????, alignment, size);
#else
	return memalign(alignment, size);
#endif
}

But I couldn't even begin to tell ya what goes in the first arg of the function ¯\_(ツ)_/¯

@RulerOf RulerOf changed the title Unable to build on Mac OS due to use of malloc and memalign() Unable to build master branch on Mac OS due to use of malloc and memalign() Jul 12, 2022
@RulerOf
Copy link
Author

RulerOf commented Aug 1, 2022

Just confirming this still happens on the most-recent updates:

[ 82%] Building CXX object CMakeFiles/stratagus.dir/src/ui/botpanel.cpp.o
/Users/andrewbobulsky/Projects/personal/stratagus/src/stratagus/util.cpp:44:10: fatal error: malloc.h: No such file or directory
   44 | #include <malloc.h>
      |          ^~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/stratagus.dir/src/stratagus/util.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/stratagus.dir/all] Error 2
make: *** [all] Error 2
(2) 17s andrewbobulsky ~/Projects/personal/build-stratagus
% git -C ../stratagus rev-parse HEAD
361ee0b05c9cb28a827a0385c36dadb8b132434b

@timfel
Copy link
Member

timfel commented Aug 1, 2022

Hi, thanks for the report. Unfortunately none of the devs have macs anymore, so we cannot really test. If you figure it out, I would be very happy to merge a fix :)

@timfel
Copy link
Member

timfel commented Aug 4, 2022

Ah, just remembered, the usage of memalign is a performance optimization, so you might get away with simply using malloc and free in those functions

@timfel timfel closed this as completed in 7466c8d Aug 27, 2022
@timfel
Copy link
Member

timfel commented Aug 27, 2022

I got access to a mac over the weekend, and figured I'd quickly take a look ;)

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