MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pfs_global.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #ifndef PFS_GLOBAL_H
17 #define PFS_GLOBAL_H
18 
19 #include "my_global.h"
20 #include "my_compiler.h"
21 
28 extern bool pfs_initialized;
30 extern size_t pfs_allocated_memory;
31 
32 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) || defined(HAVE_ALIGNED_MALLOC)
33 #define PFS_ALIGNEMENT 64
34 #define PFS_ALIGNED MY_ALIGNED(PFS_ALIGNEMENT)
35 #else
36 /*
37  Known platforms that do not provide aligned memory:
38  - MacOSX Darwin (osx10.5)
39  For these platforms, compile without the alignment optimization.
40 */
41 #define PFS_ALIGNED
42 #endif /* HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_MALLOC */
43 
44 void *pfs_malloc(size_t size, myf flags);
45 
52 #define PFS_MALLOC_ARRAY(n, T, f) \
53  reinterpret_cast<T*> (pfs_malloc((n) * sizeof(T), (f)))
54 
56 void pfs_free(void *ptr);
57 
58 
59 uint pfs_get_socket_address(char *host,
60  uint host_len,
61  uint *port,
62  const struct sockaddr_storage *src_addr,
63  socklen_t src_len);
64 
71 inline uint randomized_index(const void *ptr, uint max_size)
72 {
73  static uint seed1= 0;
74  static uint seed2= 0;
75  uint result;
76  register intptr value;
77 
78  if (unlikely(max_size == 0))
79  return 0;
80 
81  /*
82  ptr is typically an aligned structure, and can be in an array.
83  - The last bits are not random because of alignment,
84  so we divide by 8.
85  - The high bits are mostly constant, especially with 64 bits architectures,
86  but we keep most of them anyway, by doing computation in intptr.
87  The high bits are significant depending on where the data is
88  stored (the data segment, the stack, the heap, ...).
89  - To spread consecutive cells in an array further, we multiply by
90  a factor A. This factor should not be too high, which would cause
91  an overflow and cause loss of randomness (droping the top high bits).
92  The factor is a prime number, to help spread the distribution.
93  - To add more noise, and to be more robust if the calling code is
94  passing a constant value instead of a random identity,
95  we add the previous results, for hysteresys, with a degree 2 polynom,
96  X^2 + X + 1.
97  - Last, a modulo is applied to be within the [0, max_size - 1] range.
98  Note that seed1 and seed2 are static, and are *not* thread safe,
99  which is even better.
100  Effect with arrays: T array[N]
101  - ptr(i) = & array[i] = & array[0] + i * sizeof(T)
102  - ptr(i+1) = ptr(i) + sizeof(T).
103  What we want here, is to have index(i) and index(i+1) fall into
104  very different areas in [0, max_size - 1], to avoid locality.
105  */
106  value= (reinterpret_cast<intptr> (ptr)) >> 3;
107  value*= 1789;
108  value+= seed2 + seed1 + 1;
109 
110  result= (static_cast<uint> (value)) % max_size;
111 
112  seed2= seed1*seed1;
113  seed1= result;
114 
115  DBUG_ASSERT(result < max_size);
116  return result;
117 }
118 
119 void pfs_print_error(const char *format, ...);
120 
126 #define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE) \
127  intptr offset; \
128  if ((&ARRAY[0] <= UNSAFE) && \
129  (UNSAFE < &ARRAY[MAX])) \
130  { \
131  offset= ((intptr) UNSAFE - (intptr) ARRAY) % sizeof(T); \
132  if (offset == 0) \
133  return UNSAFE; \
134  } \
135  return NULL
136 
137 #endif
138