All of lore.kernel.org
 help / color / mirror / Atom feed
From: Damien Lespiau <damien.lespiau@intel.com>
To: intel-gfx@lists.freedesktop.org
Subject: [PATCH 23/90] assembler: Import ralloc from Mesa
Date: Mon,  4 Feb 2013 15:27:18 +0000	[thread overview]
Message-ID: <1359991705-5254-24-git-send-email-damien.lespiau@intel.com> (raw)
In-Reply-To: <1359991705-5254-1-git-send-email-damien.lespiau@intel.com>

This also add a new brw_compat.h that should help maintaining the
diff between mesa's version and our as small as possible.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 assembler/Makefile.am  |    3 +
 assembler/brw_compat.h |   64 +++++++
 assembler/ralloc.c     |  482 ++++++++++++++++++++++++++++++++++++++++++++++++
 assembler/ralloc.h     |  407 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 956 insertions(+), 0 deletions(-)
 create mode 100644 assembler/brw_compat.h
 create mode 100644 assembler/ralloc.c
 create mode 100644 assembler/ralloc.h

diff --git a/assembler/Makefile.am b/assembler/Makefile.am
index 8843e1a..d4733d3 100644
--- a/assembler/Makefile.am
+++ b/assembler/Makefile.am
@@ -10,6 +10,9 @@ BUILT_SOURCES = gram.h gram.c lex.c
 gram.h: gram.c
 
 intel_gen4asm_SOURCES =	\
+	brw_compat.h	\
+	ralloc.c	\
+	ralloc.h	\
 	brw_defines.h	\
 	brw_eu.h	\
 	brw_reg.h	\
