reprap: Delete mongoose
[simavr] / examples / board_reprap / src / c3 / c_array.h
1 /*
2         c_array.h
3
4         Copyright 2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of gcodepp.
7
8         gcodepp is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         gcodepp is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with gcodepp.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #ifndef __C_ARRAY_H___
24 #define __C_ARRAY_H___
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifndef C_ARRAY_INLINE
31 #define C_ARRAY_INLINE inline
32 #endif
33 #ifndef C_ARRAY_SIZE_TYPE
34 #define C_ARRAY_SIZE_TYPE uint32_t
35 #endif
36
37 #define DECLARE_C_ARRAY(__type, __name, __page) \
38 enum { __name##_page_size = __page }; \
39 typedef __type __name##_element_t; \
40 typedef C_ARRAY_SIZE_TYPE __name##_count_t; \
41 typedef struct __name##_t {\
42         volatile __name##_count_t count;\
43         volatile __name##_count_t size;\
44         __name##_element_t * e;\
45 } __name##_t, *__name##_p;
46
47 #define C_ARRAY_NULL { 0, 0, NULL }
48
49 #define IMPLEMENT_C_ARRAY(__name) \
50 static const __name##_t __name##_zero = C_ARRAY_NULL; \
51 static C_ARRAY_INLINE \
52         void __name##_free(\
53                         __name##_p a) \
54 {\
55         if (!a) return;\
56         if (a->e) free(a->e);\
57         *a = __name##_zero;\
58 }\
59 static C_ARRAY_INLINE \
60         void __name##_clear(\
61                         __name##_p a) \
62 {\
63         if (!a) return;\
64         a->count = 0;\
65 }\
66 static C_ARRAY_INLINE \
67         void __name##_realloc(\
68                         __name##_p a, __name##_count_t size) \
69 {\
70         if (!a || a->size == size) return; \
71         a->e = realloc(a->e, size * sizeof(__name##_element_t));\
72         a->size = size; \
73 }\
74 static C_ARRAY_INLINE \
75         void __name##_trim(\
76                         __name##_p a) \
77 {\
78         if (!a) return;\
79         __name##_count_t n = a->count + __name##_page_size;\
80         n -= (n % __name##_page_size);\
81         if (n != a->size)\
82                 __name##_realloc(a, n);\
83 }\
84 static C_ARRAY_INLINE \
85         __name##_element_t * __name##_get_ptr(\
86                         __name##_p a, __name##_count_t index) \
87 {\
88         if (!a) return NULL;\
89         if (index > a->count) index = a->count;\
90         return index < a->count ? a->e + index : NULL;\
91 }\
92 static C_ARRAY_INLINE \
93         __name##_count_t __name##_add(\
94                         __name##_p a, __name##_element_t e) \
95 {\
96         if (!a) return 0;\
97         if (a->count + 1 >= a->size)\
98                 __name##_realloc(a, a->size + __name##_page_size);\
99         a->e[a->count++] = e;\
100         return a->count;\
101 }\
102 static C_ARRAY_INLINE \
103         __name##_count_t __name##_insert(\
104                         __name##_p a, __name##_count_t index, \
105                         __name##_element_t * e, __name##_count_t count) \
106 {\
107         if (!a) return 0;\
108         if (index > a->count) index = a->count;\
109         if (a->count + count >= a->size) \
110                 __name##_realloc(a, (((a->count + count) / __name##_page_size)+1) * __name##_page_size);\
111         if (index < a->count)\
112                 memmove(&a->e[index + count], &a->e[index], \
113                                 (a->count - index + count) * sizeof(__name##_element_t));\
114         memmove(&a->e[index], e, count * sizeof(__name##_element_t));\
115         a->count += count;\
116         return a->count;\
117 }\
118 static C_ARRAY_INLINE \
119         __name##_count_t __name##_delete(\
120                         __name##_p a, __name##_count_t index, __name##_count_t count) \
121 {\
122         if (!a) return 0;\
123         if (index > a->count) index = a->count;\
124         if (index + count > a->count) \
125                 count = a->count - index;\
126         if (count && a->count - index) { \
127                 memmove(&a->e[index], &a->e[index + count], \
128                                 (a->count - index - count) * sizeof(__name##_element_t));\
129         }\
130         a->count -= count;\
131         return a->count;\
132 }
133
134 #endif /* __C_ARRAY_H___ */