1
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
5
COPYING CONDITIONS NOTICE:
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of version 2 of the GNU General Public License as
9
published by the Free Software Foundation, and provided that the
10
following conditions are met:
12
* Redistributions of source code must retain this COPYING
13
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
14
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
15
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
18
* Redistributions in binary form must reproduce this COPYING
19
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
20
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
21
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
22
GRANT (below) in the documentation and/or other materials
23
provided with the distribution.
25
You should have received a copy of the GNU General Public License
26
along with this program; if not, write to the Free Software
27
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32
TokuDB, Tokutek Fractal Tree Indexing Library.
33
Copyright (C) 2007-2013 Tokutek, Inc.
37
This program is distributed in the hope that it will be useful, but
38
WITHOUT ANY WARRANTY; without even the implied warranty of
39
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40
General Public License for more details.
42
UNIVERSITY PATENT NOTICE:
44
The technology is licensed by the Massachusetts Institute of
45
Technology, Rutgers State University of New Jersey, and the Research
46
Foundation of State University of New York at Stony Brook under
47
United States of America Serial No. 11/760379 and to the patents
48
and/or patent applications resulting from it.
50
PATENT MARKING NOTICE:
52
This software is covered by US Patent No. 8,185,551.
53
This software is covered by US Patent No. 8,489,638.
57
"THIS IMPLEMENTATION" means the copyrightable works distributed by
58
Tokutek as part of the Fractal Tree project.
60
"PATENT CLAIMS" means the claims of patents that are owned or
61
licensable by Tokutek, both currently or in the future; and that in
62
the absence of this license would be infringed by THIS
63
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
65
"PATENT CHALLENGE" shall mean a challenge to the validity,
66
patentability, enforceability and/or non-infringement of any of the
67
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
69
Tokutek hereby grants to you, for the term and geographical scope of
70
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
71
irrevocable (except as stated in this section) patent license to
72
make, have made, use, offer to sell, sell, import, transfer, and
73
otherwise run, modify, and propagate the contents of THIS
74
IMPLEMENTATION, where such license applies only to the PATENT
75
CLAIMS. This grant does not include claims that would be infringed
76
only as a consequence of further modifications of THIS
77
IMPLEMENTATION. If you or your agent or licensee institute or order
78
or agree to the institution of patent litigation against any entity
79
(including a cross-claim or counterclaim in a lawsuit) alleging that
80
THIS IMPLEMENTATION constitutes direct or contributory patent
81
infringement, or inducement of patent infringement, then any rights
82
granted to you under this License shall terminate as of the date
83
such litigation is filed. If you or your agent or exclusive
84
licensee institute or order or agree to the institution of a PATENT
85
CHALLENGE, then Tokutek may terminate any rights granted to you
89
#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
90
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
91
#ifndef TOKU_PORTABILITY_H
92
#define TOKU_PORTABILITY_H
94
#include "toku_config.h"
96
// Tokutek portability layer
98
#if defined(__clang__)
99
# define constexpr_static_assert(a, b)
101
# define constexpr_static_assert(a, b) static_assert(a, b)
104
#if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(__ICL))
106
#define TOKU_WINDOWS 1
107
#define DEV_NULL_FILE "NUL"
110
# define TOKU_WINDOWS_32 0
111
# define TOKU_WINDOWS_64 1
113
# define TOKU_WINDOWS_32 1
114
# define TOKU_WINDOWS_64 2
119
#define TOKU_WINDOWS 0
120
#define TOKU_WINDOWS_32 0
121
#define TOKU_WINDOWS_64 0
122
#define DEV_NULL_FILE "/dev/null"
126
// include here, before they get deprecated
127
#include <toku_atomic.h>
132
#define DO_GCC_PRAGMA(x) /* Nothing */
135
#define __attribute__(x) /* Nothing */
139
#include "toku_stdint.h"
141
#ifndef TOKU_OFF_T_DEFINED
142
#define TOKU_OFF_T_DEFINED
143
typedef int64_t toku_off_t;
147
#include <sys/types.h>
150
#include "toku_pthread.h"
152
#define UNUSED_WARNING(a) a=a /* To make up for missing attributes */
154
#define cast_to_typeof(v)
156
#elif defined(__INTEL_COMPILER)
158
#define DO_GCC_PRAGMA(x) /* Nothing */
164
#include <toku_stdint.h>
166
#include <sys/types.h>
167
#include <sys/time.h>
170
#define static_assert(foo, bar)
173
#if defined(__cplusplus)
174
# define cast_to_typeof(v) (decltype(v))
176
# define cast_to_typeof(v) (__typeof__(v))
179
#elif defined(__GNUC__)
182
#define DO_GCC_PRAGMA(x) _Pragma (#x)
184
#include <toku_stdint.h>
186
#include <sys/types.h>
187
#include <sys/time.h>
188
#include <sys/stat.h>
193
#if defined(HAVE_ALLOCA_H)
196
#if defined(__cplusplus)
197
# include <type_traits>
200
#if defined(__cplusplus)
201
# define cast_to_typeof(v) (decltype(v))
203
# define cast_to_typeof(v) (__typeof__(v))
208
#error Not ICC and not GNUC. What compiler?
212
// Define some constants for Yama in case the build-machine's software is too old.
213
#if !defined(HAVE_PR_SET_PTRACER)
215
* Set specific pid that is allowed to ptrace the current task.
216
* A value of 0 mean "no process".
218
// Well defined ("Yama" in ascii)
219
#define PR_SET_PTRACER 0x59616d61
221
#if !defined(HAVE_PR_SET_PTRACER_ANY)
222
#define PR_SET_PTRACER_ANY ((unsigned long)-1)
225
#if defined(__cplusplus)
226
// decltype() here gives a reference-to-pointer instead of just a pointer,
227
// just use __typeof__
228
# define CAST_FROM_VOIDP(name, value) name = static_cast<__typeof__(name)>(value)
230
# define CAST_FROM_VOIDP(name, value) name = cast_to_typeof(name) (value)
233
#ifndef TOKU_OFF_T_DEFINED
234
#define TOKU_OFF_T_DEFINED
235
typedef int64_t toku_off_t;
239
#include "toku_htod.h"
240
#include "toku_assert.h"
241
#include "toku_crash.h"
243
#define UU(x) x __attribute__((__unused__))
245
#if defined(__cplusplus)
249
// Deprecated functions.
250
#if !defined(TOKU_ALLOW_DEPRECATED)
251
# if defined(__ICL) || defined(__ICC) // Intel Compiler
252
# pragma deprecated (creat, fstat, stat, getpid, syscall, sysconf, mkdir, strdup)
253
//# pragma poison off_t
254
//# pragma poison pthread_attr_t pthread_t
255
//# pragma poison pthread_mutexattr_t pthread_mutex_t
256
//# pragma poison pthread_condattr_t pthread_cond_t
257
//# pragma poison pthread_rwlockattr_t pthread_rwlock_t
258
//# pragma poison timespec
259
# ifndef DONT_DEPRECATE_WRITES
260
# pragma poison write pwrite
262
# ifndef DONT_DEPRECATE_MALLOC
263
# pragma deprecated (malloc, free, realloc)
265
# ifndef DONT_DEPRECATE_ERRNO
266
# pragma deprecated (errno)
268
# ifndef TOKU_WINDOWS_ALLOW_DEPRECATED
270
# pragma poison _dup2
273
int creat(const char *pathname, mode_t mode) __attribute__((__deprecated__));
274
int fstat(int fd, struct stat *buf) __attribute__((__deprecated__));
275
int stat(const char *path, struct stat *buf) __attribute__((__deprecated__));
276
int getpid(void) __attribute__((__deprecated__));
277
# if defined(__FreeBSD__) || defined(__APPLE__)
278
int syscall(int __sysno, ...) __attribute__((__deprecated__));
280
long int syscall(long int __sysno, ...) __attribute__((__deprecated__));
282
// Sadly, dlmalloc needs sysconf, and on linux this causes trouble with -combine. So let the warnings show up under windows only.
283
// long int sysconf(int) __attribute__((__deprecated__));
284
int mkdir(const char *pathname, mode_t mode) __attribute__((__deprecated__));
285
int dup2(int fd, int fd2) __attribute__((__deprecated__));
286
int _dup2(int fd, int fd2) __attribute__((__deprecated__));
287
// strdup is a macro in some libraries.
289
# if defined(__FreeBSD__)
290
char* strdup(const char *) __malloc_like __attribute__((__deprecated__));
291
# elif defined(__APPLE__)
292
char* strdup(const char *) __attribute__((__deprecated__));
294
char* strdup(const char *) __THROW __attribute_malloc__ __nonnull ((1)) __attribute__((__deprecated__));
297
char* __strdup(const char *) __attribute__((__deprecated__));
298
# ifndef DONT_DEPRECATE_WRITES
299
ssize_t write(int, const void *, size_t) __attribute__((__deprecated__));
300
ssize_t pwrite(int, const void *, size_t, off_t) __attribute__((__deprecated__));
302
# ifndef DONT_DEPRECATE_MALLOC
303
# if defined(__FreeBSD__)
304
extern void *malloc(size_t) __malloc_like __attribute__((__deprecated__));
305
extern void free(void*) __attribute__((__deprecated__));
306
extern void *realloc(void*, size_t) __malloc_like __attribute__((__deprecated__));
307
# elif defined(__APPLE__)
308
extern void *malloc(size_t) __attribute__((__deprecated__));
309
extern void free(void*) __attribute__((__deprecated__));
310
extern void *realloc(void*, size_t) __attribute__((__deprecated__));
312
extern void *malloc(size_t) __THROW __attribute__((__deprecated__));
313
extern void free(void*) __THROW __attribute__((__deprecated__));
314
extern void *realloc(void*, size_t) __THROW __attribute__((__deprecated__));
317
# ifndef DONT_DEPRECATE_ERRNO
318
//extern int errno __attribute__((__deprecated__));
320
#if !defined(__APPLE__)
321
// Darwin headers use these types, we should not poison them
322
# pragma GCC poison u_int8_t
323
# pragma GCC poison u_int16_t
324
# pragma GCC poison u_int32_t
325
# pragma GCC poison u_int64_t
326
# pragma GCC poison BOOL
327
# pragma GCC poison FALSE
328
# pragma GCC poison TRUE
330
#pragma GCC poison __sync_fetch_and_add
331
#pragma GCC poison __sync_fetch_and_sub
332
#pragma GCC poison __sync_fetch_and_or
333
#pragma GCC poison __sync_fetch_and_and
334
#pragma GCC poison __sync_fetch_and_xor
335
#pragma GCC poison __sync_fetch_and_nand
336
#pragma GCC poison __sync_add_and_fetch
337
#pragma GCC poison __sync_sub_and_fetch
338
#pragma GCC poison __sync_or_and_fetch
339
#pragma GCC poison __sync_and_and_fetch
340
#pragma GCC poison __sync_xor_and_fetch
341
#pragma GCC poison __sync_nand_and_fetch
342
#pragma GCC poison __sync_bool_compare_and_swap
343
#pragma GCC poison __sync_val_compare_and_swap
344
#pragma GCC poison __sync_synchronize
345
#pragma GCC poison __sync_lock_test_and_set
346
#pragma GCC poison __sync_release
350
#if defined(__cplusplus)
354
void *os_malloc(size_t) __attribute__((__visibility__("default")));
355
// Effect: See man malloc(2)
357
void *os_malloc_aligned(size_t /*alignment*/, size_t /*size*/) __attribute__((__visibility__("default")));
358
// Effect: Perform a malloc(size) with the additional property that the returned pointer is a multiple of ALIGNMENT.
359
// Requires: alignment is a power of two.
362
void *os_realloc(void*,size_t) __attribute__((__visibility__("default")));
363
// Effect: See man realloc(2)
365
void *os_realloc_aligned(size_t/*alignment*/, void*,size_t) __attribute__((__visibility__("default")));
366
// Effect: Perform a realloc(p, size) with the additional property that the returned pointer is a multiple of ALIGNMENT.
367
// Requires: alignment is a power of two.
369
void os_free(void*) __attribute__((__visibility__("default")));
370
// Effect: See man free(2)
372
size_t os_malloc_usable_size(const void *p) __attribute__((__visibility__("default")));
373
// Effect: Return an estimate of the usable size inside a pointer. If this function is not defined the memory.cc will
374
// look for the jemalloc, libc, or darwin versions of the function for computing memory footprint.
376
// full_pwrite and full_write performs a pwrite, and checks errors. It doesn't return unless all the data was written. */
377
void toku_os_full_pwrite (int fd, const void *buf, size_t len, toku_off_t off) __attribute__((__visibility__("default")));
378
void toku_os_full_write (int fd, const void *buf, size_t len) __attribute__((__visibility__("default")));
380
// os_write returns 0 on success, otherwise an errno.
381
ssize_t toku_os_pwrite (int fd, const void *buf, size_t len, toku_off_t off) __attribute__((__visibility__("default")));
382
int toku_os_write (int fd, const void *buf, size_t len) __attribute__((__visibility__("default")));
384
// wrappers around file system calls
385
FILE * toku_os_fdopen(int fildes, const char *mode);
386
FILE * toku_os_fopen(const char *filename, const char *mode);
387
int toku_os_open(const char *path, int oflag, int mode);
388
int toku_os_open_direct(const char *path, int oflag, int mode);
389
int toku_os_close(int fd);
390
int toku_os_fclose(FILE * stream);
391
ssize_t toku_os_read(int fd, void *buf, size_t count);
392
ssize_t toku_os_pread(int fd, void *buf, size_t count, off_t offset);
393
void toku_os_recursive_delete(const char *path);
395
// wrapper around fsync
396
void toku_file_fsync_without_accounting(int fd);
397
void toku_file_fsync(int fd);
398
int toku_fsync_directory(const char *fname);
400
// get the number of fsync calls and the fsync times (total)
401
void toku_get_fsync_times(uint64_t *fsync_count, uint64_t *fsync_time, uint64_t *long_fsync_threshold, uint64_t *long_fsync_count, uint64_t *long_fsync_time);
403
void toku_set_func_fsync (int (*fsync_function)(int));
404
void toku_set_func_pwrite (ssize_t (*)(int, const void *, size_t, toku_off_t));
405
void toku_set_func_full_pwrite (ssize_t (*)(int, const void *, size_t, toku_off_t));
406
void toku_set_func_write (ssize_t (*)(int, const void *, size_t));
407
void toku_set_func_full_write (ssize_t (*)(int, const void *, size_t));
408
void toku_set_func_fdopen (FILE * (*)(int, const char *));
409
void toku_set_func_fopen (FILE * (*)(const char *, const char *));
410
void toku_set_func_open (int (*)(const char *, int, int));
411
void toku_set_func_fclose(int (*)(FILE*));
412
void toku_set_func_read(ssize_t (*)(int, void *, size_t));
413
void toku_set_func_pread (ssize_t (*)(int, void *, size_t, off_t));
415
int toku_portability_init(void);
416
void toku_portability_destroy(void);
418
static inline uint64_t roundup_to_multiple(uint64_t alignment, uint64_t v)
419
// Effect: Return X, where X the smallest multiple of ALIGNMENT such that X>=V.
420
// Requires: ALIGNMENT is a power of two
422
assert(0==(alignment&(alignment-1))); // alignment must be a power of two
423
uint64_t result = (v+alignment-1)&~(alignment-1);
424
assert(result>=v); // The result is >=V.
425
assert(result%alignment==0); // The result is a multiple of alignment.
426
assert(result<v+alignment); // The result is the smallest such multiple of alignment.
431
#endif /* TOKU_PORTABILITY_H */