Mercurial > dovecot > original-hg > dovecot-1.2
changeset 4720:b0daeec3d416 HEAD
Use malloc attribute for the most commonly used memory and string allocation
functions. Use warn_unused_result attribute for reallocs.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 01 Nov 2006 21:19:35 +0200 |
parents | 1995cb3763db |
children | 688c9c14b20d |
files | src/lib/data-stack.h src/lib/imem.h src/lib/macros.h src/lib/mempool.h src/lib/strfuncs.h |
diffstat | 5 files changed, 55 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/data-stack.h Wed Nov 01 20:47:37 2006 +0200 +++ b/src/lib/data-stack.h Wed Nov 01 21:19:35 2006 +0200 @@ -53,8 +53,8 @@ t_malloc() calls never fail, but return NULL if size == 0. If there's not enough memory left, i_panic() will be called. */ -void *t_malloc(size_t size); -void *t_malloc0(size_t size); +void *t_malloc(size_t size) __attr_malloc__; +void *t_malloc0(size_t size) __attr_malloc__; /* Try growing allocated memory. Returns TRUE if successful. Works only for last allocated memory in current stack frame. */
--- a/src/lib/imem.h Wed Nov 01 20:47:37 2006 +0200 +++ b/src/lib/imem.h Wed Nov 01 21:19:35 2006 +0200 @@ -7,21 +7,26 @@ #define i_new(type, count) p_new(default_pool, type, count) -void *i_malloc(size_t size); -void *i_realloc(void *mem, size_t old_size, size_t new_size); +void *i_malloc(size_t size) __attr_malloc__; +void *i_realloc(void *mem, size_t old_size, size_t new_size) + __attr_warn_unused_result__; #define i_free(mem) p_free(default_pool, mem) #define i_free_and_null(mem) p_free_and_null(default_pool, mem) /* string functions */ -char *i_strdup(const char *str); -char *i_strdup_empty(const char *str); /* like i_strdup(), but if str == "", return NULL */ -char *i_strdup_until(const void *str, const void *end); /* *end isn't included */ -char *i_strndup(const void *str, size_t max_chars); -char *i_strdup_printf(const char *format, ...) __attr_format__(1, 2); -char *i_strdup_vprintf(const char *format, va_list args) __attr_format__(1, 0); +char *i_strdup(const char *str) __attr_malloc__; +/* like i_strdup(), but if str == "", return NULL */ +char *i_strdup_empty(const char *str) __attr_malloc__; +/* *end isn't included */ +char *i_strdup_until(const void *str, const void *end) __attr_malloc__; +char *i_strndup(const void *str, size_t max_chars) __attr_malloc__; +char *i_strdup_printf(const char *format, ...) + __attr_format__(1, 2) __attr_malloc__; +char *i_strdup_vprintf(const char *format, va_list args) + __attr_format__(1, 0) __attr_malloc__; -char *i_strconcat(const char *str1, ...) __attr_sentinel__; +char *i_strconcat(const char *str1, ...) __attr_sentinel__ __attr_malloc__; void imem_init(void);
--- a/src/lib/macros.h Wed Nov 01 20:47:37 2006 +0200 +++ b/src/lib/macros.h Wed Nov 01 21:19:35 2006 +0200 @@ -102,6 +102,8 @@ # define __attr_unused__ __attribute__((unused)) # define __attr_noreturn__ __attribute__((noreturn)) # define __attr_const__ __attribute__((const)) +# define __attr_malloc__ __attribute__((malloc)) +# define __attr_warn_unused_result__ __attribute__((warn_unused_result)) # if __GNUC__ > 3 /* GCC 4.0 and later */ # define __attr_sentinel__ __attribute__((sentinel))
--- a/src/lib/mempool.h Wed Nov 01 20:47:37 2006 +0200 +++ b/src/lib/mempool.h Wed Nov 01 21:19:35 2006 +0200 @@ -20,7 +20,8 @@ /* memory in old_size..new_size will be zeroed */ void *(*realloc)(pool_t pool, void *mem, - size_t old_size, size_t new_size); + size_t old_size, size_t new_size) + __attr_warn_unused_result__; /* Frees all the memory in pool. NOTE: system_pool doesn't support this and crashes if it's used */
--- a/src/lib/strfuncs.h Wed Nov 01 20:47:37 2006 +0200 +++ b/src/lib/strfuncs.h Wed Nov 01 21:19:35 2006 +0200 @@ -12,26 +12,34 @@ int i_snprintf(char *dest, size_t max_chars, const char *format, ...) __attr_format__(3, 4); -char *p_strdup(pool_t pool, const char *str); -char *p_strdup_empty(pool_t pool, const char *str); /* return NULL if str = "" */ -char *p_strdup_until(pool_t pool, const void *start, const void *end); /* *end isn't included */ -char *p_strndup(pool_t pool, const void *str, size_t max_chars); +char *p_strdup(pool_t pool, const char *str) __attr_malloc__; +/* return NULL if str = "" */ +char *p_strdup_empty(pool_t pool, const char *str) __attr_malloc__; +/* *end isn't included */ +char *p_strdup_until(pool_t pool, const void *start, const void *end) + __attr_malloc__; +char *p_strndup(pool_t pool, const void *str, size_t max_chars) __attr_malloc__; char *p_strdup_printf(pool_t pool, const char *format, ...) - __attr_format__(2, 3); + __attr_format__(2, 3) __attr_malloc__; char *p_strdup_vprintf(pool_t pool, const char *format, va_list args) - __attr_format__(2, 0); -char *p_strconcat(pool_t pool, const char *str1, ...) __attr_sentinel__; + __attr_format__(2, 0) __attr_malloc__; +char *p_strconcat(pool_t pool, const char *str1, ...) + __attr_sentinel__ __attr_malloc__; /* same with temporary memory allocations: */ -const char *t_strdup(const char *str); -char *t_strdup_noconst(const char *str); -const char *t_strdup_empty(const char *str); /* return NULL if str = "" */ -const char *t_strdup_until(const void *start, const void *end); /* *end isn't included */ -const char *t_strndup(const void *str, size_t max_chars); -const char *t_strdup_printf(const char *format, ...) __attr_format__(1, 2); +const char *t_strdup(const char *str) __attr_malloc__; +char *t_strdup_noconst(const char *str) __attr_malloc__; +/* return NULL if str = "" */ +const char *t_strdup_empty(const char *str) __attr_malloc__; +/* *end isn't included */ +const char *t_strdup_until(const void *start, const void *end) __attr_malloc__; +const char *t_strndup(const void *str, size_t max_chars) __attr_malloc__; +const char *t_strdup_printf(const char *format, ...) + __attr_format__(1, 2) __attr_malloc__; const char *t_strdup_vprintf(const char *format, va_list args) - __attr_format__(1, 0); -const char *t_strconcat(const char *str1, ...) __attr_sentinel__; + __attr_format__(1, 0) __attr_malloc__; +const char *t_strconcat(const char *str1, ...) + __attr_sentinel__ __attr_malloc__; /* Like t_strdup(), but stop at cutchar. */ const char *t_strcut(const char *str, char cutchar); @@ -63,12 +71,16 @@ int strcasecmp_p(const void *p1, const void *p2); /* separators is an array of separator characters, not a separator string. */ -char **p_strsplit(pool_t pool, const char *data, const char *separators); -const char **t_strsplit(const char *data, const char *separators); +char **p_strsplit(pool_t pool, const char *data, const char *separators) + __attr_malloc__; +const char **t_strsplit(const char *data, const char *separators) + __attr_malloc__; /* like p_strsplit(), but treats multiple adjacent separators as a single separator. */ -char **p_strsplit_spaces(pool_t pool, const char *data, const char *separators); -const char **t_strsplit_spaces(const char *data, const char *separators); +char **p_strsplit_spaces(pool_t pool, const char *data, const char *separators) + __attr_malloc__; +const char **t_strsplit_spaces(const char *data, const char *separators) + __attr_malloc__; void p_strsplit_free(pool_t pool, char **arr); const char *dec2str(uintmax_t number); @@ -76,11 +88,13 @@ /* Return length of NULL-terminated list string array */ unsigned int strarray_length(const char *const *arr); /* Return all strings from array joined into one string. */ -const char *t_strarray_join(const char *const *arr, const char *separator); +const char *t_strarray_join(const char *const *arr, const char *separator) + __attr_malloc__; /* Removes a value from NULL-terminated string array. Returns TRUE if found. */ bool strarray_remove(const char **arr, const char *value); /* INTERNAL */ -char *_vstrconcat(const char *str1, va_list args, size_t *ret_len); +char *_vstrconcat(const char *str1, va_list args, size_t *ret_len) + __attr_malloc__; #endif