summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/benchmark.cc7
-rw-r--r--util/mutex.h43
-rw-r--r--util/rune.cc10
-rw-r--r--util/stringprintf.cc9
-rw-r--r--util/test.cc4
-rw-r--r--util/util.h9
-rw-r--r--util/valgrind.cc2
7 files changed, 59 insertions, 25 deletions
diff --git a/util/benchmark.cc b/util/benchmark.cc
index c3aad7e..8f72f3c 100644
--- a/util/benchmark.cc
+++ b/util/benchmark.cc
@@ -25,10 +25,17 @@ void Benchmark::Register() {
}
static int64 nsec() {
+#if defined(__APPLE__) || defined(_WIN32)
struct timeval tv;
if(gettimeofday(&tv, 0) < 0)
return -1;
return (int64)tv.tv_sec*1000*1000*1000 + tv.tv_usec*1000;
+#else
+ struct timespec tp;
+ if(clock_gettime(CLOCK_REALTIME, &tp) < 0)
+ return -1;
+ return (int64)tp.tv_sec*1000*1000*1000 + tp.tv_nsec;
+#endif
}
static int64 bytes;
diff --git a/util/mutex.h b/util/mutex.h
index 4a8de4c..9cb6de3 100644
--- a/util/mutex.h
+++ b/util/mutex.h
@@ -12,17 +12,38 @@
#include <stdlib.h>
+#if !defined(_WIN32)
+#include <unistd.h> // For POSIX options
+#endif
+
namespace re2 {
-#define HAVE_PTHREAD 1
-#define HAVE_RWLOCK 1
+#if !defined(_WIN32)
+ // Possible values of POSIX options:
+ // -1 means not supported,
+ // 0 means maybe supported (query at runtime),
+ // >0 means supported.
+# if defined(_POSIX_THREADS) && _POSIX_THREADS > 0
+# define HAVE_PTHREAD 1
+# else
+# define HAVE_PTHREAD 0
+# endif
+# if defined(_POSIX_READER_WRITER_LOCKS) && _POSIX_READER_WRITER_LOCKS > 0
+# define HAVE_RWLOCK 1
+# else
+# define HAVE_RWLOCK 0
+# endif
+#else
+# define HAVE_PTHREAD 0
+# define HAVE_RWLOCK 0
+#endif
#if defined(NO_THREADS)
typedef int MutexType; // to keep a lock-count
-#elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
+#elif HAVE_PTHREAD && HAVE_RWLOCK
// Needed for pthread_rwlock_*. If it causes problems, you could take it
- // out, but then you'd have to unset HAVE_RWLOCK (at least on linux -- it
- // *does* cause problems for FreeBSD, or MacOSX, but isn't needed
+ // out, but then you'd have to set HAVE_RWLOCK to 0 (at least on linux --
+ // it *does* cause problems for FreeBSD, or MacOSX, but isn't needed
// for locking there.)
# ifdef __linux__
# undef _XOPEN_SOURCE
@@ -30,10 +51,10 @@ namespace re2 {
# endif
# include <pthread.h>
typedef pthread_rwlock_t MutexType;
-#elif defined(HAVE_PTHREAD)
+#elif HAVE_PTHREAD
# include <pthread.h>
typedef pthread_mutex_t MutexType;
-#elif defined(WIN32)
+#elif defined(_WIN32)
# define WIN32_LEAN_AND_MEAN // We only need minimal includes
# ifdef GMUTEX_TRYLOCK
// We need Windows NT or later for TryEnterCriticalSection(). If you
@@ -102,7 +123,7 @@ bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; }
void Mutex::ReaderLock() { assert(++mutex_ > 0); }
void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
-#elif defined(HAVE_PTHREAD) && defined(HAVE_RWLOCK)
+#elif HAVE_PTHREAD && HAVE_RWLOCK
#define SAFE_PTHREAD(fncall) do { if ((fncall) != 0) abort(); } while (0)
@@ -116,7 +137,7 @@ void Mutex::ReaderUnlock() { SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_)); }
#undef SAFE_PTHREAD
-#elif defined(HAVE_PTHREAD)
+#elif HAVE_PTHREAD
#define SAFE_PTHREAD(fncall) do { if ((fncall) != 0) abort(); } while (0)
@@ -129,7 +150,7 @@ void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
void Mutex::ReaderUnlock() { Unlock(); }
#undef SAFE_PTHREAD
-#elif defined(WIN32)
+#elif defined(_WIN32)
Mutex::Mutex() { InitializeCriticalSection(&mutex_); }
Mutex::~Mutex() { DeleteCriticalSection(&mutex_); }
@@ -186,7 +207,7 @@ class WriterMutexLock {
#define WriterMutexLock(x) COMPILE_ASSERT(0, wmutex_lock_decl_missing_var_name)
// Provide safe way to declare and use global, linker-initialized mutex. Sigh.
-#ifdef HAVE_PTHREAD
+#if HAVE_PTHREAD
#define GLOBAL_MUTEX(name) \
static pthread_mutex_t (name) = PTHREAD_MUTEX_INITIALIZER
diff --git a/util/rune.cc b/util/rune.cc
index 26442b0..e6231ce 100644
--- a/util/rune.cc
+++ b/util/rune.cc
@@ -133,7 +133,7 @@ runetochar(char *str, const Rune *rune)
*/
c = *rune;
if(c <= Rune1) {
- str[0] = c;
+ str[0] = static_cast<char>(c);
return 1;
}
@@ -142,7 +142,7 @@ runetochar(char *str, const Rune *rune)
* 0080-07FF => T2 Tx
*/
if(c <= Rune2) {
- str[0] = T2 | (c >> 1*Bitx);
+ str[0] = T2 | static_cast<char>(c >> 1*Bitx);
str[1] = Tx | (c & Maskx);
return 2;
}
@@ -161,9 +161,9 @@ runetochar(char *str, const Rune *rune)
* 0800-FFFF => T3 Tx Tx
*/
if (c <= Rune3) {
- str[0] = T3 | (c >> 2*Bitx);
+ str[0] = T3 | static_cast<char>(c >> 2*Bitx);
str[1] = Tx | ((c >> 1*Bitx) & Maskx);
- str[2] = Tx | (c & Maskx);
+ str[2] = Tx | (c & Maskx);
return 3;
}
@@ -171,7 +171,7 @@ runetochar(char *str, const Rune *rune)
* four character sequence (21-bit value)
* 10000-1FFFFF => T4 Tx Tx Tx
*/
- str[0] = T4 | (c >> 3*Bitx);
+ str[0] = T4 | static_cast<char>(c >> 3*Bitx);
str[1] = Tx | ((c >> 2*Bitx) & Maskx);
str[2] = Tx | ((c >> 1*Bitx) & Maskx);
str[3] = Tx | (c & Maskx);
diff --git a/util/stringprintf.cc b/util/stringprintf.cc
index 79ba7fc..e71d993 100644
--- a/util/stringprintf.cc
+++ b/util/stringprintf.cc
@@ -4,7 +4,7 @@
#include "util/util.h"
-namespace re2 {
+namespace re2 {
static void StringAppendV(string* dst, const char* format, va_list ap) {
// First try with a small fixed size buffer
@@ -38,7 +38,14 @@ static void StringAppendV(string* dst, const char* format, va_list ap) {
// Restore the va_list before we use it again
va_copy(backup_ap, ap);
+#if !defined(_WIN32)
result = vsnprintf(buf, length, format, backup_ap);
+#else
+ // On Windows, the function takes five arguments, not four. With an array,
+ // the buffer size will be inferred, but not with a pointer. C'est la vie.
+ // (See https://github.com/google/re2/issues/40 for more details.)
+ result = vsnprintf(buf, length, _TRUNCATE, format, backup_ap);
+#endif
va_end(backup_ap);
if ((result >= 0) && (result < length)) {
diff --git a/util/test.cc b/util/test.cc
index 220bfb0..85055b2 100644
--- a/util/test.cc
+++ b/util/test.cc
@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.
#include <stdio.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/resource.h>
#endif
#include "util/test.h"
@@ -25,7 +25,7 @@ void RegisterTest(void (*fn)(void), const char *name) {
namespace re2 {
int64 VirtualProcessSize() {
-#ifdef WIN32
+#ifdef _WIN32
return 0;
#else
struct rusage ru;
diff --git a/util/util.h b/util/util.h
index a7ccafe..f49ebd1 100644
--- a/util/util.h
+++ b/util/util.h
@@ -12,10 +12,10 @@
#include <stddef.h> // For size_t
#include <assert.h>
#include <stdarg.h>
-#include <time.h>
+#include <time.h> // For clock_gettime, CLOCK_REALTIME
#include <ctype.h> // For isdigit, isalpha
-#if !defined(WIN32)
+#if !defined(_WIN32)
#include <sys/time.h> // For gettimeofday
#endif
@@ -53,7 +53,7 @@ using std::tr1::unordered_set;
#else
#include <unordered_set>
-#if defined(WIN32)
+#if defined(_WIN32)
using std::tr1::unordered_set;
#else
using std::unordered_set;
@@ -61,7 +61,7 @@ using std::unordered_set;
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define snprintf _snprintf_s
#define sprintf sprintf_s
@@ -72,7 +72,6 @@ using std::unordered_set;
#define vsnprintf vsnprintf_s
#pragma warning(disable: 4018) // signed/unsigned mismatch
-#pragma warning(disable: 4244) // possible data loss in int conversion
#pragma warning(disable: 4800) // conversion from int to bool
#endif
diff --git a/util/valgrind.cc b/util/valgrind.cc
index ee257d0..82f9a4c 100644
--- a/util/valgrind.cc
+++ b/util/valgrind.cc
@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.
#include "util/util.h"
-#ifndef WIN32
+#ifndef _WIN32
#include "util/valgrind.h"
#endif