diff --git a/assembler/brw_compat.h b/assembler/brw_compat.h
new file mode 100644
index 0000000..9300190
--- /dev/null
+++ b/assembler/brw_compat.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright © 2013 Intel Corporation
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
+ */
+
+/*
+ * To share code with mesa without having to do big modifications and still be
+ * able to sync files together at a later point, this file holds macros and
+ * types defined in mesa's core headers.
+ */
+
+#ifndef __BRW_COMPAT_H__
+#define __BRW_COMPAT_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ *  * __builtin_expect macros
+ *   */
+#if !defined(__GNUC__)
+#  define __builtin_expect(x, y) (x)
+#endif
+
+#ifndef likely
+#  ifdef __GNUC__
+#    define likely(x)   __builtin_expect(!!(x), 1)
+#    define unlikely(x) __builtin_expect(!!(x), 0)
+#  else
+#    define likely(x)   (x)
+#    define unlikely(x) (x)
+#  endif
+#endif
+
+#if (__GNUC__ >= 3)
+#define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
+#else
+#define PRINTFLIKE(f, a)
+#endif
+
+#ifdef __cplusplus
+} /* end of extern "C" */
+#endif
+
+#endif /* __BRW_COMPAT_H__ */
diff --git a/assembler/ralloc.c b/assembler/ralloc.c
new file mode 100644
index 0000000..59e71c4
--- /dev/null
+++ b/assembler/ralloc.c
@@ -0,0 +1,482 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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 <assert.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+/* Android defines SIZE_MAX in limits.h, instead of the standard stdint.h */
+#ifdef ANDROID
+#include <limits.h>
+#endif
+
+/* Some versions of MinGW are missing _vscprintf's declaration, although they
+ * still provide the symbol in the import library. */
+#ifdef __MINGW32__
+_CRTIMP int _vscprintf(const char *format, va_list argptr);
+#endif
+
+#include "ralloc.h"
+
+#ifndef va_copy
+#ifdef __va_copy
+#define va_copy(dest, src) __va_copy((dest), (src))
+#else
+#define va_copy(dest, src) (dest) = (src)
+#endif
+#endif
+
+#define CANARY 0x5A1106
+
+struct ralloc_header
+{
+   /* A canary value used to determine whether a pointer is ralloc'd. */
+   unsigned canary;
+
+   struct ralloc_header *parent;
+
+   /* The first child (head of a linked list) */
+   struct ralloc_header *child;
+
+   /* Linked list of siblings */
+   struct ralloc_header *prev;
+   struct ralloc_header *next;
+
+   void (*destructor)(void *);
+};
+
+typedef struct ralloc_header ralloc_header;
+
+static void unlink_block(ralloc_header *info);
+static void unsafe_free(ralloc_header *info);
+
+static ralloc_header *
+get_header(const void *ptr)
+{
+   ralloc_header *info = (ralloc_header *) (((char *) ptr) -
+					    sizeof(ralloc_header));
+   assert(info->canary == CANARY);
+   return info;
+}
+
+#define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
+
+static void
+add_child(ralloc_header *parent, ralloc_header *info)
+{
+   if (parent != NULL) {
+      info->parent = parent;
+      info->next = parent->child;
+      parent->child = info;
+
+      if (info->next != NULL)
+	 info->next->prev = info;
+   }
+}
+
+void *
+ralloc_context(const void *ctx)
+{
+   return ralloc_size(ctx, 0);
+}
+
+void *
+ralloc_size(const void *ctx, size_t size)
+{
+   void *block = calloc(1, size + sizeof(ralloc_header));
+
+   ralloc_header *info = (ralloc_header *) block;
+   ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
+
+   add_child(parent, info);
+
+   info->canary = CANARY;
+
+   return PTR_FROM_HEADER(info);
+}
+
+void *
+rzalloc_size(const void *ctx, size_t size)
+{
+   void *ptr = ralloc_size(ctx, size);
+   if (likely(ptr != NULL))
+      memset(ptr, 0, size);
+   return ptr;
+}
+
+/* helper function - assumes ptr != NULL */
+static void *
+resize(void *ptr, size_t size)
+{
+   ralloc_header *child, *old, *info;
+
+   old = get_header(ptr);
+   info = realloc(old, size + sizeof(ralloc_header));
+
+   if (info == NULL)
+      return NULL;
+
+   /* Update parent and sibling's links to the reallocated node. */
+   if (info != old && info->parent != NULL) {
+      if (info->parent->child == old)
+	 info->parent->child = info;
+
+      if (info->prev != NULL)
+	 info->prev->next = info;
+
+      if (info->next != NULL)
+	 info->next->prev = info;
+   }
+
+   /* Update child->parent links for all children */
+   for (child = info->child; child != NULL; child = child->next)
+      child->parent = info;
+
+   return PTR_FROM_HEADER(info);
+}
+
+void *
+reralloc_size(const void *ctx, void *ptr, size_t size)
+{
+   if (unlikely(ptr == NULL))
+      return ralloc_size(ctx, size);
+
+   assert(ralloc_parent(ptr) == ctx);
+   return resize(ptr, size);
+}
+
+void *
+ralloc_array_size(const void *ctx, size_t size, unsigned count)
+{
+   if (count > SIZE_MAX/size)
+      return NULL;
+
+   return ralloc_size(ctx, size * count);
+}
+
+void *
+rzalloc_array_size(const void *ctx, size_t size, unsigned count)
+{
+   if (count > SIZE_MAX/size)
+      return NULL;
+
+   return rzalloc_size(ctx, size * count);
+}
+
+void *
+reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
+{
+   if (count > SIZE_MAX/size)
+      return NULL;
+
+   return reralloc_size(ctx, ptr, size * count);
+}
+
+void
+ralloc_free(void *ptr)
+{
+   ralloc_header *info;
+
+   if (ptr == NULL)
+      return;
+
+   info = get_header(ptr);
+   unlink_block(info);
+   unsafe_free(info);
+}
+
+static void
+unlink_block(ralloc_header *info)
+{
+   /* Unlink from parent & siblings */
+   if (info->parent != NULL) {
+      if (info->parent->child == info)
+	 info->parent->child = info->next;
+
+      if (info->prev != NULL)
+	 info->prev->next = info->next;
+
+      if (info->next != NULL)
+	 info->next->prev = info->prev;
+   }
+   info->parent = NULL;
+   info->prev = NULL;
+   info->next = NULL;
+}
+
+static void
+unsafe_free(ralloc_header *info)
+{
+   /* Recursively free any children...don't waste time unlinking them. */
+   ralloc_header *temp;
+   while (info->child != NULL) {
+      temp = info->child;
+      info->child = temp->next;
+      unsafe_free(temp);
+   }
+
+   /* Free the block itself.  Call the destructor first, if any. */
+   if (info->destructor != NULL)
+      info->destructor(PTR_FROM_HEADER(info));
+
+   free(info);
+}
+
+void
+ralloc_steal(const void *new_ctx, void *ptr)
+{
+   ralloc_header *info, *parent;
+
+   if (unlikely(ptr == NULL))
+      return;
+
+   info = get_header(ptr);
+   parent = get_header(new_ctx);
+
+   unlink_block(info);
+
+   add_child(parent, info);
+}
+
+void *
+ralloc_parent(const void *ptr)
+{
+   ralloc_header *info;
+
+   if (unlikely(ptr == NULL))
+      return NULL;
+
+   info = get_header(ptr);
+   return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
+}
+
+static void *autofree_context = NULL;
+
+static void
+autofree(void)
+{
+   ralloc_free(autofree_context);
+}
+
+void *
+ralloc_autofree_context(void)
+{
+   if (unlikely(autofree_context == NULL)) {
+      autofree_context = ralloc_context(NULL);
+      atexit(autofree);
+   }
+   return autofree_context;
+}
+
+void
+ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
+{
+   ralloc_header *info = get_header(ptr);
+   info->destructor = destructor;
+}
+
+char *
+ralloc_strdup(const void *ctx, const char *str)
+{
+   size_t n;
+   char *ptr;
+
+   if (unlikely(str == NULL))
+      return NULL;
+
+   n = strlen(str);
+   ptr = ralloc_array(ctx, char, n + 1);
+   memcpy(ptr, str, n);
+   ptr[n] = '\0';
+   return ptr;
+}
+
+char *
+ralloc_strndup(const void *ctx, const char *str, size_t max)
+{
+   size_t n;
+   char *ptr;
+
+   if (unlikely(str == NULL))
+      return NULL;
+
+   n = strlen(str);
+   if (n > max)
+      n = max;
+
+   ptr = ralloc_array(ctx, char, n + 1);
+   memcpy(ptr, str, n);
+   ptr[n] = '\0';
+   return ptr;
+}
+
+/* helper routine for strcat/strncat - n is the exact amount to copy */
+static bool
+cat(char **dest, const char *str, size_t n)
+{
+   char *both;
+   size_t existing_length;
+   assert(dest != NULL && *dest != NULL);
+
+   existing_length = strlen(*dest);
+   both = resize(*dest, existing_length + n + 1);
+   if (unlikely(both == NULL))
+      return false;
+
+   memcpy(both + existing_length, str, n);
+   both[existing_length + n] = '\0';
+
+   *dest = both;
+   return true;
+}
+
+
+bool
+ralloc_strcat(char **dest, const char *str)
+{
+   return cat(dest, str, strlen(str));
+}
+
+bool
+ralloc_strncat(char **dest, const char *str, size_t n)
+{
+   /* Clamp n to the string length */
+   size_t str_length = strlen(str);
+   if (str_length < n)
+      n = str_length;
+
+   return cat(dest, str, n);
+}
+
+char *
+ralloc_asprintf(const void *ctx, const char *fmt, ...)
+{
+   char *ptr;
+   va_list args;
+   va_start(args, fmt);
+   ptr = ralloc_vasprintf(ctx, fmt, args);
+   va_end(args);
+   return ptr;
+}
+
+/* Return the length of the string that would be generated by a printf-style
+ * format and argument list, not including the \0 byte.
+ */
+static size_t
+printf_length(const char *fmt, va_list untouched_args)
+{
+   int size;
+   char junk;
+
+   /* Make a copy of the va_list so the original caller can still use it */
+   va_list args;
+   va_copy(args, untouched_args);
+
+#ifdef _WIN32
+   /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
+    * if the number of characters to write is greater than count.
+    */
+   size = _vscprintf(fmt, args);
+   (void)junk;
+#else
+   size = vsnprintf(&junk, 1, fmt, args);
+#endif
+   assert(size >= 0);
+
+   va_end(args);
+
+   return size;
+}
+
+char *
+ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
+{
+   size_t size = printf_length(fmt, args) + 1;
+
+   char *ptr = ralloc_size(ctx, size);
+   if (ptr != NULL)
+      vsnprintf(ptr, size, fmt, args);
+
+   return ptr;
+}
+
+bool
+ralloc_asprintf_append(char **str, const char *fmt, ...)
+{
+   bool success;
+   va_list args;
+   va_start(args, fmt);
+   success = ralloc_vasprintf_append(str, fmt, args);
+   va_end(args);
+   return success;
+}
+
+bool
+ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
+{
+   size_t existing_length;
+   assert(str != NULL);
+   existing_length = *str ? strlen(*str) : 0;
+   return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
+}
+
+bool
+ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
+{
+   bool success;
+   va_list args;
+   va_start(args, fmt);
+   success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
+   va_end(args);
+   return success;
+}
+
+bool
+ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
+			      va_list args)
+{
+   size_t new_length;
+   char *ptr;
+
+   assert(str != NULL);
+
+   if (unlikely(*str == NULL)) {
+      // Assuming a NULL context is probably bad, but it's expected behavior.
+      *str = ralloc_vasprintf(NULL, fmt, args);
+      return true;
+   }
+
+   new_length = printf_length(fmt, args);
+
+   ptr = resize(*str, *start + new_length + 1);
+   if (unlikely(ptr == NULL))
+      return false;
+
+   vsnprintf(ptr + *start, new_length + 1, fmt, args);
+   *str = ptr;
+   *start += new_length;
+   return true;
+}
diff --git a/assembler/ralloc.h b/assembler/ralloc.h
new file mode 100644
index 0000000..6228d5b
--- /dev/null
+++ b/assembler/ralloc.h
@@ -0,0 +1,407 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
+ */
+
+/**
+ * \file ralloc.h
+ *
+ * ralloc: a recursive memory allocator
+ *
+ * The ralloc memory allocator creates a hierarchy of allocated
+ * objects. Every allocation is in reference to some parent, and
+ * every allocated object can in turn be used as the parent of a
+ * subsequent allocation. This allows for extremely convenient
+ * discarding of an entire tree/sub-tree of allocations by calling
+ * ralloc_free on any particular object to free it and all of its
+ * children.
+ *
+ * The conceptual working of ralloc was directly inspired by Andrew
+ * Tridgell's talloc, but ralloc is an independent implementation
+ * released under the MIT license and tuned for Mesa.
+ *
+ * The talloc implementation is available under the GNU Lesser
+ * General Public License (GNU LGPL), version 3 or later. It is
+ * more sophisticated than ralloc in that it includes reference
+ * counting and debugging features. See: http://talloc.samba.org/
+ */
+
+#ifndef RALLOC_H
+#define RALLOC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include "brw_compat.h"
+
+/**
+ * \def ralloc(ctx, type)
+ * Allocate a new object chained off of the given context.
+ *
+ * This is equivalent to:
+ * \code
+ * ((type *) ralloc_size(ctx, sizeof(type))
+ * \endcode
+ */
+#define ralloc(ctx, type)  ((type *) ralloc_size(ctx, sizeof(type)))
+
+/**
+ * \def rzalloc(ctx, type)
+ * Allocate a new object out of the given context and initialize it to zero.
+ *
+ * This is equivalent to:
+ * \code
+ * ((type *) rzalloc_size(ctx, sizeof(type))
+ * \endcode
+ */
+#define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
+
+/**
+ * Allocate a new ralloc context.
+ *
+ * While any ralloc'd pointer can be used as a context, sometimes it is useful
+ * to simply allocate a context with no associated memory.
+ *
+ * It is equivalent to:
+ * \code
+ * ((type *) ralloc_size(ctx, 0)
+ * \endcode
+ */
+void *ralloc_context(const void *ctx);
+
+/**
+ * Allocate memory chained off of the given context.
+ *
+ * This is the core allocation routine which is used by all others.  It
+ * simply allocates storage for \p size bytes and returns the pointer,
+ * similar to \c malloc.
+ */
+void *ralloc_size(const void *ctx, size_t size);
+
+/**
+ * Allocate zero-initialized memory chained off of the given context.
+ *
+ * This is similar to \c calloc with a size of 1.
+ */
+void *rzalloc_size(const void *ctx, size_t size);
+
+/**
+ * Resize a piece of ralloc-managed memory, preserving data.
+ *
+ * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
+ * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
+ * calling ralloc_size(ctx, 0).  This is different from talloc.
+ *
+ * \param ctx  The context to use for new allocation.  If \p ptr != NULL,
+ *             it must be the same as ralloc_parent(\p ptr).
+ * \param ptr  Pointer to the memory to be resized.  May be NULL.
+ * \param size The amount of memory to allocate, in bytes.
+ */
+void *reralloc_size(const void *ctx, void *ptr, size_t size);
+
+/// \defgroup array Array Allocators @{
+
+/**
+ * \def ralloc_array(ctx, type, count)
+ * Allocate an array of objects chained off the given context.
+ *
+ * Similar to \c calloc, but does not initialize the memory to zero.
+ *
+ * More than a convenience function, this also checks for integer overflow when
+ * multiplying \c sizeof(type) and \p count.  This is necessary for security.
+ *
+ * This is equivalent to:
+ * \code
+ * ((type *) ralloc_array_size(ctx, sizeof(type), count)
+ * \endcode
+ */
+#define ralloc_array(ctx, type, count) \
+   ((type *) ralloc_array_size(ctx, sizeof(type), count))
+
+/**
+ * \def rzalloc_array(ctx, type, count)
+ * Allocate a zero-initialized array chained off the given context.
+ *
+ * Similar to \c calloc.
+ *
+ * More than a convenience function, this also checks for integer overflow when
+ * multiplying \c sizeof(type) and \p count.  This is necessary for security.
+ *
+ * This is equivalent to:
+ * \code
+ * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
+ * \endcode
+ */
+#define rzalloc_array(ctx, type, count) \
+   ((type *) rzalloc_array_size(ctx, sizeof(type), count))
+
+/**
+ * \def reralloc(ctx, ptr, type, count)
+ * Resize a ralloc-managed array, preserving data.
+ *
+ * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
+ * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
+ * calling ralloc_size(ctx, 0).  This is different from talloc.
+ *
+ * More than a convenience function, this also checks for integer overflow when
+ * multiplying \c sizeof(type) and \p count.  This is necessary for security.
+ *
+ * \param ctx   The context to use for new allocation.  If \p ptr != NULL,
+ *              it must be the same as ralloc_parent(\p ptr).
+ * \param ptr   Pointer to the array to be resized.  May be NULL.
+ * \param type  The element type.
+ * \param count The number of elements to allocate.
+ */
+#define reralloc(ctx, ptr, type, count) \
+   ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
+
+/**
+ * Allocate memory for an array chained off the given context.
+ *
+ * Similar to \c calloc, but does not initialize the memory to zero.
+ *
+ * More than a convenience function, this also checks for integer overflow when
+ * multiplying \p size and \p count.  This is necessary for security.
+ */
+void *ralloc_array_size(const void *ctx, size_t size, unsigned count);
+
+/**
+ * Allocate a zero-initialized array chained off the given context.
+ *
+ * Similar to \c calloc.
+ *
+ * More than a convenience function, this also checks for integer overflow when
+ * multiplying \p size and \p count.  This is necessary for security.
+ */
+void *rzalloc_array_size(const void *ctx, size_t size, unsigned count);
+
+/**
+ * Resize a ralloc-managed array, preserving data.
+ *
+ * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
+ * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
+ * calling ralloc_size(ctx, 0).  This is different from talloc.
+ *
+ * More than a convenience function, this also checks for integer overflow when
+ * multiplying \c sizeof(type) and \p count.  This is necessary for security.
+ *
+ * \param ctx   The context to use for new allocation.  If \p ptr != NULL,
+ *              it must be the same as ralloc_parent(\p ptr).
+ * \param ptr   Pointer to the array to be resized.  May be NULL.
+ * \param size  The size of an individual element.
+ * \param count The number of elements to allocate.
+ *
+ * \return True unless allocation failed.
+ */
+void *reralloc_array_size(const void *ctx, void *ptr, size_t size,
+			  unsigned count);
+/// @}
+
+/**
+ * Free a piece of ralloc-managed memory.
+ *
+ * This will also free the memory of any children allocated this context.
+ */
+void ralloc_free(void *ptr);
+
+/**
+ * "Steal" memory from one context, changing it to another.
+ *
+ * This changes \p ptr's context to \p new_ctx.  This is quite useful if
+ * memory is allocated out of a temporary context.
+ */
+void ralloc_steal(const void *new_ctx, void *ptr);
+
+/**
+ * Return the given pointer's ralloc context.
+ */
+void *ralloc_parent(const void *ptr);
+
+/**
+ * Return a context whose memory will be automatically freed at program exit.
+ *
+ * The first call to this function creates a context and registers a handler
+ * to free it using \c atexit.  This may cause trouble if used in a library
+ * loaded with \c dlopen.
+ */
+void *ralloc_autofree_context(void);
+
+/**
+ * Set a callback to occur just before an object is freed.
+ */
+void ralloc_set_destructor(const void *ptr, void(*destructor)(void *));
+
+/// \defgroup array String Functions @{
+/**
+ * Duplicate a string, allocating the memory from the given context.
+ */
+char *ralloc_strdup(const void *ctx, const char *str);
+
+/**
+ * Duplicate a string, allocating the memory from the given context.
+ *
+ * Like \c strndup, at most \p n characters are copied.  If \p str is longer
+ * than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
+ */
+char *ralloc_strndup(const void *ctx, const char *str, size_t n);
+
+/**
+ * Concatenate two strings, allocating the necessary space.
+ *
+ * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize
+ * to expand \p *dest to the appropriate size.  \p dest will be updated to the
+ * new pointer unless allocation fails.
+ *
+ * The result will always be null-terminated.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_strcat(char **dest, const char *str);
+
+/**
+ * Concatenate two strings, allocating the necessary space.
+ *
+ * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize
+ * to expand \p *dest to the appropriate size.  \p dest will be updated to the
+ * new pointer unless allocation fails.
+ *
+ * The result will always be null-terminated; \p str does not need to be null
+ * terminated if it is longer than \p n.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_strncat(char **dest, const char *str, size_t n);
+
+/**
+ * Print to a string.
+ *
+ * This is analogous to \c sprintf, but allocates enough space (using \p ctx
+ * as the context) for the resulting string.
+ *
+ * \return The newly allocated string.
+ */
+char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3);
+
+/**
+ * Print to a string, given a va_list.
+ *
+ * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
+ * as the context) for the resulting string.
+ *
+ * \return The newly allocated string.
+ */
+char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
+
+/**
+ * Rewrite the tail of an existing string, starting at a given index.
+ *
+ * Overwrites the contents of *str starting at \p start with newly formatted
+ * text, including a new null-terminator.  Allocates more memory as necessary.
+ *
+ * This can be used to append formatted text when the length of the existing
+ * string is already known, saving a strlen() call.
+ *
+ * \sa ralloc_asprintf_append
+ *
+ * \param str   The string to be updated.
+ * \param start The index to start appending new data at.
+ * \param fmt   A printf-style formatting string
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ * \p start will be increased by the length of the newly formatted text.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
+				  const char *fmt, ...)
+				  PRINTFLIKE(3, 4);
+
+/**
+ * Rewrite the tail of an existing string, starting at a given index.
+ *
+ * Overwrites the contents of *str starting at \p start with newly formatted
+ * text, including a new null-terminator.  Allocates more memory as necessary.
+ *
+ * This can be used to append formatted text when the length of the existing
+ * string is already known, saving a strlen() call.
+ *
+ * \sa ralloc_vasprintf_append
+ *
+ * \param str   The string to be updated.
+ * \param start The index to start appending new data at.
+ * \param fmt   A printf-style formatting string
+ * \param args  A va_list containing the data to be formatted
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ * \p start will be increased by the length of the newly formatted text.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
+				   va_list args);
+
+/**
+ * Append formatted text to the supplied string.
+ *
+ * This is equivalent to
+ * \code
+ * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
+ * \endcode
+ *
+ * \sa ralloc_asprintf
+ * \sa ralloc_asprintf_rewrite_tail
+ * \sa ralloc_strcat
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_asprintf_append (char **str, const char *fmt, ...)
+			     PRINTFLIKE(2, 3);
+
+/**
+ * Append formatted text to the supplied string, given a va_list.
+ *
+ * This is equivalent to
+ * \code
+ * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
+ * \endcode
+ *
+ * \sa ralloc_vasprintf
+ * \sa ralloc_vasprintf_rewrite_tail
+ * \sa ralloc_strcat
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
+/// @}
+
+#ifdef __cplusplus
+} /* end of extern "C" */
+#endif
+
+#endif
-- 
1.7.7.5

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

  parent reply	other threads:[~2013-02-04 15:29 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-04 15:26 Sync the assembler with Mesa's opcode emission code Damien Lespiau
2013-02-04 15:26 ` [PATCH 01/90] build: Add CAIRO_FLAGS to the debugger compilation Damien Lespiau
2013-02-04 15:26 ` [PATCH 02/90] gitignore: Ignore TAGS files Damien Lespiau
2013-02-04 15:26 ` [PATCH 03/90] build: Don't use AM_MAINTAINER_MODE Damien Lespiau
2013-02-04 15:26 ` [PATCH 04/90] build: Only build the assembler if flex and bison are found Damien Lespiau
2013-02-04 15:27 ` [PATCH 05/90] build: Add the debugger compilation status to the summary Damien Lespiau
2013-02-04 15:27 ` [PATCH 06/90] assembler: Sync brw_instruction's header with mesa's Damien Lespiau
2013-02-04 15:27 ` [PATCH 07/90] assembler: Rename three_src_gen6 to da3src Damien Lespiau
2013-02-04 15:27 ` [PATCH 08/90] assembler: Rename dp_read_gen6 to gen6_dp_sampler_const_cache Damien Lespiau
2013-02-04 15:27 ` [PATCH 09/90] assembler: Rename dp_gen6 to gen6_dp and sync with Mesa's Damien Lespiau
2013-02-04 15:27 ` [PATCH 10/90] assembler: Rename dp_gen7 to gen7_dp and sync it " Damien Lespiau
2013-02-04 15:27 ` [PATCH 11/90] assembler: Remove struct dp_write_gen6 and struct use gen6_dp Damien Lespiau
2013-02-04 15:27 ` [PATCH 12/90] assembler: Rename gen5 DP pixel_scoreboard_clear to last_render_target Damien Lespiau
2013-02-04 15:27 ` [PATCH 13/90] assembler: Rename branch to branch_gen6 Damien Lespiau
2013-02-04 15:27 ` [PATCH 14/90] assembler: Rename branch_2_offset to break_cont Damien Lespiau
2013-02-04 15:27 ` [PATCH 15/90] assembler: Rename bits3.id and bits3.fd Damien Lespiau
2013-02-04 15:27 ` [PATCH 16/90] assembler: Adopt brw_structs.h from mesa Damien Lespiau
2013-02-04 15:27 ` [PATCH 17/90] assembler: Remove trailing white spaces from brw_structs.h Damien Lespiau
2013-02-04 15:27 ` [PATCH 18/90] assembler: Adopt enum brw_message_target from mesa Damien Lespiau
2013-02-04 15:27 ` [PATCH 19/90] assembler: Rename BRW_ACCWRCTRL_ACCWRCTRL Damien Lespiau
2013-02-04 15:27 ` [PATCH 20/90] assembler: Import brw_defines.h from Mesa Damien Lespiau
2013-02-04 15:27 ` [PATCH 21/90] assembler: Remove trailing white space from brw_defines.h Damien Lespiau
2013-02-04 15:27 ` [PATCH 22/90] assembler: Update the disassembler code Damien Lespiau
2013-02-04 15:27 ` Damien Lespiau [this message]
2013-02-04 15:27 ` [PATCH 24/90] assembler: Remove white space from brw_eu.h Damien Lespiau
2013-02-04 15:27 ` [PATCH 25/90] assembler: Introduce struct brw_context Damien Lespiau
2013-02-04 15:27 ` [PATCH 26/90] assembler: Make an libbrw library Damien Lespiau
2013-02-04 15:27 ` [PATCH 27/90] assembler: Protect gen4asm.h from multiple inclusions Damien Lespiau
2013-02-04 15:27 ` [PATCH 28/90] assembler: Import brw_eu_compact.c Damien Lespiau
2013-02-04 15:27 ` [PATCH 29/90] assembler: Import brw_eu.c Damien Lespiau
2013-02-04 15:27 ` [PATCH 30/90] assembler: Don't use -Wpointer-arith Damien Lespiau
2013-02-04 15:27 ` [PATCH 31/90] assembler: Import brw_eu_emit.c Damien Lespiau
2013-02-04 15:27 ` [PATCH 32/90] assembler: Use BRW_WRITEMASK_XYZW instead of the 0xf constant Damien Lespiau
2013-02-04 15:27 ` [PATCH 33/90] assembler: Remove the writemask_set field of struct dest_operand Damien Lespiau
2013-02-04 15:27 ` [PATCH 34/90] assembler: Use subreg_nr to store the address register subreg Damien Lespiau
2013-02-04 15:27 ` [PATCH 35/90] assembler: Simplify get_subreg_address() Damien Lespiau
2013-02-04 15:27 ` [PATCH 36/90] assembler: Make print_instruction() take an instruction Damien Lespiau
2013-02-04 15:27 ` [PATCH 37/90] assembler: Refactor the code adding instructions and labels Damien Lespiau
2013-02-04 15:27 ` [PATCH 38/90] assembler: Make explicit that labels are part of the instructions list Damien Lespiau
2013-02-04 15:27 ` [PATCH 39/90] assembler: Don't change the size of opcodes! Damien Lespiau
2013-02-04 15:27 ` [PATCH 40/90] assembler: Make sure nobody adds a field back to struct brw_instruction Damien Lespiau
2013-02-04 15:27 ` [PATCH 41/90] assembler: Don't expose functions only used in main.c Damien Lespiau
2013-02-04 15:27 ` [PATCH 42/90] assembler: Make struct declared_register use struct brw_reg Damien Lespiau
2013-02-04 15:27 ` [PATCH 43/90] assembler: Replace struct direct_reg by " Damien Lespiau
2013-02-04 15:27 ` [PATCH 44/90] assembler: Replace struct indirect_reg " Damien Lespiau
2013-02-04 15:27 ` [PATCH 45/90] assembler: Unify the direct and indirect register type Damien Lespiau
2013-02-04 15:27 ` [PATCH 46/90] assembler: Replace struct dst_operand by struct brw_reg Damien Lespiau
2013-02-04 15:27 ` [PATCH 47/90] assembler: Consolidate the swizzling configuration on 8 bits Damien Lespiau
2013-02-04 15:27 ` [PATCH 48/90] assembler: Get rid of src operand's swizzle_set Damien Lespiau
2013-02-04 15:27 ` [PATCH 49/90] assembler: Use brw_reg in the source operand Damien Lespiau
2013-02-04 15:27 ` [PATCH 50/90] assembler: Factor out the destination register validation Damien Lespiau
2013-02-04 15:27 ` [PATCH 51/90] assembler: Use brw_set_dest() to encode the destination Damien Lespiau
2013-02-04 15:27 ` [PATCH 52/90] assembler: Factor out the source register validation Damien Lespiau
2013-02-04 15:27 ` [PATCH 53/90] assembler: ExecSize can be as big as 32 channels Damien Lespiau
2013-02-04 15:27 ` [PATCH 54/90] assembler: Fix comparisons between reg.type and Architecture registers Damien Lespiau
2013-02-04 15:27 ` [PATCH 55/90] assembler: Store immediate values in reg.dw1.ud Damien Lespiau
2013-02-04 15:27 ` [PATCH 56/90] assembler: Don't warn if identical declared registers are redefined Damien Lespiau
2013-02-04 15:27 ` [PATCH 57/90] assembler: Add location support Damien Lespiau
2013-02-04 15:27 ` [PATCH 58/90] assembler: Add error() and warn() shorthands and use them in set_src[01] Damien Lespiau
2013-02-04 15:27 ` [PATCH 59/90] assembler: Add a check for when width is 1 and hstride is not 0 Damien Lespiau
2013-02-04 15:27 ` [PATCH 60/90] assembler: Add a check for when ExecSize and width are 1 Damien Lespiau
2013-02-04 15:27 ` [PATCH 61/90] assembler: Add the input filename to the error/warning messages Damien Lespiau
2013-02-04 15:27 ` [PATCH 62/90] assembler: Use brw_set_src0() Damien Lespiau
2013-02-04 15:27 ` [PATCH 63/90] assembler: Port the warning and error reporting to warn()/error() Damien Lespiau
2013-02-04 15:27 ` [PATCH 64/90] assembler: Cleanup visibility of a few global variables/functions Damien Lespiau
2013-02-04 15:28 ` [PATCH 65/90] assembler: Fix ')' placement in condition Damien Lespiau
2013-02-04 15:28 ` [PATCH 66/90] assembler: Implement register-indirect addressing mode in brw_set_src1() Damien Lespiau
2013-02-04 15:28 ` [PATCH 67/90] assembler: Use brw_set_src1() Damien Lespiau
2013-02-04 15:28 ` [PATCH 68/90] assembler: Renamed the instruction field to insn Damien Lespiau
2013-02-04 15:28 ` [PATCH 69/90] assembler: Unify all instructions to be brw_program_instructions Damien Lespiau
2013-02-04 15:28 ` [PATCH 70/90] assembler: Move struct relocation out of relocatable instructions Damien Lespiau
2013-02-04 15:28 ` [PATCH 71/90] assembler: Gather all predicate data in its own structure Damien Lespiau
2013-02-04 15:28 ` [PATCH 72/90] assembler: Unify adding options to the header Damien Lespiau
2013-02-04 15:28 ` [PATCH 73/90] assembler: Isolate all the options in their own structure Damien Lespiau
2013-02-04 15:28 ` [PATCH 74/90] assembler: Introduce set_instruction_opcode() Damien Lespiau
2013-02-04 15:28 ` [PATCH 75/90] assembler: Introduce set_intruction_pred_cond() Damien Lespiau
2013-02-04 15:28 ` [PATCH 76/90] assembler: Introduce set_instruction_saturate() Damien Lespiau
2013-02-04 15:28 ` [PATCH 77/90] assembler: Expose setters for 3src operands Damien Lespiau
2013-02-04 15:28 ` [PATCH 78/90] assembler: Add support for D and UD in 3-src instructions Damien Lespiau
2013-02-04 15:28 ` [PATCH 79/90] assembler: Use brw_*() functions for " Damien Lespiau
2013-02-04 15:28 ` [PATCH 80/90] assembler: Don't pollute the library files with gen4asm.h Damien Lespiau
2013-02-04 15:28 ` [PATCH 81/90] assembler: Put struct opcode_desc back in brw_context.h Damien Lespiau
2013-02-04 15:28 ` [PATCH 82/90] assembler: Use set_instruction_src1() in send Damien Lespiau
2013-02-04 15:28 ` [PATCH 83/90] assembler: Finish importing brw_eu_*c from mesa Damien Lespiau
2013-02-04 15:28 ` [PATCH 84/90] assembler: Merge declared_register's type into the reg structure Damien Lespiau
2013-02-04 15:28 ` [PATCH 85/90] assembler: Use defines for width Damien Lespiau
2013-02-04 15:28 ` [PATCH 86/90] assembler: Remove trailing white space Damien Lespiau
2013-02-04 15:28 ` [PATCH 87/90] assembler: Don't use GL types Damien Lespiau
2013-02-04 15:28 ` [PATCH 88/90] assembler: Group the header inclusions together Damien Lespiau
2013-02-04 15:28 ` [PATCH 89/90] assembler: Fix the decoding of the destination horizontal stride Damien Lespiau
2013-02-04 15:28 ` [PATCH 90/90] assembler: Mark format() as PRINTFLIKE in the disassembler Damien Lespiau
2013-02-14 19:18 ` Sync the assembler with Mesa's opcode emission code Damien Lespiau
2013-03-04 16:38   ` Damien Lespiau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1359991705-5254-24-git-send-email-damien.lespiau@intel.com \
    --to=damien.lespiau@intel.com \
    --cc=intel-gfx@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.