wined3d: Introduce helper function to reserve memory for arrays.

Signed-off-by: Józef Kucia <jkucia@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Józef Kucia 2017-02-28 14:42:36 +01:00 committed by Alexandre Julliard
parent 0b1b7d7b11
commit 36b9bc946b
5 changed files with 64 additions and 76 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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++];

View File

@ -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;