mesa: make use of ralloc when creating ARB asm gl_program fields

This will allow us to move the ARB asm fields in gl_program into
a union as we will be able call ralloc_free() on the entire struct
when destroying the context.

In this change we switch over to using ralloc for the Instructions,
String and LocalParams fields of gl_program.

Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
This commit is contained in:
Timothy Arceri 2016-11-05 22:35:41 +11:00
parent 9c9589f1e2
commit 0ad69e6b51
14 changed files with 60 additions and 74 deletions

View file

@ -275,7 +275,9 @@ get_local_param_pointer(struct gl_context *ctx, const char *func,
}
if (!prog->LocalParams) {
prog->LocalParams = calloc(maxParams, sizeof(float[4]));
prog->LocalParams = rzalloc_array_size(prog, sizeof(float[4]),
maxParams);
if (!prog->LocalParams)
return GL_FALSE;
}

View file

@ -586,7 +586,8 @@ static void emit_op3fn(struct tnl_program *p,
/* double the size */
p->max_inst *= 2;
newInst = _mesa_alloc_instructions(p->max_inst);
newInst =
rzalloc_array(p->program, struct prog_instruction, p->max_inst);
if (!newInst) {
_mesa_error(NULL, GL_OUT_OF_MEMORY, "vertex program build");
return;
@ -595,8 +596,7 @@ static void emit_op3fn(struct tnl_program *p,
_mesa_copy_instructions(newInst, p->program->Instructions,
p->program->NumInstructions);
_mesa_free_instructions(p->program->Instructions,
p->program->NumInstructions);
ralloc_free(p->program->Instructions);
p->program->Instructions = newInst;
}
@ -1632,7 +1632,8 @@ create_new_program( const struct state_key *key,
* If we need more, we'll grow the instruction array as needed.
*/
p.max_inst = 32;
p.program->Instructions = _mesa_alloc_instructions(p.max_inst);
p.program->Instructions = rzalloc_array(program, struct prog_instruction,
p.max_inst);
p.program->String = NULL;
p.program->NumInstructions =
p.program->NumTemporaries =

View file

@ -170,17 +170,20 @@ _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
memset(&prog, 0, sizeof(prog));
memset(&state, 0, sizeof(state));
state.prog = &prog;
state.mem_ctx = program;
if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
&state)) {
ralloc_free(prog.Instructions);
ralloc_free(prog.String);
_mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
return;
}
if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0)
_mesa_optimize_program(ctx, &prog);
_mesa_optimize_program(ctx, &prog, program);
free(program->String);
ralloc_free(program->String);
/* Copy the relevant contents of the arb_program struct into the
* vertex_program struct.
@ -202,7 +205,7 @@ _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
program->IsPositionInvariant = (state.option.PositionInvariant)
? GL_TRUE : GL_FALSE;
free(program->Instructions);
ralloc_free(program->Instructions);
program->Instructions = prog.Instructions;
if (program->Parameters)

View file

@ -2933,7 +2933,7 @@ get_mesa_program(struct gl_context *ctx,
_mesa_reference_program(ctx, &shader->Program, prog);
if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0) {
_mesa_optimize_program(ctx, prog);
_mesa_optimize_program(ctx, prog, prog);
}
/* This has to be done last. Any operation that can cause

View file

@ -58,19 +58,6 @@ _mesa_init_instructions(struct prog_instruction *inst, GLuint count)
}
/**
* Allocate an array of program instructions.
* \param numInst number of instructions
* \return pointer to instruction memory
*/
struct prog_instruction *
_mesa_alloc_instructions(GLuint numInst)
{
return
calloc(numInst, sizeof(struct prog_instruction));
}
/**
* Copy an array of program instructions.
* \param dest pointer to destination.
@ -87,16 +74,6 @@ _mesa_copy_instructions(struct prog_instruction *dest,
}
/**
* Free an array of instructions
*/
void
_mesa_free_instructions(struct prog_instruction *inst, GLuint count)
{
free(inst);
}
/**
* Basic info about each instruction
*/

View file

