Skip to content

Commit 4abc2d6

Browse files
committed
utils: add hint to enable/disable object validity checks
1 parent e960bf6 commit 4abc2d6

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

include/SDL3/SDL_hints.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,25 @@ extern "C" {
591591
*/
592592
#define SDL_HINT_CAMERA_DRIVER "SDL_CAMERA_DRIVER"
593593

594+
/**
595+
* A variable that controls whether object validity checks are enabled.
596+
*
597+
* Object validity checks prevent undefined behaviour when passing invalid
598+
* pointers to SDL functions. A function will return invalid argument error
599+
* if you pass pointer to unitialized/freed SDL object. You may want to
600+
* disable these checks to improve performance.
601+
*
602+
* The variable can be set to the following values:
603+
*
604+
* - "0": Disable object validity checks
605+
* - "1": Enable object validity checks
606+
*
607+
* This hint should be set before SDL is initialized.
608+
*
609+
* \since This hint is avaliable since ???
610+
*/
611+
#define SDL_HINT_CHECK_OBJECT_VALIDITY "SDL_CHECK_OBJECT_VALIDITY"
612+
594613
/**
595614
* A variable that limits what CPU features are available.
596615
*

src/SDL_utils.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
misrepresented as being the original software.
1919
3. This notice may not be removed or altered from any source distribution.
2020
*/
21+
#include "SDL3/SDL_hints.h"
2122
#include "SDL_internal.h"
2223

2324
#if defined(HAVE_GETHOSTNAME) && !defined(SDL_PLATFORM_WINDOWS)
@@ -137,6 +138,7 @@ Uint32 SDL_GetNextObjectID(void)
137138

138139
static SDL_InitState SDL_objects_init;
139140
static SDL_HashTable *SDL_objects;
141+
static bool check_validity = true;
140142

141143
static Uint32 SDLCALL SDL_HashObject(void *unused, const void *key)
142144
{
@@ -153,6 +155,11 @@ void SDL_SetObjectValid(void *object, SDL_ObjectType type, bool valid)
153155
SDL_assert(object != NULL);
154156

155157
if (SDL_ShouldInit(&SDL_objects_init)) {
158+
check_validity = SDL_GetHintBoolean(SDL_HINT_CHECK_OBJECT_VALIDITY, true);
159+
if(!check_validity) {
160+
SDL_SetInitialized(&SDL_objects_init, true);
161+
return;
162+
}
156163
SDL_objects = SDL_CreateHashTable(0, true, SDL_HashObject, SDL_KeyMatchObject, NULL, NULL);
157164
const bool initialized = (SDL_objects != NULL);
158165
SDL_SetInitialized(&SDL_objects_init, initialized);
@@ -161,10 +168,12 @@ void SDL_SetObjectValid(void *object, SDL_ObjectType type, bool valid)
161168
}
162169
}
163170

164-
if (valid) {
165-
SDL_InsertIntoHashTable(SDL_objects, object, (void *)(uintptr_t)type, true);
166-
} else {
167-
SDL_RemoveFromHashTable(SDL_objects, object);
171+
if (check_validity) {
172+
if (valid) {
173+
SDL_InsertIntoHashTable(SDL_objects, object, (void *)(uintptr_t)type, true);
174+
} else {
175+
SDL_RemoveFromHashTable(SDL_objects, object);
176+
}
168177
}
169178
}
170179

@@ -174,6 +183,10 @@ bool SDL_ObjectValid(void *object, SDL_ObjectType type)
174183
return false;
175184
}
176185

186+
if (!check_validity) {
187+
return true;
188+
}
189+
177190
const void *object_type;
178191
if (!SDL_FindInHashTable(SDL_objects, object, &object_type)) {
179192
return false;
@@ -205,6 +218,9 @@ static bool SDLCALL GetOneObject(void *userdata, const SDL_HashTable *table, con
205218

206219
int SDL_GetObjects(SDL_ObjectType type, void **objects, int count)
207220
{
221+
if (!check_validity) {
222+
return 0;
223+
}
208224
GetOneObjectData data = { type, objects, count, 0 };
209225
SDL_IterateHashTable(SDL_objects, GetOneObject, &data);
210226
return data.num_objects;
@@ -236,11 +252,13 @@ static bool SDLCALL LogOneLeakedObject(void *userdata, const SDL_HashTable *tabl
236252
void SDL_SetObjectsInvalid(void)
237253
{
238254
if (SDL_ShouldQuit(&SDL_objects_init)) {
239-
// Log any leaked objects
240-
SDL_IterateHashTable(SDL_objects, LogOneLeakedObject, NULL);
241-
SDL_assert(SDL_HashTableEmpty(SDL_objects));
242-
SDL_DestroyHashTable(SDL_objects);
243-
SDL_objects = NULL;
255+
if (check_validity) {
256+
// Log any leaked objects
257+
SDL_IterateHashTable(SDL_objects, LogOneLeakedObject, NULL);
258+
SDL_assert(SDL_HashTableEmpty(SDL_objects));
259+
SDL_DestroyHashTable(SDL_objects);
260+
SDL_objects = NULL;
261+
}
244262
SDL_SetInitialized(&SDL_objects_init, false);
245263
}
246264
}

0 commit comments

Comments
 (0)