1 From 7d8907397a37b403888ba9e57dd22d3b4ac7c7a5 Mon Sep 17 00:00:00 2001
2 From: Michael Wild <themiwi@users.sourceforge.net>
3 Date: Thu, 16 Jun 2011 17:16:54 +0200
4 Subject: [PATCH] Properly detect malloc.h and mallinfo
6 and make GKLIB_PATH absolute.
8 Signed-off-by: Michael Wild <themiwi@users.sourceforge.net>
10 GKlib/GKlibSystem.cmake | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
11 GKlib/gk_config.h.in | 34 ++++++++++++++++++
12 GKlib/gk_dlmalloc.h | 11 +++---
13 3 files changed, 126 insertions(+), 5 deletions(-)
14 create mode 100644 GKlib/gk_config.h.in
16 diff --git a/GKlib/GKlibSystem.cmake b/GKlib/GKlibSystem.cmake
17 index a7af9cb..e2de364 100644
18 --- a/GKlib/GKlibSystem.cmake
19 +++ b/GKlib/GKlibSystem.cmake
20 @@ -7,6 +7,10 @@ option(ASSERT2 "additional assertions" OFF)
21 option(DEBUG "add debugging support" OFF)
22 option(OPENMP "enable OpenMP support" OFF)
24 +if(NOT IS_ABSOLUTE "${GKLIB_PATH}")
25 + set(GKLIB_PATH "${PROJECT_SOURCE_DIR}/${GKLIB_PATH}")
29 set(GKlib_COPTS "/Ox")
30 set(GKlib_COPTIONS "-DWIN32 -DMSC -D_CRT_SECURE_NO_DEPRECATE -DUSE_GKREGEX")
31 @@ -77,3 +81,85 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GKlib_COPTIONS} ${GKlib_COPTS}")
33 file(GLOB GKlib_sources ${GKLIB_PATH}/*.c)
34 file(GLOB GKlib_includes ${GKLIB_PATH}/*.h)
36 +# do we have mallinfo() and struct mallinfo? this is a bit tedious...
37 +check_function_exists(mallinfo HAVE_MALLINFO)
38 +mark_as_advanced(HAVE_MALLINFO)
39 +if(HAVE_MALLINFO AND NOT DEFINED MALLINFO_FIELD_TYPE)
41 + # sizeof(int);sizeof(size_t);sizeof(mallinfo.arena);signedness of mallinfo.arena
45 +int main(int argc, char* argv[]){
46 + struct mallinfo mi = mallinfo();
48 + char sig = mi.arena<0;
49 + printf(\"%lu;%lu;%lu;%d\\n\",sizeof(int),sizeof(size_t),sizeof(mi.arena),sig);
53 + file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
56 + try_run(MALLINFO_RUNS MALLINFO_COMPILES
57 + ${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
58 + RUN_OUTPUT_VARIABLE MALLINFO_OUTPUT
60 + set(MALLINFO_USABLE FALSE)
61 + if(MALLINFO_COMPILES AND NOT MALLINFO_RUNS STREQUAL FAILED_TO_RUN)
62 + set(MALLINFO_OUTPUT ${MALLINFO_OUTPUT})
63 + list(GET MALLINFO_OUTPUT 0 SIZEOF_INT)
64 + list(GET MALLINFO_OUTPUT 1 SIZEOF_SIZE_T)
65 + list(GET MALLINFO_OUTPUT 2 SIZEOF_MALLINFO_ARENA)
66 + list(GET MALLINFO_OUTPUT 3 MALLINFO_ARENA_SIGNED)
67 + if(CMAKE_SIZEOF_VOID_P EQUAL 4)
68 + # 32 bit, only look out for signedness
69 + if(MALLINFO_ARENA_SIGNED)
70 + set(DEFAULT_MALLINFO_FIELD_TYPE int)
72 + set(DEFAULT_MALLINFO_FIELD_TYPE size_t)
75 + set(MALLINFO_USABLE TRUE)
76 + elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
77 + # 64 bit, more complicated...
78 + if(SIZEOF_MALLINFO_ARENA EQUAL ${SIZEOF_INT})
80 + if(MALLINFO_ARENA_SIGNED)
81 + set(DEFAULT_MALLINFO_FIELD_TYPE int)
83 + set(DEFAULT_MALLINFO_FIELD_TYPE unsigned)
86 + set(MALLINFO_USABLE TRUE)
87 + elseif(SIZEOF_MALLINFO_ARENA EQUAL ${SIZEOF_SIZE_T})
88 + # it is of long int size (i.e. size_t)
89 + if(MALLINFO_ARENA_SIGNED)
90 + set(DEFAULT_MALLINFO_FIELD_TYPE "long int")
92 + set(DEFAULT_MALLINFO_FIELD_TYPE size_t)
95 + set(MALLINFO_USABLE TRUE)
99 + # was above code succesful?
100 + if(NOT MALLINFO_USABLE)
101 + set(HAVE_MALLINFO FALSE CACHE BOOL "Failed to run simple code using mallinfo." FORCE)
102 + message(STATUS "mallinfo not usable")
105 +if(NOT DEFINED DEFAULT_MALLINFO_FIELD_TYPE)
107 + set(DEFAULT_MALLINFO_FIELD_TYPE size_t)
109 +# let the user override
110 +set(MALLINFO_FIELD_TYPE "${DEFAULT_MALLINFO_FIELD_TYPE}"
111 + CACHE STRING "Type of the fields in the mallinfo struct")
112 +mark_as_advanced(MALLINFO_FIELD_TYPE)
114 +configure_file("${GKLIB_PATH}/gk_config.h.in" "${PROJECT_BINARY_DIR}/gk_config.h")
116 +include_directories("${PROJECT_BINARY_DIR}")
117 diff --git a/GKlib/gk_config.h.in b/GKlib/gk_config.h.in
119 index 0000000..00cd2de
121 +++ b/GKlib/gk_config.h.in
123 +/* AUTOMATICALLY GENERATED. DO NOT EDIT! */
125 +#ifndef _GK_CONFIG_H
126 +#define _GK_CONFIG_H
128 +/* MALLINFO_FIELD_TYPE default: size_t
129 + * The type of the fields in the mallinfo struct. This was originally
130 + * defined as "int" in SVID etc, but is more usefully defined as
131 + * size_t. The value is used only if HAVE_MALLINFO is not set
133 +#define MALLINFO_FIELD_TYPE @MALLINFO_FIELD_TYPE@
135 +/* This version of malloc supports the standard SVID/XPG mallinfo
136 + routine that returns a struct containing usage properties and
137 + statistics. It should work on any system that has a
138 + /usr/include/malloc.h defining struct mallinfo. The main
139 + declaration needed is the mallinfo struct that is returned (by-copy)
140 + by mallinfo(). The malloinfo struct contains a bunch of fields that
141 + are not even meaningful in this version of malloc. These fields are
142 + are instead filled by mallinfo() with other numbers that might be of
145 + HAVE_MALLINFO should be set if you have a /usr/include/malloc.h file that
146 + includes a declaration of struct mallinfo. If so, it is included; else a
147 + compliant version is declared below. These must be precisely the same for
148 + mallinfo() to work. The original SVID version of this struct, defined on
149 + most systems with mallinfo, declares all fields as ints. But some others
150 + define as unsigned long. If your system defines the fields using a type of
151 + different width than listed here, you MUST #include your system version and
152 + #define HAVE_MALLINFO.
154 +#cmakedefine HAVE_MALLINFO
156 +#endif // _GK_CONFIG_H
157 diff --git a/GKlib/gk_dlmalloc.h b/GKlib/gk_dlmalloc.h
158 index a72b924..8006e1c 100644
159 --- a/GKlib/gk_dlmalloc.h
160 +++ b/GKlib/gk_dlmalloc.h
162 \version\verbatim $Id: gk_dlmalloc.h 1711 2007-05-18 17:05:27Z karypis $\endverbatim
165 +#include "gk_config.h"
169 /**********************************************************************
170 @@ -692,9 +694,9 @@ MAX_RELEASE_CHECK_RATE default: 255 unless not HAVE_MMAP
172 /* #define HAVE_USR_INCLUDE_MALLOC_H */
174 -#ifdef HAVE_USR_INCLUDE_MALLOC_H
175 -#include "/usr/include/malloc.h"
176 -#else /* HAVE_USR_INCLUDE_MALLOC_H */
177 +#ifdef HAVE_MALLINFO
179 +#else /* HAVE_MALLINFO */
182 MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
183 @@ -709,8 +711,7 @@ struct mallinfo {
184 MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
187 -#endif /* HAVE_USR_INCLUDE_MALLOC_H */
188 +#endif /* HAVE_MALLINFO */
189 #endif /* NO_MALLINFO */