~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to storage/perfschema/pfs_global.h

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/**
 
20
  @file storage/perfschema/pfs_global.h
 
21
  Miscellaneous global dependencies (declarations).
 
22
*/
 
23
 
 
24
extern bool pfs_initialized;
 
25
extern size_t pfs_allocated_memory;
 
26
 
 
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);
 
31
 
 
32
inline uint randomized_index(const void *ptr, uint max_size)
 
33
{
 
34
  static uint seed1= 0;
 
35
  static uint seed2= 0;
 
36
  uint result;
 
37
  register intptr value;
 
38
 
 
39
  if (unlikely(max_size == 0))
 
40
    return 0;
 
41
 
 
42
  /*
 
43
    ptr is typically an aligned structure, and can be in an array.
 
44
    - The last bits are not random because of alignment,
 
45
      so we divide by 8.
 
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,
 
57
      X^2 + X + 1.
 
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,
 
60
    which is even better.
 
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.
 
66
  */
 
67
  value= (reinterpret_cast<intptr> (ptr)) >> 3;
 
68
  value*= 1789;
 
69
  value+= seed2 + seed1 + 1;
 
70
  
 
71
  result= (static_cast<uint> (value)) % max_size;
 
72
 
 
73
  seed2= seed1*seed1;
 
74
  seed1= result;
 
75
 
 
76
  DBUG_ASSERT(result < max_size);
 
77
  return result;
 
78
}
 
79
 
 
80
void pfs_print_error(const char *format, ...);
 
81
 
 
82
/**
 
83
  Given an array defined as T ARRAY[MAX],
 
84
  check that an UNSAFE pointer actually points to an element
 
85
  within the array.
 
86
*/
 
87
#define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE)          \
 
88
  intptr offset;                                            \
 
89
  if ((&ARRAY[0] <= UNSAFE) &&                              \
 
90
      (UNSAFE < &ARRAY[MAX]))                               \
 
91
  {                                                         \
 
92
    offset= ((intptr) UNSAFE - (intptr) ARRAY) % sizeof(T); \
 
93
    if (offset == 0)                                        \
 
94
      return UNSAFE;                                        \
 
95
  }                                                         \
 
96
  return NULL
 
97
 
 
98
#endif
 
99