=?UTF-8?Q?J=C3=B3zef=20Kucia=20?=: wined3d: Introduce helper function to reserve memory for arrays.

Alexandre Julliard julliard at winehq.org
Tue Feb 28 15:27:04 CST 2017


Module: wine
Branch: master
Commit: 36b9bc946b9cb73142af28cef36f9fb598c7e7fb
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=36b9bc946b9cb73142af28cef36f9fb598c7e7fb

Author: Józef Kucia <jkucia at codeweavers.com>
Date:   Tue Feb 28 14:42:36 2017 +0100

wined3d: Introduce helper function to reserve memory for arrays.

Signed-off-by: Józef Kucia <jkucia at codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/wined3d/buffer.c          | 15 ++++--------
 dlls/wined3d/context.c         | 54 ++++++++++++------------------------------
 dlls/wined3d/utils.c           | 30 +++++++++++++++++++++++
 dlls/wined3d/wined3d_main.c    | 25 +++++--------------
 dlls/wined3d/wined3d_private.h | 16 +++++++------
 5 files changed, 64 insertions(+), 76 deletions(-)

diff --git a/dlls/wined3d/buffer.c b/dlls/wined3d/buffer.c
index 5ea7eff..c584147 100644
--- a/dlls/wined3d/buffer.c
+++ b/dlls/wined3d/buffer.c
@@ -64,18 +64,11 @@ static void buffer_invalidate_bo_range(struct wined3d_buffer *buffer, unsigned i
         goto invalidate_all;
     }
 
-    if (buffer->modified_areas >= buffer->maps_size)
+    if (!wined3d_array_reserve((void **)&buffer->maps, &buffer->maps_size,
+            buffer->modified_areas + 1, sizeof(*buffer->maps)))
     {
-        struct wined3d_map_range *new;
-
-        if (!(new = HeapReAlloc(GetProcessHeap(), 0, buffer->maps, 2 * buffer->maps_size * sizeof(*buffer->maps))))
-        {
-            ERR("Failed to allocate maps array, invalidating entire buffer.\n");
-            goto invalidate_all;
-        }
-
-        buffer->maps = new;
-        buffer->maps_size *= 2;
+        ERR("Failed to allocate maps array, invalidating entire buffer.\n");
+        goto invalidate_all;
     }
 
     buffer->maps[buffer->modified_areas].offset = offset;
diff --git a/dlls/wined3d/context.c b/dlls/wined3d/context.c
index 076ebee..10a7222 100644
--- a/dlls/wined3d/context.c
+++ b/dlls/wined3d/context.c
@@ -719,20 +719,12 @@ void context_free_occlusion_query(struct wined3d_occlusion_query *query)
     list_remove(&query->entry);
     query->context = NULL;
 
-    if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
+    if (!wined3d_array_reserve((void **)&context->free_occlusion_queries,
+            &context->free_occlusion_query_size, context->free_occlusion_query_count + 1,
+            sizeof(*context->free_occlusion_queries)))
     {
-        UINT new_size = context->free_occlusion_query_size << 1;
-        GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
-                new_size * sizeof(*context->free_occlusion_queries));
-
-        if (!new_data)
-        {
-            ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
-            return;
-        }
-
-        context->free_occlusion_query_size = new_size;
-        context->free_occlusion_queries = new_data;
+        ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
+        return;
     }
 
     context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
@@ -787,20 +779,12 @@ void context_free_event_query(struct wined3d_event_query *query)
     list_remove(&query->entry);
     query->context = NULL;
 
-    if (context->free_event_query_count >= context->free_event_query_size - 1)
+    if (!wined3d_array_reserve((void **)&context->free_event_queries,
+            &context->free_event_query_size, context->free_event_query_count + 1,
+            sizeof(*context->free_event_queries)))
     {
-        UINT new_size = context->free_event_query_size << 1;
-        union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
-                new_size * sizeof(*context->free_event_queries));
-
-        if (!new_data)
-        {
-            ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
-            return;
-        }
-
-        context->free_event_query_size = new_size;
-        context->free_event_queries = new_data;
+        ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
+        return;
     }
 
     context->free_event_queries[context->free_event_query_count++] = query->object;
@@ -834,20 +818,12 @@ void context_free_timestamp_query(struct wined3d_timestamp_query *query)
     list_remove(&query->entry);
     query->context = NULL;
 
-    if (context->free_timestamp_query_count >= context->free_timestamp_query_size - 1)
+    if (!wined3d_array_reserve((void **)&context->free_timestamp_queries,
+            &context->free_timestamp_query_size, context->free_timestamp_query_count + 1,
+            sizeof(*context->free_timestamp_queries)))
     {
-        UINT new_size = context->free_timestamp_query_size << 1;
-        GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_timestamp_queries,
-                new_size * sizeof(*context->free_timestamp_queries));
-
-        if (!new_data)
-        {
-            ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
-            return;
-        }
-
-        context->free_timestamp_query_size = new_size;
-        context->free_timestamp_queries = new_data;
+        ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
+        return;
     }
 
     context->free_timestamp_queries[context->free_timestamp_query_count++] = query->id;
