1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
35 #include "c11/threads.h"
37 #include "eglglobals.h"
38 #include "egldisplay.h"
39 #include "egldriver.h"
48 static mtx_t _eglGlobalMutex
= _MTX_INITIALIZER_NP
;
50 struct _egl_global _eglGlobal
=
52 .Mutex
= &_eglGlobalMutex
,
56 /* default AtExitCalls, called in reverse order */
57 _eglUnloadDrivers
, /* always called last */
61 .ClientOnlyExtensionString
=
62 "EGL_EXT_client_extensions"
63 " EGL_EXT_platform_base"
64 " EGL_KHR_client_get_all_proc_addresses"
67 .PlatformExtensionString
=
68 #ifdef HAVE_WAYLAND_PLATFORM
69 " EGL_EXT_platform_wayland"
71 #ifdef HAVE_X11_PLATFORM
72 " EGL_EXT_platform_x11"
74 #ifdef HAVE_DRM_PLATFORM
75 " EGL_MESA_platform_gbm"
77 #ifdef HAVE_SURFACELESS_PLATFORM
78 " EGL_MESA_platform_surfaceless"
82 .ClientExtensionString
= NULL
,
84 .debugCallback
= NULL
,
85 .debugTypesEnabled
= _EGL_DEBUG_BIT_CRITICAL
| _EGL_DEBUG_BIT_ERROR
,
93 for (i
= _eglGlobal
.NumAtExitCalls
- 1; i
>= 0; i
--)
94 _eglGlobal
.AtExitCalls
[i
]();
99 _eglAddAtExitCall(void (*func
)(void))
102 static EGLBoolean registered
= EGL_FALSE
;
104 mtx_lock(_eglGlobal
.Mutex
);
108 registered
= EGL_TRUE
;
111 assert(_eglGlobal
.NumAtExitCalls
< ARRAY_SIZE(_eglGlobal
.AtExitCalls
));
112 _eglGlobal
.AtExitCalls
[_eglGlobal
.NumAtExitCalls
++] = func
;
114 mtx_unlock(_eglGlobal
.Mutex
);
119 _eglGetClientExtensionString(void)
123 mtx_lock(_eglGlobal
.Mutex
);
125 if (_eglGlobal
.ClientExtensionString
== NULL
) {
126 size_t clientLen
= strlen(_eglGlobal
.ClientOnlyExtensionString
);
127 size_t platformLen
= strlen(_eglGlobal
.PlatformExtensionString
);
129 _eglGlobal
.ClientExtensionString
= (char *) malloc(clientLen
+ platformLen
+ 1);
130 if (_eglGlobal
.ClientExtensionString
!= NULL
) {
131 char *ptr
= _eglGlobal
.ClientExtensionString
;
133 memcpy(ptr
, _eglGlobal
.ClientOnlyExtensionString
, clientLen
);
136 if (platformLen
> 0) {
137 // Note that if PlatformExtensionString is not empty, then it will
138 // already have a leading space.
139 assert(_eglGlobal
.PlatformExtensionString
[0] == ' ');
140 memcpy(ptr
, _eglGlobal
.PlatformExtensionString
, platformLen
);
146 ret
= _eglGlobal
.ClientExtensionString
;
148 mtx_unlock(_eglGlobal
.Mutex
);
153 _eglPointerIsDereferencable(void *p
)
156 uintptr_t addr
= (uintptr_t) p
;
157 unsigned char valid
= 0;
158 const long page_size
= getpagesize();
163 /* align addr to page_size */
164 addr
&= ~(page_size
- 1);
166 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
167 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
171 /* mincore() returns 0 on success, and -1 on failure. The last parameter
172 * is a vector of bytes with one entry for each page queried. mincore
173 * returns page residency information in the first bit of each byte in the
176 * Residency doesn't actually matter when determining whether a pointer is
177 * dereferenceable, so the output vector can be ignored. What matters is
178 * whether mincore succeeds. See:
180 * http://man7.org/linux/man-pages/man2/mincore.2.html