2023-10-10 12:38:22 -07:00
|
|
|
/*
|
2024-10-04 21:52:23 -05:00
|
|
|
Simple DirectMedia Layer
|
2026-01-01 09:48:19 -08:00
|
|
|
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
|
2023-10-10 12:38:22 -07:00
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
appreciated but is not required.
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
|
|
|
|
#include "SDL_internal.h"
|
2024-09-30 22:15:38 -07:00
|
|
|
|
2023-11-12 19:06:23 -08:00
|
|
|
#include "SDL_hints_c.h"
|
2023-10-10 12:38:22 -07:00
|
|
|
#include "SDL_properties_c.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2023-11-12 09:47:31 -08:00
|
|
|
SDL_PropertyType type;
|
|
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
void *pointer_value;
|
|
|
|
|
char *string_value;
|
|
|
|
|
Sint64 number_value;
|
|
|
|
|
float float_value;
|
2024-08-22 09:21:26 -07:00
|
|
|
bool boolean_value;
|
2023-11-12 09:47:31 -08:00
|
|
|
} value;
|
|
|
|
|
|
2023-11-12 19:06:23 -08:00
|
|
|
char *string_storage;
|
|
|
|
|
|
2024-05-26 12:17:34 -04:00
|
|
|
SDL_CleanupPropertyCallback cleanup;
|
2023-10-10 12:38:22 -07:00
|
|
|
void *userdata;
|
|
|
|
|
} SDL_Property;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
SDL_HashTable *props;
|
|
|
|
|
SDL_Mutex *lock;
|
|
|
|
|
} SDL_Properties;
|
|
|
|
|
|
2024-09-17 02:55:23 -07:00
|
|
|
static SDL_InitState SDL_properties_init;
|
2023-10-10 12:38:22 -07:00
|
|
|
static SDL_HashTable *SDL_properties;
|
2024-12-12 11:51:27 -08:00
|
|
|
static SDL_AtomicU32 SDL_last_properties_id;
|
2024-09-17 02:55:23 -07:00
|
|
|
static SDL_AtomicU32 SDL_global_properties;
|
2023-10-10 12:38:22 -07:00
|
|
|
|
|
|
|
|
|
2024-08-22 09:21:26 -07:00
|
|
|
static void SDL_FreePropertyWithCleanup(const void *key, const void *value, void *data, bool cleanup)
|
2023-10-10 12:38:22 -07:00
|
|
|
{
|
|
|
|
|
SDL_Property *property = (SDL_Property *)value;
|
2023-11-12 09:47:31 -08:00
|
|
|
if (property) {
|
|
|
|
|
switch (property->type) {
|
|
|
|
|
case SDL_PROPERTY_TYPE_POINTER:
|
2023-12-04 07:48:13 -08:00
|
|
|
if (property->cleanup && cleanup) {
|
2023-11-12 09:47:31 -08:00
|
|
|
property->cleanup(property->userdata, property->value.pointer_value);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_STRING:
|
2024-07-26 18:57:18 -07:00
|
|
|
SDL_free(property->value.string_value);
|
2023-11-12 09:47:31 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-07-26 18:57:18 -07:00
|
|
|
SDL_free(property->string_storage);
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
SDL_free((void *)key);
|
|
|
|
|
SDL_free((void *)value);
|
|
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
static void SDLCALL SDL_FreeProperty(void *data, const void *key, const void *value)
|
2023-12-04 07:48:13 -08:00
|
|
|
{
|
2024-08-22 09:21:26 -07:00
|
|
|
SDL_FreePropertyWithCleanup(key, value, data, true);
|
2023-12-04 07:48:13 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-12 14:09:09 -08:00
|
|
|
static void SDL_FreeProperties(SDL_Properties *properties)
|
2023-10-10 12:38:22 -07:00
|
|
|
{
|
|
|
|
|
if (properties) {
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
SDL_DestroyHashTable(properties->props);
|
|
|
|
|
SDL_DestroyMutex(properties->lock);
|
2023-10-10 12:38:22 -07:00
|
|
|
SDL_free(properties);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
bool SDL_InitProperties(void)
|
2023-10-10 12:38:22 -07:00
|
|
|
{
|
2024-09-17 02:55:23 -07:00
|
|
|
if (!SDL_ShouldInit(&SDL_properties_init)) {
|
|
|
|
|
return true;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
2024-09-17 02:55:23 -07:00
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
SDL_properties = SDL_CreateHashTable(0, true, SDL_HashID, SDL_KeyMatchID, NULL, NULL);
|
|
|
|
|
const bool initialized = (SDL_properties != NULL);
|
|
|
|
|
SDL_SetInitialized(&SDL_properties_init, initialized);
|
|
|
|
|
return initialized;
|
|
|
|
|
}
|
2024-09-17 02:55:23 -07:00
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
static bool SDLCALL FreeOneProperties(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
|
|
|
|
|
{
|
|
|
|
|
SDL_FreeProperties((SDL_Properties *)value);
|
|
|
|
|
return true; // keep iterating.
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SDL_QuitProperties(void)
|
|
|
|
|
{
|
2024-09-17 02:55:23 -07:00
|
|
|
if (!SDL_ShouldQuit(&SDL_properties_init)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_PropertiesID props;
|
|
|
|
|
do {
|
|
|
|
|
props = SDL_GetAtomicU32(&SDL_global_properties);
|
|
|
|
|
} while (!SDL_CompareAndSwapAtomicU32(&SDL_global_properties, props, 0));
|
|
|
|
|
|
|
|
|
|
if (props) {
|
|
|
|
|
SDL_DestroyProperties(props);
|
2023-11-08 11:55:04 -08:00
|
|
|
}
|
2024-09-17 02:55:23 -07:00
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
// this can't just DestroyHashTable with SDL_FreeProperties as the destructor, because
|
|
|
|
|
// other destructors under this might cause use to attempt a recursive lock on SDL_properties,
|
|
|
|
|
// which isn't allowed with rwlocks. So manually iterate and free everything.
|
2025-02-26 08:34:14 -08:00
|
|
|
SDL_HashTable *properties = SDL_properties;
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
SDL_properties = NULL;
|
2025-02-26 08:34:14 -08:00
|
|
|
SDL_IterateHashTable(properties, FreeOneProperties, NULL);
|
|
|
|
|
SDL_DestroyHashTable(properties);
|
2024-09-17 02:55:23 -07:00
|
|
|
|
|
|
|
|
SDL_SetInitialized(&SDL_properties_init, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool SDL_CheckInitProperties(void)
|
|
|
|
|
{
|
|
|
|
|
return SDL_InitProperties();
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-08 11:55:04 -08:00
|
|
|
SDL_PropertiesID SDL_GetGlobalProperties(void)
|
|
|
|
|
{
|
2024-09-17 02:55:23 -07:00
|
|
|
SDL_PropertiesID props = SDL_GetAtomicU32(&SDL_global_properties);
|
|
|
|
|
if (!props) {
|
|
|
|
|
props = SDL_CreateProperties();
|
|
|
|
|
if (!SDL_CompareAndSwapAtomicU32(&SDL_global_properties, 0, props)) {
|
|
|
|
|
// Somebody else created global properties before us, just use those
|
|
|
|
|
SDL_DestroyProperties(props);
|
|
|
|
|
props = SDL_GetAtomicU32(&SDL_global_properties);
|
|
|
|
|
}
|
2023-11-08 11:55:04 -08:00
|
|
|
}
|
2024-09-17 02:55:23 -07:00
|
|
|
return props;
|
2023-11-08 11:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
2023-10-10 12:38:22 -07:00
|
|
|
SDL_PropertiesID SDL_CreateProperties(void)
|
|
|
|
|
{
|
2024-09-17 02:55:23 -07:00
|
|
|
if (!SDL_CheckInitProperties()) {
|
2023-10-10 12:38:22 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
SDL_Properties *properties = (SDL_Properties *)SDL_calloc(1, sizeof(*properties));
|
2023-10-10 12:38:22 -07:00
|
|
|
if (!properties) {
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
return 0;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 02:55:23 -07:00
|
|
|
properties->lock = SDL_CreateMutex();
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
if (!properties->lock) {
|
|
|
|
|
SDL_free(properties);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-10-10 12:38:22 -07:00
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
properties->props = SDL_CreateHashTable(0, false, SDL_HashString, SDL_KeyMatchString, SDL_FreeProperty, NULL);
|
|
|
|
|
if (!properties->props) {
|
|
|
|
|
SDL_DestroyMutex(properties->lock);
|
|
|
|
|
SDL_free(properties);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_PropertiesID props = 0;
|
|
|
|
|
while (true) {
|
2024-12-12 11:51:27 -08:00
|
|
|
props = (SDL_GetAtomicU32(&SDL_last_properties_id) + 1);
|
|
|
|
|
if (props == 0) {
|
|
|
|
|
continue;
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
} else if (SDL_CompareAndSwapAtomicU32(&SDL_last_properties_id, props - 1, props)) {
|
2024-12-12 11:51:27 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
|
|
|
|
|
SDL_assert(!SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, NULL)); // should NOT be in the hash table already.
|
|
|
|
|
|
|
|
|
|
if (!SDL_InsertIntoHashTable(SDL_properties, (const void *)(uintptr_t)props, properties, false)) {
|
|
|
|
|
SDL_FreeProperties(properties);
|
|
|
|
|
return 0;
|
2023-10-25 10:00:26 -04:00
|
|
|
}
|
2023-10-10 12:38:22 -07:00
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
return props; // All done!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct CopyOnePropertyData
|
|
|
|
|
{
|
|
|
|
|
SDL_Properties *dst_properties;
|
|
|
|
|
bool result;
|
|
|
|
|
} CopyOnePropertyData;
|
|
|
|
|
|
|
|
|
|
static bool SDLCALL CopyOneProperty(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
|
|
|
|
|
{
|
|
|
|
|
const SDL_Property *src_property = (const SDL_Property *)value;
|
|
|
|
|
if (src_property->cleanup) {
|
|
|
|
|
// Can't copy properties with cleanup functions, we don't know how to duplicate the data
|
|
|
|
|
return true; // keep iterating.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CopyOnePropertyData *data = (CopyOnePropertyData *) userdata;
|
|
|
|
|
SDL_Properties *dst_properties = data->dst_properties;
|
|
|
|
|
const char *src_name = (const char *)key;
|
|
|
|
|
SDL_Property *dst_property;
|
|
|
|
|
|
|
|
|
|
char *dst_name = SDL_strdup(src_name);
|
|
|
|
|
if (!dst_name) {
|
|
|
|
|
data->result = false;
|
|
|
|
|
return true; // keep iterating (I guess...?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dst_property = (SDL_Property *)SDL_malloc(sizeof(*dst_property));
|
|
|
|
|
if (!dst_property) {
|
|
|
|
|
SDL_free(dst_name);
|
|
|
|
|
data->result = false;
|
|
|
|
|
return true; // keep iterating (I guess...?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_copyp(dst_property, src_property);
|
|
|
|
|
if (src_property->type == SDL_PROPERTY_TYPE_STRING) {
|
|
|
|
|
dst_property->value.string_value = SDL_strdup(src_property->value.string_value);
|
|
|
|
|
if (!dst_property->value.string_value) {
|
|
|
|
|
SDL_free(dst_name);
|
|
|
|
|
SDL_free(dst_property);
|
|
|
|
|
data->result = false;
|
|
|
|
|
return true; // keep iterating (I guess...?)
|
|
|
|
|
}
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
if (!SDL_InsertIntoHashTable(dst_properties->props, dst_name, dst_property, true)) {
|
|
|
|
|
SDL_FreePropertyWithCleanup(dst_name, dst_property, NULL, false);
|
|
|
|
|
data->result = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true; // keep iterating.
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_CopyProperties(SDL_PropertiesID src, SDL_PropertiesID dst)
|
2024-02-02 14:19:02 -08:00
|
|
|
{
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!src) {
|
2024-02-02 14:19:02 -08:00
|
|
|
return SDL_InvalidParamError("src");
|
|
|
|
|
}
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!dst) {
|
2024-02-02 14:19:02 -08:00
|
|
|
return SDL_InvalidParamError("dst");
|
|
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
SDL_Properties *src_properties = NULL;
|
|
|
|
|
SDL_Properties *dst_properties = NULL;
|
|
|
|
|
|
2024-02-02 14:19:02 -08:00
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)src, (const void **)&src_properties);
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!src_properties) {
|
2024-02-02 14:19:02 -08:00
|
|
|
return SDL_InvalidParamError("src");
|
|
|
|
|
}
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)dst, (const void **)&dst_properties);
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!dst_properties) {
|
2024-02-02 14:19:02 -08:00
|
|
|
return SDL_InvalidParamError("dst");
|
|
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
bool result = true;
|
2024-02-02 14:19:02 -08:00
|
|
|
SDL_LockMutex(src_properties->lock);
|
|
|
|
|
SDL_LockMutex(dst_properties->lock);
|
|
|
|
|
{
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
CopyOnePropertyData data = { dst_properties, true };
|
|
|
|
|
SDL_IterateHashTable(src_properties->props, CopyOneProperty, &data);
|
|
|
|
|
result = data.result;
|
2024-02-02 14:19:02 -08:00
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(dst_properties->lock);
|
|
|
|
|
SDL_UnlockMutex(src_properties->lock);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_LockProperties(SDL_PropertiesID props)
|
2023-10-10 12:38:22 -07:00
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!props) {
|
2023-10-10 12:38:22 -07:00
|
|
|
return SDL_InvalidParamError("props");
|
|
|
|
|
}
|
2023-10-25 10:00:26 -04:00
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!properties) {
|
2023-10-25 10:00:26 -04:00
|
|
|
return SDL_InvalidParamError("props");
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
2023-10-25 10:00:26 -04:00
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SDL_UnlockProperties(SDL_PropertiesID props)
|
|
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-25 10:00:26 -04:00
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
2023-10-10 12:38:22 -07:00
|
|
|
if (!properties) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-25 10:00:26 -04:00
|
|
|
|
2023-10-10 12:38:22 -07:00
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
static bool SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_Property *property)
|
2023-10-10 12:38:22 -07:00
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
2024-08-22 17:33:49 -07:00
|
|
|
bool result = true;
|
2023-10-10 12:38:22 -07:00
|
|
|
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!props) {
|
2024-08-22 09:21:26 -07:00
|
|
|
SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
|
2023-10-10 12:38:22 -07:00
|
|
|
return SDL_InvalidParamError("props");
|
|
|
|
|
}
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!name || !*name) {
|
2024-08-22 09:21:26 -07:00
|
|
|
SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
|
2023-10-10 12:38:22 -07:00
|
|
|
return SDL_InvalidParamError("name");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 10:00:26 -04:00
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!properties) {
|
2024-08-22 09:21:26 -07:00
|
|
|
SDL_FreePropertyWithCleanup(NULL, property, NULL, true);
|
2023-10-10 12:38:22 -07:00
|
|
|
return SDL_InvalidParamError("props");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_RemoveFromHashTable(properties->props, name);
|
|
|
|
|
if (property) {
|
|
|
|
|
char *key = SDL_strdup(name);
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
if (!key || !SDL_InsertIntoHashTable(properties->props, key, property, false)) {
|
2024-08-22 09:21:26 -07:00
|
|
|
SDL_FreePropertyWithCleanup(key, property, NULL, true);
|
2024-08-22 17:33:49 -07:00
|
|
|
result = false;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_SetPointerPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *value, SDL_CleanupPropertyCallback cleanup, void *userdata)
|
2023-11-12 09:47:31 -08:00
|
|
|
{
|
|
|
|
|
SDL_Property *property;
|
|
|
|
|
|
|
|
|
|
if (!value) {
|
2024-05-06 23:50:28 +02:00
|
|
|
if (cleanup) {
|
|
|
|
|
cleanup(userdata, value);
|
|
|
|
|
}
|
2023-11-12 09:47:31 -08:00
|
|
|
return SDL_ClearProperty(props, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
|
|
|
|
|
if (!property) {
|
2024-05-06 23:50:28 +02:00
|
|
|
if (cleanup) {
|
|
|
|
|
cleanup(userdata, value);
|
|
|
|
|
}
|
2024-08-22 09:21:26 -07:00
|
|
|
SDL_FreePropertyWithCleanup(NULL, property, NULL, false);
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
property->type = SDL_PROPERTY_TYPE_POINTER;
|
|
|
|
|
property->value.pointer_value = value;
|
|
|
|
|
property->cleanup = cleanup;
|
|
|
|
|
property->userdata = userdata;
|
|
|
|
|
return SDL_PrivateSetProperty(props, name, property);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_SetPointerProperty(SDL_PropertiesID props, const char *name, void *value)
|
2023-11-12 09:47:31 -08:00
|
|
|
{
|
|
|
|
|
SDL_Property *property;
|
|
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
|
return SDL_ClearProperty(props, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
|
|
|
|
|
if (!property) {
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
property->type = SDL_PROPERTY_TYPE_POINTER;
|
|
|
|
|
property->value.pointer_value = value;
|
|
|
|
|
return SDL_PrivateSetProperty(props, name, property);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 17:29:20 +03:00
|
|
|
static void SDLCALL CleanupFreeableProperty(void *userdata, void *value)
|
2024-02-10 08:04:27 -08:00
|
|
|
{
|
|
|
|
|
SDL_free(value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
bool SDL_SetFreeableProperty(SDL_PropertiesID props, const char *name, void *value)
|
2024-02-10 08:04:27 -08:00
|
|
|
{
|
2024-07-12 09:39:58 -07:00
|
|
|
return SDL_SetPointerPropertyWithCleanup(props, name, value, CleanupFreeableProperty, NULL);
|
2024-02-10 08:04:27 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-11 17:29:20 +03:00
|
|
|
static void SDLCALL CleanupSurface(void *userdata, void *value)
|
2024-02-09 07:09:59 -08:00
|
|
|
{
|
|
|
|
|
SDL_Surface *surface = (SDL_Surface *)value;
|
|
|
|
|
|
|
|
|
|
SDL_DestroySurface(surface);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
bool SDL_SetSurfaceProperty(SDL_PropertiesID props, const char *name, SDL_Surface *surface)
|
2024-02-09 07:09:59 -08:00
|
|
|
{
|
2024-07-12 09:39:58 -07:00
|
|
|
return SDL_SetPointerPropertyWithCleanup(props, name, surface, CleanupSurface, NULL);
|
2024-02-09 07:09:59 -08:00
|
|
|
}
|
2023-11-12 09:47:31 -08:00
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *value)
|
2023-11-12 09:47:31 -08:00
|
|
|
{
|
|
|
|
|
SDL_Property *property;
|
|
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
|
return SDL_ClearProperty(props, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
|
|
|
|
|
if (!property) {
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
property->type = SDL_PROPERTY_TYPE_STRING;
|
|
|
|
|
property->value.string_value = SDL_strdup(value);
|
|
|
|
|
if (!property->value.string_value) {
|
|
|
|
|
SDL_free(property);
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
return SDL_PrivateSetProperty(props, name, property);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value)
|
2023-11-12 09:47:31 -08:00
|
|
|
{
|
|
|
|
|
SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
|
|
|
|
|
if (!property) {
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
property->type = SDL_PROPERTY_TYPE_NUMBER;
|
|
|
|
|
property->value.number_value = value;
|
|
|
|
|
return SDL_PrivateSetProperty(props, name, property);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value)
|
2023-11-12 09:47:31 -08:00
|
|
|
{
|
|
|
|
|
SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
|
|
|
|
|
if (!property) {
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
property->type = SDL_PROPERTY_TYPE_FLOAT;
|
|
|
|
|
property->value.float_value = value;
|
|
|
|
|
return SDL_PrivateSetProperty(props, name, property);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, bool value)
|
2023-11-12 18:34:22 -08:00
|
|
|
{
|
|
|
|
|
SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
|
|
|
|
|
if (!property) {
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2023-11-12 18:34:22 -08:00
|
|
|
}
|
|
|
|
|
property->type = SDL_PROPERTY_TYPE_BOOLEAN;
|
2024-08-22 09:21:26 -07:00
|
|
|
property->value.boolean_value = value ? true : false;
|
2023-11-12 18:34:22 -08:00
|
|
|
return SDL_PrivateSetProperty(props, name, property);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_HasProperty(SDL_PropertiesID props, const char *name)
|
2024-02-17 07:59:04 -08:00
|
|
|
{
|
|
|
|
|
return (SDL_GetPropertyType(props, name) != SDL_PROPERTY_TYPE_INVALID);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 09:47:31 -08:00
|
|
|
SDL_PropertyType SDL_GetPropertyType(SDL_PropertiesID props, const char *name)
|
|
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
SDL_PropertyType type = SDL_PROPERTY_TYPE_INVALID;
|
|
|
|
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
return SDL_PROPERTY_TYPE_INVALID;
|
|
|
|
|
}
|
|
|
|
|
if (!name || !*name) {
|
|
|
|
|
return SDL_PROPERTY_TYPE_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
|
|
|
|
if (!properties) {
|
|
|
|
|
return SDL_PROPERTY_TYPE_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_Property *property = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
|
|
|
|
type = property->type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 09:39:58 -07:00
|
|
|
void *SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *default_value)
|
2023-10-10 12:38:22 -07:00
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
2023-11-12 09:47:31 -08:00
|
|
|
void *value = default_value;
|
2023-10-10 12:38:22 -07:00
|
|
|
|
|
|
|
|
if (!props) {
|
2023-11-12 09:47:31 -08:00
|
|
|
return value;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
if (!name || !*name) {
|
2023-11-12 09:47:31 -08:00
|
|
|
return value;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-25 10:00:26 -04:00
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
2023-10-10 12:38:22 -07:00
|
|
|
if (!properties) {
|
2023-11-12 09:47:31 -08:00
|
|
|
return value;
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
// Note that taking the lock here only guarantees that we won't read the
|
|
|
|
|
// hashtable while it's being modified. The value itself can easily be
|
|
|
|
|
// freed from another thread after it is returned here.
|
2023-10-10 12:38:22 -07:00
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_Property *property = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
2023-11-12 09:47:31 -08:00
|
|
|
if (property->type == SDL_PROPERTY_TYPE_POINTER) {
|
|
|
|
|
value = property->value.pointer_value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *SDL_GetStringProperty(SDL_PropertiesID props, const char *name, const char *default_value)
|
|
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
const char *value = default_value;
|
|
|
|
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (!name || !*name) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
|
|
|
|
if (!properties) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_Property *property = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
2023-11-12 19:06:23 -08:00
|
|
|
switch (property->type) {
|
|
|
|
|
case SDL_PROPERTY_TYPE_STRING:
|
2023-11-12 09:47:31 -08:00
|
|
|
value = property->value.string_value;
|
2023-11-12 19:06:23 -08:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_NUMBER:
|
|
|
|
|
if (property->string_storage) {
|
|
|
|
|
value = property->string_storage;
|
|
|
|
|
} else {
|
2024-01-18 17:25:50 +03:00
|
|
|
SDL_asprintf(&property->string_storage, "%" SDL_PRIs64, property->value.number_value);
|
2023-11-12 19:06:23 -08:00
|
|
|
if (property->string_storage) {
|
|
|
|
|
value = property->string_storage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_FLOAT:
|
|
|
|
|
if (property->string_storage) {
|
|
|
|
|
value = property->string_storage;
|
|
|
|
|
} else {
|
|
|
|
|
SDL_asprintf(&property->string_storage, "%f", property->value.float_value);
|
|
|
|
|
if (property->string_storage) {
|
|
|
|
|
value = property->string_storage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_BOOLEAN:
|
|
|
|
|
value = property->value.boolean_value ? "true" : "false";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sint64 SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 default_value)
|
|
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
Sint64 value = default_value;
|
|
|
|
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (!name || !*name) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
|
|
|
|
if (!properties) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_Property *property = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
2023-11-12 19:06:23 -08:00
|
|
|
switch (property->type) {
|
|
|
|
|
case SDL_PROPERTY_TYPE_STRING:
|
2024-09-09 14:54:50 -07:00
|
|
|
value = (Sint64)SDL_strtoll(property->value.string_value, NULL, 0);
|
2023-11-12 19:06:23 -08:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_NUMBER:
|
2023-11-12 09:47:31 -08:00
|
|
|
value = property->value.number_value;
|
2023-11-12 19:06:23 -08:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_FLOAT:
|
|
|
|
|
value = (Sint64)SDL_round((double)property->value.float_value);
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_BOOLEAN:
|
|
|
|
|
value = property->value.boolean_value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float SDL_GetFloatProperty(SDL_PropertiesID props, const char *name, float default_value)
|
|
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
float value = default_value;
|
|
|
|
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (!name || !*name) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
|
|
|
|
if (!properties) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_Property *property = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
2023-11-12 19:06:23 -08:00
|
|
|
switch (property->type) {
|
|
|
|
|
case SDL_PROPERTY_TYPE_STRING:
|
|
|
|
|
value = (float)SDL_atof(property->value.string_value);
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_NUMBER:
|
|
|
|
|
value = (float)property->value.number_value;
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_FLOAT:
|
2023-11-12 09:47:31 -08:00
|
|
|
value = property->value.float_value;
|
2023-11-12 19:06:23 -08:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_BOOLEAN:
|
|
|
|
|
value = (float)property->value.boolean_value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_GetBooleanProperty(SDL_PropertiesID props, const char *name, bool default_value)
|
2023-11-12 18:34:22 -08:00
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
2024-08-22 09:21:26 -07:00
|
|
|
bool value = default_value ? true : false;
|
2023-11-12 18:34:22 -08:00
|
|
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (!name || !*name) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
|
|
|
|
if (!properties) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
|
|
|
|
SDL_Property *property = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
|
2023-11-12 19:06:23 -08:00
|
|
|
switch (property->type) {
|
|
|
|
|
case SDL_PROPERTY_TYPE_STRING:
|
|
|
|
|
value = SDL_GetStringBoolean(property->value.string_value, default_value);
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_NUMBER:
|
|
|
|
|
value = (property->value.number_value != 0);
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_FLOAT:
|
|
|
|
|
value = (property->value.float_value != 0.0f);
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_BOOLEAN:
|
2023-11-12 18:34:22 -08:00
|
|
|
value = property->value.boolean_value;
|
2023-11-12 19:06:23 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2023-11-12 18:34:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_ClearProperty(SDL_PropertiesID props, const char *name)
|
2023-10-11 16:59:51 -07:00
|
|
|
{
|
2023-11-12 09:47:31 -08:00
|
|
|
return SDL_PrivateSetProperty(props, name, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
typedef struct EnumerateOnePropertyData
|
|
|
|
|
{
|
|
|
|
|
SDL_EnumeratePropertiesCallback callback;
|
|
|
|
|
void *userdata;
|
|
|
|
|
SDL_PropertiesID props;
|
|
|
|
|
} EnumerateOnePropertyData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool SDLCALL EnumerateOneProperty(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
|
|
|
|
|
{
|
|
|
|
|
(void) table;
|
|
|
|
|
(void) value;
|
|
|
|
|
const EnumerateOnePropertyData *data = (const EnumerateOnePropertyData *) userdata;
|
|
|
|
|
data->callback(data->userdata, data->props, (const char *)key);
|
|
|
|
|
return true; // keep iterating.
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 07:52:28 -07:00
|
|
|
bool SDL_EnumerateProperties(SDL_PropertiesID props, SDL_EnumeratePropertiesCallback callback, void *userdata)
|
2023-11-12 09:47:31 -08:00
|
|
|
{
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!props) {
|
2023-11-12 09:47:31 -08:00
|
|
|
return SDL_InvalidParamError("props");
|
|
|
|
|
}
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!callback) {
|
2023-11-12 09:47:31 -08:00
|
|
|
return SDL_InvalidParamError("callback");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties);
|
2025-09-16 21:51:03 -07:00
|
|
|
CHECK_PARAM(!properties) {
|
2023-11-12 09:47:31 -08:00
|
|
|
return SDL_InvalidParamError("props");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_LockMutex(properties->lock);
|
|
|
|
|
{
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
EnumerateOnePropertyData data = { callback, userdata, props };
|
|
|
|
|
SDL_IterateHashTable(properties->props, EnumerateOneProperty, &data);
|
2023-11-12 09:47:31 -08:00
|
|
|
}
|
|
|
|
|
SDL_UnlockMutex(properties->lock);
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2023-10-11 16:59:51 -07:00
|
|
|
}
|
|
|
|
|
|
2024-05-29 10:30:19 -07:00
|
|
|
static void SDLCALL SDL_DumpPropertiesCallback(void *userdata, SDL_PropertiesID props, const char *name)
|
|
|
|
|
{
|
|
|
|
|
switch (SDL_GetPropertyType(props, name)) {
|
|
|
|
|
case SDL_PROPERTY_TYPE_POINTER:
|
2025-01-22 12:59:57 -08:00
|
|
|
SDL_Log("%s: %p", name, SDL_GetPointerProperty(props, name, NULL));
|
2024-05-29 10:30:19 -07:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_STRING:
|
2025-01-22 12:59:57 -08:00
|
|
|
SDL_Log("%s: \"%s\"", name, SDL_GetStringProperty(props, name, ""));
|
2024-05-29 10:30:19 -07:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_NUMBER:
|
|
|
|
|
{
|
|
|
|
|
Sint64 value = SDL_GetNumberProperty(props, name, 0);
|
2025-01-22 12:59:57 -08:00
|
|
|
SDL_Log("%s: %" SDL_PRIs64 " (%" SDL_PRIx64 ")", name, value, value);
|
2024-05-29 10:30:19 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_FLOAT:
|
2025-01-22 12:59:57 -08:00
|
|
|
SDL_Log("%s: %g", name, SDL_GetFloatProperty(props, name, 0.0f));
|
2024-05-29 10:30:19 -07:00
|
|
|
break;
|
|
|
|
|
case SDL_PROPERTY_TYPE_BOOLEAN:
|
2025-01-22 12:59:57 -08:00
|
|
|
SDL_Log("%s: %s", name, SDL_GetBooleanProperty(props, name, false) ? "true" : "false");
|
2024-05-29 10:30:19 -07:00
|
|
|
break;
|
|
|
|
|
default:
|
2025-01-22 12:59:57 -08:00
|
|
|
SDL_Log("%s UNKNOWN TYPE", name);
|
2024-05-29 10:30:19 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
bool SDL_DumpProperties(SDL_PropertiesID props)
|
2024-05-29 10:30:19 -07:00
|
|
|
{
|
|
|
|
|
return SDL_EnumerateProperties(props, SDL_DumpPropertiesCallback, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 12:38:22 -07:00
|
|
|
void SDL_DestroyProperties(SDL_PropertiesID props)
|
|
|
|
|
{
|
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
|
|
|
if (props) {
|
|
|
|
|
// this can't just use RemoveFromHashTable with SDL_FreeProperties as the destructor, because
|
|
|
|
|
// other destructors under this might cause use to attempt a recursive lock on SDL_properties,
|
|
|
|
|
// which isn't allowed with rwlocks. So manually look it up and remove/free it.
|
|
|
|
|
SDL_Properties *properties = NULL;
|
|
|
|
|
if (SDL_FindInHashTable(SDL_properties, (const void *)(uintptr_t)props, (const void **)&properties)) {
|
|
|
|
|
SDL_FreeProperties(properties);
|
|
|
|
|
SDL_RemoveFromHashTable(SDL_properties, (const void *)(uintptr_t)props);
|
|
|
|
|
}
|
2024-12-12 14:09:09 -08:00
|
|
|
}
|
2023-10-10 12:38:22 -07:00
|
|
|
}
|