On 08/11/2016 10:11 AM, Chad Versace wrote: > The new function is identical to _mesa_enum_to_string(), except that it > returns false if the given number is an invalid enum. Bundling > validation and lookup into a single function allows us to do both with > a single lookup into the enum table. > > Cc: Haixia Shi <h...@chromium.org> > Change-Id: I83d9a6e53223d1a971cf8dc22cb34b05b48d7889 > --- > src/mapi/glapi/gen/gl_enums.py | 20 ++++++++++++++++---- > src/mesa/main/enums.h | 2 ++ > 2 files changed, 18 insertions(+), 4 deletions(-) > > diff --git a/src/mapi/glapi/gen/gl_enums.py b/src/mapi/glapi/gen/gl_enums.py > index 4fc43ab..af21aaf 100644 > --- a/src/mapi/glapi/gen/gl_enums.py > +++ b/src/mapi/glapi/gen/gl_enums.py > @@ -89,6 +89,17 @@ static char token_tmp[20]; > */ > const char *_mesa_enum_to_string( int nr ) > { > + const char *str; > + _mesa_enum_to_string2(nr, &str); > + return str; > +} > + > +/** > + * Identical to _mesa_enum_to_string(), except that this function returns > false > + * when the number is an invalid enum. > + */ > +bool _mesa_enum_to_string2(int nr, const char **str) > +{ > enum_elt *elt; >
Add an assert(str != NULL); and maybe document that in the function header. > elt = bsearch(& nr, enum_string_table_offsets, > @@ -97,13 +108,14 @@ const char *_mesa_enum_to_string( int nr ) > (cfunc) compar_nr); > > if (elt != NULL) { > - return &enum_string_table[elt->offset]; > - } > - else { > + *str = &enum_string_table[elt->offset]; > + return true; > + } else { > /* this is not re-entrant safe, no big deal here */ > _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr); > token_tmp[sizeof(token_tmp) - 1] = '\\0'; > - return token_tmp; > + *str = token_tmp; > + return false; > } > } > > diff --git a/src/mesa/main/enums.h b/src/mesa/main/enums.h > index 0e18cd4..ae804d6 100644 > --- a/src/mesa/main/enums.h > +++ b/src/mesa/main/enums.h > @@ -36,6 +36,7 @@ > #ifndef _ENUMS_H_ > #define _ENUMS_H_ > > +#include <stdbool.h> > > #ifdef __cplusplus > extern "C" { > @@ -43,6 +44,7 @@ extern "C" { > > > extern const char *_mesa_enum_to_string( int nr ); > +bool _mesa_enum_to_string2(int nr, const char **str); > > /* Get the name of an enum given that it is a primitive type. Avoids > * GL_FALSE/GL_POINTS ambiguity and others. > _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev