| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
/* Copyright (c) 2007, Google Inc. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* * Redistributions in binary form must reproduce the above |
| 12 |
* copyright notice, this list of conditions and the following disclaimer |
| 13 |
* in the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* * Neither the name of Google Inc. nor the names of its |
| 16 |
* contributors may be used to endorse or promote products derived from |
| 17 |
* this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
* --- |
| 32 |
* Author: Craig Silverstein |
| 33 |
* |
| 34 |
* These are some portability typedefs and defines to make it a bit |
| 35 |
* easier to compile this code under VC++. |
| 36 |
* |
| 37 |
* Several of these are taken from glib: |
| 38 |
* http://developer.gnome.org/doc/API/glib/glib-windows-compatability-functions.html |
| 39 |
*/ |
| 40 |
|
| 41 |
#ifndef GOOGLE_BASE_WINDOWS_H_ |
| 42 |
#define GOOGLE_BASE_WINDOWS_H_ |
| 43 |
|
| 44 |
/* You should never include this file directly, but always include it |
| 45 |
from either config.h (MSVC) or mingw.h (MinGW/msys). */ |
| 46 |
#if !defined(GOOGLE_PERFTOOLS_WINDOWS_CONFIG_H_) && \ |
| 47 |
!defined(GOOGLE_PERFTOOLS_WINDOWS_MINGW_H_) |
| 48 |
# error "port.h should only be included from config.h or mingw.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef _WIN32 |
| 52 |
|
| 53 |
#ifndef WIN32_LEAN_AND_MEAN |
| 54 |
#define WIN32_LEAN_AND_MEAN /* We always want minimal includes */ |
| 55 |
#endif |
| 56 |
#include <windows.h> |
| 57 |
#include <io.h> /* because we so often use open/close/etc */ |
| 58 |
#include <direct.h> /* for _getcwd */ |
| 59 |
#include <process.h> /* for _getpid */ |
| 60 |
#include <limits.h> /* for PATH_MAX */ |
| 61 |
#include <stdarg.h> /* for va_list */ |
| 62 |
#include <stdio.h> /* need this to override stdio's (v)snprintf */ |
| 63 |
#include <sys/types.h> /* for _off_t */ |
| 64 |
#include <assert.h> |
| 65 |
#include <stdlib.h> /* for rand, srand, _strtoxxx */ |
| 66 |
|
| 67 |
#if defined(_MSC_VER) && _MSC_VER >= 1900 |
| 68 |
#define _TIMESPEC_DEFINED |
| 69 |
#include <time.h> |
| 70 |
#endif |
| 71 |
|
| 72 |
/* |
| 73 |
* 4018: signed/unsigned mismatch is common (and ok for signed_i < unsigned_i) |
| 74 |
* 4244: otherwise we get problems when subtracting two size_t's to an int |
| 75 |
* 4288: VC++7 gets confused when a var is defined in a loop and then after it |
| 76 |
* 4267: too many false positives for "conversion gives possible data loss" |
| 77 |
* 4290: it's ok windows ignores the "throw" directive |
| 78 |
* 4996: Yes, we're ok using "unsafe" functions like vsnprintf and getenv() |
| 79 |
* 4146: internal_logging.cc intentionally negates an unsigned value |
| 80 |
*/ |
| 81 |
#ifdef _MSC_VER |
| 82 |
#pragma warning(disable:4018 4244 4288 4267 4290 4996 4146) |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifndef __cplusplus |
| 86 |
/* MSVC does not support C99 */ |
| 87 |
# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L |
| 88 |
# ifdef _MSC_VER |
| 89 |
# define inline __inline |
| 90 |
# else |
| 91 |
# define inline static |
| 92 |
# endif |
| 93 |
# endif |
| 94 |
#endif |
| 95 |
|
| 96 |
#ifdef __cplusplus |
| 97 |
# define EXTERN_C extern "C" |
| 98 |
#else |
| 99 |
# define EXTERN_C extern |
| 100 |
#endif |
| 101 |
|
| 102 |
/* ----------------------------------- BASIC TYPES */ |
| 103 |
|
| 104 |
/* I guess MSVC's <types.h> doesn't include ssize_t by default? */ |
| 105 |
#ifdef _MSC_VER |
| 106 |
typedef intptr_t ssize_t; |
| 107 |
#endif |
| 108 |
|
| 109 |
/* ----------------------------------- THREADS */ |
| 110 |
|
| 111 |
#ifndef HAVE_PTHREAD /* not true for MSVC, but may be true for MSYS */ |
| 112 |
typedef DWORD pthread_t; |
| 113 |
typedef DWORD pthread_key_t; |
| 114 |
|
| 115 |
inline pthread_t pthread_self(void) { |
| 116 |
return GetCurrentThreadId(); |
| 117 |
} |
| 118 |
|
| 119 |
#ifdef __cplusplus |
| 120 |
inline bool pthread_equal(pthread_t left, pthread_t right) { |
| 121 |
return left == right; |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* windows/port.h defines compatibility APIs for several .h files, which |
| 126 |
* we therefore shouldn't be #including directly. This hack keeps us from |
| 127 |
* doing so. TODO(csilvers): do something more principled. |
| 128 |
*/ |
| 129 |
#define HAVE_PERFTOOLS_PTHREAD_KEYS |
| 130 |
|
| 131 |
EXTERN_C pthread_key_t PthreadKeyCreate(void (*destr_fn)(void*)); /* port.cc */ |
| 132 |
|
| 133 |
inline int perftools_pthread_key_create(pthread_key_t *pkey, |
| 134 |
void (*destructor)(void*)) { |
| 135 |
pthread_key_t key = PthreadKeyCreate(destructor); |
| 136 |
if (key != TLS_OUT_OF_INDEXES) { |
| 137 |
*(pkey) = key; |
| 138 |
return 0; |
| 139 |
} else { |
| 140 |
return GetLastError(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
inline void* perftools_pthread_getspecific(DWORD key) { |
| 145 |
DWORD err = GetLastError(); |
| 146 |
void* rv = TlsGetValue(key); |
| 147 |
if (err) SetLastError(err); |
| 148 |
return rv; |
| 149 |
} |
| 150 |
|
| 151 |
inline int perftools_pthread_setspecific(pthread_key_t key, const void *value) { |
| 152 |
if (TlsSetValue(key, (LPVOID)value)) |
| 153 |
return 0; |
| 154 |
else |
| 155 |
return GetLastError(); |
| 156 |
} |
| 157 |
|
| 158 |
#endif /* __cplusplus */ |
| 159 |
|
| 160 |
inline void sched_yield(void) { |
| 161 |
Sleep(0); |
| 162 |
} |
| 163 |
|
| 164 |
#endif /* HAVE_PTHREAD */ |
| 165 |
|
| 166 |
/* |
| 167 |
* __declspec(thread) isn't usable in a dll opened via LoadLibrary(). |
| 168 |
* But it doesn't work to LoadLibrary() us anyway, because of all the |
| 169 |
* things we need to do before main()! So this kind of TLS is safe for us. |
| 170 |
*/ |
| 171 |
#define __thread __declspec(thread) |
| 172 |
|
| 173 |
/* ----------------------------------- MMAP and other memory allocation */ |
| 174 |
|
| 175 |
#ifndef HAVE_MMAP /* not true for MSVC, but may be true for msys */ |
| 176 |
#define MAP_FAILED 0 |
| 177 |
#define MREMAP_FIXED 2 /* the value in linux, though it doesn't really matter */ |
| 178 |
/* These, when combined with the mmap invariants below, yield the proper action */ |
| 179 |
#define PROT_READ PAGE_READWRITE |
| 180 |
#define PROT_WRITE PAGE_READWRITE |
| 181 |
#define MAP_ANONYMOUS MEM_RESERVE |
| 182 |
#define MAP_PRIVATE MEM_COMMIT |
| 183 |
#define MAP_SHARED MEM_RESERVE /* value of this #define is 100% arbitrary */ |
| 184 |
|
| 185 |
#if __STDC__ && !defined(__MINGW32__) |
| 186 |
typedef _off_t off_t; |
| 187 |
#endif |
| 188 |
|
| 189 |
/* VirtualAlloc only replaces for mmap when certain invariants are kept. */ |
| 190 |
inline void *mmap(void *addr, size_t length, int prot, int flags, |
| 191 |
int fd, off_t offset) { |
| 192 |
if (addr == NULL && fd == -1 && offset == 0 && |
| 193 |
prot == (PROT_READ|PROT_WRITE) && flags == (MAP_PRIVATE|MAP_ANONYMOUS)) { |
| 194 |
return VirtualAlloc(0, length, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 195 |
} else { |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
inline int munmap(void *addr, size_t length) { |
| 201 |
return VirtualFree(addr, 0, MEM_RELEASE) ? 0 : -1; |
| 202 |
} |
| 203 |
#endif /* HAVE_MMAP */ |
| 204 |
|
| 205 |
/* We could maybe use VirtualAlloc for sbrk as well, but no need */ |
| 206 |
inline void *sbrk(intptr_t increment) { |
| 207 |
// sbrk returns -1 on failure |
| 208 |
return (void*)-1; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* ----------------------------------- STRING ROUTINES */ |
| 213 |
|
| 214 |
/* |
| 215 |
* We can't just use _vsnprintf and _snprintf as drop-in-replacements, |
| 216 |
* because they don't always NUL-terminate. :-( We also can't use the |
| 217 |
* name vsnprintf, since windows defines that (but not snprintf (!)). |
| 218 |
*/ |
| 219 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 220 |
/* We can use safe CRT functions, which the required functionality */ |
| 221 |
inline int perftools_vsnprintf(char *str, size_t size, const char *format, |
| 222 |
va_list ap) { |
| 223 |
return vsnprintf_s(str, size, _TRUNCATE, format, ap); |
| 224 |
} |
| 225 |
#else |
| 226 |
inline int perftools_vsnprintf(char *str, size_t size, const char *format, |
| 227 |
va_list ap) { |
| 228 |
if (size == 0) /* not even room for a \0? */ |
| 229 |
return -1; /* not what C99 says to do, but what windows does */ |
| 230 |
str[size-1] = '\0'; |
| 231 |
return _vsnprintf(str, size-1, format, ap); |
| 232 |
} |
| 233 |
#endif |
| 234 |
|
| 235 |
/* ----------------------------------- FILE IO */ |
| 236 |
|
| 237 |
#ifndef PATH_MAX |
| 238 |
#define PATH_MAX 1024 |
| 239 |
#endif |
| 240 |
#ifndef __MINGW32__ |
| 241 |
enum { STDIN_FILENO = 0, STDOUT_FILENO = 1, STDERR_FILENO = 2 }; |
| 242 |
#endif |
| 243 |
#ifndef O_RDONLY |
| 244 |
#define O_RDONLY _O_RDONLY |
| 245 |
#endif |
| 246 |
|
| 247 |
#if __STDC__ && !defined(__MINGW32__) |
| 248 |
/* These functions are considered non-standard */ |
| 249 |
inline int access(const char *pathname, int mode) { |
| 250 |
return _access(pathname, mode); |
| 251 |
} |
| 252 |
inline int open(const char *pathname, int flags, int mode = 0) { |
| 253 |
return _open(pathname, flags, mode); |
| 254 |
} |
| 255 |
inline int close(int fd) { |
| 256 |
return _close(fd); |
| 257 |
} |
| 258 |
inline ssize_t read(int fd, void *buf, size_t count) { |
| 259 |
return _read(fd, buf, count); |
| 260 |
} |
| 261 |
inline ssize_t write(int fd, const void *buf, size_t count) { |
| 262 |
return _write(fd, buf, count); |
| 263 |
} |
| 264 |
inline off_t lseek(int fd, off_t offset, int whence) { |
| 265 |
return _lseek(fd, offset, whence); |
| 266 |
} |
| 267 |
inline char *getcwd(char *buf, size_t size) { |
| 268 |
return _getcwd(buf, size); |
| 269 |
} |
| 270 |
inline int mkdir(const char *pathname, int) { |
| 271 |
return _mkdir(pathname); |
| 272 |
} |
| 273 |
|
| 274 |
inline FILE *popen(const char *command, const char *type) { |
| 275 |
return _popen(command, type); |
| 276 |
} |
| 277 |
inline int pclose(FILE *stream) { |
| 278 |
return _pclose(stream); |
| 279 |
} |
| 280 |
#endif |
| 281 |
|
| 282 |
EXTERN_C PERFTOOLS_DLL_DECL void WriteToStderr(const char* buf, int len); |
| 283 |
|
| 284 |
/* ----------------------------------- SYSTEM/PROCESS */ |
| 285 |
|
| 286 |
#ifndef HAVE_PID_T |
| 287 |
typedef int pid_t; |
| 288 |
#endif |
| 289 |
|
| 290 |
#if __STDC__ && !defined(__MINGW32__) |
| 291 |
inline pid_t getpid(void) { return _getpid(); } |
| 292 |
#endif |
| 293 |
inline pid_t getppid(void) { return 0; } |
| 294 |
|
| 295 |
/* Handle case when poll is used to simulate sleep. */ |
| 296 |
inline int poll(struct pollfd* fds, int nfds, int timeout) { |
| 297 |
assert(fds == NULL); |
| 298 |
assert(nfds == 0); |
| 299 |
Sleep(timeout); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
EXTERN_C PERFTOOLS_DLL_DECL int getpagesize(); /* in port.cc */ |
| 304 |
|
| 305 |
/* ----------------------------------- OTHER */ |
| 306 |
|
| 307 |
inline void srandom(unsigned int seed) { srand(seed); } |
| 308 |
inline long random(void) { return rand(); } |
| 309 |
|
| 310 |
#ifndef HAVE_DECL_SLEEP |
| 311 |
#define HAVE_DECL_SLEEP 0 |
| 312 |
#endif |
| 313 |
|
| 314 |
#if !HAVE_DECL_SLEEP |
| 315 |
inline unsigned int sleep(unsigned int seconds) { |
| 316 |
Sleep(seconds * 1000); |
| 317 |
return 0; |
| 318 |
} |
| 319 |
#endif |
| 320 |
|
| 321 |
// mingw64 seems to define timespec (though mingw.org mingw doesn't), |
| 322 |
// protected by the _TIMESPEC_DEFINED macro. |
| 323 |
#ifndef _TIMESPEC_DEFINED |
| 324 |
struct timespec { |
| 325 |
int tv_sec; |
| 326 |
int tv_nsec; |
| 327 |
}; |
| 328 |
#endif |
| 329 |
|
| 330 |
#ifndef HAVE_DECL_NANOSLEEP |
| 331 |
#define HAVE_DECL_NANOSLEEP 0 |
| 332 |
#endif |
| 333 |
|
| 334 |
// latest mingw64 has nanosleep. Earlier mingw and MSVC do not |
| 335 |
#if !HAVE_DECL_NANOSLEEP |
| 336 |
inline int nanosleep(const struct timespec *req, struct timespec *rem) { |
| 337 |
Sleep(req->tv_sec * 1000 + req->tv_nsec / 1000000); |
| 338 |
return 0; |
| 339 |
} |
| 340 |
#endif |
| 341 |
|
| 342 |
#ifndef __MINGW32__ |
| 343 |
#if defined(_MSC_VER) && _MSC_VER < 1800 |
| 344 |
inline long long int strtoll(const char *nptr, char **endptr, int base) { |
| 345 |
return _strtoi64(nptr, endptr, base); |
| 346 |
} |
| 347 |
inline unsigned long long int strtoull(const char *nptr, char **endptr, |
| 348 |
int base) { |
| 349 |
return _strtoui64(nptr, endptr, base); |
| 350 |
} |
| 351 |
inline long long int strtoq(const char *nptr, char **endptr, int base) { |
| 352 |
return _strtoi64(nptr, endptr, base); |
| 353 |
} |
| 354 |
#endif |
| 355 |
inline unsigned long long int strtouq(const char *nptr, char **endptr, |
| 356 |
int base) { |
| 357 |
return _strtoui64(nptr, endptr, base); |
| 358 |
} |
| 359 |
inline long long atoll(const char *nptr) { |
| 360 |
return _atoi64(nptr); |
| 361 |
} |
| 362 |
#endif |
| 363 |
|
| 364 |
#define __THROW throw() |
| 365 |
|
| 366 |
/* ----------------------------------- TCMALLOC-SPECIFIC */ |
| 367 |
|
| 368 |
/* tcmalloc.cc calls this so we can patch VirtualAlloc() et al. */ |
| 369 |
extern void PatchWindowsFunctions(); |
| 370 |
|
| 371 |
#endif /* _WIN32 */ |
| 372 |
|
| 373 |
#undef inline |
| 374 |
#undef EXTERN_C |
| 375 |
|
| 376 |
#endif /* GOOGLE_BASE_WINDOWS_H_ */ |