2004-03-03 00:56:51 +01:00
|
|
|
// Copyright (c) 2003 Jan Wassenberg
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// Contact info:
|
|
|
|
// Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
|
|
// http://www.stud.uni-karlsruhe.de/~urkt/
|
|
|
|
|
|
|
|
#ifndef LIB_H
|
|
|
|
#define LIB_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
#include "sysdep/sysdep.h"
|
|
|
|
|
|
|
|
|
|
|
|
// yikes! avoid template warning spew on VC6
|
|
|
|
#if _MSC_VER <= 1200
|
|
|
|
#pragma warning(disable:4786)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
#define STMT(STMT_code__) do { STMT_code__; } while(0)
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// must not be used before main entered! (i.e. not from NLS constructors / functions)
|
|
|
|
#define ONCE(ONCE_code__)\
|
2004-05-06 19:14:30 +02:00
|
|
|
STMT(\
|
2004-03-03 00:56:51 +01:00
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\
|
|
|
|
static bool ONCE_done__ = false;\
|
|
|
|
if(pthread_mutex_trylock(&(mutex)) == 0 && !ONCE_done__)\
|
|
|
|
{\
|
|
|
|
ONCE_done__ = true;\
|
|
|
|
ONCE_code__;\
|
|
|
|
}\
|
2004-05-06 19:14:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
#define CHECK_ERR(func)\
|
|
|
|
STMT(\
|
|
|
|
int err = (int)(func);\
|
|
|
|
if(err < 0)\
|
2004-05-13 15:52:48 +02:00
|
|
|
{\
|
|
|
|
assert(0 && "FYI: CHECK_ERR reports that a function failed."\
|
|
|
|
"feel free to ignore or suppress this warning.");\
|
2004-05-06 19:14:30 +02:00
|
|
|
return err;\
|
2004-05-13 15:52:48 +02:00
|
|
|
}\
|
2004-05-06 19:14:30 +02:00
|
|
|
)
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
enum LibError
|
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
ERR_INVALID_HANDLE = -1000,
|
|
|
|
ERR_NO_MEM = -1001,
|
|
|
|
ERR_EOF = -1002, // attempted to read beyond EOF
|
|
|
|
ERR_INVALID_PARAM = -1003,
|
|
|
|
ERR_FILE_NOT_FOUND = -1004,
|
|
|
|
ERR_PATH_NOT_FOUND = -1005,
|
|
|
|
|
|
|
|
ERR_VFS_PATH_LENGTH = -1006,
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
ERR_LAST
|
2004-03-03 00:56:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) (((a) < (b))? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) (((a) > (b))? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define UNUSED(param) (void)param;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// compile-time assert, especially useful for testing sizeof().
|
|
|
|
// no runtime overhead; may be used anywhere, including file scope.
|
|
|
|
//
|
|
|
|
|
|
|
|
// generate a symbol containing the line number of the macro invocation.
|
|
|
|
// used to give a unique name (per file) to types made by cassert.
|
2004-05-06 19:14:30 +02:00
|
|
|
// we can't prepend __FILE__ to make it globally unique - the filename
|
|
|
|
// may be enclosed in quotes. need the 2 macro expansions to make sure
|
|
|
|
// __LINE__ is expanded correctly.
|
2004-03-03 00:56:51 +01:00
|
|
|
#define MAKE_UID2__(l) LINE_ ## l
|
|
|
|
#define MAKE_UID1__(l) MAKE_UID2__(l)
|
|
|
|
#define UID__ MAKE_UID1__(__LINE__)
|
|
|
|
|
|
|
|
// more descriptive error message, but may cause a struct redefinition
|
|
|
|
// warning if used from the same line in different files.
|
|
|
|
#define cassert(expr) struct UID__ { int CASSERT_FAILURE: (expr); };
|
|
|
|
|
|
|
|
// less helpful error message, but redefinition doesn't trigger warnings.
|
2004-05-06 19:14:30 +02:00
|
|
|
#define cassert2(expr) extern char CASSERT_FAILURE[1][(expr)];
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// note: alternative method in C++: specialize a struct only for true;
|
2004-05-06 19:14:30 +02:00
|
|
|
// using it will raise 'incomplete type' errors if instantiated with false.
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#define debug_warn(str) assert(0 && (str))
|
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
// converts 4 character string to u32 for easy comparison
|
|
|
|
// can't pass code as string, and use s[0]..s[3], because
|
|
|
|
// VC6/7 don't realize the macro is constant
|
|
|
|
// (it should be useable as a switch{} expression)
|
|
|
|
#ifdef BIG_ENDIAN
|
|
|
|
#define FOURCC(a,b,c,d) ( ((u32)a << 24) | ((u32)b << 16) | \
|
|
|
|
((u32)c << 8 ) | ((u32)d << 0 ) )
|
|
|
|
#else
|
|
|
|
#define FOURCC(a,b,c,d) ( ((u32)a << 0 ) | ((u32)b << 8 ) | \
|
|
|
|
((u32)c << 16) | ((u32)d << 24) )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
const size_t KB = 1ul << 10;
|
|
|
|
const size_t MB = 1ul << 20;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define DIR_SEP '\\'
|
|
|
|
#else
|
|
|
|
#define DIR_SEP '/'
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define BIT(n) (1ul << (n))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// call from main as early as possible.
|
2004-05-06 19:14:30 +02:00
|
|
|
extern void lib_init();
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
enum CallConvention // number of parameters and convention
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
|
|
|
CC_CDECL_0,
|
|
|
|
CC_CDECL_1,
|
|
|
|
#ifdef _WIN32
|
|
|
|
CC_STDCALL_0,
|
|
|
|
CC_STDCALL_1,
|
|
|
|
#endif
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
CC_UNUSED // get rid of trailing comma when !_WIN32
|
2004-03-03 00:56:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// more powerful atexit: registers an exit handler, with 0 or 1 parameters.
|
|
|
|
// callable before libc initialized, and frees up the real atexit table.
|
|
|
|
// stdcall convention is provided on Windows to call APIs (e.g. WSACleanup).
|
|
|
|
// for these to be called at exit, lib_main must be invoked after _cinit.
|
2004-05-06 19:14:30 +02:00
|
|
|
extern int atexit2(void* func, uintptr_t arg, CallConvention cc = CC_CDECL_1);
|
|
|
|
|
|
|
|
// no parameters, cdecl (CC_CDECL_0)
|
|
|
|
extern int atexit2(void* func);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
#include "posix.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // #ifndef LIB_H
|