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 # if 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 test
58 # else
59 # define ccl_malloc(sz) ccl_real_malloc(sz)
60 # define ccl_calloc(nb,sz) ccl_real_calloc(nb,sz)
61 # define ccl_realloc(p,sz) ccl_real_realloc(p,sz)
62 # define ccl_free(p) ccl_real_free(p)
63 # endif
64 
65 # define CCL_DECLARE_DELETE_PROC(_id) extern void _id (void *p)
66 # define CCL_DEFINE_DELETE_PROC(_id) void _id (void *p) { ccl_delete (p); }
67 
71 # define ccl_new_array(_type_,_sz_) \
72  ((_type_ *)ccl_calloc (_sz_, sizeof(_type_)))
73 
78 # define ccl_new(_type_) ccl_new_array (_type_, 1)
79 
83 # define ccl_delete(_ptr_) ccl_free (_ptr_)
84 
88 # define ccl_zdelete(_del,_ptr_) \
89  do \
90  { \
91  if ((_ptr_) != NULL) \
92  _del (_ptr_); \
93  } \
94  while(0)
95 
99 # define ccl_memzero(_p,_size) ccl_memset (_p, 0, _size)
100 
105 # define ccl_memset(_p,_c,_size) memset (_p, _c, _size)
106 
110 # define ccl_memcpy(_dst,_src,_size) memcpy (_dst, _src, _size)
111 
116 # define ccl_memcmp(_p1,_p2,_size) memcmp (_p1, _p2, _size)
117 
118 extern void *
119 ccl_real_malloc (size_t size);
120 extern void
121 ccl_real_free (void *ptr);
122 extern void *
123 ccl_real_realloc (void *ptr, size_t size);
124 extern void *
125 ccl_real_calloc (size_t nb_el, size_t el_size);
126 
127 extern void *
128 ccl_debug_malloc (size_t size, const char *filename, int line);
129 extern void
130 ccl_debug_free (void *ptr, const char *filename, int line);
131 extern void *
132 ccl_debug_realloc (void *ptr, size_t size, const char *filename, int line);
133 extern void *
134 ccl_debug_calloc (size_t nb_el, size_t el_size, const char *filename,
135  int line);
136 extern void
137 ccl_debug_log_statistics_per_module (void);
138 
139 END_C_DECLS
140 
141 #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.