1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2008, Google Inc.
5 
6 Portions of this file contain modifications contributed and copyrighted by
7 Google, Inc. Those modifications are gratefully acknowledged and are described
8 briefly in the InnoDB documentation. The contributions by Google are
9 incorporated with their permission, and subject to the conditions contained in
10 the file COPYING.Google.
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License, version 2.0,
14 as published by the Free Software Foundation.
15 
16 This program is also distributed with certain software (including
17 but not limited to OpenSSL) that is licensed under separate terms,
18 as designated in a particular file or component or in included license
19 documentation.  The authors of MySQL hereby grant you an additional
20 permission to link the program and your derivative works with the
21 separately licensed software that they have included with MySQL.
22 
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License, version 2.0, for more details.
27 
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
31 
32 *****************************************************************************/
33 
34 /***********************************************************************//**
35 @file include/univ.i
36 Version control for database, common definitions, and include files
37 
38 Created 1/20/1994 Heikki Tuuri
39 ****************************************************************************/
40 
41 #ifndef univ_i
42 #define univ_i
43 
44 #ifdef UNIV_HOTBACKUP
45 #include "hb_univ.i"
46 #endif /* UNIV_HOTBACKUP */
47 
48 /* aux macros to convert M into "123" (string) if M is defined like
49 #define M 123 */
50 #define _IB_TO_STR(s)	#s
51 #define IB_TO_STR(s)	_IB_TO_STR(s)
52 
53 #define INNODB_VERSION_MAJOR	MYSQL_VERSION_MAJOR
54 #define INNODB_VERSION_MINOR	MYSQL_VERSION_MINOR
55 #define INNODB_VERSION_BUGFIX	MYSQL_VERSION_PATCH
56 
57 /* The following is the InnoDB version as shown in
58 SELECT plugin_version FROM information_schema.plugins;
59 calculated in make_version_string() in sql/sql_show.cc like this:
60 "version >> 8" . "version & 0xff"
61 because the version is shown with only one dot, we skip the last
62 component, i.e. we show M.N.P as M.N */
63 #define INNODB_VERSION_SHORT	\
64 	(INNODB_VERSION_MAJOR << 8 | INNODB_VERSION_MINOR)
65 
66 #define INNODB_VERSION_STR			\
67 	IB_TO_STR(INNODB_VERSION_MAJOR) "."	\
68 	IB_TO_STR(INNODB_VERSION_MINOR) "."	\
69 	IB_TO_STR(INNODB_VERSION_BUGFIX)
70 
71 #define REFMAN "http://dev.mysql.com/doc/refman/"	\
72 	IB_TO_STR(MYSQL_VERSION_MAJOR) "."		\
73 	IB_TO_STR(MYSQL_VERSION_MINOR) "/en/"
74 
75 #ifdef MYSQL_DYNAMIC_PLUGIN
76 /* In the dynamic plugin, redefine some externally visible symbols
77 in order not to conflict with the symbols of a builtin InnoDB. */
78 
79 /* Rename all C++ classes that contain virtual functions, because we
80 have not figured out how to apply the visibility=hidden attribute to
81 the virtual method table (vtable) in GCC 3. */
82 # define ha_innobase ha_innodb
83 #endif /* MYSQL_DYNAMIC_PLUGIN */
84 
85 #if (defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)) && !defined(MYSQL_SERVER) && !defined(__WIN__)
86 # undef __WIN__
87 # define __WIN__
88 
89 # include <windows.h>
90 
91 # ifdef _NT_
92 #  define __NT__
93 # endif
94 
95 #else
96 /* The defines used with MySQL */
97 
98 /* Include two header files from MySQL to make the Unix flavor used
99 in compiling more Posix-compatible. These headers also define __WIN__
100 if we are compiling on Windows. */
101 
102 #ifndef UNIV_HOTBACKUP
103 # include <my_global.h>
104 # include <my_pthread.h>
105 #endif /* UNIV_HOTBACKUP */
106 
107 /* Include <sys/stat.h> to get S_I... macros defined for os0file.cc */
108 # include <sys/stat.h>
109 # if !defined(__WIN__)
110 #  include <sys/mman.h> /* mmap() for os0proc.cc */
111 # endif
112 
113 /* Include the header file generated by GNU autoconf */
114 # ifndef __WIN__
115 #  ifndef UNIV_HOTBACKUP
116 #   include "config.h"
117 #  endif /* UNIV_HOTBACKUP */
118 # endif
119 
120 # ifdef HAVE_SCHED_H
121 #  include <sched.h>
122 # endif
123 
124 /* We only try to do explicit inlining of functions with gcc and
125 Sun Studio */
126 
127 # ifdef HAVE_PREAD
128 #  define HAVE_PWRITE
129 # endif
130 
131 #endif /* #if (defined(WIN32) || ... */
132 
133 #ifndef __WIN__
134 #define __STDC_FORMAT_MACROS    /* Enable C99 printf format macros */
135 #include <inttypes.h>
136 #endif /* !__WIN__ */
137 
138 /* Following defines are to enable performance schema
139 instrumentation in each of four InnoDB modules if
140 HAVE_PSI_INTERFACE is defined. */
141 #if defined HAVE_PSI_INTERFACE && !defined UNIV_HOTBACKUP
142 # define UNIV_PFS_MUTEX
143 # define UNIV_PFS_RWLOCK
144 
145 # define UNIV_PFS_IO
146 # define UNIV_PFS_THREAD
147 
148 /* There are mutexes/rwlocks that we want to exclude from
149 instrumentation even if their corresponding performance schema
150 define is set. And this PFS_NOT_INSTRUMENTED is used
151 as the key value to identify those objects that would
152 be excluded from instrumentation. */
153 # define PFS_NOT_INSTRUMENTED		ULINT32_UNDEFINED
154 
155 # define PFS_IS_INSTRUMENTED(key)	((key) != PFS_NOT_INSTRUMENTED)
156 
157 #endif /* HAVE_PSI_INTERFACE */
158 
159 #ifdef __WIN__
160 # define YY_NO_UNISTD_H 1
161 #endif /* __WIN__ */
162 
163 /*			DEBUG VERSION CONTROL
164 			===================== */
165 
166 /* When this macro is defined then additional test functions will be
167 compiled. These functions live at the end of each relevant source file
168 and have "test_" prefix. These functions are not called from anywhere in
169 the code, they can be called from gdb after
170 innobase_start_or_create_for_mysql() has executed using the call
171 command. Not tested on Windows. */
172 /*
173 #define UNIV_COMPILE_TEST_FUNCS
174 */
175 
176 #if defined HAVE_VALGRIND
177 # define UNIV_DEBUG_VALGRIND
178 #endif /* HAVE_VALGRIND */
179 #if 0
180 #define UNIV_DEBUG_VALGRIND			/* Enable extra
181 						Valgrind instrumentation */
182 #define UNIV_DEBUG_PRINT			/* Enable the compilation of
183 						some debug print functions */
184 #define UNIV_AHI_DEBUG				/* Enable adaptive hash index
185 						debugging without UNIV_DEBUG */
186 #define UNIV_BUF_DEBUG				/* Enable buffer pool
187 						debugging without UNIV_DEBUG */
188 #define UNIV_BLOB_LIGHT_DEBUG			/* Enable off-page column
189 						debugging without UNIV_DEBUG */
190 #define UNIV_DEBUG				/* Enable ut_ad() assertions
191 						and disable UNIV_INLINE */
192 #define UNIV_DEBUG_LOCK_VALIDATE		/* Enable
193 						ut_ad(lock_rec_validate_page())
194 						assertions. */
195 #define UNIV_DEBUG_FILE_ACCESSES		/* Enable freed block access
196 						debugging without UNIV_DEBUG */
197 #define UNIV_LRU_DEBUG				/* debug the buffer pool LRU */
198 #define UNIV_HASH_DEBUG				/* debug HASH_ macros */
199 #define UNIV_LIST_DEBUG				/* debug UT_LIST_ macros */
200 #define UNIV_LOG_LSN_DEBUG			/* write LSN to the redo log;
201 this will break redo log file compatibility, but it may be useful when
202 debugging redo log application problems. */
203 #define UNIV_MEM_DEBUG				/* detect memory leaks etc */
204 #define UNIV_IBUF_DEBUG				/* debug the insert buffer */
205 #define UNIV_BLOB_DEBUG				/* track BLOB ownership;
206 assumes that no BLOBs survive server restart */
207 #define UNIV_IBUF_COUNT_DEBUG			/* debug the insert buffer;
208 this limits the database to IBUF_COUNT_N_SPACES and IBUF_COUNT_N_PAGES,
209 and the insert buffer must be empty when the database is started */
210 #define UNIV_PERF_DEBUG                         /* debug flag that enables
211                                                 light weight performance
212                                                 related stuff. */
213 #define UNIV_SYNC_DEBUG				/* debug mutex and latch
214 operations (very slow); also UNIV_DEBUG must be defined */
215 #define UNIV_SEARCH_DEBUG			/* debug B-tree comparisons */
216 #define UNIV_SYNC_PERF_STAT			/* operation counts for
217 						rw-locks and mutexes */
218 #define UNIV_SEARCH_PERF_STAT			/* statistics for the
219 						adaptive hash index */
220 #define UNIV_SRV_PRINT_LATCH_WAITS		/* enable diagnostic output
221 						in sync0sync.cc */
222 #define UNIV_BTR_PRINT				/* enable functions for
223 						printing B-trees */
224 #define UNIV_ZIP_DEBUG				/* extensive consistency checks
225 						for compressed pages */
226 #define UNIV_ZIP_COPY				/* call page_zip_copy_recs()
227 						more often */
228 #define UNIV_AIO_DEBUG				/* prints info about
229 						submitted and reaped AIO
230 						requests to the log. */
231 #define UNIV_STATS_DEBUG			/* prints various stats
232 						related debug info from
233 						dict0stats.c */
234 #define FTS_INTERNAL_DIAG_PRINT                 /* FTS internal debugging
235                                                 info output */
236 #endif
237 
238 #define UNIV_BTR_DEBUG				/* check B-tree links */
239 #define UNIV_LIGHT_MEM_DEBUG			/* light memory debugging */
240 
241 /*
242 #define UNIV_SQL_DEBUG
243 #define UNIV_LOG_DEBUG
244 */
245 			/* the above option prevents forcing of log to disk
246 			at a buffer page write: it should be tested with this
247 			option off; also some ibuf tests are suppressed */
248 
249 /* Linkage specifier for non-static InnoDB symbols (variables and functions)
250 that are only referenced from within InnoDB, not from MySQL. We disable the
251 GCC visibility directive on all Sun operating systems because there is no
252 easy way to get it to work. See http://bugs.mysql.com/bug.php?id=52263. */
253 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(sun) || defined(__INTEL_COMPILER)
254 # define UNIV_INTERN MY_ATTRIBUTE((visibility ("hidden")))
255 #else
256 # define UNIV_INTERN
257 #endif
258 #if defined(INNODB_COMPILER_HINTS)      \
259     && defined __GNUC__                 \
260     && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
261 /** Starting with GCC 4.3, the "cold" attribute is used to inform the
262 compiler that a function is unlikely executed.  The function is
263 optimized for size rather than speed and on many targets it is placed
264 into special subsection of the text section so all cold functions
265 appears close together improving code locality of non-cold parts of
266 program.  The paths leading to call of cold functions within code are
267 marked as unlikely by the branch prediction mechanism.  optimize a
268 rarely invoked function for size instead for speed. */
269 # define UNIV_COLD MY_ATTRIBUTE((cold))
270 #else
271 # define UNIV_COLD /* empty */
272 #endif
273 
274 #ifndef UNIV_MUST_NOT_INLINE
275 /* Definition for inline version */
276 
277 #define UNIV_INLINE static inline
278 
279 #else /* !UNIV_MUST_NOT_INLINE */
280 /* If we want to compile a noninlined version we use the following macro
281 definitions: */
282 
283 #define UNIV_NONINL
284 #define UNIV_INLINE	UNIV_INTERN
285 
286 #endif /* !UNIV_MUST_NOT_INLINE */
287 
288 #ifdef _WIN32
289 #define UNIV_WORD_SIZE		4
290 #elif defined(_WIN64)
291 #define UNIV_WORD_SIZE		8
292 #else
293 /** MySQL config.h generated by GNU autoconf will define SIZEOF_LONG in Posix */
294 #define UNIV_WORD_SIZE		SIZEOF_LONG
295 #endif
296 
297 /** The following alignment is used in memory allocations in memory heap
298 management to ensure correct alignment for doubles etc. */
299 #define UNIV_MEM_ALIGNMENT	8
300 
301 /** The following alignment is used in aligning lints etc. */
302 #define UNIV_WORD_ALIGNMENT	UNIV_WORD_SIZE
303 
304 /*
305 			DATABASE VERSION CONTROL
306 			========================
307 */
308 
309 /** There are currently two InnoDB file formats which are used to group
310 features with similar restrictions and dependencies. Using an enum allows
311 switch statements to give a compiler warning when a new one is introduced. */
312 enum innodb_file_formats_enum {
313 	/** Antelope File Format: InnoDB/MySQL up to 5.1.
314 	This format includes REDUNDANT and COMPACT row formats */
315 	UNIV_FORMAT_A		= 0,
316 
317 	/** Barracuda File Format: Introduced in InnoDB plugin for 5.1:
318 	This format includes COMPRESSED and DYNAMIC row formats.  It
319 	includes the ability to create secondary indexes from data that
320 	is not on the clustered index page and the ability to store more
321 	data off the clustered index page. */
322 	UNIV_FORMAT_B		= 1
323 };
324 
325 typedef enum innodb_file_formats_enum innodb_file_formats_t;
326 
327 /** Minimum supported file format */
328 #define UNIV_FORMAT_MIN		UNIV_FORMAT_A
329 
330 /** Maximum supported file format */
331 #define UNIV_FORMAT_MAX		UNIV_FORMAT_B
332 
333 /** The 2-logarithm of UNIV_PAGE_SIZE: */
334 #define UNIV_PAGE_SIZE_SHIFT	srv_page_size_shift
335 
336 /** The universal page size of the database */
337 #define UNIV_PAGE_SIZE		((ulint) srv_page_size)
338 
339 /** log2 of smallest compressed page size (1<<10 == 1024 bytes)
340 Note: This must never change! */
341 #define UNIV_ZIP_SIZE_SHIFT_MIN		10
342 
343 /** log2 of largest compressed page size (1<<14 == 16384 bytes).
344 A compressed page directory entry reserves 14 bits for the start offset
345 and 2 bits for flags. This limits the uncompressed page size to 16k.
346 Even though a 16k uncompressed page can theoretically be compressed
347 into a larger compressed page, it is not a useful feature so we will
348 limit both with this same constant. */
349 #define UNIV_ZIP_SIZE_SHIFT_MAX		14
350 
351 /* Define the Min, Max, Default page sizes. */
352 /** Minimum Page Size Shift (power of 2) */
353 #define UNIV_PAGE_SIZE_SHIFT_MIN	12
354 /** Maximum Page Size Shift (power of 2) */
355 #define UNIV_PAGE_SIZE_SHIFT_MAX	14
356 /** Default Page Size Shift (power of 2) */
357 #define UNIV_PAGE_SIZE_SHIFT_DEF	14
358 /** Original 16k InnoDB Page Size Shift, in case the default changes */
359 #define UNIV_PAGE_SIZE_SHIFT_ORIG	14
360 
361 /** Minimum page size InnoDB currently supports. */
362 #define UNIV_PAGE_SIZE_MIN	(1 << UNIV_PAGE_SIZE_SHIFT_MIN)
363 /** Maximum page size InnoDB currently supports. */
364 #define UNIV_PAGE_SIZE_MAX	(1 << UNIV_PAGE_SIZE_SHIFT_MAX)
365 /** Default page size for InnoDB tablespaces. */
366 #define UNIV_PAGE_SIZE_DEF	(1 << UNIV_PAGE_SIZE_SHIFT_DEF)
367 /** Original 16k page size for InnoDB tablespaces. */
368 #define UNIV_PAGE_SIZE_ORIG	(1 << UNIV_PAGE_SIZE_SHIFT_ORIG)
369 
370 /** Smallest compressed page size */
371 #define UNIV_ZIP_SIZE_MIN	(1 << UNIV_ZIP_SIZE_SHIFT_MIN)
372 
373 /** Largest compressed page size */
374 #define UNIV_ZIP_SIZE_MAX	(1 << UNIV_ZIP_SIZE_SHIFT_MAX)
375 
376 /** Number of supported page sizes (The convention 'ssize' is used
377 for 'log2 minus 9' or the number of shifts starting with 512.)
378 This number varies depending on UNIV_PAGE_SIZE. */
379 #define UNIV_PAGE_SSIZE_MAX					\
380 	(UNIV_PAGE_SIZE_SHIFT - UNIV_ZIP_SIZE_SHIFT_MIN + 1)
381 
382 /** Maximum number of parallel threads in a parallelized operation */
383 #define UNIV_MAX_PARALLELISM	32
384 
385 /** This is the "mbmaxlen" for my_charset_filename (defined in
386 strings/ctype-utf8.c), which is used to encode File and Database names. */
387 #define FILENAME_CHARSET_MAXNAMLEN	5
388 
389 /** The maximum length of an encode table name in bytes.  The max
390 table and database names are NAME_CHAR_LEN (64) characters. After the
391 encoding, the max length would be NAME_CHAR_LEN (64) *
392 FILENAME_CHARSET_MAXNAMLEN (5) = 320 bytes. The number does not include a
393 terminating '\0'. InnoDB can handle longer names internally */
394 #define MAX_TABLE_NAME_LEN	320
395 
396 /** The maximum length of a database name. Like MAX_TABLE_NAME_LEN this is
397 the MySQL's NAME_LEN, see check_and_convert_db_name(). */
398 #define MAX_DATABASE_NAME_LEN	MAX_TABLE_NAME_LEN
399 
400 /** MAX_FULL_NAME_LEN defines the full name path including the
401 database name and table name. In addition, 14 bytes is added for:
402 	2 for surrounding quotes around table name
403 	1 for the separating dot (.)
404 	9 for the #mysql50# prefix */
405 #define MAX_FULL_NAME_LEN				\
406 	(MAX_TABLE_NAME_LEN + MAX_DATABASE_NAME_LEN + 14)
407 
408 /** The maximum length in bytes that a database name can occupy when stored in
409 UTF8, including the terminating '\0', see dict_fs2utf8(). You must include
410 mysql_com.h if you are to use this macro. */
411 #define MAX_DB_UTF8_LEN		(NAME_LEN + 1)
412 
413 /** The maximum length in bytes that a table name can occupy when stored in
414 UTF8, including the terminating '\0', see dict_fs2utf8(). You must include
415 mysql_com.h if you are to use this macro. */
416 #define MAX_TABLE_UTF8_LEN	(NAME_LEN + sizeof(srv_mysql50_table_name_prefix))
417 
418 /*
419 			UNIVERSAL TYPE DEFINITIONS
420 			==========================
421 */
422 
423 /* Note that inside MySQL 'byte' is defined as char on Linux! */
424 #define byte			unsigned char
425 
426 /* Another basic type we use is unsigned long integer which should be equal to
427 the word size of the machine, that is on a 32-bit platform 32 bits, and on a
428 64-bit platform 64 bits. We also give the printf format for the type as a
429 macro ULINTPF. */
430 
431 
432 #ifdef __WIN__
433 /* Use the integer types and formatting strings defined in Visual Studio. */
434 # define UINT32PF	"%I32u"
435 # define INT64PF	"%I64d"
436 # define UINT64PF	"%I64u"
437 # define UINT64PFx	"%016I64x"
438 # define DBUG_LSN_PF    "%llu"
439 typedef __int64 ib_int64_t;
440 typedef unsigned __int64 ib_uint64_t;
441 typedef unsigned __int32 ib_uint32_t;
442 #else
443 /* Use the integer types and formatting strings defined in the C99 standard. */
444 # define UINT32PF	"%" PRIu32
445 # define INT64PF	"%" PRId64
446 # define UINT64PF	"%" PRIu64
447 # define UINT64PFx	"%016" PRIx64
448 # define DBUG_LSN_PF    UINT64PF
449 typedef int64_t ib_int64_t;
450 typedef uint64_t ib_uint64_t;
451 typedef uint32_t ib_uint32_t;
452 # endif /* __WIN__ */
453 
454 # define IB_ID_FMT	UINT64PF
455 
456 #ifdef _WIN64
457 typedef unsigned __int64	ulint;
458 typedef __int64			lint;
459 # define ULINTPF		UINT64PF
460 #else
461 typedef unsigned long int	ulint;
462 typedef long int		lint;
463 # define ULINTPF		"%lu"
464 #endif /* _WIN64 */
465 
466 #ifndef UNIV_HOTBACKUP
467 typedef unsigned long long int	ullint;
468 #endif /* UNIV_HOTBACKUP */
469 
470 #ifndef __WIN__
471 #if SIZEOF_LONG != SIZEOF_VOIDP
472 #error "Error: InnoDB's ulint must be of the same size as void*"
473 #endif
474 #endif
475 
476 /** The 'undefined' value for a ulint */
477 #define ULINT_UNDEFINED		((ulint)(-1))
478 
479 #define ULONG_UNDEFINED		((ulong)(-1))
480 
481 /** The 'undefined' value for a ib_uint64_t */
482 #define UINT64_UNDEFINED	((ib_uint64_t)(-1))
483 
484 /** The bitmask of 32-bit unsigned integer */
485 #define ULINT32_MASK		0xFFFFFFFF
486 /** The undefined 32-bit unsigned integer */
487 #define	ULINT32_UNDEFINED	ULINT32_MASK
488 
489 /** Maximum value for a ulint */
490 #define ULINT_MAX		((ulint)(-2))
491 
492 /** Maximum value for ib_uint64_t */
493 #define IB_UINT64_MAX		((ib_uint64_t) (~0ULL))
494 
495 /** The generic InnoDB system object identifier data type */
496 typedef ib_uint64_t		ib_id_t;
497 #define IB_ID_MAX		IB_UINT64_MAX
498 
499 /** The 'undefined' value for a ullint */
500 #define ULLINT_UNDEFINED        ((ullint)(-1))
501 
502 /** This 'ibool' type is used within Innobase. Remember that different included
503 headers may define 'bool' differently. Do not assume that 'bool' is a ulint! */
504 #define ibool			ulint
505 
506 #ifndef TRUE
507 
508 #define TRUE    1
509 #define FALSE   0
510 
511 #endif
512 
513 #define UNIV_NOTHROW
514 
515 /** The following number as the length of a logical field means that the field
516 has the SQL NULL as its value. NOTE that because we assume that the length
517 of a field is a 32-bit integer when we store it, for example, to an undo log
518 on disk, we must have also this number fit in 32 bits, also in 64-bit
519 computers! */
520 
521 #define UNIV_SQL_NULL ULINT32_UNDEFINED
522 
523 /** Lengths which are not UNIV_SQL_NULL, but bigger than the following
524 number indicate that a field contains a reference to an externally
525 stored part of the field in the tablespace. The length field then
526 contains the sum of the following flag and the locally stored len. */
527 
528 #define UNIV_EXTERN_STORAGE_FIELD (UNIV_SQL_NULL - UNIV_PAGE_SIZE_MAX)
529 
530 #if defined(__GNUC__) && (__GNUC__ > 2) && ! defined(__INTEL_COMPILER)
531 #define HAVE_GCC_GT_2
532 /* Tell the compiler that variable/function is unused. */
533 # define UNIV_UNUSED    MY_ATTRIBUTE ((unused))
534 #else
535 # define UNIV_UNUSED
536 #endif /* CHECK FOR GCC VER_GT_2 */
537 
538 /* Some macros to improve branch prediction and reduce cache misses */
539 #if defined(INNODB_COMPILER_HINTS) && defined(HAVE_GCC_GT_2)
540 /* Tell the compiler that 'expr' probably evaluates to 'constant'. */
541 # define UNIV_EXPECT(expr,constant) __builtin_expect(expr, constant)
542 /* Tell the compiler that a pointer is likely to be NULL */
543 # define UNIV_LIKELY_NULL(ptr) __builtin_expect((ulint) ptr, 0)
544 /* Minimize cache-miss latency by moving data at addr into a cache before
545 it is read. */
546 # define UNIV_PREFETCH_R(addr) __builtin_prefetch(addr, 0, 3)
547 /* Minimize cache-miss latency by moving data at addr into a cache before
548 it is read or written. */
549 # define UNIV_PREFETCH_RW(addr) __builtin_prefetch(addr, 1, 3)
550 
551 /* Sun Studio includes sun_prefetch.h as of version 5.9 */
552 #elif (defined(__SUNPRO_C) && __SUNPRO_C >= 0x590) \
553        || (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x590)
554 
555 # include <sun_prefetch.h>
556 
557 #if __SUNPRO_C >= 0x550
558 # undef UNIV_INTERN
559 # define UNIV_INTERN __hidden
560 #endif /* __SUNPRO_C >= 0x550 */
561 
562 # define UNIV_EXPECT(expr,value) (expr)
563 # define UNIV_LIKELY_NULL(expr) (expr)
564 
565 # if defined(INNODB_COMPILER_HINTS)
566 //# define UNIV_PREFETCH_R(addr) sun_prefetch_read_many((void*) addr)
567 #  define UNIV_PREFETCH_R(addr) ((void) 0)
568 #  define UNIV_PREFETCH_RW(addr) sun_prefetch_write_many(addr)
569 # else
570 #  define UNIV_PREFETCH_R(addr) ((void) 0)
571 #  define UNIV_PREFETCH_RW(addr) ((void) 0)
572 # endif /* INNODB_COMPILER_HINTS */
573 
574 #else
575 /* Dummy versions of the macros */
576 # define UNIV_EXPECT(expr,value) (expr)
577 # define UNIV_LIKELY_NULL(expr) (expr)
578 # define UNIV_PREFETCH_R(addr) ((void) 0)
579 # define UNIV_PREFETCH_RW(addr) ((void) 0)
580 #endif
581 
582 /* Tell the compiler that cond is likely to hold */
583 #define UNIV_LIKELY(cond) UNIV_EXPECT(cond, TRUE)
584 /* Tell the compiler that cond is unlikely to hold */
585 #define UNIV_UNLIKELY(cond) UNIV_EXPECT(cond, FALSE)
586 
587 /* Compile-time constant of the given array's size. */
588 #define UT_ARR_SIZE(a) (sizeof(a) / sizeof((a)[0]))
589 
590 /* The return type from a thread's start function differs between Unix and
591 Windows, so define a typedef for it and a macro to use at the end of such
592 functions. */
593 
594 #ifdef __WIN__
595 typedef ulint os_thread_ret_t;
596 #define OS_THREAD_DUMMY_RETURN return(0)
597 #else
598 typedef void* os_thread_ret_t;
599 #define OS_THREAD_DUMMY_RETURN return(NULL)
600 #endif
601 
602 #include <stdio.h>
603 #include "ut0dbg.h"
604 #include "ut0ut.h"
605 #include "db0err.h"
606 #ifdef UNIV_DEBUG_VALGRIND
607 # include <valgrind/memcheck.h>
608 # define UNIV_MEM_VALID(addr, size) VALGRIND_MAKE_MEM_DEFINED(addr, size)
609 # define UNIV_MEM_INVALID(addr, size) VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
610 # define UNIV_MEM_FREE(addr, size) VALGRIND_MAKE_MEM_NOACCESS(addr, size)
611 # define UNIV_MEM_ALLOC(addr, size) VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
612 # define UNIV_MEM_DESC(addr, size) VALGRIND_CREATE_BLOCK(addr, size, #addr)
613 # define UNIV_MEM_UNDESC(b) VALGRIND_DISCARD(b)
614 # define UNIV_MEM_ASSERT_RW_LOW(addr, size, should_abort) do {		\
615 	const void* _p = (const void*) (ulint)				\
616 		VALGRIND_CHECK_MEM_IS_DEFINED(addr, size);		\
617 	if (UNIV_LIKELY_NULL(_p)) {					\
618 		fprintf(stderr, "%s:%d: %p[%u] undefined at %ld\n",	\
619 			__FILE__, __LINE__,				\
620 			(const void*) (addr), (unsigned) (size), (long)	\
621 			(((const char*) _p) - ((const char*) (addr))));	\
622 		if (should_abort) {					\
623 			ut_error;					\
624 		}							\
625 	}								\
626 } while (0)
627 # define UNIV_MEM_ASSERT_RW(addr, size)					\
628 	UNIV_MEM_ASSERT_RW_LOW(addr, size, false)
629 # define UNIV_MEM_ASSERT_RW_ABORT(addr, size)				\
630 	UNIV_MEM_ASSERT_RW_LOW(addr, size, true)
631 # define UNIV_MEM_ASSERT_W(addr, size) do {				\
632 	const void* _p = (const void*) (ulint)				\
633 		VALGRIND_CHECK_MEM_IS_ADDRESSABLE(addr, size);		\
634 	if (UNIV_LIKELY_NULL(_p))					\
635 		fprintf(stderr, "%s:%d: %p[%u] unwritable at %ld\n",	\
636 			__FILE__, __LINE__,				\
637 			(const void*) (addr), (unsigned) (size), (long)	\
638 			(((const char*) _p) - ((const char*) (addr))));	\
639 	} while (0)
640 # define UNIV_MEM_TRASH(addr, c, size) do {				\
641 	ut_d(memset(addr, c, size));					\
642 	UNIV_MEM_INVALID(addr, size);					\
643 	} while (0)
644 #else
645 # define UNIV_MEM_VALID(addr, size) do {} while(0)
646 # define UNIV_MEM_INVALID(addr, size) do {} while(0)
647 # define UNIV_MEM_FREE(addr, size) do {} while(0)
648 # define UNIV_MEM_ALLOC(addr, size) do {} while(0)
649 # define UNIV_MEM_DESC(addr, size) do {} while(0)
650 # define UNIV_MEM_UNDESC(b) do {} while(0)
651 # define UNIV_MEM_ASSERT_RW_LOW(addr, size, should_abort) do {} while(0)
652 # define UNIV_MEM_ASSERT_RW(addr, size) do {} while(0)
653 # define UNIV_MEM_ASSERT_RW_ABORT(addr, size) do {} while(0)
654 # define UNIV_MEM_ASSERT_W(addr, size) do {} while(0)
655 # define UNIV_MEM_TRASH(addr, c, size) do {} while(0)
656 #endif
657 #define UNIV_MEM_ASSERT_AND_FREE(addr, size) do {	\
658 	UNIV_MEM_ASSERT_W(addr, size);			\
659 	UNIV_MEM_FREE(addr, size);			\
660 } while (0)
661 #define UNIV_MEM_ASSERT_AND_ALLOC(addr, size) do {	\
662 	UNIV_MEM_ASSERT_W(addr, size);			\
663 	UNIV_MEM_ALLOC(addr, size);			\
664 } while (0)
665 
666 extern ulong	srv_page_size_shift;
667 extern ulong	srv_page_size;
668 
669 #endif
670