Skip to content

Commit 304fff2

Browse files
committed
cleanup(template): loader template style and ownership cleanup
Signed-off-by: Steven Noonan <steven@uplinklabs.net>
1 parent 52d5f18 commit 304fff2

3 files changed

Lines changed: 130 additions & 71 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/generator/c/templates/header.h.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,10 @@ typedef struct {{ fs.context_name }} {
420420
NOT taken (gloam will not close it). Present on all context types so
421421
user code can use a single field name regardless of API. */
422422
void *gloam_loader_handle;
423-
{% if fs.is_vulkan %}
424423
/* Non-zero if gloam opened the library handle itself and is responsible
425424
for closing it in gloamVulkanFinalize / gloamLoaderUnload. */
426425
uint8_t gloam_loader_owns_handle;
426+
{% if fs.is_vulkan %}
427427
/* The last VkInstance this context loaded entry points from */
428428
VkInstance vk_loaded_instance;
429429
/* The last VkDevice this context loaded entry points from */

src/generator/c/templates/loader.j2

Lines changed: 127 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ static GloamAPIProc gloam_gl_get_proc(const char *name)
2727
{
2828
GloamAPIProc result = NULL;
2929
struct gloam_gl_load_userptr *u = &gloam_gl_load_state;
30-
if (!u->handle) return NULL;
30+
if (!u->handle)
31+
return NULL;
3132
#if defined(__APPLE__) || defined(__HAIKU__)
3233
result = (GloamAPIProc)gloam_dlsym(u->handle, name);
3334
#elif defined(GLOAM_PLATFORM_WINDOWS)
34-
if (u->wgl_get_proc) result = u->wgl_get_proc(name);
35-
if (!result) result = (GloamAPIProc)gloam_dlsym(u->handle, name);
35+
if (u->wgl_get_proc)
36+
result = u->wgl_get_proc(name);
37+
if (!result)
38+
result = (GloamAPIProc)gloam_dlsym(u->handle, name);
3639
#else
37-
if (u->glx_get_proc) result = u->glx_get_proc(name);
38-
if (!result) result = (GloamAPIProc)gloam_dlsym(u->handle, name);
40+
if (u->glx_get_proc)
41+
result = u->glx_get_proc(name);
42+
if (!result)
43+
result = (GloamAPIProc)gloam_dlsym(u->handle, name);
3944
#endif
4045
return result;
4146
}
@@ -45,15 +50,18 @@ static GloamAPIProc gloam_gl_get_proc(const char *name)
4550
static GloamAPIProc gloam_gles_get_proc(const char *name)
4651
{
4752
struct gloam_gl_load_userptr *u = &gloam_gl_load_state;
48-
if (!u->handle) return NULL;
53+
if (!u->handle)
54+
return NULL;
4955
return (GloamAPIProc)gloam_dlsym(u->handle, name);
5056
}
5157

5258
{%- for api in fs.apis %}
5359

5460
int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg() }})
5561
{
56-
int did_open = 0, version;
62+
int did_open = 0;
63+
int version;
64+
void *handle;
5765
{% if api in ["gles1", "gles2"] %}
5866
static const char * const kLibNames[] = {
5967
#if defined(__APPLE__)
@@ -79,22 +87,21 @@ int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg() }})
7987
#endif
8088
};
8189
{% endif %}
82-
if (!context->gloam_loader_handle) {
83-
context->gloam_loader_handle = gloam_open_library(
84-
kLibNames, GLOAM_ARRAYSIZE(kLibNames));
90+
handle = context->gloam_loader_handle;
91+
92+
if (!handle) {
93+
handle = gloam_open_library(kLibNames, GLOAM_ARRAYSIZE(kLibNames));
8594
did_open = 1;
8695
}
87-
if (!context->gloam_loader_handle) return 0;
8896

89-
gloam_gl_load_state.handle = context->gloam_loader_handle;
97+
if (!handle)
98+
return 0;
99+
100+
gloam_gl_load_state.handle = handle;
90101
#if defined(GLOAM_PLATFORM_WINDOWS)
91-
gloam_gl_load_state.wgl_get_proc =
92-
(GloamAPIProc (WINAPI *)(const char *))
93-
gloam_dlsym(context->gloam_loader_handle, "wglGetProcAddress");
102+
gloam_gl_load_state.wgl_get_proc = (GloamAPIProc (WINAPI *)(const char *))gloam_dlsym(handle, "wglGetProcAddress");
94103
#elif !defined(__APPLE__) && !defined(__HAIKU__)
95-
gloam_gl_load_state.glx_get_proc =
96-
(GloamAPIProc (*)(const char *))
97-
gloam_dlsym(context->gloam_loader_handle, "glXGetProcAddressARB");
104+
gloam_gl_load_state.glx_get_proc = (GloamAPIProc (*)(const char *))gloam_dlsym(handle, "glXGetProcAddressARB");
98105
#endif
99106

100107
{% if api in ["gles1", "gles2"] %}
@@ -103,10 +110,15 @@ int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg() }})
103110
version = gloamLoad{{ api | api_display }}Context(context, gloam_gl_get_proc);
104111
{% endif %}
105112
gloam_gl_load_state.handle = NULL;
113+
106114
if (!version && did_open) {
107-
gloam_dlclose(context->gloam_loader_handle);
108-
context->gloam_loader_handle = NULL;
115+
gloam_dlclose(handle);
116+
return 0;
109117
}
118+
119+
context->gloam_loader_handle = handle;
120+
context->gloam_loader_owns_handle |= (uint8_t)did_open;
121+
110122
return version;
111123
}
112124

