From 69481d198e2838f97e257d456f80f5fa49b037a4 Mon Sep 17 00:00:00 2001 From: olsner Date: Tue, 25 Nov 2003 02:24:24 +0000 Subject: [PATCH] Typo, Thread-secure ONCE (this time it is, too ;-), linux-compat This was SVN commit r89. --- source/lib/misc.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ source/lib/misc.h | 28 +++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/source/lib/misc.cpp b/source/lib/misc.cpp index 0a2304b660..086451e7b8 100755 --- a/source/lib/misc.cpp +++ b/source/lib/misc.cpp @@ -236,3 +236,48 @@ void base32(const int len, const u8* in, u8* out) *out++ = tbl[c]; } } + +#ifndef _WIN32 + +char *_itoa(int val, char *buffer, int radix) +{ + return _ltoa(val, buffer, radix); +} + +static const char digits[]="0123456789abcdef"; + +char *_ultoa(unsigned long int value, char *out, int radix) +{ + char buf[21]; + char *p=buf+21; + + while (value) + { + *(--p)=digits[value % radix]; + value /= radix; + } + + memcpy(out, p, (buf+21)-p); + return out; +} + +char *_ltoa(long val, char *out, int radix) +{ + char buf[21]; + char *p=buf+21; + bool sign=val < 0; + if (sign) val=-val; + + while (val) + { + *(--p)=digits[val % radix]; + val /= radix; + } + + if (sign) *(--p) = '-'; + + memcpy(out, p, (buf+21)-p); + return out; +} + +#endif diff --git a/source/lib/misc.h b/source/lib/misc.h index a40c181f96..ea6ce01b84 100755 --- a/source/lib/misc.h +++ b/source/lib/misc.h @@ -29,8 +29,8 @@ // check if compiler supports C99 // nested #if to avoid ICC warning about undefined macro value #undef HAVE_C99 -#ifdef __STDC_VERSION__ -#if __STDC_VERSION__ >= 199901L) +#if defined(__STDC_VERSION__) +#if __STDC_VERSION__ >= 199901L #define HAVE_C99 #endif #endif @@ -38,7 +38,17 @@ #define UNUSED(param) (void)param; -#define ONCE(code) { static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; if(pthread_mutex_trylock(&(mutex))==0) { code; } } +#define ONCE(code) \ +{ \ + static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; \ + static bool done=false; \ + if(pthread_mutex_lock(&(mutex))==0 && !done) \ + { \ + code; \ + done=true; \ + } \ + pthread_mutex_unlock(&mutex); \ +} template @@ -86,6 +96,14 @@ extern u32 fnv_hash(const char* str, size_t len); #define vsnprintf _vsnprintf #endif +#ifndef _WIN32 + +extern char *_itoa(int value, char* buffer, int radix); +extern char *_ultoa(unsigned long int value, char *buffer, int radix); +extern char *_ltoa(long value, char *buffer, int radix); + +#endif + #define BIT(n) (1ul << (n)) #ifndef min @@ -125,10 +143,11 @@ static inline u32 read_le32(const void* p) { #ifdef BIG_ENDIAN u32 t = 0; + const u8 *_p=(const u8 *)p; for(int i = 0; i < 4; i++) { t <<= 8; - t |= *(const u8*)p++; + t |= *(_p++); } return t; #else @@ -147,7 +166,6 @@ extern int ilog2(const int n); extern uintptr_t round_up(uintptr_t val, uintptr_t multiple); - // provide fminf for non-C99 compilers #ifndef HAVE_C99 extern float fminf(float, float);