TaPAS  0.2
ccl-memory.h
Go to the documentation of this file.
1 /*
2  * ccl-memory.h -- Memory allocators
3  *
4  * This file is a part of the C Common Library (CCL) project.
5  *
6  * Copyright (C) 2010 CNRS UMR 5800 & Université Bordeaux I (see AUTHORS file).
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the AltaRica Public License that comes with this
10  * package.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  */
16 
22 #ifndef __CCL_MEMORY_H__
23 # define __CCL_MEMORY_H__
24 
25 # ifdef HAVE_CONFIG_H
26 # include <config.h>
27 # endif
28 # include <ccl/ccl-common.h>
29 # include <ccl/ccl-exception.h>
30 
31 BEGIN_C_DECLS
32 
33 typedef void ccl_memory_exhaustion_handler (void *data);
34 
38 CCL_DECLARE_EXCEPTION (memory_exhausted_exception, runtime_exception);
39 
40 extern void
41 ccl_memory_add_exhaustion_handler (ccl_memory_exhaustion_handler *hdl,
42  void *data);
43 
44 extern void
45 ccl_memory_del_exhaustion_handler (ccl_memory_exhaustion_handler *hdl,
46  void *data);
47 
52 # ifdef CCL_DEBUG_MEMORY
53 # define ccl_malloc(sz) ccl_debug_malloc(sz,__FILE__,__LINE__)
54 # define ccl_calloc(nb,sz) ccl_debug_calloc(nb,sz,__FILE__,__LINE__)
55 # define ccl_realloc(p,sz) ccl_debug_realloc(p,sz,__FILE__,__LINE__)
56 # define ccl_free(p) ccl_debug_free(p,__FILE__,__LINE__)
57 # else
58 # define ccl_malloc(sz) ccl_real_malloc(sz)
59 # define ccl_calloc(nb,sz) ccl_real_calloc(nb,sz)
60 # define ccl_realloc(p,sz) ccl_real_realloc(p,sz)
61 # define ccl_free(p) ccl_real_free(p)
62 # endif
63 
64 # define CCL_DECLARE_DELETE_PROC(_id) extern void _id (void *p)
65 # define CCL_DEFINE_DELETE_PROC(_id) void _id (void *p) { ccl_delete (p); }
66 
70 # define ccl_new_array(_type_,_sz_) \
71  ((_type_ *)ccl_calloc (_sz_, sizeof(_type_)))
72 
77 # define ccl_new(_type_) ccl_new_array (_type_, 1)
78 
82 # define ccl_delete(_ptr_) ccl_free (_ptr_)
83 
87 # define ccl_zdelete(_del,_ptr_) \
88  do \
89  { \
90  if ((_ptr_) != NULL) \
91  _del (_ptr_); \
92  } \
93  while(0)
94 
98 # define ccl_memzero(_p,_size) ccl_memset (_p, 0, _size)
99 
104 # define ccl_memset(_p,_c,_size) memset (_p, _c, _size)
105 
109 # define ccl_memcpy(_dst,_src,_size) memcpy (_dst, _src, _size)
110 
115 # define ccl_memcmp(_p1,_p2,_size) memcmp (_p1, _p2, _size)
116 
117 extern void *
118 ccl_real_malloc (size_t size);
119 extern void
120 ccl_real_free (void *ptr);
121 extern void *
122 ccl_real_realloc (void *ptr, size_t size);
123 extern void *
124 ccl_real_calloc (size_t nb_el, size_t el_size);
125 
126 extern void *
127 ccl_debug_malloc (size_t size, const char *filename, int line);
128 extern void
129 ccl_debug_free (void *ptr, const char *filename, int line);
130 extern void *
131 ccl_debug_realloc (void *ptr, size_t size, const char *filename, int line);
132 extern void *
133 ccl_debug_calloc (size_t nb_el, size_t el_size, const char *filename,
134  int line);
135 extern void
136 ccl_debug_log_statistics_per_module (void);
137 
138 END_C_DECLS
139 
140 #endif /* ! __CCL_MEMORY_H__ */
An exception mechanism.
Some useful and common macros.
#define CCL_DECLARE_EXCEPTION(exc, super)
Macro-function used to declare an exception exc.