[Mesa-dev] [PATCH] glsl: Implement ralloc with talloc
Carl Worth
cworth at cworth.org
Tue Jan 31 16:19:17 PST 2012
Since talloc has more debugging features than ralloc, such as the
very useful:
talloc_report_full(ctx, stdout);
we here conditionally implement ralloc by simply calling directly into
talloc.
NOTE: This commit is simply example code that I used recently, and I
thought I should share in case someone else might find useful. I'm not
proposing pushing it upstream in its current state. If there is any
interest in pushing this upstream, at least the following would need
to be done:
* The hard-coded #define of RALLOC_USE_TALLOC should be removed from
ralloc.h and some configuration mechanism should instead be provided
so that the user can choose to build with talloc.
* Said configuration mechanism should also arrange for linking with
-ltalloc. In the meantime, a user can do:
LDFLAGS=-ltalloc ./autogen.sh
Some other things could be done that aren't strictly necessary, but
might be nice for cleaner code:
* The three blocks of "#if ! RALLOC_USE_TALLOC" in ralloc.c could be
reduced to one by simply moving some code around. I didn't do this
simply to avoid making the patch look more involved than it actually
is.
* The conditional support for a destructor to be void (for ralloc) and
to return int (for talloc) is really ugly. I would recommend just
changing ralloc's destructor type to return int.
* This current patch changes one line of code even when
RALLOC_USE_TALLOC is not defined to a true value. Namely, inside of
ralloc_asprintf_rewrite_tail, a call to the internal resize function
is replaced to a call to reralloc_size. I think this is correct, but
Ken should confirm that.
---
src/glsl/glsl_symbol_table.h | 20 ++++++++++++++++-
src/glsl/ralloc.c | 12 +++++++++-
src/glsl/ralloc.h | 48 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 2 deletions(-)
diff --git a/src/glsl/glsl_symbol_table.h b/src/glsl/glsl_symbol_table.h
index 637bc03..a051e29 100644
--- a/src/glsl/glsl_symbol_table.h
+++ b/src/glsl/glsl_symbol_table.h
@@ -44,10 +44,22 @@ class symbol_table_entry;
*/
struct glsl_symbol_table {
private:
+
+/* Ralloc wants a void destructor while talloc wants a return value of
+ * int. Support both based on the RALLOC_USE_TALLOC conditional. */
+#if RALLOC_USE_TALLOC
+ static int
+#else
static void
+#endif
_glsl_symbol_table_destructor (glsl_symbol_table *table)
{
table->~glsl_symbol_table();
+
+#if RALLOC_USE_TALLOC
+ return 0;
+#endif
+
}
public:
@@ -60,7 +72,13 @@ public:
table = ralloc_size(ctx, size);
assert(table != NULL);
- ralloc_set_destructor(table, (void (*)(void*)) _glsl_symbol_table_destructor);
+ ralloc_set_destructor(table,
+#if RALLOC_USE_TALLOC
+ (int (*)(void*))
+#else
+ (void (*)(void*))
+#endif
+ _glsl_symbol_table_destructor);
return table;
}
diff --git a/src/glsl/ralloc.c b/src/glsl/ralloc.c
index 91e4bab..cbd3407 100644
--- a/src/glsl/ralloc.c
+++ b/src/glsl/ralloc.c
@@ -49,6 +49,8 @@ _CRTIMP int _vscprintf(const char *format, va_list argptr);
#define unlikely(x) !!(x)
#endif
+#if ! RALLOC_USE_TALLOC
+
#ifndef va_copy
#ifdef __va_copy
#define va_copy(dest, src) __va_copy((dest), (src))
@@ -184,6 +186,7 @@ ralloc_array_size(const void *ctx, size_t size, unsigned count)
return ralloc_size(ctx, size * count);
}
+#endif /* ! RALLOC_USE_TALLOC */
void *
rzalloc_array_size(const void *ctx, size_t size, unsigned count)
@@ -203,6 +206,7 @@ reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
return reralloc_size(ctx, ptr, size * count);
}
+#if ! RALLOC_USE_TALLOC
void
ralloc_free(void *ptr)
{
@@ -390,6 +394,8 @@ ralloc_asprintf(const void *ctx, const char *fmt, ...)
return ptr;
}
+#endif /* ! RALLOC_USE_TALLOC */
+
/* Return the length of the string that would be generated by a printf-style
* format and argument list, not including the \0 byte.
*/
@@ -419,6 +425,8 @@ printf_length(const char *fmt, va_list untouched_args)
return size;
}
+#if ! RALLOC_USE_TALLOC
+
char *
ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
{
@@ -451,6 +459,8 @@ ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
return ralloc_vasprintf_rewrite_tail(str, existing_length, fmt, args);
}
+#endif /* ! RALLOC_USE_TALLOC */
+
bool
ralloc_asprintf_rewrite_tail(char **str, size_t start, const char *fmt, ...)
{
@@ -479,7 +489,7 @@ ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
new_length = printf_length(fmt, args);
- ptr = resize(*str, start + new_length + 1);
+ ptr = reralloc_size(ralloc_parent(*str), *str, start + new_length + 1);
if (unlikely(ptr == NULL))
return false;
diff --git a/src/glsl/ralloc.h b/src/glsl/ralloc.h
index 1324f34..67e9100 100644
--- a/src/glsl/ralloc.h
+++ b/src/glsl/ralloc.h
@@ -55,6 +55,52 @@ extern "C" {
#include <stdarg.h>
#include <stdbool.h>
+#define RALLOC_USE_TALLOC 1
+
+#if RALLOC_USE_TALLOC
+
+#include <talloc.h>
+
+/* Most ralloc functions have direct talloc counterparts */
+
+#define ralloc(ctx, type) talloc(ctx, type)
+#define rzalloc(ctx, type) talloc_zero(ctx, type)
+#define ralloc_context(ctx) talloc_new(ctx)
+#define ralloc_size(ctx, size) talloc_size(ctx, size)
+#define rzalloc_size(ctx, size) talloc_zero_size(ctx, size)
+#define reralloc_size(ctx, ptr, size) talloc_realloc_size(ctx, ptr, size)
+#define ralloc_array(ctx, type, count) talloc_array(ctx, type, count)
+#define rzalloc_array(ctx, type, count) talloc_zero_array(ctx, type, count)
+#define reralloc(ctx, ptr, type, count) talloc_realloc(ctx, ptr, type, count)
+#define ralloc_array_size(ctx, size, count) talloc_array_size(cyx, size, count)
+#define ralloc_free(ptr) talloc_free(ptr)
+#define ralloc_steal(new_ctx, ptr) talloc_steal(new_ctx, ptr)
+#define ralloc_parent(ptr) talloc_parent(ptr)
+#define ralloc_autofree_context talloc_autofree_context
+#define ralloc_set_destructor talloc_set_destructor
+#define ralloc_strdup(ctx, str) talloc_strdup(ctx, str)
+#define ralloc_strndup(ctx, str, n) talloc_strndup(ctx, str, n)
+#define ralloc_strcat(dest, str) *(dest) = talloc_strdup_append(*(dest), str)
+#define ralloc_strncat(dest, str, n) *(dest) = talloc_strndup_append(*(dest), str, n)
+#define ralloc_asprintf talloc_asprintf
+#define ralloc_vasprintf(ctx, fmt, args) talloc_vasprintf(ctx, fmt, args)
+#define ralloc_asprintf_append(str, start, fmt, ...) *(str) = talloc_asprintf_append(*(str), start, fmt, ##__VA_ARGS__)
+#define ralloc_vasprintf_append(str, fmt, args) *(str) = talloc_vasprintf_append(*(str), fmt, args)
+
+/* A few ralloc functions are original and need a non-talloc implementation */
+void *rzalloc_array_size(const void *ctx, size_t size, unsigned count);
+
+void *reralloc_array_size(const void *ctx, void *ptr, size_t size,
+ unsigned count);
+
+bool ralloc_asprintf_rewrite_tail(char **str, size_t start,
+ const char *fmt, ...);
+
+bool ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+ va_list args);
+
+#else
+
/**
* \def ralloc(ctx, type)
* Allocate a new object chained off of the given context.
@@ -395,6 +441,8 @@ bool ralloc_asprintf_append (char **str, const char *fmt, ...);
bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
/// @}
+#endif /* RALLOC_USE_TALLOC */
+
#ifdef __cplusplus
} /* end of extern "C" */
#endif
--
1.7.8.3
More information about the mesa-dev
mailing list