diff --git a/src/gallium/auxiliary/os/os_memory_debug.h b/src/gallium/auxiliary/os/os_memory_debug.h deleted file mode 100644 index 9a487de..0000000 --- a/src/gallium/auxiliary/os/os_memory_debug.h +++ /dev/null @@ -1,92 +0,0 @@ -/************************************************************************** - * - * Copyright 2008-2010 VMware, Inc. - * 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 VMWARE 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. - * - **************************************************************************/ - - -/* - * Debugging wrappers for OS memory management abstractions. - */ - - -#ifndef _OS_MEMORY_H_ -#error "Must not be included directly. Include os_memory.h instead" -#endif - - -#include "pipe/p_compiler.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -void * -debug_malloc(const char *file, unsigned line, const char *function, - size_t size); - -void * -debug_calloc(const char *file, unsigned line, const char *function, - size_t count, size_t size ); - -void -debug_free(const char *file, unsigned line, const char *function, - void *ptr); - -void * -debug_realloc(const char *file, unsigned line, const char *function, - void *old_ptr, size_t old_size, size_t new_size ); - -void -debug_memory_tag(void *ptr, unsigned tag); - -void -debug_memory_check_block(void *ptr); - -void -debug_memory_check(void); - - -#ifdef __cplusplus -} -#endif - - -#ifndef DEBUG_MEMORY_IMPLEMENTATION - -#define os_malloc( _size ) \ - debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size ) -#define os_calloc( _count, _size ) \ - debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size ) -#define os_free( _ptr ) \ - debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr ) -#define os_realloc( _ptr, _old_size, _new_size ) \ - debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _new_size ) - -/* TODO: wrap os_malloc_aligned() and os_free_aligned() too */ -#include "os_memory_aligned.h" - -#endif /* !DEBUG_MEMORY_IMPLEMENTATION */ diff --git a/src/gallium/auxiliary/util/u_memory.h b/src/gallium/auxiliary/util/u_memory.h index d53575e..70a75ac 100644 --- a/src/gallium/auxiliary/util/u_memory.h +++ b/src/gallium/auxiliary/util/u_memory.h @@ -37,7 +37,7 @@ #include "util/u_pointer.h" #include "util/u_debug.h" -#include "os/os_memory.h" +#include "util/os_memory.h" #ifdef __cplusplus diff --git a/src/gallium/meson.build b/src/gallium/meson.build index e839640..ac2c855 100644 --- a/src/gallium/meson.build +++ b/src/gallium/meson.build @@ -92,13 +92,9 @@ sources_libgallium = [ 'auxiliary/tgsi/tgsi_util.c', 'auxiliary/tgsi/tgsi_sanity.h', 'auxiliary/tgsi/tgsi_transform.c', - 'auxiliary/os/os_memory_aligned.h', 'auxiliary/os/os_thread.h', 'auxiliary/os/os_mman.h', 'auxiliary/os/os_misc.h', - 'auxiliary/os/os_memory.h', - 'auxiliary/os/os_memory_debug.h', - 'auxiliary/os/os_memory_stdc.h', 'auxiliary/os/os_misc.c', ] diff --git a/src/gallium/auxiliary/os/os_memory.h b/src/mesa/util/os_memory.h similarity index 87% rename from src/gallium/auxiliary/os/os_memory.h rename to src/mesa/util/os_memory.h index 46a6b6e..ad88e70 100644 --- a/src/gallium/auxiliary/os/os_memory.h +++ b/src/mesa/util/os_memory.h @@ -1,6 +1,6 @@ /************************************************************************** * - * Copyright 2010 Vmware, Inc. + * Copyright 2010 VMware, Inc. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a @@ -34,12 +34,7 @@ #ifndef _OS_MEMORY_H_ #define _OS_MEMORY_H_ - -#include "pipe/p_config.h" -#include "pipe/p_compiler.h" - - -#if defined(PIPE_SUBSYSTEM_EMBEDDED) +#if defined(EMBEDDED_DEVICE) #ifdef __cplusplus extern "C" { @@ -63,14 +58,13 @@ os_malloc_aligned(size_t size, size_t alignment); void os_free_aligned(void *ptr); +void * +os_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignemnt); + #ifdef __cplusplus } #endif -#elif defined(PIPE_OS_WINDOWS) && defined(DEBUG) && !defined(DEBUG_MEMORY_IMPLEMENTATION) - -# include "os_memory_debug.h" - #else # include "os_memory_stdc.h" diff --git a/src/gallium/auxiliary/os/os_memory_aligned.h b/src/mesa/util/os_memory_aligned.h similarity index 59% rename from src/gallium/auxiliary/os/os_memory_aligned.h rename to src/mesa/util/os_memory_aligned.h index 90a5609..08f1206 100644 --- a/src/gallium/auxiliary/os/os_memory_aligned.h +++ b/src/mesa/util/os_memory_aligned.h @@ -36,8 +36,33 @@ #endif -#include "pipe/p_compiler.h" +/** + * Add two size_t values with integer overflow check. + * TODO: leverage __builtin_add_overflow where available + */ +static inline bool +add_overflow_size_t(size_t a, size_t b, size_t *res) +{ + *res = a + b; + return *res < a || *res < b; +} + + +#if defined(HAVE_POSIX_MEMALIGN) + +static inline void * +os_malloc_aligned(size_t size, size_t alignment) +{ + void *ptr; + alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1); + if(posix_memalign(&ptr, alignment, size) != 0) + return NULL; + return ptr; +} + +#define os_free_aligned(_ptr) free(_ptr) +#else /** * Return memory on given byte alignment @@ -46,8 +71,21 @@ static inline void * os_malloc_aligned(size_t size, size_t alignment) { char *ptr, *buf; + size_t alloc_size; - ptr = (char *) os_malloc(size + alignment + sizeof(void *)); + /* + * Calculate + * + * alloc_size = size + alignment + sizeof(void *) + * + * while checking for overflow. + */ + if (add_overflow_size_t(size, alignment, &alloc_size) || + add_overflow_size_t(alloc_size, sizeof(void *), &alloc_size)) { + return NULL; + } + + ptr = (char *) os_malloc(alloc_size); if (!ptr) return NULL; @@ -59,7 +97,7 @@ os_malloc_aligned(size_t size, size_t alignment) /** - * Free memory returned by align_malloc(). + * Free memory returned by os_malloc_aligned(). */ static inline void os_free_aligned(void *ptr) @@ -70,3 +108,21 @@ os_free_aligned(void *ptr) os_free(realAddr); } } + +#endif + +/** + * Reallocate memeory, with alignment + */ +static inline void * +os_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignment) +{ + const size_t copySize = MIN2(oldsize, newsize); + void *newBuf = os_malloc_aligned(newsize, alignment); + if (newBuf && ptr && copySize > 0) { + memcpy(newBuf, ptr, copySize); + } + + os_free_aligned(ptr); + return newBuf; +} diff --git a/src/gallium/auxiliary/os/os_memory_stdc.h b/src/mesa/util/os_memory_stdc.h similarity index 83% rename from src/gallium/auxiliary/os/os_memory_stdc.h rename to src/mesa/util/os_memory_stdc.h index c9fde06..bda5715 100644 --- a/src/gallium/auxiliary/os/os_memory_stdc.h +++ b/src/mesa/util/os_memory_stdc.h @@ -37,8 +37,6 @@ #include -#include "pipe/p_compiler.h" - #define os_malloc(_size) malloc(_size) #define os_calloc(_count, _size ) calloc(_count, _size ) @@ -47,27 +45,13 @@ #define os_realloc( _old_ptr, _old_size, _new_size) \ realloc(_old_ptr, _new_size + 0*(_old_size)) - -#if defined(HAVE_POSIX_MEMALIGN) - -static inline void * -os_malloc_aligned(size_t size, size_t alignment) -{ - void *ptr; - alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1); - if(posix_memalign(&ptr, alignment, size) != 0) - return NULL; - return ptr; -} - -#define os_free_aligned(_ptr) free(_ptr) - -#elif defined(PIPE_OS_WINDOWS) +#if DETECT_OS_WINDOWS #include #define os_malloc_aligned(_size, _align) _aligned_malloc(_size, _align) #define os_free_aligned(_ptr) _aligned_free(_ptr) +#define os_realloc_aligned(_ptr, _oldsize, _newsize, _alignment) _aligned_realloc(_ptr, _newsize, _alignment) #else