use gcc attributes for BLI alloc functions

This commit is contained in:
Campbell Barton 2012-07-14 12:47:49 +00:00
parent ac8c56c6fc
commit 98520ce4de
4 changed files with 98 additions and 32 deletions

@ -60,8 +60,8 @@
#ifndef __MEM_GUARDEDALLOC_H__
#define __MEM_GUARDEDALLOC_H__
#include <stdio.h> /* needed for FILE* */
#include "MEM_sys_types.h" /* needed for uintptr_t */
#include <stdio.h> /* needed for FILE* */
#include "MEM_sys_types.h" /* needed for uintptr_t */
#ifdef __cplusplus
extern "C" {
@ -70,7 +70,7 @@ extern "C" {
/** Returns the length of the allocated memory segment pointed at
* by vmemh. If the pointer was not previously allocated by this
* module, the result is undefined.*/
size_t MEM_allocN_len(void *vmemh)
size_t MEM_allocN_len(const void *vmemh)
#ifdef __GNUC__
__attribute__((warn_unused_result))
#endif
@ -111,10 +111,10 @@ extern "C" {
* Allocate a block of memory of size len, with tag name str. The
* memory is cleared. The name must be static, because only a
* pointer to it is stored ! */
void *MEM_callocN(size_t len, const char * str)
void *MEM_callocN(size_t len, const char *str)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull))
__attribute__((nonnull(2)))
__attribute__((alloc_size(1)))
#endif
;
@ -122,10 +122,10 @@ extern "C" {
/** Allocate a block of memory of size len, with tag name str. The
* name must be a static, because only a pointer to it is stored !
* */
void *MEM_mallocN(size_t len, const char * str)
void *MEM_mallocN(size_t len, const char *str)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull))
__attribute__((nonnull(2)))
__attribute__((alloc_size(1)))
#endif
;
@ -133,10 +133,10 @@ extern "C" {
/** Same as callocN, clears memory and uses mmap (disk cached) if supported.
* Can be free'd with MEM_freeN as usual.
* */
void *MEM_mapallocN(size_t len, const char * str)
void *MEM_mapallocN(size_t len, const char *str)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull))
__attribute__((nonnull(2)))
__attribute__((alloc_size(1)))
#endif
;
@ -213,11 +213,10 @@ public: \
MEM_freeN(mem); \
} \
#endif
#endif /* __cplusplus */
#ifdef __cplusplus
}
#endif
#endif /* __cplusplus */
#endif
#endif /* __MEM_GUARDEDALLOC_H__ */

@ -222,10 +222,10 @@ void MEM_set_memory_debug(void)
malloc_debug_memset = 1;
}
size_t MEM_allocN_len(void *vmemh)
size_t MEM_allocN_len(const void *vmemh)
{
if (vmemh) {
MemHead *memh = vmemh;
const MemHead *memh = vmemh;
memh--;
return memh->len;

@ -50,16 +50,43 @@ extern "C" {
struct MemArena;
typedef struct MemArena MemArena;
struct MemArena *BLI_memarena_new(const int bufsize, const char *name)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull(2)))
#endif
;
struct MemArena *BLI_memarena_new(int bufsize, const char *name);
void BLI_memarena_free(struct MemArena *ma);
void BLI_memarena_free(struct MemArena *ma)
#ifdef __GNUC__
__attribute__((nonnull(1)))
#endif
;
void BLI_memarena_use_malloc(struct MemArena *ma);
void BLI_memarena_use_calloc(struct MemArena *ma);
void BLI_memarena_use_malloc(struct MemArena *ma)
#ifdef __GNUC__
__attribute__((nonnull(1)))
#endif
;
void BLI_memarena_use_calloc(struct MemArena *ma)
#ifdef __GNUC__
__attribute__((nonnull(1)))
#endif
;
void BLI_memarena_use_align(struct MemArena *ma, int align);
void BLI_memarena_use_align(struct MemArena *ma, const int align)
#ifdef __GNUC__
__attribute__((nonnull(1)))
#endif
;
void *BLI_memarena_alloc(struct MemArena *ma, int size);
void *BLI_memarena_alloc(struct MemArena *ma, int size)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull(1)))
__attribute__((alloc_size(2)))
#endif
;
#ifdef __cplusplus
}

@ -48,16 +48,47 @@ typedef struct BLI_mempool BLI_mempool;
* first four bytes of the elements never contain the character string
* 'free'. use with care.*/
BLI_mempool *BLI_mempool_create(int esize, int totelem, int pchunk, int flag);
void *BLI_mempool_alloc(BLI_mempool *pool);
void *BLI_mempool_calloc(BLI_mempool *pool);
void BLI_mempool_free(BLI_mempool *pool, void *addr);
void BLI_mempool_destroy(BLI_mempool *pool);
int BLI_mempool_count(BLI_mempool *pool);
void *BLI_mempool_findelem(BLI_mempool *pool, int index);
BLI_mempool *BLI_mempool_create(int esize, int totelem, int pchunk, int flag)
#ifdef __GNUC__
__attribute__((warn_unused_result))
#endif
;
void *BLI_mempool_alloc(BLI_mempool *pool)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull(1)))
#endif
;
void *BLI_mempool_calloc(BLI_mempool *pool)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull(1)))
#endif
;
void BLI_mempool_free(BLI_mempool *pool, void *addr)
#ifdef __GNUC__
__attribute__((nonnull(1, 2)))
#endif
;
void BLI_mempool_destroy(BLI_mempool *pool)
#ifdef __GNUC__
__attribute__((nonnull(1)))
#endif
;
int BLI_mempool_count(BLI_mempool *pool)
#ifdef __GNUC__
__attribute__((nonnull(1)))
#endif
;
void *BLI_mempool_findelem(BLI_mempool *pool, int index)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull(1)))
#endif
;
/** iteration stuff. note: this may easy to produce bugs with **/
/*private structure*/
/* private structure */
typedef struct BLI_mempool_iter {
BLI_mempool *pool;
struct BLI_mempool_chunk *curchunk;
@ -70,11 +101,20 @@ enum {
BLI_MEMPOOL_ALLOW_ITER = (1 << 1)
};
void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter);
void *BLI_mempool_iterstep(BLI_mempool_iter *iter);
void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter)
#ifdef __GNUC__
__attribute__((nonnull(1, 2)))
#endif
;
void *BLI_mempool_iterstep(BLI_mempool_iter *iter)
#ifdef __GNUC__
__attribute__((warn_unused_result))
__attribute__((nonnull(1)))
#endif
;
#ifdef __cplusplus
}
#endif
#endif
#endif /* __BLI_MEMPOOL_H__ */