TaPAS  0.2
ccl-array.h
Go to the documentation of this file.
1 /*
2  * ccl-array.h -- A kind of template for dynamic arrays
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 
21 #ifndef __CCL_ARRAY_H__
22 # define __CCL_ARRAY_H__
23 
24 # include <ccl/ccl-common.h>
25 # include <ccl/ccl-memory.h>
26 
27 BEGIN_C_DECLS
28 
32 # define CCL_ARRAY(_t_) \
33  struct { \
34  int size; \
35  _t_ *data; \
36  }
37 
41 # define ccl_array_init(_a_) \
42  ccl_array_init_with_size(_a_, 0)
43 
44 
48 # define ccl_array_init_with_size(_a_,_sz_) \
49  do { \
50  (_a_).size = 0; (_a_).data = NULL; \
51  ccl_array_ensure_size (_a_, _sz_); \
52  } while(0)
53 
58 # define ccl_array_add(_a_,_el_) \
59  do { \
60  ccl_array_ensure_size (_a_, (_a_).size + 1); \
61  (_a_).data[(_a_).size-1] = _el_; \
62  } while(0)
63 
68 CCL_DECLARE_DELETE_PROC(ccl_array_delete_array);
69 # define ccl_array_delete(_a_) ccl_array_delete_array ((_a_).data)
70 # define ccl_array_clean_and_delete(_a_,_del_) \
71  do { \
72  while ((_a_).size--) _del_ ((_a_).data[(_a_).size]); \
73  ccl_array_delete ((_a_)); \
74  } while (0)
75 
80 # define ccl_array_ensure_size(_a_,_sz_) \
81  if ((_sz_) >= (_a_).size) \
82  ccl_array_resize(&(_a_).size, sizeof ((_a_).data[0]), \
83  (void **) &((_a_).data), \
84  (_sz_), 0)
85 
89 # define ccl_array_ensure_size_plus_one(_a_) \
90  ccl_array_ensure_size (_a_, (_a_).size + 1)
91 
95 # define ccl_array_trim(_a_,_sz_) \
96  ccl_array_resize (&(_a_).size, sizeof ((_a_).data[0]), \
97  (void **) &((_a_).data), \
98  _sz_, 1)
99 
103 # define ccl_array_trim_to_size(_a_) ccl_array_trim (_a_, (_a_).size)
104 
105 extern void
106 ccl_array_resize (int *poldsz, int elsz, void **data, int newsz, int trim);
107 
108 END_C_DECLS
109 
110 #endif /* ! __CCL_ARRAY_H__ */
CCL_DECLARE_DELETE_PROC(ccl_array_delete_array)
Deletes the memory allocated for a. Objects stored into the array are not deleted.
Some useful and common macros.
Memory Allocators.