This a simple table which maps pointers to pointers.
---
 mingw-w64-libraries/winpthreads/src/misc.c | 175 +++++++++++++++++++++
 mingw-w64-libraries/winpthreads/src/misc.h |  26 +++
 2 files changed, 201 insertions(+)

diff --git a/mingw-w64-libraries/winpthreads/src/misc.c 
b/mingw-w64-libraries/winpthreads/src/misc.c
index b2d5cebc..ea8d3551 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -32,6 +32,10 @@
 /* internal header files */
 #include "misc.h"
 
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
 void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME) = NULL;
 static ULONGLONG (WINAPI *_pthread_get_tick_count_64) (VOID);
 HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR) = NULL;
@@ -232,3 +236,174 @@ _pthread_wait_for_multiple_objects (unsigned long count, 
void **handles, unsigne
 
   return result;
 }
+
+#define TABLE_ALLOCATION_MINIMUM 8U
+#define TABLE_ALLOCATION_MAXIMUM (UINT_MAX / sizeof 
(winpthreads_table_entry_t))
+
+/**
+ * winpthreads_table_initialize:
+ *
+ * Initializes a table that is dynamically allocated. Note that
+ * zero-initialization works fine, so a default-initialized
+ * winpthreads_table_t need not be initialized explicitly.
+ */
+void
+winpthreads_table_initialize (winpthreads_table_t *table)
+{
+  memset (table, 0, sizeof (*table));
+}
+
+/**
+ * winpthreads_table_finalize:
+ *
+ * Frees internal data associated with the table. Values are not
+ * freed, that's responsibility of the user.
+ */
+void
+winpthreads_table_finalize (winpthreads_table_t *table)
+{
+  free (table->entries);
+
+  memset (table, 0, sizeof (*table));
+}
+
+static int
+table_try_expand (winpthreads_table_t *table)
+{
+#if defined (__STD_C_VERSION__) && __STD_C_VERSION__ >= 201112L
+  _Static_assert (SIZE_MAX >= UINT_MAX, "");
+#endif
+
+  if (table->entries_allocated > TABLE_ALLOCATION_MAXIMUM / 2)
+    return FALSE;
+
+  unsigned int new_entries_allocated = MAX (TABLE_ALLOCATION_MINIMUM, 
table->entries_allocated * 2);
+  winpthreads_table_entry_t *new_entries = realloc (table->entries, 
new_entries_allocated * sizeof (winpthreads_table_entry_t));
+
+  if (!new_entries)
+    return FALSE;
+
+  memset (new_entries + table->entries_allocated, 0,
+          (new_entries_allocated - table->entries_allocated) * sizeof 
(winpthreads_table_entry_t));
+
+  table->entries = new_entries;
+  table->entries_allocated = new_entries_allocated;
+
+  return TRUE;
+}
+
+static void
+table_maybe_shrink (winpthreads_table_t *table)
+{
+  if (table->entries_count <= table->entries_allocated / 4 &&
+      table->entries_allocated / 2 >= TABLE_ALLOCATION_MINIMUM)
+  {
+    /* Move busy entries on the right-half into free
+     * entries on the left-half
+     */
+    unsigned int index_right = table->entries_allocated - 1;
+    unsigned int index_left = 0;
+
+    for (; index_right >= table->entries_allocated / 2; index_right--) {
+      if (table->entries[index_right].key != NULL)
+      {
+        for (;; index_left++) {
+          if (table->entries[index_left].key == NULL)
+          {
+            memcpy (&table->entries[index_left],
+                    &table->entries[index_right],
+                    sizeof (table->entries[0]));
+            index_left++;
+            break;
+          }
+        }
+
+        assert (index_left < table->entries_allocated);
+      }
+    }
+
+    unsigned int new_entries_allocated = table->entries_allocated / 2;
+    winpthreads_table_entry_t *new_entries = realloc (table->entries, 
new_entries_allocated * sizeof (winpthreads_table_entry_t));
+
+    if (new_entries == NULL)
+      return;
+
+    table->entries_allocated = new_entries_allocated;
+    table->entries = new_entries;
+  }
+}
+
+/**
+ * winpthreads_table_find_entry:
+ *
+ * Finds an entry for the given key. Returns NULL if not present.
+ * You can pass NULL to search for a free entry, but that's used
+ * only by the implementation; users shouldn't call this function
+ * wiht a NULL key.
+ */
+winpthreads_table_entry_t *
+winpthreads_table_find_entry (winpthreads_table_t *table,
+                              void                *key)
+{
+  for (unsigned int index = 0; index < table->entries_allocated; index++) {
+    if (table->entries[index].key == key)
+      return &table->entries[index];
+  }
+
+  return NULL;
+}
+
+/**
+ * winpthreads_table_add_entry:
+ *
+ * Add a new entry for the given key, expanding the table if needed.
+ * Note that there are a few constraints:
+ *
+ *  - key must not be NULL
+ *  - key must not be present already in the table.
+ *
+ * This function is failable. If memory cannot be reallocated, the
+ * return value is NULL.
+ */
+winpthreads_table_entry_t *
+winpthreads_table_add_entry (winpthreads_table_t *table,
+                             void                *key)
+{
+  winpthreads_table_entry_t *entry = NULL;
+
+  assert (table->entries_count <= table->entries_allocated);
+
+  if (table->entries_count < table->entries_allocated || table_try_expand 
(table)) {
+    assert (table->entries_count < table->entries_allocated);
+
+    /* Find a free entry (NULL key) */
+    entry = winpthreads_table_find_entry (table, NULL);
+    entry->key = key;
+
+    table->entries_count++;
+  }
+
+  return entry;
+}
+
+
+/**
+ * winpthreads_table_remove_entry:
+ *
+ * Remove an entry from the table. It's an error to pass an entry
+ * that belongs to a different table.
+ */
+void
+winpthreads_table_remove_entry (winpthreads_table_t       *table,
+                                winpthreads_table_entry_t *entry)
+{
+  assert ((uintptr_t)entry >= (uintptr_t)&table->entries[0] &&
+          (uintptr_t)entry < 
(uintptr_t)&table->entries[table->entries_allocated]);
+
+  memset (entry, 0, sizeof (*entry));
+
+  assert (table->entries_count > 0);
+  table->entries_count--;
+
+  table_maybe_shrink (table);
+}
diff --git a/mingw-w64-libraries/winpthreads/src/misc.h 
b/mingw-w64-libraries/winpthreads/src/misc.h
index dc11eaff..cc9e3df4 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.h
+++ b/mingw-w64-libraries/winpthreads/src/misc.h
@@ -95,6 +95,28 @@ unsigned long _pthread_wait_for_multiple_objects (unsigned 
long count, void **ha
 extern void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME);
 extern HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR);
 
