jkuhlmann / cgltf

:diamond_shape_with_a_dot_inside: Single-file glTF 2.0 loader and writer written in C99
MIT License
1.42k stars 135 forks source link

Stack allocate cgltf_data structure? #224

Open Torphedo opened 1 year ago

Torphedo commented 1 year ago

Is there some technical reason behind using a dynamic allocation for the cgltf_data output structure? Since it's a static size, I think it would be more convenient and practical to do something like this:

#define CGLTF_IMPLEMENTATION
#include "cgltf.h"

void* buf; /* Pointer to glb or gltf file data */
size_t size; /* Size of the file data */

cgltf_options options = {0};
cgltf_data data = {0};
cgltf_result result = cgltf_parse(&options, buf, size, &data);
if (result == cgltf_result_success) {
    /* [Do stuff with data] */
}
zeux commented 1 year ago

You’d still need cgltf_free; it’s easier to forget in code like above. With that it doesn’t seem like ergonomics is any better, and cgltf allocates enough that one extra allocation won’t matter for performance.