X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Futil%2Fralloc.c;h=0d20223d98f8a9d8c128913267b46ea503fb8244;hb=79af30768d6cf8e28c7cf49f99fff6c2b2cb030b;hp=bf46439df4ea5fe9268ecd8d7ef397ed55f8a5d8;hpb=26f4657c3f075045527c4568e8cbbaaa5d0b08e4;p=mesa.git diff --git a/src/util/ralloc.c b/src/util/ralloc.c index bf46439df4e..0d20223d98f 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -61,7 +61,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 +88,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 +138,7 @@ ralloc_size(const void *ctx, size_t size) add_child(parent, info); -#ifdef DEBUG +#ifndef NDEBUG info->canary = CANARY; #endif @@ -199,6 +197,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 +239,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) { @@ -285,7 +308,7 @@ ralloc_steal(const void *new_ctx, void *ptr) return; info = get_header(ptr); - parent = get_header(new_ctx); + parent = new_ctx ? get_header(new_ctx) : NULL; unlink_block(info); @@ -311,10 +334,12 @@ ralloc_adopt(const void *new_ctx, void *old_ctx) for (child = old_info->child; child->next != NULL; child = child->next) { child->parent = new_info; } + child->parent = new_info; /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */ child->next = new_info->child; - child->parent = new_info; + if (child->next) + child->next->prev = child; new_info->child = old_info->child; old_info->child = NULL; } @@ -551,14 +576,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 */ @@ -608,7 +639,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; @@ -649,6 +680,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]; }