1
/* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
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.
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.
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 */
20
@file storage/perfschema/pfs_global.h
21
Miscellaneous global dependencies (declarations).
24
extern bool pfs_initialized;
25
extern size_t pfs_allocated_memory;
27
void *pfs_malloc(size_t size, myf flags);
28
#define PFS_MALLOC_ARRAY(n, T, f) \
29
reinterpret_cast<T*> (pfs_malloc((n) * sizeof(T), (f)))
30
void pfs_free(void *ptr);
32
inline uint randomized_index(const void *ptr, uint max_size)
37
register intptr value;
39
if (unlikely(max_size == 0))
43
ptr is typically an aligned structure, and can be in an array.
44
- The last bits are not random because of alignment,
46
- The high bits are mostly constant, especially with 64 bits architectures,
47
but we keep most of them anyway, by doing computation in intptr.
48
The high bits are significant depending on where the data is
49
stored (the data segment, the stack, the heap, ...).
50
- To spread consecutive cells in an array further, we multiply by
51
a factor A. This factor should not be too high, which would cause
52
an overflow and cause loss of randomness (droping the top high bits).
53
The factor is a prime number, to help spread the distribution.
54
- To add more noise, and to be more robust if the calling code is
55
passing a constant value instead of a random identity,
56
we add the previous results, for hysteresys, with a degree 2 polynom,
58
- Last, a modulo is applied to be within the [0, max_size - 1] range.
59
Note that seed1 and seed2 are static, and are *not* thread safe,
61
Effect with arrays: T array[N]
62
- ptr(i) = & array[i] = & array[0] + i * sizeof(T)
63
- ptr(i+1) = ptr(i) + sizeof(T).
64
What we want here, is to have index(i) and index(i+1) fall into
65
very different areas in [0, max_size - 1], to avoid locality.
67
value= (reinterpret_cast<intptr> (ptr)) >> 3;
69
value+= seed2 + seed1 + 1;
71
result= (static_cast<uint> (value)) % max_size;
76
DBUG_ASSERT(result < max_size);
80
void pfs_print_error(const char *format, ...);
83
Given an array defined as T ARRAY[MAX],
84
check that an UNSAFE pointer actually points to an element
87
#define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE) \
89
if ((&ARRAY[0] <= UNSAFE) && \
90
(UNSAFE < &ARRAY[MAX])) \
92
offset= ((intptr) UNSAFE - (intptr) ARRAY) % sizeof(T); \