1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef _Common_memory_H
9 #define _Common_memory_H
15 /** If a C compiler other than GCC is used the macro below will substitute all
16 * GCC-specific non-standard attributes with an empty string. */
18 #define __attribute__(arg)
27 * \defgroup mem Memory management and expstring functions
28 * \brief Functions for memory management and string handling.
29 * \author Janos Zoltan Szabo <Janos.Zoltan.Szabo@ericsson.com>
31 * Primarily used in the TTCN-3 Test Executor (TTCN-3, ASN.1 compilers and
32 * runtime environment).
37 * Same as the standard \c malloc(), but it never returns NULL.
38 * It increases a malloc counter. If there is not enough memory,
39 * it calls \c fatal_error(), which exits the application.
41 * @param size number of bytes to allocate
42 * @return pointer to the beginning of the allocated memory, or
43 * NULL if and only if \c size is 0
45 extern void *Malloc(size_t size
);
47 extern void *Malloc_dbg(const char *filename
, int line
, size_t size
);
48 #define Malloc(s) Malloc_dbg(__FILE__, __LINE__, s)
52 * Same as the standard \c realloc(), but it never returns NULL if \a size is
53 * positive. It updates the malloc or free counters if necessary.
54 * Exits if there is not enough memory.
56 * @param ptr pointer to a memory block allocated by Malloc(). If \p ptr
57 * is NULL, calls Malloc(size)
58 * @param size new size for the memory block. If \p size is 0, it calls
59 * Free(ptr) and returns NULL.
60 * @return pointer to the beginning of the re-allocated memory.
61 * Will only be NULL if size==0.
63 extern void *Realloc(void *ptr
, size_t size
);
65 extern void *Realloc_dbg(const char *filename
, int line
, void *ptr
, size_t size
);
66 #define Realloc(p,s) Realloc_dbg(__FILE__, __LINE__, p, s)
70 * Same as the standard \c free(). It increases the free counter if \a ptr
73 * @param ptr pointer to a memory block allocated by Malloc(). If \p ptr
74 * is NULL, this function does nothing.
76 extern void Free(void *ptr
);
78 extern void Free_dbg(const char *filename
, int line
, void *ptr
);
79 #define Free(p) Free_dbg(__FILE__, __LINE__, p)
83 * Prints a warning message to stderr if the malloc and free counters
84 * are not equals. It shall be called immediately before the end of
87 extern void check_mem_leak(const char *program_name
);
91 * Checks all allocated blocks of the program. Prints an error message and
92 * aborts if memory over-indexing is detected.
94 extern void check_mem_corrupt(const char *program_name
);
96 #ifdef MEMORY_DEBUG_ADDRESS
97 /** @brief Memory allocation checkpoint.
99 If this variable is set to a nonzero value, allocations starting
100 at that address will be logged to stderr.
102 This can be used in conjunction with the output of check_mem_leak().
104 extern void * memory_debug_address
;
109 * Character string type with exponential buffer allocation. The size of
110 * allocated buffer is always a power of 2. The terminating '\\0' character
111 * is always present and the remaining bytes in the buffer are also set to
112 * zero. This allows binary search for the end of string, which is
113 * significantly faster than the linear one especially for long strings.
114 * The spare bytes at the end make appending of small chunks very efficient.
116 * \warning If a function takes expstring_t as argument
117 * you should not pass a regular string (or a static string literal)
118 * to it. This may result in an unpredictable behaviour. You can
119 * convert any regular string to expstring_t using mcopystr:
120 * myexpstring = mcopystr(myregularstring);
122 typedef char *expstring_t
;
125 * mprintf() takes its arguments like \c printf() and prints according
126 * to the format string \a fmt into a string buffer. It allocates
127 * enough memory for the resulting string and returns the pointer
128 * to the result string. The result string is an exponential string.
129 * mprintf() never returns NULL.
131 extern expstring_t
mprintf(const char *fmt
, ...)
132 __attribute__ ((__format__ (__printf__
, 1, 2)));
134 extern expstring_t
mprintf_dbg(const char *filename
, int line
, const char *fmt
, ...)
135 __attribute__ ((__format__ (__printf__
, 3, 4)));
136 #if defined(__GNUC__) && __GNUC__ < 3
137 # define mprintf(f, args...) mprintf_dbg(__FILE__, __LINE__, f, ## args)
139 # define mprintf(f,...) mprintf_dbg(__FILE__, __LINE__, f, __VA_ARGS__)
144 * The same as \a mprintf(), but it takes the arguments as va_list.
145 * It is useful in wrapper functions with printf style argument strings.
147 extern expstring_t
mprintf_va_list(const char *fmt
, va_list pvar
);
149 extern expstring_t
mprintf_va_list_dbg(const char *filename
, int line
,
150 const char *fmt
, va_list pvar
);
151 #define mprintf_va_list(f,v) mprintf_va_list_dbg(__FILE__, __LINE__, f, v)
155 * mputprintf() prints its additional arguments according to the
156 * format string \a fmt at the end of \a str. The buffer of \a str is
157 * increased if the appended bytes do not fit in it. The result
158 * string, which is also an expstring, is returned.
159 * mputprintf() never returns NULL.
160 * \note If str is NULL it is equivalent to \a mprintf().
161 * \warning The first argument must be an exponential string,
162 * otherwise its behaviour may be unpredictable.
164 extern expstring_t
mputprintf(expstring_t str
, const char *fmt
, ...)
165 __attribute__ ((__format__ (__printf__
, 2, 3)));
168 * The same as \a mputprintf(), but it takes the arguments as va_list.
169 * It is useful in wrapper functions with printf style argument strings.
171 extern expstring_t
mputprintf_va_list(expstring_t str
, const char *fmt
,
175 * memptystr() creates and returns a new empty exponential string.
176 * The returned value is never NULL,
177 * it shall be deallocated using \a Free().
179 extern expstring_t
memptystr(void);
181 extern expstring_t
memptystr_dbg(const char *filename
, int line
);
182 #define memptystr() memptystr_dbg(__FILE__, __LINE__)
187 * mcopystr() creates a new exponential string and copies the contents of
188 * \a str into it. The resulting expstring is returned.
189 * The regular string \a str will not be deallocated and it may be
190 * a static string literal.
191 * If \a str is NULL an empty exponential string is returned.
192 * mcopystr() never returns NULL.
194 extern expstring_t
mcopystr(const char *str
);
196 extern expstring_t
mcopystr_dbg(const char *filename
, int line
, const char *str
);
197 #define mcopystr(s) mcopystr_dbg(__FILE__, __LINE__, s)
201 * Create a new exponential string when the length is known.
202 * Works exactly like mcopystr(), except the length is not measured;
203 * the given length is used instead.
205 * @param str pointer to the original string; does not need to be 0-terminated
206 * @param len number of characters to copy
207 * @return the newly constructed string (it needs to be Free()-d)
209 extern expstring_t
mcopystrn(const char *str
, size_t len
);
211 extern expstring_t
mcopystrn_dbg(const char *filename
, int line
, const char *str
,
213 #define mcopystrn(s, len) mcopystrn_dbg(__FILE__, __LINE__, s, len)
217 * mputstr() appends the regular string \a str2 to the end of
218 * expstring \a str. The resulting expstring is returned.
219 * The buffer of \a str is increased if necessary.
220 * If \a str is NULL then \a str2 is copied into a new exponential string
221 * (i.e. mputstr(NULL, str) is identical to mcopystr(str)).
222 * If \a str2 is NULL then \a str is returned and remains unchanged
223 * (i.e. mputstr(str, NULL) is identical to str,
224 * mputstr(NULL, NULL) always returns NULL).
225 * \warning The first argument must be an exponential string,
226 * otherwise its behaviour may be unpredictable.
228 extern expstring_t
mputstr(expstring_t str
, const char *str2
);
230 /** Appends \a len2 characters from the regular string \a str2 to the end of
231 * expstring \a str. The resulting expstring is returned. @see mputstr()
232 * @param str destination string
233 * @param str2 pointer to characters; does not need to be 0-terminated
234 * @param len2 number of characters to copy
235 * @return the (possibly reallocated) str
237 extern expstring_t
mputstrn(expstring_t str
, const char *str2
, size_t len2
);
240 * mputc() appends the single character \a c to the end of
241 * expstring \a str. The buffer of \a str is increased if necessary.
242 * The resulting expstring is returned.
243 * If \a str is NULL then \a c is converted to a new exponential string.
244 * If \a c is '\\0' then \a str is returned.
245 * mputc() never returns NULL.
246 * \warning The first argument must be an exponential string,
247 * otherwise its behaviour may be unpredictable.
249 extern expstring_t
mputc(expstring_t str
, char c
);
252 * mtruncstr() truncates the expstring \a str by keeping only the first
253 * \a newlen characters and returns the resulting string.
254 * If the string is shorter than \a newlen it remains unchanged.
255 * mtruncstr() may perform memory reallocation if necessary.
256 * If \a str is NULL then a NULL pointer is returned.
257 * If \a str is not an exponential string the behaviour of mtruncstr() may
260 extern expstring_t
mtruncstr(expstring_t str
, size_t newlen
);
263 * mstrlen() returns the length of expstring \a str or zero if \a str is
264 * NULL. If \a str is not NULL the function has identical result as libc's
265 * strlen(), but operates significantly faster. The behaviour may be
266 * unpredictable if \a str is not an exponential string.
268 extern size_t mstrlen(const expstring_t str
);
270 /** @} end of mem group */
272 /** Return the string for the build number.
274 * @param b build number.
275 * @return a string which must be Free()-d by the caller
276 * @pre b > 0 and b <= 99, or else NULL is returned
278 char * buildstr(int b
);
281 /** Convert a patch level to the "Ericsson letter" */
282 inline char eri(unsigned int p
) { /* p stands for patch level */
283 char i
= (char)('A' + p
); /* i stands for "if only it was that simple" */
284 return i
+ (i
>= 'I') + 4 * (i
>= 'N') + (i
>= 'R');
291 #endif /* _Common_memory_H */
This page took 0.063017 seconds and 5 git commands to generate.