From 0ad69e6b51b06c483db37c3b7eeb1da1a95d0629 Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Sat, 5 Nov 2016 22:35:41 +1100 Subject: [PATCH] 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 --- src/mesa/main/arbprogram.c | 4 +++- src/mesa/main/ffvertex_prog.c | 9 +++++---- src/mesa/program/arbprogparse.c | 9 ++++++--- src/mesa/program/ir_to_mesa.cpp | 2 +- src/mesa/program/prog_instruction.c | 23 ----------------------- src/mesa/program/prog_instruction.h | 8 ++------ src/mesa/program/prog_optimize.c | 28 +++++++++++++++------------- src/mesa/program/prog_optimize.h | 3 ++- src/mesa/program/prog_statevars.c | 8 ++++++-- src/mesa/program/program.c | 17 ++++++----------- src/mesa/program/program.h | 3 ++- src/mesa/program/program_parse.y | 5 +++-- src/mesa/program/program_parser.h | 3 +++ src/mesa/program/programopt.c | 12 ++++++------ 14 files changed, 60 insertions(+), 74 deletions(-) diff --git a/src/mesa/main/arbprogram.c b/src/mesa/main/arbprogram.c index 9c7622ad5e5..8b25699b6bf 100644 --- a/src/mesa/main/arbprogram.c +++ b/src/mesa/main/arbprogram.c @@ -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; } diff --git a/src/mesa/main/ffvertex_prog.c b/src/mesa/main/ffvertex_prog.c index 8cec1cbaa60..5bc64f18c74 100644 --- a/src/mesa/main/ffvertex_prog.c +++ b/src/mesa/main/ffvertex_prog.c @@ -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 = diff --git a/src/mesa/program/arbprogparse.c b/src/mesa/program/arbprogparse.c index e545d710e52..a7e21d91672 100644 --- a/src/mesa/program/arbprogparse.c +++ b/src/mesa/program/arbprogparse.c @@ -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) diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp index c22eb0a5efc..7ea375f0362 100644 --- a/src/mesa/program/ir_to_mesa.cpp +++ b/src/mesa/program/ir_to_mesa.cpp @@ -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 diff --git a/src/mesa/program/prog_instruction.c b/src/mesa/program/prog_instruction.c index 28858f6da0d..9f03867f0b0 100644 --- a/src/mesa/program/prog_instruction.c +++ b/src/mesa/program/prog_instruction.c @@ -59,19 +59,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. * \param src pointer to source. @@ -88,16 +75,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 */ struct instruction_info diff --git a/src/mesa/program/prog_instruction.h b/src/mesa/program/prog_instruction.h index 05a9a040d5a..328566a10f1 100644 --- a/src/mesa/program/prog_instruction.h +++ b/src/mesa/program/prog_instruction.h @@ -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); diff --git a/src/mesa/program/prog_optimize.c b/src/mesa/program/prog_optimize.c index 321cffa3bdf..d8bba6d42e7 100644 --- a/src/mesa/program/prog_optimize.c +++ b/src/mesa/program/prog_optimize.c @@ -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; diff --git a/src/mesa/program/prog_optimize.h b/src/mesa/program/prog_optimize.h index 1f20ac0f89a..c99ce9ecee5 100644 --- a/src/mesa/program/prog_optimize.h +++ b/src/mesa/program/prog_optimize.h @@ -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); diff --git a/src/mesa/program/prog_statevars.c b/src/mesa/program/prog_statevars.c index f1786e5ba42..82ee5d4cbd4 100644 --- a/src/mesa/program/prog_statevars.c +++ b/src/mesa/program/prog_statevars.c @@ -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; } diff --git a/src/mesa/program/program.c b/src/mesa/program/program.c index f4b36f4cbf2..25ec4893e51 100644 --- a/src/mesa/program/program.c +++ b/src/mesa/program/program.c @@ -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; diff --git a/src/mesa/program/program.h b/src/mesa/program/program.h index 6460948044f..18122f75b74 100644 --- a/src/mesa/program/program.h +++ b/src/mesa/program/program.h @@ -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, diff --git a/src/mesa/program/program_parse.y b/src/mesa/program/program_parse.y index 41aeb241ffe..e861340a023 100644 --- a/src/mesa/program/program_parse.y +++ b/src/mesa/program/program_parse.y @@ -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; diff --git a/src/mesa/program/program_parser.h b/src/mesa/program/program_parser.h index 05ceb92dfab..412aca1e53d 100644 --- a/src/mesa/program/program_parser.h +++ b/src/mesa/program/program_parser.h @@ -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 */ diff --git a/src/mesa/program/programopt.c b/src/mesa/program/programopt.c index 1b50b5b7a0e..e7cb8aab4d3 100644 --- a/src/mesa/program/programopt.c +++ b/src/mesa/program/programopt.c @@ -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; -- 2.11.0