+typedef struct {
+  void *key;
+  void *value;
+} winpthreads_table_entry_t;
+
+typedef struct {
+  winpthreads_table_entry_t *entries;
+  unsigned int entries_count;
+  unsigned int entries_allocated;
+} winpthreads_table_t;
+
+void                        winpthreads_table_initialize   
(winpthreads_table_t *table);
+void                        winpthreads_table_finalize     
(winpthreads_table_t *table);
+
+winpthreads_table_entry_t * winpthreads_table_find_entry   
(winpthreads_table_t *table,
+                                                            void               
 *key);
+winpthreads_table_entry_t * winpthreads_table_add_entry    
(winpthreads_table_t *table,
+                                                            void               
 *key);
+void                        winpthreads_table_remove_entry 
(winpthreads_table_t       *table,
+                                                            
winpthreads_table_entry_t *entry);
+
+
 #if defined(__GNUC__) || defined(__clang__)
 #define likely(cond) __builtin_expect((cond) != 0, 1)
 #define unlikely(cond) __builtin_expect((cond) != 0, 0)
@@ -109,4 +131,8 @@ extern HRESULT (WINAPI *_pthread_set_thread_description) 
(HANDLE, PCWSTR);
 #define UNREACHABLE() __assume(0)
 #endif
 
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
 #endif
-- 
2.55.0.windows.5



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to