diff --git a/dlls/wined3d/utils.c b/dlls/wined3d/utils.c
index c392388..04317f1 100644
--- a/dlls/wined3d/utils.c
+++ b/dlls/wined3d/utils.c
@@ -6007,3 +6007,33 @@ void wined3d_gl_limits_get_texture_unit_range(const struct wined3d_gl_limits *gl
         *base = gl_limits->graphics_samplers - 1;
     *count = gl_limits->compute_samplers;
 }
+
+BOOL wined3d_array_reserve(void **elements, SIZE_T *capacity, SIZE_T count, SIZE_T size)
+{
+    SIZE_T max_capacity, new_capacity;
+    void *new_elements;
+
+    if (count <= *capacity)
+        return TRUE;
+
+    max_capacity = ~(SIZE_T)0 / size;
+    if (count > max_capacity)
+        return FALSE;
+
+    new_capacity = max(1, *capacity);
+    while (new_capacity < count && new_capacity <= max_capacity / 2)
+        new_capacity *= 2;
+    if (new_capacity < count)
+        new_capacity = count;
+
+    if (!*elements)
+        new_elements = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, new_capacity * size);
+    else
+        new_elements = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *elements, new_capacity * size);
+    if (!new_elements)
+        return FALSE;
+
+    *elements = new_elements;
+    *capacity = new_capacity;
+    return TRUE;
+}
diff --git a/dlls/wined3d/wined3d_main.c b/dlls/wined3d/wined3d_main.c
index 442a970..c59a956 100644
--- a/dlls/wined3d/wined3d_main.c
+++ b/dlls/wined3d/wined3d_main.c
@@ -43,7 +43,7 @@ struct wined3d_wndproc_table
 {
     struct wined3d_wndproc *entries;
     unsigned int count;
-    unsigned int size;
+    SIZE_T size;
 };
 
 static struct wined3d_wndproc_table wndproc_table;
@@ -443,25 +443,12 @@ BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
         return TRUE;
     }
 
-    if (wndproc_table.size == wndproc_table.count)
+    if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
+            wndproc_table.count + 1, sizeof(*entry)))
     {
-        unsigned int new_size = max(1, wndproc_table.size * 2);
-        struct wined3d_wndproc *new_entries;
-
-        if (!wndproc_table.entries)
-            new_entries = wined3d_calloc(new_size, sizeof(*new_entries));
-        else
-            new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
-
-        if (!new_entries)
-        {
-            wined3d_wndproc_mutex_unlock();
-            ERR("Failed to grow table.\n");
-            return FALSE;
-        }
-
-        wndproc_table.entries = new_entries;
-        wndproc_table.size = new_size;
+        wined3d_wndproc_mutex_unlock();
+        ERR("Failed to grow table.\n");
+        return FALSE;
     }
 
     entry = &wndproc_table.entries[wndproc_table.count++];
diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
index 613d1c9..1164cf8 100644
--- a/dlls/wined3d/wined3d_private.h
+++ b/dlls/wined3d/wined3d_private.h
@@ -1660,18 +1660,18 @@ struct wined3d_context
 
     /* Queries */
     GLuint *free_occlusion_queries;
-    UINT free_occlusion_query_size;
-    UINT free_occlusion_query_count;
+    SIZE_T free_occlusion_query_size;
+    unsigned int free_occlusion_query_count;
     struct list occlusion_queries;
 
     union wined3d_gl_query_object *free_event_queries;
-    UINT free_event_query_size;
-    UINT free_event_query_count;
+    SIZE_T free_event_query_size;
+    unsigned int free_event_query_count;
     struct list event_queries;
 
     GLuint *free_timestamp_queries;
-    UINT free_timestamp_query_size;
-    UINT free_timestamp_query_count;
+    SIZE_T free_timestamp_query_size;
+    unsigned int free_timestamp_query_count;
     struct list timestamp_queries;
 
     struct wined3d_stream_info stream_info;
@@ -3296,7 +3296,7 @@ struct wined3d_buffer
     void *map_ptr;
 
     struct wined3d_map_range *maps;
-    ULONG maps_size, modified_areas;
+    SIZE_T maps_size, modified_areas;
     DWORD locations;
     struct wined3d_event_query *query;
 
@@ -3879,6 +3879,8 @@ void wined3d_format_get_float_color_key(const struct wined3d_format *format,
 const struct wined3d_color_key_conversion * wined3d_format_get_color_key_conversion(
         const struct wined3d_texture *texture, BOOL need_alpha_ck) DECLSPEC_HIDDEN;
 
+BOOL wined3d_array_reserve(void **elements, SIZE_T *capacity, SIZE_T count, SIZE_T size) DECLSPEC_HIDDEN;
+
 static inline BOOL wined3d_format_is_typeless(const struct wined3d_format *format)
 {
     return format->id == format->typeless_id && format->id != WINED3DFMT_UNKNOWN;




More information about the wine-cvs mailing list