ReactOS 0.4.17-dev-934-g091855f
memory.c
Go to the documentation of this file.
1/*
2 * Copyright 2016 Henri Verbeet for CodeWeavers
3 * Copyright 2017 Józef Kucia for CodeWeavers
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20#include "vkd3d_memory.h"
21
22bool vkd3d_array_reserve(void **elements, size_t *capacity, size_t element_count, size_t element_size)
23{
24 size_t new_capacity, max_capacity;
25 void *new_elements;
26
27 if (element_count <= *capacity)
28 return true;
29
30 max_capacity = ~(size_t)0 / element_size;
31 if (max_capacity < element_count)
32 return false;
33
34 new_capacity = max(*capacity, 4);
35 while (new_capacity < element_count && new_capacity <= max_capacity / 2)
36 new_capacity *= 2;
37
38 if (new_capacity < element_count)
39 new_capacity = element_count;
40
41 if (!(new_elements = vkd3d_realloc(*elements, new_capacity * element_size)))
42 return false;
43
44 *elements = new_elements;
45 *capacity = new_capacity;
46
47 return true;
48}
size_t const element_size
Definition: debug_heap.cpp:510
unsigned int size_t
Definition: corecrt.h:203
static ULONG ** element_count
Definition: exception.c:124
bool vkd3d_array_reserve(void **elements, size_t *capacity, size_t element_count, size_t element_size)
Definition: memory.c:22
#define max(a, b)
Definition: svc.c:63
static void * vkd3d_realloc(void *ptr, size_t size)
Definition: vkd3d_memory.h:36