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  ccl_array_resize(&(_a_).size, sizeof ((_a_).data[0]), \
82  (void **) &((_a_).data), \
83  _sz_, 0)
84 
87 # define ccl_array_ensure_size_plus_one(_a_) \
88  ccl_array_ensure_size (_a_, (_a_).size + 1)
89 
93 # define ccl_array_trim(_a_,_sz_) \
94  ccl_array_resize (&(_a_).size, sizeof ((_a_).data[0]), \
95  (void **) &((_a_).data), \
96  _sz_, 1)
97 
101 # define ccl_array_trim_to_size(_a_) ccl_array_trim (_a_, (_a_).size)
102 
103 extern void
104 ccl_array_resize (int *poldsz, int elsz, void **data, int newsz, int trim);
105 
106 END_C_DECLS
107 
108 #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.