Skip to content

Commit 2e20405

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

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
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: 12 additions & 4 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,7 @@ 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);
156158
SDL_objects = SDL_CreateHashTable(0, true, SDL_HashObject, SDL_KeyMatchObject, NULL, NULL);
157159
const bool initialized = (SDL_objects != NULL);
158160
SDL_SetInitialized(&SDL_objects_init, initialized);
@@ -161,10 +163,12 @@ void SDL_SetObjectValid(void *object, SDL_ObjectType type, bool valid)
161163
}
162164
}
163165

164-
if (valid) {
165-
SDL_InsertIntoHashTable(SDL_objects, object, (void *)(uintptr_t)type, true);
166-
} else {
167-
SDL_RemoveFromHashTable(SDL_objects, object);
166+
if (check_validity) {
167+
if (valid) {
168+
SDL_InsertIntoHashTable(SDL_objects, object, (void *)(uintptr_t)type, true);
169+
} else {
170+
SDL_RemoveFromHashTable(SDL_objects, object);
171+
}
168172
}
169173
}
170174

@@ -174,6 +178,10 @@ bool SDL_ObjectValid(void *object, SDL_ObjectType type)
174178
return false;
175179
}
176180

181+
if (!check_validity) {
182+
return true;
183+
}
184+
177185
const void *object_type;
178186
if (!SDL_FindInHashTable(SDL_objects, object, &object_type)) {
179187
return false;

0 commit comments

Comments
 (0)