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 "egldevice.h"
39 #include "egldisplay.h"
41 #include "util/macros.h"
49 static mtx_t _eglGlobalMutex
= _MTX_INITIALIZER_NP
;
51 struct _egl_global _eglGlobal
=
53 .Mutex
= &_eglGlobalMutex
,
55 .DeviceList
= &_eglSoftwareDevice
,
58 /* default AtExitCalls, called in reverse order */
59 _eglFiniDevice
, /* always called last */
64 .ClientOnlyExtensionString
=
66 .ClientExtensionString
=
68 "EGL_EXT_client_extensions"
69 " EGL_EXT_device_base"
70 " EGL_EXT_device_enumeration"
71 " EGL_EXT_device_query"
72 " EGL_EXT_platform_base"
73 " EGL_KHR_client_get_all_proc_addresses"
78 .PlatformExtensionString
=
83 "EGL_EXT_platform_device"
84 #ifdef HAVE_WAYLAND_PLATFORM
85 " EGL_EXT_platform_wayland"
86 " EGL_KHR_platform_wayland"
88 #ifdef HAVE_X11_PLATFORM
89 " EGL_EXT_platform_x11"
90 " EGL_KHR_platform_x11"
92 #ifdef HAVE_DRM_PLATFORM
93 " EGL_MESA_platform_gbm"
94 " EGL_KHR_platform_gbm"
96 " EGL_MESA_platform_surfaceless"
99 .debugCallback
= NULL
,
100 .debugTypesEnabled
= _EGL_DEBUG_BIT_CRITICAL
| _EGL_DEBUG_BIT_ERROR
,
108 for (i
= _eglGlobal
.NumAtExitCalls
- 1; i
>= 0; i
--)
109 _eglGlobal
.AtExitCalls
[i
]();
114 _eglAddAtExitCall(void (*func
)(void))
117 static EGLBoolean registered
= EGL_FALSE
;
119 mtx_lock(_eglGlobal
.Mutex
);
123 registered
= EGL_TRUE
;
126 assert(_eglGlobal
.NumAtExitCalls
< ARRAY_SIZE(_eglGlobal
.AtExitCalls
));
127 _eglGlobal
.AtExitCalls
[_eglGlobal
.NumAtExitCalls
++] = func
;
129 mtx_unlock(_eglGlobal
.Mutex
);
134 _eglPointerIsDereferencable(void *p
)
136 uintptr_t addr
= (uintptr_t) p
;
137 const long page_size
= getpagesize();
139 unsigned char valid
= 0;
144 /* align addr to page_size */
145 addr
&= ~(page_size
- 1);
147 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
151 /* mincore() returns 0 on success, and -1 on failure. The last parameter
152 * is a vector of bytes with one entry for each page queried. mincore
153 * returns page residency information in the first bit of each byte in the
156 * Residency doesn't actually matter when determining whether a pointer is
157 * dereferenceable, so the output vector can be ignored. What matters is
158 * whether mincore succeeds. See:
160 * http://man7.org/linux/man-pages/man2/mincore.2.html
164 // Without mincore(), we just assume that the first page is unmapped.
165 return addr
>= page_size
;