On 03/16/2018 06:26 PM, Michael Clark wrote: > This version uses a constant size memory buffer sized for > the maximum possible ISA string length. It also uses g_new > instead of g_new0, uses more efficient logic to append > extensions and adds manual zero termination of the string. > > Cc: Palmer Dabbelt <pal...@sifive.com> > Cc: Peter Maydell <peter.mayd...@linaro.org> > Cc: Philippe Mathieu-Daudé <f4...@amsat.org> > Signed-off-by: Michael Clark <m...@sifive.com> > --- > target/riscv/cpu.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c > index 4851890..298abbd 100644 > --- a/target/riscv/cpu.c > +++ b/target/riscv/cpu.c > @@ -391,16 +391,16 @@ static const TypeInfo riscv_cpu_type_info = { > char *riscv_isa_string(RISCVCPU *cpu) > { > int i; > - size_t maxlen = 5 + ctz32(cpu->env.misa); > - char *isa_string = g_new0(char, maxlen); > - snprintf(isa_string, maxlen, "rv%d", TARGET_LONG_BITS); > + const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1; > + char *isa_str = g_new(char, maxlen); > + char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS); > for (i = 0; i < sizeof(riscv_exts); i++) { > if (cpu->env.misa & RV(riscv_exts[i])) { > - isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; > - > + *p++ = riscv_exts[i] - 'A' + 'a'; /* tolower() */
I prefer unobfuscated code (think newcomers and reviewers) :| Reviewed-by: Philippe Mathieu-Daudé <f4...@amsat.org> > } > } > - return isa_string; > + *p = '\0'; > + return isa_str; > } > > void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf) >