@ -261,19 +261,15 @@ struct prog_instruction
extern "C" {
#endif
struct gl_program;
extern void
_mesa_init_instructions(struct prog_instruction *inst, GLuint count);
extern struct prog_instruction *
_mesa_alloc_instructions(GLuint numInst);
extern struct prog_instruction *
_mesa_copy_instructions(struct prog_instruction *dest,
const struct prog_instruction *src, GLuint n);
extern void
_mesa_free_instructions(struct prog_instruction *inst, GLuint count);
extern GLuint
_mesa_num_inst_src_regs(enum prog_opcode opcode);

View file

@ -159,7 +159,8 @@ is_swizzle_regular(GLuint swz)
* \return number of instructions removed
*/
static GLuint
remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
remove_instructions(struct gl_program *prog, const GLboolean *removeFlags,
void *mem_ctx)
{
GLint i, removeEnd = 0, removeCount = 0;
GLuint totalRemoved = 0;
@ -184,7 +185,7 @@ remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
*/
if (removeCount > 0) {
GLint removeStart = removeEnd - removeCount + 1;
_mesa_delete_instructions(prog, removeStart, removeCount);
_mesa_delete_instructions(prog, removeStart, removeCount, mem_ctx);
removeStart = removeCount = 0; /* reset removal info */
}
}
@ -192,7 +193,7 @@ remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
/* Finish removing if the first instruction was to be removed. */
if (removeCount > 0) {
GLint removeStart = removeEnd - removeCount + 1;
_mesa_delete_instructions(prog, removeStart, removeCount);
_mesa_delete_instructions(prog, removeStart, removeCount, mem_ctx);
}
return totalRemoved;
}
@ -236,7 +237,7 @@ replace_regs(struct gl_program *prog, gl_register_file file, const GLint map[])
* write to such registers. Be careful with condition code setters.
*/
static GLboolean
_mesa_remove_dead_code_global(struct gl_program *prog)
_mesa_remove_dead_code_global(struct gl_program *prog, void *mem_ctx)
{
GLboolean tempRead[REG_ALLOCATE_MAX_PROGRAM_TEMPS][4];
GLboolean *removeInst; /* per-instruction removal flag */
@ -325,7 +326,7 @@ _mesa_remove_dead_code_global(struct gl_program *prog)
}
/* now remove the instructions which aren't needed */
rem = remove_instructions(prog, removeInst);
rem = remove_instructions(prog, removeInst, mem_ctx);
if (dbg) {
printf("Optimize: End dead code removal.\n");
@ -568,7 +569,7 @@ _mesa_remove_extra_move_use(struct gl_program *prog)
* with a proper control flow graph
*/
static GLboolean
_mesa_remove_dead_code_local(struct gl_program *prog)
_mesa_remove_dead_code_local(struct gl_program *prog, void *mem_ctx)
{
GLboolean *removeInst;
GLuint i, arg, rem = 0;
@ -600,7 +601,7 @@ _mesa_remove_dead_code_local(struct gl_program *prog)
removeInst[i] = GL_TRUE;
}
rem = remove_instructions(prog, removeInst);
rem = remove_instructions(prog, removeInst, mem_ctx);
done:
free(removeInst);
@ -704,7 +705,7 @@ _mesa_merge_mov_into_inst(struct prog_instruction *inst,
* Try to remove extraneous MOV instructions from the given program.
*/
static GLboolean
_mesa_remove_extra_moves(struct gl_program *prog)
_mesa_remove_extra_moves(struct gl_program *prog, void *mem_ctx)
{
GLboolean *removeInst; /* per-instruction removal flag */
GLuint i, rem = 0, nesting = 0;
@ -790,7 +791,7 @@ _mesa_remove_extra_moves(struct gl_program *prog)
}
/* now remove the instructions which aren't needed */
rem = remove_instructions(prog, removeInst);
rem = remove_instructions(prog, removeInst, mem_ctx);
free(removeInst);
@ -1310,7 +1311,8 @@ _mesa_simplify_cmp(struct gl_program * program)
* instructions, temp regs, etc.
*/
void
_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program)
_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program,
void *mem_ctx)
{
GLboolean any_change;
@ -1319,11 +1321,11 @@ _mesa_optimize_program(struct gl_context *ctx, struct gl_program *program)
do {
any_change = GL_FALSE;
_mesa_remove_extra_move_use(program);
if (_mesa_remove_dead_code_global(program))
if (_mesa_remove_dead_code_global(program, mem_ctx))
any_change = GL_TRUE;
if (_mesa_remove_extra_moves(program))
if (_mesa_remove_extra_moves(program, mem_ctx))
any_change = GL_TRUE;
if (_mesa_remove_dead_code_local(program))
if (_mesa_remove_dead_code_local(program, mem_ctx))
any_change = GL_TRUE;
any_change = _mesa_constant_fold(program) || any_change;

View file

@ -46,7 +46,8 @@ _mesa_find_temp_intervals(const struct prog_instruction *instructions,
GLint intEnd[MAX_PROGRAM_TEMPS]);
extern void
_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program);
_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program,
void *mem_ctx);
extern GLboolean
_mesa_constant_fold(struct gl_program *prog);

View file

