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

Support custom malloc()/free() for memory allocation #98

Closed
raysan5 opened this issue Jan 16, 2020 · 8 comments · Fixed by #99
Closed

Support custom malloc()/free() for memory allocation #98

raysan5 opened this issue Jan 16, 2020 · 8 comments · Fixed by #99
Assignees

Comments

@raysan5
Copy link
Contributor

raysan5 commented Jan 16, 2020

Similar to stb libraries, it would be nice to support custom memory allocators:

#if defined(CGLTF_MALLOC) && defined(CGLTF_FREE)
// ok
#elif !defined(CGLTF_MALLOC) && !defined(CGLTF_FREE) 
// ok
#else
#error "Must define both or none of CGLTF_MALLOC and CGLTF_FREE
#endif

#ifndef CGLTF_MALLOC
#define CGLTF_MALLOC(sz)       malloc(sz)
#define CGLTF_FREE(p)          free(p)
#endif
@LxLasso
Copy link

LxLasso commented Jan 17, 2020

Take a look at the contents of cgltf_options, a struct that is passed to cgltf_parse. It has two fields, memory_alloc and memory_free that I think is what you're looking for?

@raysan5
Copy link
Contributor Author

raysan5 commented Jan 17, 2020

@LxLasso thanks for the answer! Yes, that's an option for custom allocators but calling malloc() and free() in cgltf_default_alloc() and cgltf_default_free() forces to link with a library providing those symbols (stdlib).

@LxLasso
Copy link

LxLasso commented Jan 17, 2020

I see @raysan5 . In that case there's an include for stdlib.h to get rid of as well. A hackaround for not having to link stdlib could be providing your own malloc and free symbols though?

@raysan5
Copy link
Contributor Author

raysan5 commented Jan 17, 2020

@LxLasso actually, there are also a couple of calls to atoi() and atof() that require stdlib, but they could also be replaced with custom implementations if required.

A hackaround for not having to link stdlib could be providing your own malloc and free symbols though?

Using a macro like the one I mention and replacing malloc() by CGLTF_MALLOC() would be enough to avoid symbols requirement (when user overrides macro in user code):

#ifndef CGLTF_MALLOC
#define CGLTF_MALLOC(sz)   malloc(sz)
#define CGLTF_FREE(p)      free(p)
#endif

Users can just define the macro in their code:

#define CGLTF_MALLOC(sz)    custom_malloc(sz)
#define CGLTF_FREE(p)       custom_free(p)

#define CGLTF_IMPLEMENTATION
#include "cgltf.h"          // malloc()/free() symbols never defined, stdlib not required

@jkuhlmann
Copy link
Owner

So, do I get this right, your goal is to get rid of the stdlib dependency?

@raysan5
Copy link
Contributor Author

raysan5 commented Jan 18, 2020

@jkuhlmann Actually the goal is having a mechanism to not only allow custom allocator but also avoid system dependencies to default ones. It's just to increase a bit library portability.

Here it is my related issue: raysan5/raylib#1074

@jkuhlmann
Copy link
Owner

@raysan5, does something like PR #99 fit the bill?

@raysan5
Copy link
Contributor Author

raysan5 commented Jan 22, 2020

@jkuhlmann that's perfect! thank you very much for implementing it! 👍😄

@raysan5 raysan5 closed this as completed Jan 22, 2020
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

Successfully merging a pull request may close this issue.

3 participants