Skip to content

Commit e176b83

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

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-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: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Uint32 SDL_GetNextObjectID(void)
137137

138138
static SDL_InitState SDL_objects_init;
139139
static SDL_HashTable *SDL_objects;
140+
static bool check_validity = true;
140141

141142
static Uint32 SDLCALL SDL_HashObject(void *unused, const void *key)
142143
{
@@ -153,6 +154,11 @@ void SDL_SetObjectValid(void *object, SDL_ObjectType type, bool valid)
153154
SDL_assert(object != NULL);
154155

155156
if (SDL_ShouldInit(&SDL_objects_init)) {
157+
check_validity = SDL_GetHintBoolean(SDL_HINT_CHECK_OBJECT_VALIDITY, true);
158+
if(!check_validity) {
159+
SDL_SetInitialized(&SDL_objects_init, true);
160+
return;
161+
}
156162
SDL_objects = SDL_CreateHashTable(0, true, SDL_HashObject, SDL_KeyMatchObject, NULL, NULL);
157163
const bool initialized = (SDL_objects != NULL);
158164
SDL_SetInitialized(&SDL_objects_init, initialized);
@@ -161,10 +167,12 @@ void SDL_SetObjectValid(void *object, SDL_ObjectType type, bool valid)
161167
}
162168
}
163169

164-
if (valid) {
165-
SDL_InsertIntoHashTable(SDL_objects, object, (void *)(uintptr_t)type, true);
166-
} else {
167-
SDL_RemoveFromHashTable(SDL_objects, object);
170+
if (check_validity) {
171+
if (valid) {
172+
SDL_InsertIntoHashTable(SDL_objects, object, (void *)(uintptr_t)type, true);
173+
} else {
174+
SDL_RemoveFromHashTable(SDL_objects, object);
175+
}
168176
}
169177
}
170178

@@ -174,6 +182,10 @@ bool SDL_ObjectValid(void *object, SDL_ObjectType type)
174182
return false;
175183
}
176184

185+
if (!check_validity) {
186+
return true;
187+
}
188+
177189
const void *object_type;
178190
if (!SDL_FindInHashTable(SDL_objects, object, &object_type)) {
179191
return false;
@@ -205,6 +217,9 @@ static bool SDLCALL GetOneObject(void *userdata, const SDL_HashTable *table, con
205217

206218
int SDL_GetObjects(SDL_ObjectType type, void **objects, int count)
207219
{
220+
if (!check_validity) {
221+
return 0;
222+
}
208223
GetOneObjectData data = { type, objects, count, 0 };
209224
SDL_IterateHashTable(SDL_objects, GetOneObject, &data);
210225
return data.num_objects;
@@ -236,11 +251,13 @@ static bool SDLCALL LogOneLeakedObject(void *userdata, const SDL_HashTable *tabl
236251
void SDL_SetObjectsInvalid(void)
237252
{
238253
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;
254+
if (check_validity) {
255+
// Log any leaked objects
256+
SDL_IterateHashTable(SDL_objects, LogOneLeakedObject, NULL);
257+
SDL_assert(SDL_HashTableEmpty(SDL_objects));
258+
SDL_DestroyHashTable(SDL_objects);
259+
SDL_objects = NULL;
260+
}
244261
SDL_SetInitialized(&SDL_objects_init, false);
245262
}
246263
}

0 commit comments

Comments
 (0)