@ -375,7 +375,9 @@ _mesa_fetch_state(struct gl_context *ctx, const gl_state_index state[],
case STATE_LOCAL:
if (!ctx->FragmentProgram.Current->LocalParams) {
ctx->FragmentProgram.Current->LocalParams =
calloc(MAX_PROGRAM_LOCAL_PARAMS, sizeof(float[4]));
rzalloc_array_size(ctx->FragmentProgram.Current,
sizeof(float[4]),
MAX_PROGRAM_LOCAL_PARAMS);
if (!ctx->FragmentProgram.Current->LocalParams)
return;
}
@ -401,7 +403,9 @@ _mesa_fetch_state(struct gl_context *ctx, const gl_state_index state[],
case STATE_LOCAL:
if (!ctx->VertexProgram.Current->LocalParams) {
ctx->VertexProgram.Current->LocalParams =
calloc(MAX_PROGRAM_LOCAL_PARAMS, sizeof(float[4]));
rzalloc_array_size(ctx->VertexProgram.Current,
sizeof(float[4]),
MAX_PROGRAM_LOCAL_PARAMS);
if (!ctx->VertexProgram.Current->LocalParams)
return;
}

View file

@ -247,12 +247,6 @@ _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
if (prog == &_mesa_DummyProgram)
return;
free(prog->String);
free(prog->LocalParams);
if (prog->Instructions) {
_mesa_free_instructions(prog->Instructions, prog->NumInstructions);
}
if (prog->Parameters) {
_mesa_free_parameter_list(prog->Parameters);
}
@ -358,7 +352,7 @@ _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
}
/* Alloc storage for new instructions */
newInst = _mesa_alloc_instructions(newLen);
newInst = rzalloc_array(prog, struct prog_instruction, newLen);
if (!newInst) {
return GL_FALSE;
}
@ -375,7 +369,7 @@ _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
origLen - start);
/* free old instructions */
_mesa_free_instructions(prog->Instructions, origLen);
ralloc_free(prog->Instructions);
/* install new instructions */
prog->Instructions = newInst;
@ -389,7 +383,8 @@ _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
* Adjust branch targets accordingly.
*/
GLboolean
_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
void *mem_ctx)
{
const GLuint origLen = prog->NumInstructions;
const GLuint newLen = origLen - count;
@ -407,7 +402,7 @@ _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
}
/* Alloc storage for new instructions */
newInst = _mesa_alloc_instructions(newLen);
newInst = rzalloc_array(mem_ctx, struct prog_instruction, newLen);
if (!newInst) {
return GL_FALSE;
}
@ -421,7 +416,7 @@ _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
newLen - start);
/* free old instructions */
_mesa_free_instructions(prog->Instructions, origLen);
ralloc_free(prog->Instructions);
/* install new instructions */
prog->Instructions = newInst;

View file

@ -93,7 +93,8 @@ extern GLboolean
_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count);
extern GLboolean
_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count);
_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
void *mem_ctx);
extern void
_mesa_find_used_registers(const struct gl_program *prog,

View file

@ -2511,7 +2511,7 @@ _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *st
/* Make a copy of the program string and force it to be NUL-terminated.
*/
strz = (GLubyte *) malloc(len + 1);
strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1);
if (strz == NULL) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
return GL_FALSE;
@ -2565,7 +2565,8 @@ _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *st
/* Add one instruction to store the "END" instruction.
*/
state->prog->Instructions =
_mesa_alloc_instructions(state->prog->NumInstructions + 1);
rzalloc_array(state->mem_ctx, struct prog_instruction,
state->prog->NumInstructions + 1);
if (state->prog->Instructions == NULL) {
goto error;

View file

@ -132,6 +132,9 @@ struct asm_parser_state {
struct gl_context *ctx;
struct gl_program *prog;
/** Memory context to attach instructions to. */
void *mem_ctx;
/**
* Per-program target limits
*/

View file

@ -70,7 +70,7 @@ _mesa_insert_mvp_dp4_code(struct gl_context *ctx, struct gl_program *vprog)
}
/* Alloc storage for new instructions */
newInst = _mesa_alloc_instructions(newLen);
newInst = rzalloc_array(vprog, struct prog_instruction, newLen);
if (!newInst) {
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glProgramString(inserting position_invariant code)");
@ -102,7 +102,7 @@ _mesa_insert_mvp_dp4_code(struct gl_context *ctx, struct gl_program *vprog)
_mesa_copy_instructions (newInst + 4, vprog->Instructions, origLen);
/* free old instructions */
_mesa_free_instructions(vprog->Instructions, origLen);
ralloc_free(vprog->Instructions);
/* install new instructions */
vprog->Instructions = newInst;
@ -138,7 +138,7 @@ _mesa_insert_mvp_mad_code(struct gl_context *ctx, struct gl_program *vprog)
}
/* Alloc storage for new instructions */
newInst = _mesa_alloc_instructions(newLen);
newInst = rzalloc_array(vprog, struct prog_instruction, newLen);
if (!newInst) {
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glProgramString(inserting position_invariant code)");
@ -203,7 +203,7 @@ _mesa_insert_mvp_mad_code(struct gl_context *ctx, struct gl_program *vprog)
_mesa_copy_instructions (newInst + 4, vprog->Instructions, origLen);
/* free old instructions */
_mesa_free_instructions(vprog->Instructions, origLen);
ralloc_free(vprog->Instructions);
/* install new instructions */
vprog->Instructions = newInst;
@ -270,7 +270,7 @@ _mesa_append_fog_code(struct gl_context *ctx, struct gl_program *fprog,
}
/* Alloc storage for new instructions */
newInst = _mesa_alloc_instructions(newLen);
newInst = rzalloc_array(fprog, struct prog_instruction, newLen);
if (!newInst) {
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glProgramString(inserting fog_option code)");
@ -403,7 +403,7 @@ _mesa_append_fog_code(struct gl_context *ctx, struct gl_program *fprog,
inst++;
/* free old instructions */
_mesa_free_instructions(fprog->Instructions, origLen);
ralloc_free(fprog->Instructions);
/* install new instructions */
fprog->Instructions = newInst;