summaryrefslogtreecommitdiff
path: root/lib/mesa/src/util/ralloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mesa/src/util/ralloc.c')
-rw-r--r--lib/mesa/src/util/ralloc.c43
1 files changed, 29 insertions, 14 deletions
diff --git a/lib/mesa/src/util/ralloc.c b/lib/mesa/src/util/ralloc.c
index 01719c888..b8fbfbc31 100644
--- a/lib/mesa/src/util/ralloc.c
+++ b/lib/mesa/src/util/ralloc.c
@@ -51,7 +51,20 @@ _CRTIMP int _vscprintf(const char *format, va_list argptr);
#define CANARY 0x5A1106
-struct ralloc_header
+/* Align the header's size so that ralloc() allocations will return with the
+ * same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on
+ * 64-bit), avoiding performance penalities on x86 and alignment faults on
+ * ARM.
+ */
+struct
+#ifdef _MSC_VER
+ __declspec(align(8))
+#elif defined(__LP64__)
+ __attribute__((aligned(16)))
+#else
+ __attribute__((aligned(8)))
+#endif
+ ralloc_header
{
#ifdef DEBUG
/* A canary value used to determine whether a pointer is ralloc'd. */
@@ -110,6 +123,18 @@ ralloc_context(const void *ctx)
void *
ralloc_size(const void *ctx, size_t size)
{
+ /* ralloc_size was originally implemented using calloc, which meant some
+ * code accidentally relied on its zero filling behavior.
+ *
+ * TODO: Make ralloc_size not zero fill memory, and cleanup any code that
+ * should instead be using rzalloc.
+ */
+ return rzalloc_size(ctx, size);
+}
+
+void *
+rzalloc_size(const void *ctx, size_t size)
+{
void *block = calloc(1, size + sizeof(ralloc_header));
ralloc_header *info;
ralloc_header *parent;
@@ -128,15 +153,6 @@ ralloc_size(const void *ctx, size_t size)
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)
@@ -293,6 +309,7 @@ ralloc_adopt(const void *new_ctx, void *old_ctx)
/* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
child->next = new_info->child;
+ child->parent = new_info;
new_info->child = old_info->child;
old_info->child = NULL;
}
@@ -359,10 +376,7 @@ ralloc_strndup(const void *ctx, const char *str, size_t max)
if (unlikely(str == NULL))
return NULL;
- n = strlen(str);
- if (n > max)
- n = max;
-
+ n = strnlen(str, max);
ptr = ralloc_array(ctx, char, n + 1);
memcpy(ptr, str, n);
ptr[n] = '\0';
@@ -502,6 +516,7 @@ ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
if (unlikely(*str == NULL)) {
// Assuming a NULL context is probably bad, but it's expected behavior.
*str = ralloc_vasprintf(NULL, fmt, args);
+ *start = strlen(*str);
return true;
}