X-Git-Url: https://git.libre-soc.org/?p=mesa.git;a=blobdiff_plain;f=src%2Futil%2Fralloc.c;h=f36f8bf365425a2c7a18d95865d9f912f9d21234;hp=42cfa2e391d52df68db265b4a4488c469294e0df;hb=273ead81f1a219b39a93abbed4db548d8eeb0e5f;hpb=c3d54d03757fcb656cc4839a2c7978d97f75508d diff --git a/src/util/ralloc.c b/src/util/ralloc.c index 42cfa2e391d..f36f8bf3654 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -53,7 +53,11 @@ _CRTIMP int _vscprintf(const char *format, va_list argptr); */ struct #ifdef _MSC_VER +#if _WIN64 +__declspec(align(16)) +#else __declspec(align(8)) +#endif #elif defined(__LP64__) __attribute__((aligned(16))) #else @@ -61,7 +65,7 @@ struct #endif ralloc_header { -#ifdef DEBUG +#ifndef NDEBUG /* A canary value used to determine whether a pointer is ralloc'd. */ unsigned canary; #endif @@ -88,9 +92,7 @@ get_header(const void *ptr) { ralloc_header *info = (ralloc_header *) (((char *) ptr) - sizeof(ralloc_header)); -#ifdef DEBUG assert(info->canary == CANARY); -#endif return info; } @@ -140,7 +142,7 @@ ralloc_size(const void *ctx, size_t size) add_child(parent, info); -#ifdef DEBUG +#ifndef NDEBUG info->canary = CANARY; #endif @@ -199,6 +201,21 @@ reralloc_size(const void *ctx, void *ptr, size_t size) return resize(ptr, size); } +void * +rerzalloc_size(const void *ctx, void *ptr, size_t old_size, size_t new_size) +{ + if (unlikely(ptr == NULL)) + return rzalloc_size(ctx, new_size); + + assert(ralloc_parent(ptr) == ctx); + ptr = resize(ptr, new_size); + + if (new_size > old_size) + memset((char *)ptr + old_size, 0, new_size - old_size); + + return ptr; +} + void * ralloc_array_size(const void *ctx, size_t size, unsigned count) { @@ -226,6 +243,16 @@ reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count) return reralloc_size(ctx, ptr, size * count); } +void * +rerzalloc_array_size(const void *ctx, void *ptr, size_t size, + unsigned old_count, unsigned new_count) +{ + if (new_count > SIZE_MAX/size) + return NULL; + + return rerzalloc_size(ctx, ptr, size * old_count, size * new_count); +} + void ralloc_free(void *ptr) { @@ -553,14 +580,20 @@ ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, * other buffers. */ -#define ALIGN_POT(x, y) (((x) + (y) - 1) & ~((y) - 1)) - #define MIN_LINEAR_BUFSIZE 2048 -#define SUBALLOC_ALIGNMENT sizeof(uintptr_t) +#define SUBALLOC_ALIGNMENT 8 #define LMAGIC 0x87b9c7d3 -struct linear_header { -#ifdef DEBUG +struct +#ifdef _MSC_VER + __declspec(align(8)) +#elif defined(__LP64__) + __attribute__((aligned(16))) +#else + __attribute__((aligned(8))) +#endif + linear_header { +#ifndef NDEBUG unsigned magic; /* for debugging */ #endif unsigned offset; /* points to the first unused byte in the buffer */ @@ -610,7 +643,7 @@ create_linear_node(void *ralloc_ctx, unsigned min_size) if (unlikely(!node)) return NULL; -#ifdef DEBUG +#ifndef NDEBUG node->magic = LMAGIC; #endif node->offset = 0; @@ -630,9 +663,7 @@ linear_alloc_child(void *parent, unsigned size) linear_size_chunk *ptr; unsigned full_size; -#ifdef DEBUG assert(first->magic == LMAGIC); -#endif assert(!latest->next); size = ALIGN_POT(size, SUBALLOC_ALIGNMENT); @@ -653,6 +684,8 @@ linear_alloc_child(void *parent, unsigned size) ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset); ptr->size = size; latest->offset += full_size; + + assert((uintptr_t)&ptr[1] % SUBALLOC_ALIGNMENT == 0); return &ptr[1]; } @@ -704,9 +737,7 @@ linear_free_parent(void *ptr) return; node = LINEAR_PARENT_TO_HEADER(ptr); -#ifdef DEBUG assert(node->magic == LMAGIC); -#endif while (node) { void *ptr = node; @@ -725,9 +756,7 @@ ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr) return; node = LINEAR_PARENT_TO_HEADER(ptr); -#ifdef DEBUG assert(node->magic == LMAGIC); -#endif while (node) { ralloc_steal(new_ralloc_ctx, node); @@ -740,9 +769,7 @@ void * ralloc_parent_of_linear_parent(void *ptr) { linear_header *node = LINEAR_PARENT_TO_HEADER(ptr); -#ifdef DEBUG assert(node->magic == LMAGIC); -#endif return node->ralloc_parent; }