1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: 3 /* -*- mode: C; c-basic-offset: 4 -*- */ 4 #ident "$Id$" 5 /*====== 6 This file is part of TokuDB 7 8 9 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. 10 11 TokuDBis is free software: you can redistribute it and/or modify 12 it under the terms of the GNU General Public License, version 2, 13 as published by the Free Software Foundation. 14 15 TokuDB is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with TokuDB. If not, see <http://www.gnu.org/licenses/>. 22 23 ======= */ 24 25 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." 26 27 #ifndef _TOKUDB_MEMORY_H 28 #define _TOKUDB_MEMORY_H 29 30 #include "hatoku_defines.h" 31 32 namespace tokudb { 33 namespace memory { 34 35 void* malloc(size_t s, myf flags); 36 void* realloc(void* p, size_t s, myf flags); 37 void free(void* ptr); 38 char* strdup(const char* p, myf flags); 39 void* multi_malloc(myf myFlags, ...); 40 41 42 inline void* malloc(size_t s, myf flags) { 43 #if 50700 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50799 44 return ::my_malloc(0, s, flags); 45 #else 46 return ::my_malloc(s, flags); 47 #endif 48 } 49 inline void* realloc(void* p, size_t s, myf flags) { 50 if (s == 0) 51 return p; 52 #if 50700 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50799 53 return ::my_realloc(0, p, s, flags); 54 #else 55 return ::my_realloc(p, s, flags | MY_ALLOW_ZERO_PTR); 56 #endif 57 } 58 inline void free(void* ptr) { 59 if (ptr) 60 ::my_free(ptr); 61 } 62 inline char* strdup(const char* p, myf flags) { 63 #if 50700 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50799 64 return ::my_strdup(0, p, flags); 65 #else 66 return ::my_strdup(p, flags); 67 #endif 68 } 69 inline void* multi_malloc(myf myFlags, ...) { 70 va_list args; 71 char** ptr; 72 char* start; 73 char* res; 74 size_t tot_length,length; 75 76 va_start(args,myFlags); 77 tot_length = 0; 78 while ((ptr = va_arg(args, char**))) { 79 length = va_arg(args, uint); 80 tot_length += ALIGN_SIZE(length); 81 } 82 va_end(args); 83 84 if (!(start = (char*)malloc(tot_length, myFlags))) { 85 return 0; 86 } 87 88 va_start(args, myFlags); 89 res = start; 90 while ((ptr = va_arg(args, char**))) { 91 *ptr = res; 92 length = va_arg(args,uint); 93 res += ALIGN_SIZE(length); 94 } 95 va_end(args); 96 return start; 97 } 98 99 } // namespace thread 100 } // namespace tokudb 101 102 #endif // _TOKUDB_MEMORY_H 103