1/* Copyright (c) 2002, 2015, Oracle and/or its affiliates.
2 Copyright (c) 2012, 2017, MariaDB Corporation.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17/* guess_malloc_library() deduces, to the best of its ability,
18 the currently used malloc library and its version */
19
20#include <stddef.h>
21#include "my_global.h"
22#include <m_string.h>
23
24typedef const char* (*tc_version_type)(int*, int*, const char**);
25typedef int (*mallctl_type)(const char*, void*, size_t*, void*, size_t);
26
27char *guess_malloc_library()
28{
29 tc_version_type tc_version_func;
30 mallctl_type mallctl_func;
31#ifndef HAVE_DLOPEN
32 return (char*) MALLOC_LIBRARY;
33#else
34 static char buf[128];
35
36 if (strcmp(MALLOC_LIBRARY, "system") != 0)
37 {
38 return (char*) MALLOC_LIBRARY;
39 }
40
41 /* tcmalloc */
42 tc_version_func= (tc_version_type) dlsym(RTLD_DEFAULT, "tc_version");
43 if (tc_version_func)
44 {
45 int major, minor;
46 const char* ver_str = tc_version_func(&major, &minor, NULL);
47 strxnmov(buf, sizeof(buf)-1, "tcmalloc ", ver_str, NULL);
48 return buf;
49 }
50
51 /* jemalloc */
52 mallctl_func= (mallctl_type) dlsym(RTLD_DEFAULT, "mallctl");
53 if (mallctl_func)
54 {
55 char *ver;
56 size_t len = sizeof(ver);
57 mallctl_func("version", &ver, &len, NULL, 0);
58 strxnmov(buf, sizeof(buf)-1, "jemalloc ", ver, NULL);
59 return buf;
60 }
61
62 return (char*) MALLOC_LIBRARY;
63#endif
64}
65
66