From 5fb4fb602bde5140dcea45464e3b70a49078ad2e Mon Sep 17 00:00:00 2001 From: marha Date: Fri, 24 Feb 2012 08:16:00 +0100 Subject: fontconfig libX11 pixman xserver git update 24 Feb 2012 --- mesalib/src/gallium/auxiliary/util/u_blitter.c | 21 +- .../src/gallium/auxiliary/util/u_debug_memory.c | 623 +++++++++++---------- mesalib/src/gallium/auxiliary/util/u_inlines.h | 22 + mesalib/src/gallium/auxiliary/util/u_slab.c | 340 +++++------ 4 files changed, 525 insertions(+), 481 deletions(-) (limited to 'mesalib/src/gallium/auxiliary') diff --git a/mesalib/src/gallium/auxiliary/util/u_blitter.c b/mesalib/src/gallium/auxiliary/util/u_blitter.c index 6a32de619..48808ae08 100644 --- a/mesalib/src/gallium/auxiliary/util/u_blitter.c +++ b/mesalib/src/gallium/auxiliary/util/u_blitter.c @@ -241,7 +241,7 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) } if (ctx->has_stream_out) { - velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + velem[0].src_format = PIPE_FORMAT_R32_UINT; ctx->velem_state_readbuf = pipe->create_vertex_elements_state(pipe, 1, &velem[0]); } @@ -263,8 +263,8 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) memset(&so, 0, sizeof(so)); so.num_outputs = 1; - so.output[0].num_components = 4; - so.stride[0] = 4; + so.output[0].num_components = 1; + so.stride[0] = 1; ctx->vs_pos_only = util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names, @@ -1247,12 +1247,23 @@ void util_blitter_copy_buffer(struct blitter_context *blitter, struct pipe_vertex_buffer vb; struct pipe_stream_output_target *so_target; + if (srcx >= src->width0 || + dstx >= dst->width0) { + return; + } + if (srcx + size > src->width0) { + size = src->width0 - srcx; + } + if (dstx + size > dst->width0) { + size = dst->width0 - dstx; + } + /* Drivers not capable of Stream Out should not call this function * in the first place. */ assert(ctx->has_stream_out); /* Some alignment is required. */ - if (srcx % 4 != 0 || dstx % 4 != 0 || size % 16 != 0 || + if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 || !ctx->has_stream_out) { struct pipe_box box; u_box_1d(srcx, size, &box); @@ -1277,7 +1288,7 @@ void util_blitter_copy_buffer(struct blitter_context *blitter, so_target = pipe->create_stream_output_target(pipe, dst, dstx, size); pipe->set_stream_output_targets(pipe, 1, &so_target, 0); - util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 16); + util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4); blitter_restore_vertex_states(ctx); blitter_unset_running_flag(ctx); diff --git a/mesalib/src/gallium/auxiliary/util/u_debug_memory.c b/mesalib/src/gallium/auxiliary/util/u_debug_memory.c index 36497921e..e24a8bc0b 100644 --- a/mesalib/src/gallium/auxiliary/util/u_debug_memory.c +++ b/mesalib/src/gallium/auxiliary/util/u_debug_memory.c @@ -1,307 +1,316 @@ -/************************************************************************** - * - * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * 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, sub license, 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 NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 - * Memory debugging. - * - * @author José Fonseca - */ - -#include "pipe/p_config.h" - -#define DEBUG_MEMORY_IMPLEMENTATION - -#include "os/os_memory.h" -#include "os/os_memory_debug.h" - -#include "util/u_debug.h" -#include "util/u_debug_stack.h" -#include "util/u_double_list.h" - - -#define DEBUG_MEMORY_MAGIC 0x6e34090aU -#define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */ - - -struct debug_memory_header -{ - struct list_head head; - - unsigned long no; - const char *file; - unsigned line; - const char *function; -#if DEBUG_MEMORY_STACK - struct debug_stack_frame backtrace[DEBUG_MEMORY_STACK]; -#endif - size_t size; - - unsigned magic; -}; - -struct debug_memory_footer -{ - unsigned magic; -}; - - -static struct list_head list = { &list, &list }; - -static unsigned long last_no = 0; - - -static INLINE struct debug_memory_header * -header_from_data(void *data) -{ - if(data) - return (struct debug_memory_header *)((char *)data - sizeof(struct debug_memory_header)); - else - return NULL; -} - -static INLINE void * -data_from_header(struct debug_memory_header *hdr) -{ - if(hdr) - return (void *)((char *)hdr + sizeof(struct debug_memory_header)); - else - return NULL; -} - -static INLINE struct debug_memory_footer * -footer_from_header(struct debug_memory_header *hdr) -{ - if(hdr) - return (struct debug_memory_footer *)((char *)hdr + sizeof(struct debug_memory_header) + hdr->size); - else - return NULL; -} - - -void * -debug_malloc(const char *file, unsigned line, const char *function, - size_t size) -{ - struct debug_memory_header *hdr; - struct debug_memory_footer *ftr; - - hdr = os_malloc(sizeof(*hdr) + size + sizeof(*ftr)); - if(!hdr) { - debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n", - file, line, function, - (long unsigned)size); - return NULL; - } - - hdr->no = last_no++; - hdr->file = file; - hdr->line = line; - hdr->function = function; - hdr->size = size; - hdr->magic = DEBUG_MEMORY_MAGIC; - -#if DEBUG_MEMORY_STACK - debug_backtrace_capture(hdr->backtrace, 0, DEBUG_MEMORY_STACK); -#endif - - ftr = footer_from_header(hdr); - ftr->magic = DEBUG_MEMORY_MAGIC; - - LIST_ADDTAIL(&hdr->head, &list); - - return data_from_header(hdr); -} - -void -debug_free(const char *file, unsigned line, const char *function, - void *ptr) -{ - struct debug_memory_header *hdr; - struct debug_memory_footer *ftr; - - if(!ptr) - return; - - hdr = header_from_data(ptr); - if(hdr->magic != DEBUG_MEMORY_MAGIC) { - debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n", - file, line, function, - ptr); - debug_assert(0); - return; - } - - ftr = footer_from_header(hdr); - if(ftr->magic != DEBUG_MEMORY_MAGIC) { - debug_printf("%s:%u:%s: buffer overflow %p\n", - hdr->file, hdr->line, hdr->function, - ptr); - debug_assert(0); - } - - LIST_DEL(&hdr->head); - hdr->magic = 0; - ftr->magic = 0; - - os_free(hdr); -} - -void * -debug_calloc(const char *file, unsigned line, const char *function, - size_t count, size_t size ) -{ - void *ptr = debug_malloc( file, line, function, count * size ); - if( ptr ) - memset( ptr, 0, count * size ); - return ptr; -} - -void * -debug_realloc(const char *file, unsigned line, const char *function, - void *old_ptr, size_t old_size, size_t new_size ) -{ - struct debug_memory_header *old_hdr, *new_hdr; - struct debug_memory_footer *old_ftr, *new_ftr; - void *new_ptr; - - if(!old_ptr) - return debug_malloc( file, line, function, new_size ); - - if(!new_size) { - debug_free( file, line, function, old_ptr ); - return NULL; - } - - old_hdr = header_from_data(old_ptr); - if(old_hdr->magic != DEBUG_MEMORY_MAGIC) { - debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n", - file, line, function, - old_ptr); - debug_assert(0); - return NULL; - } - - old_ftr = footer_from_header(old_hdr); - if(old_ftr->magic != DEBUG_MEMORY_MAGIC) { - debug_printf("%s:%u:%s: buffer overflow %p\n", - old_hdr->file, old_hdr->line, old_hdr->function, - old_ptr); - debug_assert(0); - } - - /* alloc new */ - new_hdr = os_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr)); - if(!new_hdr) { - debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n", - file, line, function, - (long unsigned)new_size); - return NULL; - } - new_hdr->no = old_hdr->no; - new_hdr->file = old_hdr->file; - new_hdr->line = old_hdr->line; - new_hdr->function = old_hdr->function; - new_hdr->size = new_size; - new_hdr->magic = DEBUG_MEMORY_MAGIC; - - new_ftr = footer_from_header(new_hdr); - new_ftr->magic = DEBUG_MEMORY_MAGIC; - - LIST_REPLACE(&old_hdr->head, &new_hdr->head); - - /* copy data */ - new_ptr = data_from_header(new_hdr); - memcpy( new_ptr, old_ptr, old_size < new_size ? old_size : new_size ); - - /* free old */ - old_hdr->magic = 0; - old_ftr->magic = 0; - os_free(old_hdr); - - return new_ptr; -} - -unsigned long -debug_memory_begin(void) -{ - return last_no; -} - -void -debug_memory_end(unsigned long start_no) -{ - size_t total_size = 0; - struct list_head *entry; - - if(start_no == last_no) - return; - - entry = list.prev; - for (; entry != &list; entry = entry->prev) { - struct debug_memory_header *hdr; - void *ptr; - struct debug_memory_footer *ftr; - - hdr = LIST_ENTRY(struct debug_memory_header, entry, head); - ptr = data_from_header(hdr); - ftr = footer_from_header(hdr); - - if(hdr->magic != DEBUG_MEMORY_MAGIC) { - debug_printf("%s:%u:%s: bad or corrupted memory %p\n", - hdr->file, hdr->line, hdr->function, - ptr); - debug_assert(0); - } - - if((start_no <= hdr->no && hdr->no < last_no) || - (last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) { - debug_printf("%s:%u:%s: %lu bytes at %p not freed\n", - hdr->file, hdr->line, hdr->function, - (unsigned long) hdr->size, ptr); -#if DEBUG_MEMORY_STACK - debug_backtrace_dump(hdr->backtrace, DEBUG_MEMORY_STACK); -#endif - total_size += hdr->size; - } - - if(ftr->magic != DEBUG_MEMORY_MAGIC) { - debug_printf("%s:%u:%s: buffer overflow %p\n", - hdr->file, hdr->line, hdr->function, - ptr); - debug_assert(0); - } - } - - if(total_size) { - debug_printf("Total of %lu KB of system memory apparently leaked\n", - (unsigned long) (total_size + 1023)/1024); - } - else { - debug_printf("No memory leaks detected.\n"); - } -} +/************************************************************************** + * + * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. + * All Rights Reserved. + * + * 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, sub license, 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 NON-INFRINGEMENT. + * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 + * Memory debugging. + * + * @author José Fonseca + */ + +#include "pipe/p_config.h" + +#define DEBUG_MEMORY_IMPLEMENTATION + +#include "os/os_memory.h" +#include "os/os_memory_debug.h" +#include "os/os_thread.h" + +#include "util/u_debug.h" +#include "util/u_debug_stack.h" +#include "util/u_double_list.h" + + +#define DEBUG_MEMORY_MAGIC 0x6e34090aU +#define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */ + + +struct debug_memory_header +{ + struct list_head head; + + unsigned long no; + const char *file; + unsigned line; + const char *function; +#if DEBUG_MEMORY_STACK + struct debug_stack_frame backtrace[DEBUG_MEMORY_STACK]; +#endif + size_t size; + + unsigned magic; +}; + +struct debug_memory_footer +{ + unsigned magic; +}; + + +static struct list_head list = { &list, &list }; + +pipe_static_mutex(list_mutex); + +static unsigned long last_no = 0; + + +static INLINE struct debug_memory_header * +header_from_data(void *data) +{ + if(data) + return (struct debug_memory_header *)((char *)data - sizeof(struct debug_memory_header)); + else + return NULL; +} + +static INLINE void * +data_from_header(struct debug_memory_header *hdr) +{ + if(hdr) + return (void *)((char *)hdr + sizeof(struct debug_memory_header)); + else + return NULL; +} + +static INLINE struct debug_memory_footer * +footer_from_header(struct debug_memory_header *hdr) +{ + if(hdr) + return (struct debug_memory_footer *)((char *)hdr + sizeof(struct debug_memory_header) + hdr->size); + else + return NULL; +} + + +void * +debug_malloc(const char *file, unsigned line, const char *function, + size_t size) +{ + struct debug_memory_header *hdr; + struct debug_memory_footer *ftr; + + hdr = os_malloc(sizeof(*hdr) + size + sizeof(*ftr)); + if(!hdr) { + debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n", + file, line, function, + (long unsigned)size); + return NULL; + } + + hdr->no = last_no++; + hdr->file = file; + hdr->line = line; + hdr->function = function; + hdr->size = size; + hdr->magic = DEBUG_MEMORY_MAGIC; + +#if DEBUG_MEMORY_STACK + debug_backtrace_capture(hdr->backtrace, 0, DEBUG_MEMORY_STACK); +#endif + + ftr = footer_from_header(hdr); + ftr->magic = DEBUG_MEMORY_MAGIC; + + pipe_mutex_lock(list_mutex); + LIST_ADDTAIL(&hdr->head, &list); + pipe_mutex_unlock(list_mutex); + + return data_from_header(hdr); +} + +void +debug_free(const char *file, unsigned line, const char *function, + void *ptr) +{ + struct debug_memory_header *hdr; + struct debug_memory_footer *ftr; + + if(!ptr) + return; + + hdr = header_from_data(ptr); + if(hdr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n", + file, line, function, + ptr); + debug_assert(0); + return; + } + + ftr = footer_from_header(hdr); + if(ftr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: buffer overflow %p\n", + hdr->file, hdr->line, hdr->function, + ptr); + debug_assert(0); + } + + pipe_mutex_lock(list_mutex); + LIST_DEL(&hdr->head); + pipe_mutex_unlock(list_mutex); + hdr->magic = 0; + ftr->magic = 0; + + os_free(hdr); +} + +void * +debug_calloc(const char *file, unsigned line, const char *function, + size_t count, size_t size ) +{ + void *ptr = debug_malloc( file, line, function, count * size ); + if( ptr ) + memset( ptr, 0, count * size ); + return ptr; +} + +void * +debug_realloc(const char *file, unsigned line, const char *function, + void *old_ptr, size_t old_size, size_t new_size ) +{ + struct debug_memory_header *old_hdr, *new_hdr; + struct debug_memory_footer *old_ftr, *new_ftr; + void *new_ptr; + + if(!old_ptr) + return debug_malloc( file, line, function, new_size ); + + if(!new_size) { + debug_free( file, line, function, old_ptr ); + return NULL; + } + + old_hdr = header_from_data(old_ptr); + if(old_hdr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n", + file, line, function, + old_ptr); + debug_assert(0); + return NULL; + } + + old_ftr = footer_from_header(old_hdr); + if(old_ftr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: buffer overflow %p\n", + old_hdr->file, old_hdr->line, old_hdr->function, + old_ptr); + debug_assert(0); + } + + /* alloc new */ + new_hdr = os_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr)); + if(!new_hdr) { + debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n", + file, line, function, + (long unsigned)new_size); + return NULL; + } + new_hdr->no = old_hdr->no; + new_hdr->file = old_hdr->file; + new_hdr->line = old_hdr->line; + new_hdr->function = old_hdr->function; + new_hdr->size = new_size; + new_hdr->magic = DEBUG_MEMORY_MAGIC; + + new_ftr = footer_from_header(new_hdr); + new_ftr->magic = DEBUG_MEMORY_MAGIC; + + pipe_mutex_lock(list_mutex); + LIST_REPLACE(&old_hdr->head, &new_hdr->head); + pipe_mutex_unlock(list_mutex); + + /* copy data */ + new_ptr = data_from_header(new_hdr); + memcpy( new_ptr, old_ptr, old_size < new_size ? old_size : new_size ); + + /* free old */ + old_hdr->magic = 0; + old_ftr->magic = 0; + os_free(old_hdr); + + return new_ptr; +} + +unsigned long +debug_memory_begin(void) +{ + return last_no; +} + +void +debug_memory_end(unsigned long start_no) +{ + size_t total_size = 0; + struct list_head *entry; + + if(start_no == last_no) + return; + + entry = list.prev; + for (; entry != &list; entry = entry->prev) { + struct debug_memory_header *hdr; + void *ptr; + struct debug_memory_footer *ftr; + + hdr = LIST_ENTRY(struct debug_memory_header, entry, head); + ptr = data_from_header(hdr); + ftr = footer_from_header(hdr); + + if(hdr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: bad or corrupted memory %p\n", + hdr->file, hdr->line, hdr->function, + ptr); + debug_assert(0); + } + + if((start_no <= hdr->no && hdr->no < last_no) || + (last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) { + debug_printf("%s:%u:%s: %lu bytes at %p not freed\n", + hdr->file, hdr->line, hdr->function, + (unsigned long) hdr->size, ptr); +#if DEBUG_MEMORY_STACK + debug_backtrace_dump(hdr->backtrace, DEBUG_MEMORY_STACK); +#endif + total_size += hdr->size; + } + + if(ftr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: buffer overflow %p\n", + hdr->file, hdr->line, hdr->function, + ptr); + debug_assert(0); + } + } + + if(total_size) { + debug_printf("Total of %lu KB of system memory apparently leaked\n", + (unsigned long) (total_size + 1023)/1024); + } + else { + debug_printf("No memory leaks detected.\n"); + } +} diff --git a/mesalib/src/gallium/auxiliary/util/u_inlines.h b/mesalib/src/gallium/auxiliary/util/u_inlines.h index c90dc5707..2fe680ff4 100644 --- a/mesalib/src/gallium/auxiliary/util/u_inlines.h +++ b/mesalib/src/gallium/auxiliary/util/u_inlines.h @@ -135,6 +135,28 @@ pipe_sampler_view_reference(struct pipe_sampler_view **ptr, struct pipe_sampler_ *ptr = view; } +/** + * Similar to pipe_sampler_view_reference() but always set the pointer to + * NULL and pass in an explicit context. Passing an explicit context is a + * work-around for fixing a dangling context pointer problem when textures + * are shared by multiple contexts. XXX fix this someday. + */ +static INLINE void +pipe_sampler_view_release(struct pipe_context *ctx, + struct pipe_sampler_view **ptr) +{ + struct pipe_sampler_view *old_view = *ptr; + if (*ptr && (*ptr)->context != ctx) { + debug_printf_once(("context mis-match in pipe_sampler_view_release()\n")); + } + if (pipe_reference_described(&(*ptr)->reference, NULL, + (debug_reference_descriptor)debug_describe_sampler_view)) { + ctx->sampler_view_destroy(ctx, old_view); + } + *ptr = NULL; +} + + static INLINE void pipe_so_target_reference(struct pipe_stream_output_target **ptr, struct pipe_stream_output_target *target) diff --git a/mesalib/src/gallium/auxiliary/util/u_slab.c b/mesalib/src/gallium/auxiliary/util/u_slab.c index 2c2175fea..f9f5ef68f 100644 --- a/mesalib/src/gallium/auxiliary/util/u_slab.c +++ b/mesalib/src/gallium/auxiliary/util/u_slab.c @@ -1,169 +1,171 @@ -/* - * Copyright 2010 Marek Olšák - * - * 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 - * on the rights to use, copy, modify, merge, publish, distribute, sub - * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL - * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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 "util/u_slab.h" - -#include "util/u_math.h" -#include "util/u_memory.h" -#include "util/u_simple_list.h" - -#include - -#define UTIL_SLAB_MAGIC 0xcafe4321 - -/* The block is either allocated memory or free space. */ -struct util_slab_block { - /* The header. */ - /* The first next free block. */ - struct util_slab_block *next_free; - - intptr_t magic; - - /* Memory after the last member is dedicated to the block itself. - * The allocated size is always larger than this structure. */ -}; - -static struct util_slab_block * -util_slab_get_block(struct util_slab_mempool *pool, - struct util_slab_page *page, unsigned index) -{ - return (struct util_slab_block*) - ((uint8_t*)page + sizeof(struct util_slab_page) + - (pool->block_size * index)); -} - -static void util_slab_add_new_page(struct util_slab_mempool *pool) -{ - struct util_slab_page *page; - struct util_slab_block *block; - int i; - - page = MALLOC(pool->page_size); - insert_at_tail(&pool->list, page); - - /* Mark all blocks as free. */ - for (i = 0; i < pool->num_blocks-1; i++) { - block = util_slab_get_block(pool, page, i); - block->next_free = util_slab_get_block(pool, page, i+1); - block->magic = UTIL_SLAB_MAGIC; - } - - block = util_slab_get_block(pool, page, pool->num_blocks-1); - block->next_free = pool->first_free; - block->magic = UTIL_SLAB_MAGIC; - pool->first_free = util_slab_get_block(pool, page, 0); - pool->num_pages++; - -#if 0 - fprintf(stderr, "New page! Num of pages: %i\n", pool->num_pages); -#endif -} - -static void *util_slab_alloc_st(struct util_slab_mempool *pool) -{ - struct util_slab_block *block; - - if (!pool->first_free) - util_slab_add_new_page(pool); - - block = pool->first_free; - assert(block->magic == UTIL_SLAB_MAGIC); - pool->first_free = block->next_free; - - return (uint8_t*)block + sizeof(struct util_slab_block); -} - -static void util_slab_free_st(struct util_slab_mempool *pool, void *ptr) -{ - struct util_slab_block *block = - (struct util_slab_block*) - ((uint8_t*)ptr - sizeof(struct util_slab_block)); - - assert(block->magic == UTIL_SLAB_MAGIC); - block->next_free = pool->first_free; - pool->first_free = block; -} - -static void *util_slab_alloc_mt(struct util_slab_mempool *pool) -{ - void *mem; - - pipe_mutex_lock(pool->mutex); - mem = util_slab_alloc_st(pool); - pipe_mutex_unlock(pool->mutex); - return mem; -} - -static void util_slab_free_mt(struct util_slab_mempool *pool, void *ptr) -{ - pipe_mutex_lock(pool->mutex); - util_slab_free_st(pool, ptr); - pipe_mutex_unlock(pool->mutex); -} - -void util_slab_set_thread_safety(struct util_slab_mempool *pool, - enum util_slab_threading threading) -{ - pool->threading = threading; - - if (threading) { - pool->alloc = util_slab_alloc_mt; - pool->free = util_slab_free_mt; - } else { - pool->alloc = util_slab_alloc_st; - pool->free = util_slab_free_st; - } -} - -void util_slab_create(struct util_slab_mempool *pool, - unsigned item_size, - unsigned num_blocks, - enum util_slab_threading threading) -{ - item_size = align(item_size, sizeof(intptr_t)); - - pool->num_pages = 0; - pool->num_blocks = num_blocks; - pool->block_size = sizeof(struct util_slab_block) + item_size; - pool->block_size = align(pool->block_size, sizeof(intptr_t)); - pool->page_size = sizeof(struct util_slab_page) + - num_blocks * pool->block_size; - pool->first_free = NULL; - - make_empty_list(&pool->list); - - pipe_mutex_init(pool->mutex); - - util_slab_set_thread_safety(pool, threading); -} - -void util_slab_destroy(struct util_slab_mempool *pool) -{ - struct util_slab_page *page, *temp; - - foreach_s(page, temp, &pool->list) { - remove_from_list(page); - FREE(page); - } - - pipe_mutex_destroy(pool->mutex); -} +/* + * Copyright 2010 Marek Olšák + * + * 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 + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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 "util/u_slab.h" + +#include "util/u_math.h" +#include "util/u_memory.h" +#include "util/u_simple_list.h" + +#include + +#define UTIL_SLAB_MAGIC 0xcafe4321 + +/* The block is either allocated memory or free space. */ +struct util_slab_block { + /* The header. */ + /* The first next free block. */ + struct util_slab_block *next_free; + + intptr_t magic; + + /* Memory after the last member is dedicated to the block itself. + * The allocated size is always larger than this structure. */ +}; + +static struct util_slab_block * +util_slab_get_block(struct util_slab_mempool *pool, + struct util_slab_page *page, unsigned index) +{ + return (struct util_slab_block*) + ((uint8_t*)page + sizeof(struct util_slab_page) + + (pool->block_size * index)); +} + +static void util_slab_add_new_page(struct util_slab_mempool *pool) +{ + struct util_slab_page *page; + struct util_slab_block *block; + int i; + + page = MALLOC(pool->page_size); + insert_at_tail(&pool->list, page); + + /* Mark all blocks as free. */ + for (i = 0; i < pool->num_blocks-1; i++) { + block = util_slab_get_block(pool, page, i); + block->next_free = util_slab_get_block(pool, page, i+1); + block->magic = UTIL_SLAB_MAGIC; + } + + block = util_slab_get_block(pool, page, pool->num_blocks-1); + block->next_free = pool->first_free; + block->magic = UTIL_SLAB_MAGIC; + pool->first_free = util_slab_get_block(pool, page, 0); + pool->num_pages++; + +#if 0 + fprintf(stderr, "New page! Num of pages: %i\n", pool->num_pages); +#endif +} + +static void *util_slab_alloc_st(struct util_slab_mempool *pool) +{ + struct util_slab_block *block; + + if (!pool->first_free) + util_slab_add_new_page(pool); + + block = pool->first_free; + assert(block->magic == UTIL_SLAB_MAGIC); + pool->first_free = block->next_free; + + return (uint8_t*)block + sizeof(struct util_slab_block); +} + +static void util_slab_free_st(struct util_slab_mempool *pool, void *ptr) +{ + struct util_slab_block *block = + (struct util_slab_block*) + ((uint8_t*)ptr - sizeof(struct util_slab_block)); + + assert(block->magic == UTIL_SLAB_MAGIC); + block->next_free = pool->first_free; + pool->first_free = block; +} + +static void *util_slab_alloc_mt(struct util_slab_mempool *pool) +{ + void *mem; + + pipe_mutex_lock(pool->mutex); + mem = util_slab_alloc_st(pool); + pipe_mutex_unlock(pool->mutex); + return mem; +} + +static void util_slab_free_mt(struct util_slab_mempool *pool, void *ptr) +{ + pipe_mutex_lock(pool->mutex); + util_slab_free_st(pool, ptr); + pipe_mutex_unlock(pool->mutex); +} + +void util_slab_set_thread_safety(struct util_slab_mempool *pool, + enum util_slab_threading threading) +{ + pool->threading = threading; + + if (threading) { + pool->alloc = util_slab_alloc_mt; + pool->free = util_slab_free_mt; + } else { + pool->alloc = util_slab_alloc_st; + pool->free = util_slab_free_st; + } +} + +void util_slab_create(struct util_slab_mempool *pool, + unsigned item_size, + unsigned num_blocks, + enum util_slab_threading threading) +{ + item_size = align(item_size, sizeof(intptr_t)); + + pool->num_pages = 0; + pool->num_blocks = num_blocks; + pool->block_size = sizeof(struct util_slab_block) + item_size; + pool->block_size = align(pool->block_size, sizeof(intptr_t)); + pool->page_size = sizeof(struct util_slab_page) + + num_blocks * pool->block_size; + pool->first_free = NULL; + + make_empty_list(&pool->list); + + pipe_mutex_init(pool->mutex); + + util_slab_set_thread_safety(pool, threading); +} + +void util_slab_destroy(struct util_slab_mempool *pool) +{ + struct util_slab_page *page, *temp; + + if (pool->list.next) { + foreach_s(page, temp, &pool->list) { + remove_from_list(page); + FREE(page); + } + } + + pipe_mutex_destroy(pool->mutex); +} -- cgit v1.2.3