mirror of
git://source.winehq.org/git/wine.git
synced 2024-10-14 23:27:17 +00:00
d3dcompiler: Introduce an array_reserve() helper.
Signed-off-by: Zebediah Figura <zfigura@codeweavers.com> Signed-off-by: Matteo Bruni <mbruni@codeweavers.com> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
5ba51ef26c
commit
cbb4a86218
|
@ -27,6 +27,35 @@
|
||||||
|
|
||||||
WINE_DEFAULT_DEBUG_CHANNEL(bytecodewriter);
|
WINE_DEFAULT_DEBUG_CHANNEL(bytecodewriter);
|
||||||
|
|
||||||
|
static BOOL array_reserve(void **elements, unsigned int *capacity, unsigned int count, unsigned int size)
|
||||||
|
{
|
||||||
|
unsigned int max_capacity, new_capacity;
|
||||||
|
void *new_elements;
|
||||||
|
|
||||||
|
if (count <= *capacity)
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
max_capacity = ~0u / size;
|
||||||
|
if (count > max_capacity)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
new_capacity = max(8, *capacity);
|
||||||
|
while (new_capacity < count && new_capacity <= max_capacity / 2)
|
||||||
|
new_capacity *= 2;
|
||||||
|
if (new_capacity < count)
|
||||||
|
new_capacity = count;
|
||||||
|
|
||||||
|
if (!(new_elements = d3dcompiler_realloc(*elements, new_capacity * size)))
|
||||||
|
{
|
||||||
|
ERR("Failed to allocate memory.\n");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*elements = new_elements;
|
||||||
|
*capacity = new_capacity;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************
|
/****************************************************************
|
||||||
* General assembler shader construction helper routines follow *
|
* General assembler shader construction helper routines follow *
|
||||||
****************************************************************/
|
****************************************************************/
|
||||||
|
@ -73,30 +102,11 @@ struct instruction *alloc_instr(unsigned int srcs) {
|
||||||
* instr: Instruction to add to the shader
|
* instr: Instruction to add to the shader
|
||||||
*/
|
*/
|
||||||
BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr) {
|
BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr) {
|
||||||
struct instruction **new_instructions;
|
|
||||||
|
|
||||||
if(!shader) return FALSE;
|
if(!shader) return FALSE;
|
||||||
|
|
||||||
if(shader->instr_alloc_size == 0) {
|
if (!array_reserve((void **)&shader->instr, &shader->instr_alloc_size,
|
||||||
shader->instr = d3dcompiler_alloc(sizeof(*shader->instr) * INSTRARRAY_INITIAL_SIZE);
|
shader->num_instrs + 1, sizeof(*shader->instr)))
|
||||||
if(!shader->instr) {
|
|
||||||
ERR("Failed to allocate the shader instruction array\n");
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
shader->instr_alloc_size = INSTRARRAY_INITIAL_SIZE;
|
|
||||||
} else if(shader->instr_alloc_size == shader->num_instrs) {
|
|
||||||
new_instructions = d3dcompiler_realloc(shader->instr,
|
|
||||||
sizeof(*shader->instr) * (shader->instr_alloc_size) * 2);
|
|
||||||
if(!new_instructions) {
|
|
||||||
ERR("Failed to grow the shader instruction array\n");
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
shader->instr = new_instructions;
|
|
||||||
shader->instr_alloc_size = shader->instr_alloc_size * 2;
|
|
||||||
} else if(shader->num_instrs > shader->instr_alloc_size) {
|
|
||||||
ERR("More instructions than allocated. This should not happen\n");
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
|
||||||
|
|
||||||
shader->instr[shader->num_instrs] = instr;
|
shader->instr[shader->num_instrs] = instr;
|
||||||
shader->num_instrs++;
|
shader->num_instrs++;
|
||||||
|
@ -310,13 +320,6 @@ static struct bytecode_buffer *allocate_buffer(void) {
|
||||||
|
|
||||||
ret = d3dcompiler_alloc(sizeof(*ret));
|
ret = d3dcompiler_alloc(sizeof(*ret));
|
||||||
if(!ret) return NULL;
|
if(!ret) return NULL;
|
||||||
|
|
||||||
ret->alloc_size = BYTECODEBUFFER_INITIAL_SIZE;
|
|
||||||
ret->data = d3dcompiler_alloc(sizeof(DWORD) * ret->alloc_size);
|
|
||||||
if(!ret->data) {
|
|
||||||
d3dcompiler_free(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
ret->state = S_OK;
|
ret->state = S_OK;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -324,18 +327,12 @@ static struct bytecode_buffer *allocate_buffer(void) {
|
||||||
static void put_dword(struct bytecode_buffer *buffer, DWORD value) {
|
static void put_dword(struct bytecode_buffer *buffer, DWORD value) {
|
||||||
if(FAILED(buffer->state)) return;
|
if(FAILED(buffer->state)) return;
|
||||||
|
|
||||||
if(buffer->alloc_size == buffer->size) {
|
if (!array_reserve((void **)&buffer->data, &buffer->alloc_size, buffer->size + 1, sizeof(*buffer->data)))
|
||||||
DWORD *newarray;
|
{
|
||||||
buffer->alloc_size *= 2;
|
buffer->state = E_OUTOFMEMORY;
|
||||||
newarray = d3dcompiler_realloc(buffer->data,
|
return;
|
||||||
sizeof(DWORD) * buffer->alloc_size);
|
|
||||||
if(!newarray) {
|
|
||||||
ERR("Failed to grow the buffer data memory\n");
|
|
||||||
buffer->state = E_OUTOFMEMORY;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
buffer->data = newarray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer->data[buffer->size++] = value;
|
buffer->data[buffer->size++] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,7 +115,6 @@ struct samplerdecl {
|
||||||
DWORD mod;
|
DWORD mod;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define INSTRARRAY_INITIAL_SIZE 8
|
|
||||||
struct bwriter_shader {
|
struct bwriter_shader {
|
||||||
enum shader_type type;
|
enum shader_type type;
|
||||||
unsigned char major_version, minor_version;
|
unsigned char major_version, minor_version;
|
||||||
|
@ -149,6 +148,8 @@ static inline void *d3dcompiler_alloc(SIZE_T size)
|
||||||
|
|
||||||
static inline void *d3dcompiler_realloc(void *ptr, SIZE_T size)
|
static inline void *d3dcompiler_realloc(void *ptr, SIZE_T size)
|
||||||
{
|
{
|
||||||
|
if (!ptr)
|
||||||
|
return d3dcompiler_alloc(size);
|
||||||
return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
|
return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,12 +294,9 @@ static inline void set_parse_status(enum parse_status *current, enum parse_statu
|
||||||
*current = PARSE_WARN;
|
*current = PARSE_WARN;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* A reasonable value as initial size */
|
|
||||||
#define BYTECODEBUFFER_INITIAL_SIZE 32
|
|
||||||
struct bytecode_buffer {
|
struct bytecode_buffer {
|
||||||
DWORD *data;
|
DWORD *data;
|
||||||
DWORD size;
|
unsigned int size, alloc_size;
|
||||||
DWORD alloc_size;
|
|
||||||
/* For tracking rare out of memory situations without passing
|
/* For tracking rare out of memory situations without passing
|
||||||
* return values around everywhere
|
* return values around everywhere
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue