Skip to content

Commit

Permalink
Simplify extension array
Browse files Browse the repository at this point in the history
Use NULL as array terminator and don't store the count of extensions
anymore
  • Loading branch information
lephilousophe committed Dec 24, 2023
1 parent 2348b07 commit 3d11431
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions glad/generator/c/templates/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ static void _post_call_{{ feature_set.name }}_callback_default(void *ret, const
#define GLAD_GL_IS_SOME_NEW_VERSION 0
#endif

static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} int version, const char **out_exts, unsigned int *out_num_exts_i, char ***out_exts_i) {
static void glad_gl_free_extensions(char **exts_i) {
if (exts_i != NULL) {
unsigned int index;
for(index = 0; exts_i[index]; index++) {
free((void *) (exts_i[index]));
}
free((void *)exts_i);
exts_i = NULL;
}
}
static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} int version, const char **out_exts, char ***out_exts_i) {
#if GLAD_GL_IS_SOME_NEW_VERSION
if(GLAD_VERSION_MAJOR(version) < 3) {
#else
GLAD_UNUSED(version);
GLAD_UNUSED(out_num_exts_i);
GLAD_UNUSED(out_exts_i);
#endif
if ({{ 'glGetString'|ctx }} == NULL) {
Expand All @@ -63,9 +72,7 @@ static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} int vers
return 0;
}
{{ 'glGetIntegerv'|ctx }}(GL_NUM_EXTENSIONS, (int*) &num_exts_i);
if (num_exts_i > 0) {
exts_i = (char **) malloc(num_exts_i * (sizeof *exts_i));
}
exts_i = (char **) malloc((num_exts_i + 1) * (sizeof *exts_i));
if (exts_i == NULL) {
return 0;
}
Expand All @@ -74,30 +81,23 @@ static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} int vers
size_t len = strlen(gl_str_tmp) + 1;

char *local_str = (char*) malloc(len * sizeof(char));
if(local_str != NULL) {
memcpy(local_str, gl_str_tmp, len * sizeof(char));
if(local_str == NULL) {
exts_i[index] = NULL;
glad_gl_free_extensions(exts_i);
return 0;
}

memcpy(local_str, gl_str_tmp, len * sizeof(char));
exts_i[index] = local_str;
}
exts_i[index] = NULL;

*out_num_exts_i = num_exts_i;
*out_exts_i = exts_i;
}
#endif
return 1;
}
static void glad_gl_free_extensions(char **exts_i, unsigned int num_exts_i) {
if (exts_i != NULL) {
unsigned int index;
for(index = 0; index < num_exts_i; index++) {
free((void *) (exts_i[index]));
}
free((void *)exts_i);
exts_i = NULL;
}
}
static int glad_gl_has_extension(int version, const char *exts, unsigned int num_exts_i, char **exts_i, const char *ext) {
static int glad_gl_has_extension(int version, const char *exts, char **exts_i, const char *ext) {
if(GLAD_VERSION_MAJOR(version) < 3 || !GLAD_GL_IS_SOME_NEW_VERSION) {
const char *extensions;
const char *loc;
Expand All @@ -120,7 +120,7 @@ static int glad_gl_has_extension(int version, const char *exts, unsigned int num
}
} else {
unsigned int index;
for(index = 0; index < num_exts_i; index++) {
for(index = 0; exts_i[index]; index++) {
const char *e = exts_i[index];
if(strcmp(e, ext) == 0) {
return 1;
Expand All @@ -137,17 +137,16 @@ static GLADapiproc glad_gl_get_proc_from_userptr(void *userptr, const char* name
{% for api in feature_set.info.apis %}
static int glad_gl_find_extensions_{{ api|lower }}({{ template_utils.context_arg(',') }} int version) {
const char *exts = NULL;
unsigned int num_exts_i = 0;
char **exts_i = NULL;
if (!glad_gl_get_extensions({{ 'context, ' if options.mx }}version, &exts, &num_exts_i, &exts_i)) return 0;
if (!glad_gl_get_extensions({{ 'context, ' if options.mx }}version, &exts, &exts_i)) return 0;

{% for extension in feature_set.extensions|select('supports', api) %}
{{ ('GLAD_' + extension.name)|ctx(name_only=True) }} = glad_gl_has_extension(version, exts, num_exts_i, exts_i, "{{ extension.name }}");
{{ ('GLAD_' + extension.name)|ctx(name_only=True) }} = glad_gl_has_extension(version, exts, exts_i, "{{ extension.name }}");
{% else %}
GLAD_UNUSED(glad_gl_has_extension);
{% endfor %}

glad_gl_free_extensions(exts_i, num_exts_i);
glad_gl_free_extensions(exts_i);

return 1;
}
Expand Down

0 comments on commit 3d11431

Please sign in to comment.