util: Reimplement all utility functions in terms of the new OS abstraction.

This commit is contained in:
José Fonseca 2010-02-03 12:13:06 +00:00
parent 0b0e705712
commit 6b424a0550
4 changed files with 26 additions and 409 deletions

View file

@ -29,37 +29,7 @@
#include "pipe/p_config.h"
#include <stdarg.h>
#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
#include <windows.h>
#include <winddi.h>
#elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <types.h>
#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif
#include <windows.h>
#include <stdio.h>
#else
#include <stdio.h>
#include <stdlib.h>
#endif
#include "pipe/p_compiler.h"
#include "pipe/p_compiler.h"
#include "util/u_debug.h"
#include "pipe/p_format.h"
#include "pipe/p_state.h"
@ -73,81 +43,16 @@
#include "util/u_prim.h"
#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
static INLINE void
_EngDebugPrint(const char *format, ...)
{
va_list ap;
va_start(ap, format);
EngDebugPrint("", (PCHAR)format, ap);
va_end(ap);
}
#endif
void _debug_vprintf(const char *format, va_list ap)
{
#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
/* EngDebugPrint does not handle float point arguments, so we need to use
* our own vsnprintf implementation. It is also very slow, so buffer until
* we find a newline. */
static char buf[512] = {'\0'};
size_t len = strlen(buf);
int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
_EngDebugPrint("%s", buf);
buf[0] = '\0';
}
#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
/* OutputDebugStringA can be very slow, so buffer until we find a newline. */
/* We buffer until we find a newline. */
static char buf[4096] = {'\0'};
size_t len = strlen(buf);
int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
OutputDebugStringA(buf);
os_log_message(buf);
buf[0] = '\0';
}
if(GetConsoleWindow() && !IsDebuggerPresent()) {
fflush(stdout);
vfprintf(stderr, format, ap);
fflush(stderr);
}
#elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
wchar_t *wide_format;
long wide_str_len;
char buf[512];
int ret;
#if (_WIN32_WCE < 600)
ret = vsprintf(buf, format, ap);
if(ret < 0){
sprintf(buf, "Cant handle debug print!");
ret = 25;
}
#else
ret = vsprintf_s(buf, 512, format, ap);
if(ret < 0){
sprintf_s(buf, 512, "Cant handle debug print!");
ret = 25;
}
#endif
buf[ret] = '\0';
/* Format is ascii - needs to be converted to wchar_t for printing */
wide_str_len = MultiByteToWideChar(CP_ACP, 0, (const char *) buf, -1, NULL, 0);
wide_format = (wchar_t *) malloc((wide_str_len+1) * sizeof(wchar_t));
if (wide_format) {
MultiByteToWideChar(CP_ACP, 0, (const char *) buf, -1,
wide_format, wide_str_len);
NKDbgPrintfW(wide_format, wide_format);
free(wide_format);
}
#elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
/* TODO */
#else /* !PIPE_SUBSYSTEM_WINDOWS */
fflush(stdout);
vfprintf(stderr, format, ap);
#endif
}
@ -169,108 +74,12 @@ void debug_print_blob( const char *name,
#endif
#ifndef debug_break
void debug_break(void)
{
#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
DebugBreak();
#elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
EngDebugBreak();
#else
abort();
#endif
}
#endif
#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
static const char *
find(const char *start, const char *end, char c)
{
const char *p;
for(p = start; !end || p != end; ++p) {
if(*p == c)
return p;
if(*p < 32)
break;
}
return NULL;
}
static int
compare(const char *start, const char *end, const char *s)
{
const char *p, *q;
for(p = start, q = s; p != end && *q != '\0'; ++p, ++q) {
if(*p != *q)
return 0;
}
return p == end && *q == '\0';
}
static void
copy(char *dst, const char *start, const char *end, size_t n)
{
const char *p;
char *q;
for(p = start, q = dst, n = n - 1; p != end && n; ++p, ++q, --n)
*q = *p;
*q = '\0';
}
#endif
static INLINE const char *
_debug_get_option(const char *name)
{
#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
/* EngMapFile creates the file if it does not exists, so it must either be
* disabled on release versions (or put in a less conspicuous place). */
#ifdef DEBUG
const char *result = NULL;
ULONG_PTR iFile = 0;
const void *pMap = NULL;
const char *sol, *eol, *sep;
static char output[1024];
pMap = EngMapFile(L"\\??\\c:\\gallium.cfg", 0, &iFile);
if(pMap) {
sol = (const char *)pMap;
while(1) {
/* TODO: handle LF line endings */
eol = find(sol, NULL, '\r');
if(!eol || eol == sol)
break;
sep = find(sol, eol, '=');
if(!sep)
break;
if(compare(sol, sep, name)) {
copy(output, sep + 1, eol, sizeof(output));
result = output;
break;
}
sol = eol + 2;
}
EngUnmapFile(iFile);
}
return result;
#else
return NULL;
#endif
#elif defined(PIPE_SUBSYSTEM_WINDOWS_CE) || defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
/* TODO: implement */
return NULL;
#else
return getenv(name);
#endif
}
const char *
debug_get_option(const char *name, const char *dfault)
{
const char *result;
result = _debug_get_option(name);
result = os_get_option(name);
if(!result)
result = dfault;
@ -282,7 +91,7 @@ debug_get_option(const char *name, const char *dfault)
boolean
debug_get_bool_option(const char *name, boolean dfault)
{
const char *str = _debug_get_option(name);
const char *str = os_get_option(name);
boolean result;
if(str == NULL)
@ -312,7 +121,7 @@ debug_get_num_option(const char *name, long dfault)
long result;
const char *str;
str = _debug_get_option(name);
str = os_get_option(name);
if(!str)
result = dfault;
else {
@ -348,7 +157,7 @@ debug_get_flags_option(const char *name,
unsigned long result;
const char *str;
str = _debug_get_option(name);
str = os_get_option(name);
if(!str)
result = dfault;
else if (!util_strcmp(str, "help")) {
@ -389,7 +198,7 @@ void _debug_assert_fail(const char *expr,
#else
if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
#endif
debug_break();
os_abort();
else
_debug_printf("continuing...\n");
}

View file

@ -39,7 +39,7 @@
#define U_DEBUG_H_
#include "pipe/p_compiler.h"
#include "os/os_misc.h"
#ifdef __cplusplus
@ -47,17 +47,6 @@ extern "C" {
#endif
#if defined(DBG) || defined(DEBUG)
#ifndef DEBUG
#define DEBUG 1
#endif
#else
#ifndef NDEBUG
#define NDEBUG 1
#endif
#endif
#if defined(__GNUC__)
#define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
#else
@ -148,13 +137,7 @@ void debug_print_format(const char *msg, unsigned fmt );
* Hard-coded breakpoint.
*/
#ifdef DEBUG
#if (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)) && defined(PIPE_CC_GCC)
#define debug_break() __asm("int3")
#elif defined(PIPE_CC_MSVC)
#define debug_break() __debugbreak()
#else
void debug_break(void);
#endif
#define debug_break() os_break()
#else /* !DEBUG */
#define debug_break() ((void)0)
#endif /* !DEBUG */
@ -321,22 +304,6 @@ debug_get_flags_option(const char *name,
unsigned long dfault);
void *
debug_malloc(const char *file, unsigned line, const char *function,
size_t size);
void
debug_free(const char *file, unsigned line, const char *function,
void *ptr);
void *
debug_calloc(const char *file, unsigned line, const char *function,
size_t count, size_t size );
void *
debug_realloc(const char *file, unsigned line, const char *function,
void *old_ptr, size_t old_size, size_t new_size );
unsigned long
debug_memory_begin(void);

View file

@ -34,15 +34,10 @@
#include "pipe/p_config.h"
#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
#include <windows.h>
#include <winddi.h>
#elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
#include <wdm.h>
#else
#include <stdio.h>
#include <stdlib.h>
#endif
#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"
@ -53,18 +48,6 @@
#define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && !defined(WINCE)
#define real_malloc(_size) EngAllocMem(0, _size, 'D3AG')
#define real_free(_ptr) EngFreeMem(_ptr)
#elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
#define real_malloc(_size) ExAllocatePool(0, _size)
#define real_free(_ptr) ExFreePool(_ptr)
#else
#define real_malloc(_size) malloc(_size)
#define real_free(_ptr) free(_ptr)
#endif
struct debug_memory_header
{
struct list_head head;
@ -127,7 +110,7 @@ debug_malloc(const char *file, unsigned line, const char *function,
struct debug_memory_header *hdr;
struct debug_memory_footer *ftr;
hdr = real_malloc(sizeof(*hdr) + size + sizeof(*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,
@ -185,7 +168,7 @@ debug_free(const char *file, unsigned line, const char *function,
hdr->magic = 0;
ftr->magic = 0;
real_free(hdr);
os_free(hdr);
}
void *
@ -232,7 +215,7 @@ debug_realloc(const char *file, unsigned line, const char *function,
}
/* alloc new */
new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
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,
@ -258,7 +241,7 @@ debug_realloc(const char *file, unsigned line, const char *function,
/* free old */
old_hdr->magic = 0;
old_ftr->magic = 0;
real_free(old_hdr);
os_free(old_hdr);
return new_ptr;
}

View file

@ -26,7 +26,7 @@
**************************************************************************/
/**
/*
* Memory functions
*/
@ -37,6 +37,7 @@
#include "util/u_pointer.h"
#include "util/u_debug.h"
#include "os/os_memory.h"
#ifdef __cplusplus
@ -44,114 +45,13 @@ extern "C" {
#endif
/* Define ENOMEM for WINCE */
#if (_WIN32_WCE < 600)
#ifndef ENOMEM
#define ENOMEM 12
#endif
#endif
#define MALLOC(_size) os_malloc(_size)
#define CALLOC(_count, _size) os_calloc(_count, _size)
#if defined(PIPE_OS_WINDOWS) && defined(DEBUG)
/* memory debugging */
#include "util/u_debug.h"
#define MALLOC( _size ) \
debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
#define CALLOC( _count, _size ) \
debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
#define FREE( _ptr ) \
debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr )
#define REALLOC( _ptr, _old_size, _size ) \
debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _size )
#elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
void * __stdcall
EngAllocMem(
unsigned long Flags,
unsigned long MemSize,
unsigned long Tag );
void __stdcall
EngFreeMem(
void *Mem );
#define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
#define _FREE( _ptr ) EngFreeMem( _ptr )
#elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
void *
ExAllocatePool(
unsigned long PoolType,
size_t NumberOfBytes);
void
ExFreePool(void *P);
#define MALLOC(_size) ExAllocatePool(0, _size)
#define _FREE(_ptr) ExFreePool(_ptr)
#else
#define MALLOC( SIZE ) malloc( SIZE )
#define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
#define FREE( PTR ) free( PTR )
static INLINE void *
_REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
{
(void) old_size;
return realloc(old_ptr, new_size);
}
#define REALLOC( a, b, c ) _REALLOC( a, b, c )
#endif
#ifndef CALLOC
static INLINE void *
CALLOC( unsigned count, unsigned size )
{
void *ptr = MALLOC( count * size );
if( ptr ) {
memset( ptr, 0, count * size );
}
return ptr;
}
#endif /* !CALLOC */
#ifndef FREE
static INLINE void
FREE( void *ptr )
{
if( ptr ) {
_FREE( ptr );
}
}
#endif /* !FREE */
#ifndef REALLOC
static INLINE void *
REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
{
void *new_ptr = NULL;
if (new_size != 0) {
unsigned copy_size = old_size < new_size ? old_size : new_size;
new_ptr = MALLOC( new_size );
if (new_ptr && old_ptr && copy_size) {
memcpy( new_ptr, old_ptr, copy_size );
}
}
FREE( old_ptr );
return new_ptr;
}
#endif /* !REALLOC */
#define FREE(_ptr ) os_free(_ptr)
#define REALLOC(_ptr, _old_size, _size) os_realloc(_ptr, _old_size, _size)
#define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
@ -160,50 +60,8 @@ REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
#define CALLOC_VARIANT_LENGTH_STRUCT(T,more_size) ((struct T *) CALLOC(1, sizeof(struct T) + more_size))
/**
* Return memory on given byte alignment
*/
static INLINE void *
align_malloc(size_t bytes, uint alignment)
{
#if defined(HAVE_POSIX_MEMALIGN)
void *mem;
alignment = (alignment + (uint)sizeof(void*) - 1) & ~((uint)sizeof(void*) - 1);
if(posix_memalign(& mem, alignment, bytes) != 0)
return NULL;
return mem;
#else
char *ptr, *buf;
assert( alignment > 0 );
ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
if (!ptr)
return NULL;
buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
*(char **)(buf - sizeof(void *)) = ptr;
return buf;
#endif /* defined(HAVE_POSIX_MEMALIGN) */
}
/**
* Free memory returned by align_malloc().
*/
static INLINE void
align_free(void *ptr)
{
#if defined(HAVE_POSIX_MEMALIGN)
FREE(ptr);
#else
if (ptr) {
void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
void *realAddr = *cubbyHole;
FREE(realAddr);
}
#endif /* defined(HAVE_POSIX_MEMALIGN) */
}
#define align_malloc(_size, _alignment) os_malloc_aligned(_size, _alignment)
#define align_free(_ptr) os_free_aligned(_ptr)
/**