diff options
Diffstat (limited to 'storage/perfschema/pfs_global.cc')
-rw-r--r-- | storage/perfschema/pfs_global.cc | 72 |
1 files changed, 61 insertions, 11 deletions
diff --git a/storage/perfschema/pfs_global.cc b/storage/perfschema/pfs_global.cc index 2351b829894..546597ef33e 100644 --- a/storage/perfschema/pfs_global.cc +++ b/storage/perfschema/pfs_global.cc @@ -18,13 +18,16 @@ Miscellaneous global dependencies (implementation). */ -#include "my_global.h" -#include "my_sys.h" #include "pfs_global.h" -#include "my_net.h" +#include <my_sys.h> +#include <my_net.h> +#ifdef HAVE_MALLOC_H +#include <malloc.h> /* memalign() may be here */ +#endif -#include <stdlib.h> -#include <string.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif #ifdef __WIN__ #include <winsock2.h> @@ -45,18 +48,65 @@ void *pfs_malloc(size_t size, myf flags) DBUG_ASSERT(! pfs_initialized); DBUG_ASSERT(size > 0); - void *ptr= malloc(size); - if (likely(ptr != NULL)) - pfs_allocated_memory+= size; - if (likely((ptr != NULL) && (flags & MY_ZEROFILL))) + void *ptr; + +#ifdef PFS_ALIGNEMENT +#ifdef HAVE_POSIX_MEMALIGN + /* Linux */ + if (unlikely(posix_memalign(& ptr, PFS_ALIGNEMENT, size))) + return NULL; +#else +#ifdef HAVE_MEMALIGN + /* Solaris */ + ptr= memalign(PFS_ALIGNEMENT, size); + if (unlikely(ptr == NULL)) + return NULL; +#else +#ifdef HAVE_ALIGNED_MALLOC + /* Windows */ + ptr= _aligned_malloc(size, PFS_ALIGNEMENT); + if (unlikely(ptr == NULL)) + return NULL; +#else +#error "Missing implementation for PFS_ALIGNENT" +#endif /* HAVE_ALIGNED_MALLOC */ +#endif /* HAVE_MEMALIGN */ +#endif /* HAVE_POSIX_MEMALIGN */ +#else /* PFS_ALIGNMENT */ + /* Everything else */ + ptr= malloc(size); + if (unlikely(ptr == NULL)) + return NULL; +#endif + + pfs_allocated_memory+= size; + if (flags & MY_ZEROFILL) memset(ptr, 0, size); return ptr; } void pfs_free(void *ptr) { - if (ptr != NULL) - free(ptr); + if (ptr == NULL) + return; + +#ifdef HAVE_POSIX_MEMALIGN + /* Allocated with posix_memalign() */ + free(ptr); +#else +#ifdef HAVE_MEMALIGN + /* Allocated with memalign() */ + free(ptr); +#else +#ifdef HAVE_ALIGNED_MALLOC + /* Allocated with _aligned_malloc() */ + _aligned_free(ptr); +#else + /* Allocated with malloc() */ + free(ptr); +#endif /* HAVE_ALIGNED_MALLOC */ +#endif /* HAVE_MEMALIGN */ +#endif /* HAVE_POSIX_MEMALIGN */ } void pfs_print_error(const char *format, ...) |