1 dnl check whether mallinfo() is available and which fields to use to find out memory usage
2 dnl it's used in konq_mainwindow.cc
3 dnl warning, ugly code ahead
4 dnl some implementations have mallinfo() in stdlib.h, others in malloc.h
5 dnl fields showing memory usage should be hblkhd, uordblks and usmblks,
6 dnl different implementations use different combinations of these (which is ok),
7 dnl but some of them "reuse" fields they don't use for other purposes
9 AC_DEFUN([KDE_MALLINFO_CHECK],
11 AC_MSG_CHECKING([for mallinfo() in $1])
12 AC_CACHE_VAL(kde_cv_func_mallinfo_$2,
16 AC_TRY_COMPILE([#include $1],
26 kde_cv_func_mallinfo_$2=yes,
27 kde_cv_func_mallinfo_$2=no)
30 if test "$kde_cv_func_mallinfo_$2" = "yes"; then
33 AC_MSG_RESULT($kde_cv_func_mallinfo_$2)
37 AC_DEFUN([KDE_MALLINFO_CHECK_FIELD],
39 AC_MSG_CHECKING([whether to use mallinfo field $1])
40 AC_CACHE_VAL(kde_cv_func_mallinfo_field_$1,
47 #include <$kde_mallinfo_type.h>
48 enum use_type { No, Yes, Never };
49 use_type use_field = No;
50 const int SIZE = 4 * 1024 * 1024;
51 const int SMALL_SIZE = 4 * 1024;
53 void dif( struct mallinfo& m1, struct mallinfo& m2, bool alloc )
55 int difval = m2.$1 - m1.$1;
58 if( difval >= SIZE && difval < 2 * SIZE && use_field != Never )
64 if( difval > SMALL_SIZE && ( difval < SIZE || difval >= 2 * SIZE ) && use_field == Yes )
71 free( malloc( SIZE * 2 )); // avoid Doug Lea's malloc feature of having total_max in usmblks
72 struct mallinfo m1 = mallinfo();
73 void* mem1 = malloc( SIZE );
74 struct mallinfo m2 = mallinfo();
76 struct mallinfo m3 = mallinfo();
77 void* mem2[ SIZE / 32 ];
81 mem2[ i ] = malloc( 32 );
82 struct mallinfo m4 = mallinfo();
87 struct mallinfo m5 = mallinfo();
88 void* mem3[ SIZE / 1024 ];
92 mem3[ i ] = malloc( 1024 );
93 struct mallinfo m6 = mallinfo();
98 struct mallinfo m7 = mallinfo();
100 dif( m2, m3, false );
102 dif( m4, m5, false );
104 dif( m6, m7, false );
105 return use_field == Yes ? 0 : 1;
109 [kde_cv_func_mallinfo_field_$1=yes],
110 [kde_cv_func_mallinfo_field_$1=no],
111 [kde_cv_func_mallinfo_field_$1=no])
114 AC_MSG_RESULT($kde_cv_func_mallinfo_field_$1)
115 kde_mallinfo_$1=$kde_cv_func_mallinfo_field_$1
116 if test "$kde_mallinfo_$1" = "yes"; then
117 AC_DEFINE( KDE_MALLINFO_FIELD_$1, 1, [Use mallinfo field $1])
123 KDE_MALLINFO_CHECK([<stdlib.h>],stdlib)
124 if test -z "$kde_mallinfo_type"; then
125 KDE_MALLINFO_CHECK([<malloc.h>],malloc)
128 AC_TRY_LINK_FUNC(mallinfo,
130 [ AC_CHECK_LIB(malloc,mallinfo,
131 [ AC_SUBST(LIBMALLOC,"-lmalloc") ],
132 [ kde_mallinfo_type="" ])
135 if test -n "$kde_mallinfo_type"; then
136 KDE_MALLINFO_CHECK_FIELD(hblkhd)
137 KDE_MALLINFO_CHECK_FIELD(uordblks)
138 KDE_MALLINFO_CHECK_FIELD(usmblks)
139 if test -z "$kde_mallinfo_hblkhd" -a \
140 -z "$kde_mallinfo_uordblks" -a \
141 -z "$kde_mallinfo_usmblks"; then
147 if test "$kde_mallinfo_type" = "stdlib"; then
148 AC_DEFINE(KDE_MALLINFO_STDLIB, 1, [mallinfo() is available in <stdlib.h>])
149 elif test "$kde_mallinfo_type" = "malloc"; then
150 AC_DEFINE(KDE_MALLINFO_MALLOC, 1, [mallinfo() is available in <malloc.h>])