[Mesa-dev] [PATCH 1/2] Allow for GLSL shaders to have #extension directive anywhere in source file.

Kenneth Graunke kenneth at whitecape.org
Tue Dec 3 01:44:49 PST 2013


On 12/03/2013 12:00 AM, Kevin Rogovin wrote:
[snip]
> diff --git a/src/glsl/glcpp/glcpp_extras.c b/src/glsl/glcpp/glcpp_extras.c
> new file mode 100644
> index 0000000..1672f9f
> --- /dev/null
> +++ b/src/glsl/glcpp/glcpp_extras.c
> @@ -0,0 +1,126 @@
> +#include <string.h>
> +#include <ctype.h>
> +#include "glcpp_extras.h"
> +#include "main/imports.h"
> +
> +#define MAX_STRING_LENGTH 200
> +
> +static const char*
> +remove_surrounding_white_space(char *value)
> +{
> +   size_t len;
> +   char *back;
> +
> +   /*
> +     remark:
> +      -since the return value of remove_surrounding_white_space
> +       is to never be freed, we can safely return a non-NULL
> +       "empty" string if value was NULL.
> +    */
> +
> +   if(value==NULL || *value==0) {
> +      return "";
> +   }
> +
> +   while(*value!=0 && isspace(*value)) {
> +      ++value;
> +   }
> +
> +   len=strlen(value);
> +   if(len==0) {
> +      return value;
> +   }
> +
> +   for(back = value + len - 1; back!=value && isspace(*back); --back) {
> +      *back=0;
> +   }
> +
> +   return value;
> +}
> +
> +static char*
> +copy_string(const char *src)
> +{
> +   char *dst;
> +   size_t len;
> +
> +   len=strnlen(src, MAX_STRING_LENGTH);
> +   dst=MALLOC(len+1);
> +   strncpy(dst, src, len);
> +
> +   dst[len]=0;
> +   return dst;
> +}

Isn't this just strdup()?  (Other than it being broken for strings
longer than an arbitrary limit of 200 characters...)

> +
> +static
> +void
> +delete_glcpp_extension_directive_entry(struct glcpp_extension_directive_list_element *p)
> +{
> +   FREE(p->line_string);
> +   FREE(p->strtoked_string);
> +   FREE(p);
> +}

I bet you could allocate p->line_string and p->strtoked_string using
ralloc---maybe ralloc_strdup(p, string_to_copy)?  Then ralloc_free(p)
would take care of the contents, and you wouldn't need to write this
function.

> +void 
> +init_glcpp_extension_directive_list(struct glcpp_extension_directive_list *p)
> +{
> +   assert(p!=NULL);
> +   p->head=p->tail=NULL;
> +   p->count=0;
> +}
> +
> +void 
> +delete_glcpp_extension_directive_list_contents(struct glcpp_extension_directive_list *p)
> +{
> +   struct glcpp_extension_directive_list_element *current;
> +
> +   assert(p!=NULL);
> +   current=p->head;
> +   while(current!=NULL) {
> +      struct glcpp_extension_directive_list_element *next;
> +
> +      next=current->next;
> +      delete_glcpp_extension_directive_entry(current);
> +      current=next;
> +   }
> +   p->head=p->tail=NULL;
> +   p->count=0;
> +}

...and if you used ralloc to allocate nodes, then you could probably
just free the top-level list and not have to write this either.


More information about the mesa-dev mailing list