#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
+#include "../pp/sl_pp_context.h"
#include "../pp/sl_pp_purify.h"
#include "../pp/sl_pp_version.h"
#include "../pp/sl_pp_process.h"
char *inbuf;
struct sl_pp_purify_options options;
char *outbuf;
+ struct sl_pp_context context;
struct sl_pp_token_info *tokens;
unsigned int version;
unsigned int tokens_eaten;
free(inbuf);
- if (sl_pp_tokenise(outbuf, &tokens)) {
+ sl_pp_context_init(&context);
+
+ if (sl_pp_tokenise(&context, outbuf, &tokens)) {
+ sl_pp_context_destroy(&context);
free(outbuf);
return 1;
}
free(outbuf);
- if (sl_pp_version(tokens, &version, &tokens_eaten)) {
+ if (sl_pp_version(&context, tokens, &version, &tokens_eaten)) {
+ sl_pp_context_destroy(&context);
free(tokens);
return -1;
}
- if (sl_pp_process(&tokens[tokens_eaten], &outtokens)) {
+ if (sl_pp_process(&context, &tokens[tokens_eaten], &outtokens)) {
+ sl_pp_context_destroy(&context);
free(tokens);
return -1;
}
out = fopen(argv[2], "wb");
if (!out) {
+ sl_pp_context_destroy(&context);
free(outtokens);
return 1;
}
break;
case SL_PP_IDENTIFIER:
- fprintf(out, "%s ", outtokens[i].data.identifier);
- free(outtokens[i].data.identifier);
+ fprintf(out, "%s ", sl_pp_context_cstr(&context, outtokens[i].data.identifier));
break;
case SL_PP_NUMBER:
- fprintf(out, "(%s) ", outtokens[i].data.number);
- free(outtokens[i].data.number);
+ fprintf(out, "%s ", sl_pp_context_cstr(&context, outtokens[i].data.number));
break;
case SL_PP_OTHER:
}
}
+ sl_pp_context_destroy(&context);
free(outtokens);
fclose(out);
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
+#include "../pp/sl_pp_context.h"
#include "../pp/sl_pp_purify.h"
#include "../pp/sl_pp_token.h"
char *inbuf;
struct sl_pp_purify_options options;
char *outbuf;
+ struct sl_pp_context context;
struct sl_pp_token_info *tokens;
FILE *out;
unsigned int i;
free(inbuf);
- if (sl_pp_tokenise(outbuf, &tokens)) {
+ sl_pp_context_init(&context);
+
+ if (sl_pp_tokenise(&context, outbuf, &tokens)) {
+ sl_pp_context_destroy(&context);
free(outbuf);
return 1;
}
out = fopen(argv[2], "wb");
if (!out) {
+ sl_pp_context_destroy(&context);
free(tokens);
return 1;
}
break;
case SL_PP_IDENTIFIER:
- fprintf(out, "%s ", tokens[i].data.identifier);
- free(tokens[i].data.identifier);
+ fprintf(out, "%s ", sl_pp_context_cstr(&context, tokens[i].data.identifier));
break;
case SL_PP_NUMBER:
- fprintf(out, "(%s) ", tokens[i].data.number);
- free(tokens[i].data.number);
+ fprintf(out, "(%s) ", sl_pp_context_cstr(&context, tokens[i].data.number));
break;
case SL_PP_OTHER:
}
}
+ sl_pp_context_destroy(&context);
free(tokens);
fclose(out);
char *inbuf;
struct sl_pp_purify_options options;
char *outbuf;
+ struct sl_pp_context context;
struct sl_pp_token_info *tokens;
unsigned int version;
unsigned int tokens_eaten;
free(inbuf);
- if (sl_pp_tokenise(outbuf, &tokens)) {
+ sl_pp_context_init(&context);
+
+ if (sl_pp_tokenise(&context, outbuf, &tokens)) {
+ sl_pp_context_destroy(&context);
free(outbuf);
return 1;
}
free(outbuf);
- if (sl_pp_version(tokens, &version, &tokens_eaten)) {
+ if (sl_pp_version(&context, tokens, &version, &tokens_eaten)) {
+ sl_pp_context_destroy(&context);
free(tokens);
return -1;
}
+ sl_pp_context_destroy(&context);
free(tokens);
out = fopen(argv[2], "wb");
glsl = env.StaticLibrary(
target = 'glsl',
source = [
+ 'sl_pp_context.c',
'sl_pp_process.c',
'sl_pp_purify.c',
'sl_pp_token.c',
--- /dev/null
+/**************************************************************************
+ *
+ * Copyright 2009 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#include <stdlib.h>
+#include "sl_pp_context.h"
+
+
+void
+sl_pp_context_init(struct sl_pp_context *context)
+{
+ memset(context, 0, sizeof(struct sl_pp_context));
+}
+
+void
+sl_pp_context_destroy(struct sl_pp_context *context)
+{
+ free(context->cstr_pool);
+}
+
+int
+sl_pp_context_add_str(struct sl_pp_context *context,
+ const char *str)
+{
+ unsigned int size;
+ unsigned int offset;
+
+ size = strlen(str) + 1;
+
+ if (context->cstr_pool_len + size > context->cstr_pool_max) {
+ context->cstr_pool_max = (context->cstr_pool_len + size + 0xffff) & ~0xffff;
+ context->cstr_pool = realloc(context->cstr_pool, context->cstr_pool_max);
+ }
+
+ if (!context->cstr_pool) {
+ return -1;
+ }
+
+ offset = context->cstr_pool_len;
+ memcpy(&context->cstr_pool[offset], str, size);
+ context->cstr_pool_len += size;
+
+ return offset;
+}
+
+const char *
+sl_pp_context_cstr(const struct sl_pp_context *context,
+ int offset)
+{
+ if (offset == -1) {
+ return NULL;
+ }
+ return &context->cstr_pool[offset];
+}
--- /dev/null
+/**************************************************************************
+ *
+ * Copyright 2009 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#ifndef SL_PP_CONTEXT_H
+#define SL_PP_CONTEXT_H
+
+
+struct sl_pp_context {
+ char *cstr_pool;
+ unsigned int cstr_pool_max;
+ unsigned int cstr_pool_len;
+};
+
+void
+sl_pp_context_init(struct sl_pp_context *context);
+
+void
+sl_pp_context_destroy(struct sl_pp_context *context);
+
+int
+sl_pp_context_add_str(struct sl_pp_context *context,
+ const char *str);
+
+const char *
+sl_pp_context_cstr(const struct sl_pp_context *context,
+ int offset);
+
+#endif /* SL_PP_VERSION_H */
**************************************************************************/
#include <stdlib.h>
-#include <string.h>
#include "sl_pp_process.h"
};
int
-sl_pp_process(const struct sl_pp_token_info *input,
+sl_pp_process(struct sl_pp_context *context,
+ const struct sl_pp_token_info *input,
struct sl_pp_token_info **output)
{
unsigned int i = 0;
out_max = new_max;
}
- if (info.token == SL_PP_IDENTIFIER) {
- info.data.identifier = strdup(info.data.identifier);
- if (!info.data.identifier) {
- return -1;
- }
- } else if (info.token == SL_PP_NUMBER) {
- info.data.number = strdup(info.data.number);
- if (!info.data.number) {
- return -1;
- }
- }
-
out[out_len++] = info;
if (info.token == SL_PP_EOF) {
int
-sl_pp_process(const struct sl_pp_token_info *input,
+sl_pp_process(struct sl_pp_context *context,
+ const struct sl_pp_token_info *input,
struct sl_pp_token_info **output);
#endif /* SL_PP_PROCESS_H */
static int
-_tokenise_identifier(const char **pinput,
+_tokenise_identifier(struct sl_pp_context *context,
+ const char **pinput,
struct sl_pp_token_info *info)
{
const char *input = *pinput;
unsigned int i = 0;
info->token = SL_PP_IDENTIFIER;
- info->data.identifier = NULL;
+ info->data.identifier = -1;
identifier[i++] = *input++;
while ((*input >= 'a' && *input <= 'z') ||
}
identifier[i++] = '\0';
- info->data.identifier = malloc(i);
- if (!info->data.identifier) {
+ info->data.identifier = sl_pp_context_add_str(context, identifier);
+ if (info->data.identifier == -1) {
return -1;
}
- memcpy(info->data.identifier, identifier, i);
*pinput = input;
return 0;
static int
-_tokenise_number(const char **pinput,
+_tokenise_number(struct sl_pp_context *context,
+ const char **pinput,
struct sl_pp_token_info *info)
{
const char *input = *pinput;
unsigned int i = 0;
info->token = SL_PP_NUMBER;
- info->data.number = NULL;
+ info->data.number = -1;
number[i++] = *input++;
while ((*input >= '0' && *input <= '9') ||
}
number[i++] = '\0';
- info->data.number = malloc(i);
- if (!info->data.number) {
+ info->data.number = sl_pp_context_add_str(context, number);
+ if (info->data.number == -1) {
return -1;
}
- memcpy(info->data.number, number, i);
*pinput = input;
return 0;
int
-sl_pp_tokenise(const char *input,
+sl_pp_tokenise(struct sl_pp_context *context,
+ const char *input,
struct sl_pp_token_info **output)
{
struct sl_pp_token_info *out = NULL;
case '.':
if (input[1] >= '0' && input[1] <= '9') {
- if (_tokenise_number(&input, &info)) {
+ if (_tokenise_number(context, &input, &info)) {
free(out);
return -1;
}
if ((*input >= 'a' && *input <= 'z') ||
(*input >= 'A' && *input <= 'Z') ||
(*input == '_')) {
- if (_tokenise_identifier(&input, &info)) {
+ if (_tokenise_identifier(context, &input, &info)) {
free(out);
return -1;
}
} else if (*input >= '0' && *input <= '9') {
- if (_tokenise_number(&input, &info)) {
+ if (_tokenise_number(context, &input, &info)) {
free(out);
return -1;
}
};
union sl_pp_token_data {
- char *identifier;
- char *number;
+ int identifier;
+ int number;
char other;
};
};
int
-sl_pp_tokenise(const char *input,
+sl_pp_tokenise(struct sl_pp_context *context,
+ const char *input,
struct sl_pp_token_info **output);
#endif /* SL_PP_TOKEN_H */
int
-sl_pp_version(const struct sl_pp_token_info *input,
+sl_pp_version(struct sl_pp_context *context,
+ const struct sl_pp_token_info *input,
unsigned int *version,
unsigned int *tokens_eaten)
{
break;
case SL_PP_IDENTIFIER:
- if (strcmp(input[i].data.identifier, "version")) {
- return 0;
+ {
+ const char *id = sl_pp_context_cstr(context, input[i].data.identifier);
+
+ if (!id) {
+ return -1;
+ }
+ if (strcmp(id, "version")) {
+ return 0;
+ }
+ i++;
+ found_version = 1;
}
- i++;
- found_version = 1;
break;
default:
break;
case SL_PP_NUMBER:
- if (_parse_integer(input[i].data.number, version)) {
- /* Expected version number. */
- return -1;
+ {
+ const char *num = sl_pp_context_cstr(context, input[i].data.number);
+
+ if (!num) {
+ return -1;
+ }
+ if (_parse_integer(num, version)) {
+ /* Expected version number. */
+ return -1;
+ }
+ i++;
+ found_number = 1;
}
- i++;
- found_number = 1;
break;
default:
#ifndef SL_PP_VERSION_H
#define SL_PP_VERSION_H
+#include "sl_pp_context.h"
#include "sl_pp_token.h"
int
-sl_pp_version(const struct sl_pp_token_info *input,
+sl_pp_version(struct sl_pp_context *context,
+ const struct sl_pp_token_info *input,
unsigned int *version,
unsigned int *tokens_eaten);