Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
kmp_wrapper_malloc.h
1 /*
2  * kmp_wrapper_malloc.h -- Wrappers for memory allocation routines
3  * (malloc(), free(), and others).
4  */
5 
6 /* <copyright>
7  Copyright (c) 1997-2015 Intel Corporation. All Rights Reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in the
17  documentation and/or other materials provided with the distribution.
18  * Neither the name of Intel Corporation nor the names of its
19  contributors may be used to endorse or promote products derived
20  from this software without specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 </copyright> */
35 
36 #ifndef KMP_WRAPPER_MALLOC_H
37 #define KMP_WRAPPER_MALLOC_H
38 
39 /*
40  This header serves for 3 purposes:
41 
42  1. Declaring standard memory allocation rourines in OS-independent way.
43  2. Passing source location info through memory allocation wrappers.
44  3. Enabling native memory debugging capabilities.
45 
46 
47  1. Declaring standard memory allocation rourines in OS-independent way.
48  -----------------------------------------------------------------------
49 
50  On Linux* OS, alloca() function is declared in <alloca.h> header, while on Windows* OS there is no
51  <alloca.h> header, function _alloca() (note underscore!) is declared in <malloc.h>. This header
52  eliminates these differences, so client code incluiding "kmp_wrapper_malloc.h" can rely on
53  following routines:
54 
55  malloc
56  calloc
57  realloc
58  free
59  alloca
60 
61  in OS-independent way. It also enables memory tracking capabilities in debug build. (Currently
62  it is available only on Windows* OS.)
63 
64 
65  2. Passing source location info through memory allocation wrappers.
66  -------------------------------------------------------------------
67 
68  Some tools may help debugging memory errors, for example, report memory leaks. However, memory
69  allocation wrappers may hinder source location.
70 
71  For example:
72 
73  void * aligned_malloc( int size ) {
74  void * ptr = malloc( size ); // All the memory leaks will be reported at this line.
75  // some adjustments...
76  return ptr;
77  };
78 
79  ptr = aligned_malloc( size ); // Memory leak will *not* be detected here. :-(
80 
81  To overcome the problem, information about original source location should be passed through all
82  the memory allocation wrappers, for example:
83 
84  void * aligned_malloc( int size, char const * file, int line ) {
85  void * ptr = _malloc_dbg( size, file, line );
86  // some adjustments...
87  return ptr;
88  };
89 
90  void * ptr = aligned_malloc( size, __FILE__, __LINE__ );
91 
92  This is a good idea for debug, but passing additional arguments impacts performance. Disabling
93  extra arguments in release version of the software introduces too many conditional compilation,
94  which makes code unreadable. This header defines few macros and functions facilitating it:
95 
96  void * _aligned_malloc( int size KMP_SRC_LOC_DECL ) {
97  void * ptr = malloc_src_loc( size KMP_SRC_LOC_PARM );
98  // some adjustments...
99  return ptr;
100  };
101  #define aligned_malloc( size ) _aligned_malloc( (size) KMP_SRC_LOC_CURR )
102  // Use macro instead of direct call to function.
103 
104  void * ptr = aligned_malloc( size ); // Bingo! Memory leak will be reported at this line.
105 
106 
107  3. Enabling native memory debugging capabilities.
108  -------------------------------------------------
109 
110  Some platforms may offer memory debugging capabilities. For example, debug version of Microsoft
111  RTL tracks all memory allocations and can report memory leaks. This header enables this, and
112  makes report more useful (see "Passing source location info through memory allocation
113  wrappers").
114 
115 */
116 
117 #include <stdlib.h>
118 
119 #include "kmp_os.h"
120 
121 // Include alloca() declaration.
122 #if KMP_OS_WINDOWS
123  #include <malloc.h> // Windows* OS: _alloca() declared in "malloc.h".
124  #define alloca _alloca // Allow to use alloca() with no underscore.
125 #elif KMP_OS_FREEBSD
126  // Declared in "stdlib.h".
127 #elif KMP_OS_UNIX
128  #include <alloca.h> // Linux* OS and OS X*: alloc() declared in "alloca".
129 #else
130  #error Unknown or unsupported OS.
131 #endif
132 
133 /*
134  KMP_SRC_LOC_DECL -- Declaring source location paramemters, to be used in function declaration.
135  KMP_SRC_LOC_PARM -- Source location paramemters, to be used to pass parameters to underlying
136  levels.
137  KMP_SRC_LOC_CURR -- Source location arguments describing current location, to be used at
138  top-level.
139 
140  Typical usage:
141 
142  void * _aligned_malloc( int size KMP_SRC_LOC_DECL ) {
143  // Note: Comma is missed before KMP_SRC_LOC_DECL.
144  KE_TRACE( 25, ( "called from %s:%d\n", KMP_SRC_LOC_PARM ) );
145  ...
146  }
147  #define aligned_malloc( size ) _aligned_malloc( (size) KMP_SRC_LOC_CURR )
148  // Use macro instead of direct call to function -- macro passes info about current
149  // source location to the func.
150 */
151 #if KMP_DEBUG
152  #define KMP_SRC_LOC_DECL , char const * _file_, int _line_
153  #define KMP_SRC_LOC_PARM , _file_, _line_
154  #define KMP_SRC_LOC_CURR , __FILE__, __LINE__
155 #else
156  #define KMP_SRC_LOC_DECL
157  #define KMP_SRC_LOC_PARM
158  #define KMP_SRC_LOC_CURR
159 #endif // KMP_DEBUG
160 
161 /*
162  malloc_src_loc() and free_src_loc() are pseudo-functions (really macros) with accepts extra
163  arguments (source location info) in debug mode. They should be used in place of malloc() and
164  free(), this allows enabling native memory debugging capabilities (if any).
165 
166  Typical usage:
167 
168  ptr = malloc_src_loc( size KMP_SRC_LOC_PARM );
169  // Inside memory allocation wrapper, or
170  ptr = malloc_src_loc( size KMP_SRC_LOC_CURR );
171  // Outside of memory allocation wrapper.
172 
173 
174 */
175 #define malloc_src_loc( args ) _malloc_src_loc( args )
176 #define free_src_loc( args ) _free_src_loc( args )
177  /*
178  Depending on build mode (debug or release), malloc_src_loc is declared with 1 or 3
179  parameters, but calls to malloc_src_loc() are always the same:
180 
181  ... malloc_src_loc( size KMP_SRC_LOC_PARM ); // or KMP_SRC_LOC_CURR
182 
183  Compiler issues warning/error "too few arguments in macro invocation". Declaring two
184  macroses, malloc_src_loc() and _malloc_src_loc() overcomes the problem.
185  */
186 
187 #if KMP_DEBUG
188 
189  #if KMP_OS_WINDOWS && _DEBUG
190  // KMP_DEBUG != _DEBUG. MS debug RTL is available only if _DEBUG is defined.
191 
192  // Windows* OS has native memory debugging capabilities. Enable them.
193 
194  #include <crtdbg.h>
195 
196  #define KMP_MEM_BLOCK _CLIENT_BLOCK
197  #define malloc( size ) _malloc_dbg( (size), KMP_MEM_BLOCK, __FILE__, __LINE__ )
198  #define calloc( num, size ) _calloc_dbg( (num), (size), KMP_MEM_BLOCK, __FILE__, __LINE__ )
199  #define realloc( ptr, size ) _realloc_dbg( (ptr), (size), KMP_MEM_BLOCK, __FILE__, __LINE__ )
200  #define free( ptr ) _free_dbg( (ptr), KMP_MEM_BLOCK )
201 
202  #define _malloc_src_loc( size, file, line ) _malloc_dbg( (size), KMP_MEM_BLOCK, (file), (line) )
203  #define _free_src_loc( ptr, file, line ) _free_dbg( (ptr), KMP_MEM_BLOCK )
204 
205  #else
206 
207  // Linux* OS, OS X*, or non-debug Windows* OS.
208 
209  #define _malloc_src_loc( size, file, line ) malloc( (size) )
210  #define _free_src_loc( ptr, file, line ) free( (ptr) )
211 
212  #endif
213 
214 #else
215 
216  // In release build malloc_src_loc() and free_src_loc() do not have extra parameters.
217  #define _malloc_src_loc( size ) malloc( (size) )
218  #define _free_src_loc( ptr ) free( (ptr) )
219 
220 #endif // KMP_DEBUG
221 
222 #endif // KMP_WRAPPER_MALLOC_H
223 
224 // end of file //