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!).
This commit is contained in:
Ryan C. Gordon committed 2025-02-15 18:52:56 -05:00
1 parent 4a9b579195
commit 84a236c92e
13 files changed
+1344 -866

No files matched your search

+101 -83
View File
@@ -651,7 +651,7 @@ static SDL_AudioDevice *CreatePhysicalAudioDevice(const char *name, bool recordi
device->instance_id = AssignAudioDeviceInstanceId(recording, /*islogical=*/false);
SDL_LockRWLockForWriting(current_audio.device_hash_lock);
if (SDL_InsertIntoHashTable(current_audio.device_hash, (const void *) (uintptr_t) device->instance_id, device)) {
if (SDL_InsertIntoHashTable(current_audio.device_hash, (const void *) (uintptr_t) device->instance_id, device, false)) {
SDL_AddAtomicInt(device_count, 1);
} else {
SDL_DestroyCondition(device->close_cond);
@@ -865,50 +865,47 @@ static void CompleteAudioEntryPoints(void)
#undef FILL_STUB
}
static SDL_AudioDevice *GetFirstAddedAudioDevice(const bool recording)
typedef struct FindLowestDeviceIDData
{
SDL_AudioDeviceID highest = (SDL_AudioDeviceID) SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK; // According to AssignAudioDeviceInstanceId, nothing can have a value this large.
SDL_AudioDevice *result = NULL;
const bool recording;
SDL_AudioDeviceID highest;
SDL_AudioDevice *result;
} FindLowestDeviceIDData;
// (Device IDs increase as new devices are added, so the first device added has the lowest SDL_AudioDeviceID value.)
SDL_LockRWLockForReading(current_audio.device_hash_lock);
const void *key;
const void *value;
void *iter = NULL;
while (SDL_IterateHashTable(current_audio.device_hash, &key, &value, &iter)) {
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
// bit #0 of devid is set for playback devices and unset for recording.
// bit #1 of devid is set for physical devices and unset for logical.
const bool devid_recording = !(devid & (1 << 0));
const bool isphysical = !!(devid & (1 << 1));
if (isphysical && (devid_recording == recording) && (devid < highest)) {
highest = devid;
result = (SDL_AudioDevice *) value;
}
static bool SDLCALL FindLowestDeviceID(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
{
FindLowestDeviceIDData *data = (FindLowestDeviceIDData *) userdata;
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
// bit #0 of devid is set for playback devices and unset for recording.
// bit #1 of devid is set for physical devices and unset for logical.
const bool devid_recording = !(devid & (1 << 0));
const bool isphysical = !!(devid & (1 << 1));
if (isphysical && (devid_recording == data->recording) && (devid < data->highest)) {
data->highest = devid;
data->result = (SDL_AudioDevice *) value;
}
SDL_UnlockRWLock(current_audio.device_hash_lock);
return result;
return true; // keep iterating.
}
static Uint32 HashAudioDeviceID(const void *key, void *data)
static SDL_AudioDevice *GetFirstAddedAudioDevice(const bool recording)
{
const SDL_AudioDeviceID highest = (SDL_AudioDeviceID) SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK; // According to AssignAudioDeviceInstanceId, nothing can have a value this large.
// (Device IDs increase as new devices are added, so the first device added has the lowest SDL_AudioDeviceID value.)
FindLowestDeviceIDData data = { recording, highest, NULL };
SDL_LockRWLockForReading(current_audio.device_hash_lock);
SDL_IterateHashTable(current_audio.device_hash, FindLowestDeviceID, &data);
SDL_UnlockRWLock(current_audio.device_hash_lock);
return data.result;
}
static Uint32 SDLCALL HashAudioDeviceID(void *userdata, const void *key)
{
// shift right 2, to dump the first two bits, since these are flags
// (recording vs playback, logical vs physical) and the rest are unique incrementing integers.
return ((Uint32) ((uintptr_t) key)) >> 2;
}
static bool MatchAudioDeviceID(const void *a, const void *b, void *data)
{
return (a == b);
}
static void NukeAudioDeviceHashItem(const void *key, const void *value, void *data)
{
// no-op, keys and values in this hashtable are treated as Plain Old Data and don't get freed here.
}
// !!! FIXME: the video subsystem does SDL_VideoInit, not SDL_InitVideo. Make this match.
bool SDL_InitAudio(const char *driver_name)
{
@@ -927,7 +924,7 @@ bool SDL_InitAudio(const char *driver_name)
return false;
}
SDL_HashTable *device_hash = SDL_CreateHashTable(NULL, 8, HashAudioDeviceID, MatchAudioDeviceID, NukeAudioDeviceHashItem, false, false);
SDL_HashTable *device_hash = SDL_CreateHashTable(0, false, HashAudioDeviceID, SDL_KeyMatchID, NULL, NULL);
if (!device_hash) {
SDL_DestroyRWLock(device_hash_lock);
return false;
@@ -1048,6 +1045,17 @@ bool SDL_InitAudio(const char *driver_name)
return true;
}
static bool SDLCALL DestroyOnePhysicalAudioDevice(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
{
// bit #1 of devid is set for physical devices and unset for logical.
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
const bool isphysical = !!(devid & (1<<1));
if (isphysical) {
DestroyPhysicalAudioDevice((SDL_AudioDevice *) value);
}
return true; // keep iterating.
}
void SDL_QuitAudio(void)
{
if (!current_audio.name) { // not initialized?!
@@ -1077,17 +1085,7 @@ void SDL_QuitAudio(void)
SDL_free(i);
}
const void *key;
const void *value;
void *iter = NULL;
while (SDL_IterateHashTable(device_hash, &key, &value, &iter)) {
// bit #1 of devid is set for physical devices and unset for logical.
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
const bool isphysical = !!(devid & (1<<1));
if (isphysical) {
DestroyPhysicalAudioDevice((SDL_AudioDevice *) value);
}
}
SDL_IterateHashTable(device_hash, DestroyOnePhysicalAudioDevice, NULL);
// Free the driver data
current_audio.impl.Deinitialize();
@@ -1384,6 +1382,28 @@ static int SDLCALL RecordingAudioThread(void *devicep) // thread entry point
return 0;
}
typedef struct CountAudioDevicesData
{
int devs_seen;
const int num_devices;
SDL_AudioDeviceID *result;
const bool recording;
} CountAudioDevicesData;
static bool SDLCALL CountAudioDevices(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
{
CountAudioDevicesData *data = (CountAudioDevicesData *) userdata;
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
// bit #0 of devid is set for playback devices and unset for recording.
// bit #1 of devid is set for physical devices and unset for logical.
const bool devid_recording = !(devid & (1<<0));
const bool isphysical = !!(devid & (1<<1));
if (isphysical && (devid_recording == data->recording)) {
SDL_assert(data->devs_seen < data->num_devices);
data->result[data->devs_seen++] = devid;
}
return true; // keep iterating.
}
static SDL_AudioDeviceID *GetAudioDevices(int *count, bool recording)
{
@@ -1396,24 +1416,10 @@ static SDL_AudioDeviceID *GetAudioDevices(int *count, bool recording)
num_devices = SDL_GetAtomicInt(recording ? &current_audio.recording_device_count : &current_audio.playback_device_count);
result = (SDL_AudioDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_AudioDeviceID));
if (result) {
int devs_seen = 0;
const void *key;
const void *value;
void *iter = NULL;
while (SDL_IterateHashTable(current_audio.device_hash, &key, &value, &iter)) {
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
// bit #0 of devid is set for playback devices and unset for recording.
// bit #1 of devid is set for physical devices and unset for logical.
const bool devid_recording = !(devid & (1<<0));
const bool isphysical = !!(devid & (1<<1));
if (isphysical && (devid_recording == recording)) {
SDL_assert(devs_seen < num_devices);
result[devs_seen++] = devid;
}
}
SDL_assert(devs_seen == num_devices);
result[devs_seen] = 0; // null-terminated.
CountAudioDevicesData data = { 0, num_devices, result, recording };
SDL_IterateHashTable(current_audio.device_hash, CountAudioDevices, &data);
SDL_assert(data.devs_seen == num_devices);
result[data.devs_seen] = 0; // null-terminated.
}
}
SDL_UnlockRWLock(current_audio.device_hash_lock);
@@ -1441,7 +1447,30 @@ SDL_AudioDeviceID *SDL_GetAudioRecordingDevices(int *count)
return GetAudioDevices(count, true);
}
typedef struct FindAudioDeviceByCallbackData
{
bool (*callback)(SDL_AudioDevice *device, void *userdata);
void *userdata;
SDL_AudioDevice *retval;
} FindAudioDeviceByCallbackData;
static bool SDLCALL FindAudioDeviceByCallback(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
{
FindAudioDeviceByCallbackData *data = (FindAudioDeviceByCallbackData *) userdata;
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
// bit #1 of devid is set for physical devices and unset for logical.
const bool isphysical = !!(devid & (1<<1));
if (isphysical) {
SDL_AudioDevice *device = (SDL_AudioDevice *) value;
if (data->callback(device, data->userdata)) { // found it?
data->retval = device;
return false; // stop iterating, we found it.
}
}
return true; // keep iterating.
}
// !!! FIXME: SDL convention is for userdata to come first in the callback's params. Fix this at some point.
SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(bool (*callback)(SDL_AudioDevice *device, void *userdata), void *userdata)
{
if (!SDL_GetCurrentAudioDriver()) {
@@ -1449,27 +1478,16 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(bool (*callback)(SDL_Audi
return NULL;
}
const void *key;
const void *value;
void *iter = NULL;
FindAudioDeviceByCallbackData data = { callback, userdata, NULL };
SDL_LockRWLockForReading(current_audio.device_hash_lock);
while (SDL_IterateHashTable(current_audio.device_hash, &key, &value, &iter)) {
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
// bit #1 of devid is set for physical devices and unset for logical.
const bool isphysical = !!(devid & (1<<1));
if (isphysical) {
SDL_AudioDevice *device = (SDL_AudioDevice *) value;
if (callback(device, userdata)) { // found it?
SDL_UnlockRWLock(current_audio.device_hash_lock);
return device;
}
}
}
SDL_IterateHashTable(current_audio.device_hash, FindAudioDeviceByCallback, &data);
SDL_UnlockRWLock(current_audio.device_hash_lock);
SDL_SetError("Device not found");
return NULL;
if (!data.retval) {
SDL_SetError("Device not found");
}
return data.retval;
}
static bool TestDeviceHandleCallback(SDL_AudioDevice *device, void *handle)
@@ -1795,7 +1813,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
if (result) {
SDL_LockRWLockForWriting(current_audio.device_hash_lock);
const bool inserted = SDL_InsertIntoHashTable(current_audio.device_hash, (const void *) (uintptr_t) result, logdev);
const bool inserted = SDL_InsertIntoHashTable(current_audio.device_hash, (const void *) (uintptr_t) result, logdev, false);
SDL_UnlockRWLock(current_audio.device_hash_lock);
if (!inserted) {
SDL_CloseAudioDevice(result);