MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pfs_global.cc
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 
21 #include "my_global.h"
22 #include "my_sys.h"
23 #include "pfs_global.h"
24 #include "my_net.h"
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 
33 #ifdef __WIN__
34  #include <winsock2.h>
35 #else
36  #include <arpa/inet.h>
37 #endif
38 
39 bool pfs_initialized= false;
41 
47 void *pfs_malloc(size_t size, myf flags)
48 {
49  DBUG_ASSERT(! pfs_initialized);
50  DBUG_ASSERT(size > 0);
51 
52  void *ptr;
53 
54 #ifdef PFS_ALIGNEMENT
55 #ifdef HAVE_POSIX_MEMALIGN
56  /* Linux */
57  if (unlikely(posix_memalign(& ptr, PFS_ALIGNEMENT, size)))
58  return NULL;
59 #else
60 #ifdef HAVE_MEMALIGN
61  /* Solaris */
62  ptr= memalign(PFS_ALIGNEMENT, size);
63  if (unlikely(ptr == NULL))
64  return NULL;
65 #else
66 #ifdef HAVE_ALIGNED_MALLOC
67  /* Windows */
68  ptr= _aligned_malloc(size, PFS_ALIGNEMENT);
69  if (unlikely(ptr == NULL))
70  return NULL;
71 #else
72 #error "Missing implementation for PFS_ALIGNENT"
73 #endif /* HAVE_ALIGNED_MALLOC */
74 #endif /* HAVE_MEMALIGN */
75 #endif /* HAVE_POSIX_MEMALIGN */
76 #else /* PFS_ALIGNMENT */
77  /* Everything else */
78  ptr= malloc(size);
79  if (unlikely(ptr == NULL))
80  return NULL;
81 #endif
82 
84  if (flags & MY_ZEROFILL)
85  memset(ptr, 0, size);
86  return ptr;
87 }
88 
89 void pfs_free(void *ptr)
90 {
91  if (ptr == NULL)
92  return;
93 
94 #ifdef HAVE_POSIX_MEMALIGN
95  /* Allocated with posix_memalign() */
96  free(ptr);
97 #else
98 #ifdef HAVE_MEMALIGN
99  /* Allocated with memalign() */
100  free(ptr);
101 #else
102 #ifdef HAVE_ALIGNED_MALLOC
103  /* Allocated with _aligned_malloc() */
104  _aligned_free(ptr);
105 #else
106  /* Allocated with malloc() */
107  free(ptr);
108 #endif /* HAVE_ALIGNED_MALLOC */
109 #endif /* HAVE_MEMALIGN */
110 #endif /* HAVE_POSIX_MEMALIGN */
111 }
112 
113 void pfs_print_error(const char *format, ...)
114 {
115  va_list args;
116  va_start(args, format);
117  /*
118  Printing to anything else, like the error log, would generate even more
119  recursive calls to the performance schema implementation
120  (file io is instrumented), so that could lead to catastrophic results.
121  Printing to something safe, and low level: stderr only.
122  */
123  vfprintf(stderr, format, args);
124  va_end(args);
125  fflush(stderr);
126 }
127 
130 uint pfs_get_socket_address(char *host,
131  uint host_len,
132  uint *port,
133  const struct sockaddr_storage *src_addr,
134  socklen_t src_len)
135 {
136  DBUG_ASSERT(host);
137  DBUG_ASSERT(src_addr);
138  DBUG_ASSERT(port);
139 
140  memset(host, 0, host_len);
141  *port= 0;
142 
143  switch (src_addr->ss_family)
144  {
145  case AF_INET:
146  {
147  if (host_len < INET_ADDRSTRLEN+1)
148  return 0;
149  struct sockaddr_in *sa4= (struct sockaddr_in *)(src_addr);
150  #ifdef __WIN__
151  /* Older versions of Windows do not support inet_ntop() */
152  getnameinfo((struct sockaddr *)sa4, sizeof(struct sockaddr_in),
153  host, host_len, NULL, 0, NI_NUMERICHOST);
154  #else
155  inet_ntop(AF_INET, &(sa4->sin_addr), host, INET_ADDRSTRLEN);
156  #endif
157  *port= ntohs(sa4->sin_port);
158  }
159  break;
160 
161 #ifdef HAVE_IPV6
162  case AF_INET6:
163  {
164  if (host_len < INET6_ADDRSTRLEN+1)
165  return 0;
166  struct sockaddr_in6 *sa6= (struct sockaddr_in6 *)(src_addr);
167  #ifdef __WIN__
168  /* Older versions of Windows do not support inet_ntop() */
169  getnameinfo((struct sockaddr *)sa6, sizeof(struct sockaddr_in6),
170  host, host_len, NULL, 0, NI_NUMERICHOST);
171  #else
172  inet_ntop(AF_INET6, &(sa6->sin6_addr), host, INET6_ADDRSTRLEN);
173  #endif
174  *port= ntohs(sa6->sin6_port);
175  }
176  break;
177 #endif
178 
179  default:
180  break;
181  }
182 
183  /* Return actual IP address string length */
184  return (strlen((const char*)host));
185 }
186