@@ -117,9 +129,8 @@ int gloamLoaderLoad{{ api | api_display }}(void)
117129

118130
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
119131
{
120-
if (context->gloam_loader_handle) {
132+
if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
121133
gloam_dlclose(context->gloam_loader_handle);
122-
context->gloam_loader_handle = NULL;
123134
}
124135
gloamLoaderReset{{ api | api_display }}Context(context);
125136
}
@@ -173,25 +184,41 @@ static GloamAPIProc gloam_egl_get_proc(const char *name)
173184
{% for api in fs.apis %}
174185
int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}EGLDisplay display)
175186
{
176-
int did_open = 0, version;
177-
if (!context->gloam_loader_handle) {
178-
context->gloam_loader_handle = gloam_open_library(
179-
gloam_egl_lib_names, GLOAM_ARRAYSIZE(gloam_egl_lib_names));
187+
int did_open = 0;
188+
int version;
189+
void *handle;
190+
191+
handle = context->gloam_loader_handle;
192+
193+
if (!handle) {
194+
handle = gloam_open_library(gloam_egl_lib_names, GLOAM_ARRAYSIZE(gloam_egl_lib_names));
180195
did_open = 1;
181196
}
182-
if (!context->gloam_loader_handle) return 0;
183197

184-
gloam_egl_load_state.handle = context->gloam_loader_handle;
198+
if (!handle)
199+
return 0;
200+
201+
gloam_egl_load_state.handle = handle;
185202
gloam_egl_load_state.get_proc_address =
186-
(PFNEGLGETPROCADDRESSPROC)gloam_dlsym(context->gloam_loader_handle, "eglGetProcAddress");
203+
(PFNEGLGETPROCADDRESSPROC)gloam_dlsym(handle, "eglGetProcAddress");
204+
187205
if (!gloam_egl_load_state.get_proc_address) {
188-
if (did_open) { gloam_dlclose(context->gloam_loader_handle); context->gloam_loader_handle = NULL; }
206+
if (did_open)
207+
gloam_dlclose(handle);
189208
return 0;
190209
}
191210

192211
version = gloamLoad{{ api | api_display }}Context(context, display, gloam_egl_get_proc);
193212
gloam_egl_load_state.handle = NULL;
194-
if (!version && did_open) { gloam_dlclose(context->gloam_loader_handle); context->gloam_loader_handle = NULL; }
213+
214+
if (!version && did_open) {
215+
gloam_dlclose(handle);
216+
return 0;
217+
}
218+
219+
context->gloam_loader_handle = handle;
220+
context->gloam_loader_owns_handle |= (uint8_t)did_open;
221+
195222
return version;
196223
}
197224

@@ -202,9 +229,8 @@ int gloamLoaderLoad{{ api | api_display }}(EGLDisplay display)
202229

203230
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
204231
{
205-
if (context->gloam_loader_handle) {
232+
if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
206233
gloam_dlclose(context->gloam_loader_handle);
207-
context->gloam_loader_handle = NULL;
208234
}
209235
gloamLoaderReset{{ api | api_display }}Context(context);
210236
}
@@ -246,8 +272,10 @@ static GloamAPIProc gloam_glx_get_proc(const char *name)
246272
{
247273
struct gloam_glx_load_userptr *u = &gloam_glx_load_state;
248274
GloamAPIProc result = NULL;
249-
if (u->get_proc_address) result = u->get_proc_address(name);
250-
if (!result) result = (GloamAPIProc)gloam_dlsym(u->handle, name);
275+
if (u->get_proc_address)
276+
result = u->get_proc_address(name);
277+
if (!result)
278+
result = (GloamAPIProc)gloam_dlsym(u->handle, name);
251279
return result;
252280
}
253281

@@ -256,25 +284,38 @@ static GloamAPIProc gloam_glx_get_proc(const char *name)
256284
int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}Display *display, int screen)
257285
{
258286
int did_open = 0, version;
259-
if (!context->gloam_loader_handle) {
260-
context->gloam_loader_handle = gloam_open_library(
261-
gloam_glx_lib_names, GLOAM_ARRAYSIZE(gloam_glx_lib_names));
287+
void *handle;
288+
289+
handle = context->gloam_loader_handle;
290+
291+
if (!handle) {
292+
handle = gloam_open_library(gloam_glx_lib_names, GLOAM_ARRAYSIZE(gloam_glx_lib_names));
262293
did_open = 1;
263294
}
264-
if (!context->gloam_loader_handle) return 0;
265295

266-
gloam_glx_load_state.handle = context->gloam_loader_handle;
267-
gloam_glx_load_state.get_proc_address =
268-
(GloamAPIProc (*)(const char *))
269-
gloam_dlsym(context->gloam_loader_handle, "glXGetProcAddressARB");
296+
if (!handle)
297+
return 0;
298+
299+
gloam_glx_load_state.handle = handle;
300+
gloam_glx_load_state.get_proc_address = (GloamAPIProc (*)(const char *))gloam_dlsym(handle, "glXGetProcAddressARB");
301+
270302
if (!gloam_glx_load_state.get_proc_address) {
271-
if (did_open) { gloam_dlclose(context->gloam_loader_handle); context->gloam_loader_handle = NULL; }
303+
if (did_open)
304+
gloam_dlclose(handle);
272305
return 0;
273306
}
274307

275308
version = gloamLoad{{ api | api_display }}Context(context, display, screen, gloam_glx_get_proc);
276309
gloam_glx_load_state.handle = NULL;
277-
if (!version && did_open) { gloam_dlclose(context->gloam_loader_handle); context->gloam_loader_handle = NULL; }
310+
311+
if (!version && did_open) {
312+
gloam_dlclose(handle);
313+
return 0;
314+
}
315+
316+
context->gloam_loader_handle = handle;
317+
context->gloam_loader_owns_handle |= (uint8_t)did_open;
318+
278319
return version;
279320
}
280321

@@ -285,9 +326,8 @@ int gloamLoaderLoad{{ api | api_display }}(Display *display, int screen)
285326

286327
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
287328
{
288-
if (context->gloam_loader_handle) {
329+
if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
289330
gloam_dlclose(context->gloam_loader_handle);
290-
context->gloam_loader_handle = NULL;
291331
}
292332
gloamLoaderReset{{ api | api_display }}Context(context);
293333
}
@@ -324,34 +364,51 @@ static GloamAPIProc gloam_wgl_get_proc(const char *name)
324364
{
325365
struct gloam_wgl_load_userptr *u = &gloam_wgl_load_state;
326366
GloamAPIProc result = NULL;
327-
if (u->wgl_get_proc) result = u->wgl_get_proc(name);
328-
if (!result) result = (GloamAPIProc)gloam_dlsym(u->handle, name);
367+
if (u->wgl_get_proc)
368+
result = u->wgl_get_proc(name);
369+
if (!result)
370+
result = (GloamAPIProc)gloam_dlsym(u->handle, name);
329371
return result;
330372
}
331373

332374
{%- for api in fs.apis %}
333375
int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}HDC hdc)
334376
{
335-
int did_open = 0, version;
336-
if (!context->gloam_loader_handle) {
337-
context->gloam_loader_handle = gloam_open_library(
377+
int did_open = 0;
378+
int version;
379+
void *handle = context->gloam_loader_handle;
380+
381+
if (!handle) {
382+
handle = gloam_open_library(
338383
gloam_wgl_lib_names, GLOAM_ARRAYSIZE(gloam_wgl_lib_names));
339384
did_open = 1;
340385
}
341-
if (!context->gloam_loader_handle) return 0;
342386

343-
gloam_wgl_load_state.handle = context->gloam_loader_handle;
387+
if (!handle)
388+
return 0;
389+
390+
gloam_wgl_load_state.handle = handle;
344391
gloam_wgl_load_state.wgl_get_proc =
345392
(GloamAPIProc (WINAPI *)(const char *))
346-
gloam_dlsym(context->gloam_loader_handle, "wglGetProcAddress");
393+
gloam_dlsym(handle, "wglGetProcAddress");
394+
347395
if (!gloam_wgl_load_state.wgl_get_proc) {
348-
if (did_open) { gloam_dlclose(context->gloam_loader_handle); context->gloam_loader_handle = NULL; }
396+
if (did_open)
397+
gloam_dlclose(handle);
349398
return 0;
350399
}
351400

352401
version = gloamLoad{{ api | api_display }}Context(context, hdc, gloam_wgl_get_proc);
353402
gloam_wgl_load_state.handle = NULL;
354-
if (!version && did_open) { gloam_dlclose(context->gloam_loader_handle); context->gloam_loader_handle = NULL; }
403+
404+
if (!version && did_open) {
405+
gloam_dlclose(handle);
406+
return 0;
407+
}
408+
409+
context->gloam_loader_handle = handle;
410+
context->gloam_loader_owns_handle |= (uint8_t)did_open;
411+
355412
return version;
356413
}
357414

@@ -362,9 +419,8 @@ int gloamLoaderLoad{{ api | api_display }}(HDC hdc)
362419

363420
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
364421
{
365-
if (context->gloam_loader_handle) {
422+
if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
366423
gloam_dlclose(context->gloam_loader_handle);
367-
context->gloam_loader_handle = NULL;
368424
}
369425
gloamLoaderReset{{ api | api_display }}Context(context);
370426
}
@@ -399,13 +455,14 @@ int gloamLoaderLoadVulkanContext({{ u.ctx_arg(', ') }}VkInstance instance, VkPhy
399455
int version;
400456
void *handle;
401457

402-
if (!context->gloam_loader_handle) {
403-
context->gloam_loader_handle = gloam_open_library(
458+
handle = context->gloam_loader_handle;
459+
460+
if (!handle) {
461+
handle = gloam_open_library(
404462
gloam_vk_lib_names, GLOAM_ARRAYSIZE(gloam_vk_lib_names));
405463
did_open = 1;
406464
}
407465

408-
handle = context->gloam_loader_handle;
409466
if (!handle)
410467
return 0;
411468

@@ -414,18 +471,21 @@ int gloamLoaderLoadVulkanContext({{ u.ctx_arg(', ') }}VkInstance instance, VkPhy
414471
(PFN_vkGetInstanceProcAddr)gloam_dlsym(handle, "vkGetInstanceProcAddr");
415472

416473
if (!context->GetInstanceProcAddr) {
417-
if (did_open) {
474+
if (did_open)
418475
gloam_dlclose(handle);
419-
context->gloam_loader_handle = NULL;
420-
}
421476
return 0;
422477
}
423478

424479
version = gloamVulkanDiscoverContext(context, instance, physical_device, device);
480+
425481
if (!version && did_open) {
426482
gloam_dlclose(handle);
427-
context->gloam_loader_handle = NULL;
483+
return 0;
428484
}
485+
486+
context->gloam_loader_handle = handle;
487+
context->gloam_loader_owns_handle |= (uint8_t)did_open;
488+
429489
return version;
430490
}
431491

@@ -438,9 +498,8 @@ int gloamLoaderLoadVulkan(VkInstance instance, VkPhysicalDevice physical_device,
438498
/* Close the library handle (if set) then zero all context state. */
439499
void gloamLoaderUnloadVulkanContext({{ u.ctx_arg() }})
440500
{
441-
if (context->gloam_loader_handle) {
501+
if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
442502
gloam_dlclose(context->gloam_loader_handle);
443-
context->gloam_loader_handle = NULL;
444503
}
445504
gloamLoaderResetVulkanContext(context);
446505
}

0 commit comments

Comments
 (0)