util: Rip-off trace's os-independent stream code.

This commit is contained in:
José Fonseca 2008-09-08 11:09:23 +09:00
parent 52c2dd1f73
commit 86a15954bf
5 changed files with 347 additions and 0 deletions

View File

@ -15,6 +15,8 @@ C_SOURCES = \
u_rect.c \
u_simple_shaders.c \
u_snprintf.c \
u_stream_stdc.c \
u_stream_wd.c \
u_tile.c \
u_time.c \
u_timed_winsys.c

View File

@ -16,6 +16,8 @@ util = env.ConvenienceLibrary(
'u_rect.c',
'u_simple_shaders.c',
'u_snprintf.c',
'u_stream_stdc.c',
'u_stream_wd.c',
'u_tile.c',
'u_time.c',
])

View File

@ -0,0 +1,56 @@
/**************************************************************************
*
* 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
* Cross-platform sequential access stream abstraction.
*/
#ifndef U_STREAM_H
#define U_STREAM_H
#include "pipe/p_compiler.h"
struct util_stream;
struct util_stream *
util_stream_create(const char *filename);
boolean
util_stream_write(struct util_stream *stream, const void *data, size_t size);
void
util_stream_flush(struct util_stream *stream);
void
util_stream_close(struct util_stream *stream);
#endif /* U_STREAM_H */

View File

@ -0,0 +1,104 @@
/**************************************************************************
*
* 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
* Stream implementation based on the Standard C Library.
*/
#include "pipe/p_config.h"
#if defined(PIPE_OS_LINUX)
#include <stdio.h>
#include "util/u_memory.h"
#include "u_stream.h"
struct util_stream
{
FILE *file;
};
struct util_stream *
util_stream_create(const char *filename)
{
struct util_stream *stream;
stream = CALLOC_STRUCT(util_stream);
if(!stream)
goto error1;
stream->file = fopen(filename, "w");
if(!stream->file)
goto error2;
return stream;
error2:
FREE(stream);
error1:
return NULL;
}
boolean
util_stream_write(struct util_stream *stream, const void *data, size_t size)
{
if(!stream)
return FALSE;
return fwrite(data, size, 1, stream->file) == size ? TRUE : FALSE;
}
void
util_stream_flush(struct util_stream *stream)
{
if(!stream)
return;
fflush(stream->file);
}
void
util_stream_close(struct util_stream *stream)
{
if(!stream)
return;
fclose(stream->file);
FREE(stream);
}
#endif

View File

@ -0,0 +1,183 @@
/**************************************************************************
*
* 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
* Stream implementation for the Windows Display driver.
*/
#include "pipe/p_config.h"
#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
#include <windows.h>
#include <winddi.h>
#include "util/u_memory.h"
#include "util/u_string.h"
#include "u_stream.h"
#define MAP_FILE_SIZE (4*1024*1024)
struct util_stream
{
char filename[MAX_PATH + 1];
WCHAR wFileName[MAX_PATH + 1];
ULONG_PTR iFile;
char *pMap;
size_t written;
unsigned suffix;
};
static INLINE boolean
util_stream_map(struct util_stream *stream)
{
ULONG BytesInUnicodeString;
static char filename[MAX_PATH + 1];
unsigned filename_len;
filename_len = util_snprintf(filename,
sizeof(filename),
"\\??\\%s.%04x",
stream->filename,
stream->suffix++);
EngMultiByteToUnicodeN(
stream->wFileName,
sizeof(stream->wFileName),
&BytesInUnicodeString,
filename,
filename_len);
stream->pMap = EngMapFile(stream->wFileName, MAP_FILE_SIZE, &stream->iFile);
if(!stream->pMap)
return FALSE;
memset(stream->pMap, 0, MAP_FILE_SIZE);
stream->written = 0;
return TRUE;
}
static INLINE void
util_stream_unmap(struct util_stream *stream)
{
EngUnmapFile(stream->iFile);
if(stream->written < MAP_FILE_SIZE) {
/* Truncate file size */
stream->pMap = EngMapFile(stream->wFileName, stream->written, &stream->iFile);
if(stream->pMap)
EngUnmapFile(stream->iFile);
}
stream->pMap = NULL;
}
struct util_stream *
util_stream_create(const char *filename)
{
struct util_stream *stream;
stream = CALLOC_STRUCT(util_stream);
if(!stream)
goto error1;
strncpy(stream->filename, filename, sizeof(stream->filename));
if(!util_stream_map(stream))
goto error2;
return stream;
error2:
FREE(stream);
error1:
return NULL;
}
static INLINE void
util_stream_copy(struct util_stream *stream, const char *data, size_t size)
{
assert(stream->written + size <= MAP_FILE_SIZE);
memcpy(stream->pMap + stream->written, data, size);
stream->written += size;
}
boolean
util_stream_write(struct util_stream *stream, const void *data, size_t size)
{
if(!stream)
return FALSE;
if(!stream->pMap)
return FALSE;
while(stream->written + size > MAP_FILE_SIZE) {
size_t step = MAP_FILE_SIZE - stream->written;
util_stream_copy(stream, data, step);
data = (const char *)data + step;
size -= step;
util_stream_unmap(stream);
if(!util_stream_map(stream))
return FALSE;
}
util_stream_copy(stream, data, size);
return TRUE;
}
void
util_stream_flush(struct util_stream *stream)
{
(void)stream;
}
void
util_stream_close(struct util_stream *stream)
{
if(!stream)
return;
util_stream_unmap(stream);
FREE(stream);
}
#endif