added hedley for more portable inline and non-null specifiers, changed some prefix to postfix incr/decr(in hope that the compiler optimizes accordingly), refactored some functions, added string_find_str(), reworked linked list header and added implementation, added memswap(), made ntoh32 more portable

AuthorKonata <konata@posteo.jp>
Date
Commit858bbe01da35ae1aa786a8f73eeeb8bd2b4bf5df
Parent1cc1a61
15 files changed, 1233 insertions(+), 21 deletions(-)
Minclude/cutils/byte_array.h
@@ -30,6 +30,7 @@ void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*));
3030
3131 bool_t bytearray_adjust_size(Bytearray* bytearray, size_t size);
3232 bool_t bytearray_shrink(Bytearray* bytearray);
33+HEDLEY_NON_NULL(3)
3334 size_t* bytearray_find(const Bytearray* haystack, const void* needle, int (*cmp)(const void*, const void*));
3435
3536
Minclude/cutils/common.h
@@ -2,6 +2,7 @@
22 #define CUTILS_COMMON_H
33
44 #include <stddef.h>
5+#include <cutils/hedley.h>
56 #ifndef SIZE_MAX
67 #define SIZE_MAX (size_t)-1;
78 #endif
Minclude/cutils/dyn_string.h
@@ -30,8 +30,10 @@ void string_remove_range(String* string, size_t index, size_t length);
3030
3131 bool_t string_adjust_size(String* string, size_t size);
3232 size_t* string_find_char(const String* haystack, const char needle);
33+size_t* string_find_str(const String* haystack, const String* needle);
3334
3435 bool_t string_concat(String* string, const String* other);
36+
3537 String* from_cstring(const char* cstring);
3638 String* from_cstring_del(char* cstring);
3739 char* to_cstring(const String* string);
Ainclude/cutils/hedley.h
@@ -0,0 +1,704 @@
1+/* Hedley - https://nemequ.github.io/hedley
2+ * Created by Evan Nemerson <evan@nemerson.com>
3+ *
4+ * To the extent possible under law, the author(s) have dedicated all
5+ * copyright and related and neighboring rights to this software to
6+ * the public domain worldwide. This software is distributed without
7+ * any warranty.
8+ *
9+ * For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
10+ */
11+
12+#if !defined(HEDLEY_VERSION) || (HEDLEY_VERSION < 3)
13+#if defined(HEDLEY_VERSION)
14+# undef HEDLEY_VERSION
15+#endif
16+#define HEDLEY_VERSION 3
17+
18+#if defined(HEDLEY_VERSION_ENCODE)
19+# undef HEDLEY_VERSION_ENCODE
20+#endif
21+#define HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
22+
23+#if defined(HEDLEY_VERSION_DECODE_MAJOR)
24+# undef HEDLEY_VERSION_DECODE_MAJOR
25+#endif
26+#define HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
27+
28+#if defined(HEDLEY_VERSION_DECODE_MINOR)
29+# undef HEDLEY_VERSION_DECODE_MINOR
30+#endif
31+#define HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
32+
33+#if defined(HEDLEY_VERSION_DECODE_REVISION)
34+# undef HEDLEY_VERSION_DECODE_REVISION
35+#endif
36+#define HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
37+
38+#if defined(HEDLEY_GCC_VERSION_CHECK)
39+# undef HEDLEY_GCC_VERSION_CHECK
40+#endif
41+#if !defined(__GNUC__) || defined(__INTEL_COMPILER)
42+# define HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
43+#else
44+# if defined(__GNUC_PATCHLEVEL__)
45+# define HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) >= HEDLEY_VERSION_ENCODE(major,minor,patch))
46+# else
47+# define HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0) >= HEDLEY_VERSION_ENCODE(major,minor,patch))
48+# endif
49+#endif
50+
51+#if defined(HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
52+# undef HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
53+#endif
54+#if defined(__clang__)
55+# define HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
56+#else
57+# define HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
58+#endif
59+
60+#if defined(HEDLEY_MSVC_VERSION_CHECK)
61+# undef HEDLEY_GCC_VERSION_CHECK
62+#endif
63+#if !defined(_MSC_VER)
64+# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
65+#elif defined(_MSC_VER) && (_MSC_VER >= 1400)
66+# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
67+#elif defined(_MSC_VER) && (_MSC_VER >= 1200)
68+# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 100000) + (minor * 1000) + (patch)))
69+#else
70+# define HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
71+#endif
72+
73+#if defined(HEDLEY_INTEL_VERSION_CHECK)
74+# undef HEDLEY_INTEL_VERSION_CHECK
75+#endif
76+#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE)
77+# define HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) ((__INTEL_COMPILER + __INTEL_COMPILER_UPDATE) >= (((major) * 100) + (minor)))
78+#elif defined(__INTEL_COMPILER)
79+# define HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (__INTEL_COMPILER >= (((major) * 100) + (minor)))
80+#else
81+# define HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
82+#endif
83+
84+#if defined(HEDLEY_PGI_VERSION_CHECK)
85+# undef HEDLEY_PGI_VERSION_CHECK
86+#endif
87+#if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
88+# define HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) >= HEDLEY_VERSION_ENCODE(major,minor,patch))
89+#else
90+# define HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
91+#endif
92+
93+#if defined(HEDLEY_SUNPRO_VERSION_CHECK)
94+# undef HEDLEY_SUNPRO_VERSION_CHECK
95+#endif
96+#if defined(__SUNPRO_C)
97+# define HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) \
98+ (__SUNPRO_C >= ((((major) > 5) || (((major) == 5) && ((minor) > 9))) ? \
99+ (((major) * 0x1000) + (((minor + (6 * (minor / 10)))) * 0x10) + (revision)) : \
100+ (((major) * 0x100) + ((minor) * 0x10) + (revision))))
101+#elif defined(__SUNPRO_CC)
102+# define HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) \
103+ (__SUNPRO_CC >= ((((major) > 5) || (((major) == 5) && ((minor) > 9))) ? \
104+ (((major) * 0x1000) + (((minor + (6 * (minor / 10)))) * 0x10) + (revision)) : \
105+ (((major) * 0x100) + ((minor) * 0x10) + (revision))))
106+#else
107+# define HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
108+#endif
109+
110+#if defined(HEDLEY_EMSCRIPTEN_VERSION_CHECK)
111+# undef HEDLEY_EMSCRIPTEN_VERSION_CHECK
112+#endif
113+#if defined(__EMSCRIPTEN__)
114+# define HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) \
115+ (HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__) >= HEDLEY_VERSION_ENCODE(major, minor, patch))
116+#else
117+# define HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
118+#endif
119+
120+#if defined(HEDLEY_ARM_VERSION_CHECK)
121+# undef HEDLEY_ARM_VERSION_CHECK
122+#endif
123+#if defined(__ARMCOMPILER_VERSION)
124+# define HEDLEY_ARM_VERSION_CHECK(major,minor,patch) ((__ARMCOMPILER_VERSION) >= ((major * 1000000) + (minor * 10000) + (patch * 100)))
125+#elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
126+# define HEDLEY_ARM_VERSION_CHECK(major,minor,patch) ((__ARMCC_VERSION) >= ((major * 1000000) + (minor * 10000) + (patch)))
127+#else
128+# define HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
129+#endif
130+
131+#if defined(HEDLEY_IBM_VERSION_CHECK)
132+# undef HEDLEY_IBM_VERSION_CHECK
133+#endif
134+#if defined(__IBMC__)
135+# define HEDLEY_IBM_VERSION_CHECK(major,minor,patch) ((((major) * 100) + ((minor) * 10) + (patch)) >= (__IBMC__))
136+#elif defined(__IBMCPP__)
137+# define HEDLEY_IBM_VERSION_CHECK(major,minor,patch) ((((major) * 100) + ((minor) * 10) + (patch)) >= (__IBMCPP__))
138+#else
139+# define HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
140+#endif
141+
142+#if \
143+ (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
144+ HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
145+ (HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
146+# define HEDLEY_PRAGMA(value) _Pragma(#value)
147+#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
148+# define HEDLEY_PRAGMA(value) __pragma(value)
149+#else
150+# define HEDLEY_PRAGMA(value)
151+#endif
152+
153+#if defined(HEDLEY_CLANG_HAS_ATTRIBUTE)
154+# undef HEDLEY_CLANG_HAS_ATTRIBUTE
155+#endif
156+#if defined(HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
157+# undef HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
158+#endif
159+#if defined(HEDLEY_CLANG_HAS_BUILTIN)
160+# undef HEDLEY_CLANG_HAS_BUILTIN
161+#endif
162+#if defined(HEDLEY_CLANG_HAS_FEATURE)
163+# undef HEDLEY_CLANG_HAS_FEATURE
164+#endif
165+#if defined(HEDLEY_CLANG_HAS_EXTENSION)
166+# undef HEDLEY_CLANG_HAS_EXTENSION
167+#endif
168+#if defined(HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE)
169+# undef HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE
170+#endif
171+#if defined(HEDLEY_CLANG_HAS_WARNING)
172+# undef HEDLEY_CLANG_HAS_WARNING
173+#endif
174+#if defined(__has_attribute)
175+# define HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
176+#else
177+# define HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) (0)
178+#endif
179+#if defined(__has_cpp_attribute) && defined(__cplusplus)
180+# define HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
181+#else
182+# define HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) (0)
183+#endif
184+#if defined(__has_builtin)
185+# define HEDLEY_CLANG_HAS_BUILTIN(builtin) __has_builtin(builtin)
186+#else
187+# define HEDLEY_CLANG_HAS_BUILTIN(builtin) (0)
188+#endif
189+#if defined(__has_feature)
190+# define HEDLEY_CLANG_HAS_FEATURE(feature) __has_feature(feature)
191+#else
192+# define HEDLEY_CLANG_HAS_FEATURE(feature) (0)
193+#endif
194+#if defined(__has_extension)
195+# define HEDLEY_CLANG_HAS_EXTENSION(extension) __has_extension(extension)
196+#else
197+# define HEDLEY_CLANG_HAS_EXTENSION(extension) (0)
198+#endif
199+#if defined(__has_declspec_attribute)
200+# define HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
201+#else
202+# define HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
203+#endif
204+#if defined(__has_warning)
205+# define HEDLEY_CLANG_HAS_WARNING(warning) __has_warning(warning)
206+#else
207+# define HEDLEY_CLANG_HAS_WARNING(warning) (0)
208+#endif
209+
210+#if defined(HEDLEY_GCC_HAS_ATTRIBUTE)
211+# undef HEDLEY_GCC_HAS_ATTRIBUTE
212+#endif
213+#if defined(__has_attribute)
214+# define HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute)
215+#else
216+# define HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
217+#endif
218+
219+#if defined(HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
220+# undef HEDLEY_GCC_HAS_CPP_ATTRIBUTE
221+#endif
222+#if defined(__has_cpp_attribute) && defined(__cplusplus)
223+# define HEDLEY_GCC_HAS_CPP_ATTRIBUTE(cpp_attribute,major,minor,patch) __has_cpp_attribute(cpp_attribute)
224+#else
225+# define HEDLEY_GCC_HAS_CPP_ATTRIBUTE(cpp_attribute,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
226+#endif
227+
228+#if defined(HEDLEY_GCC_HAS_BUILTIN)
229+# undef HEDLEY_GCC_HAS_BUILTIN
230+#endif
231+#if defined(__has_builtin)
232+# define HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
233+#else
234+# define HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
235+#endif
236+
237+#if defined(HEDLEY_GCC_HAS_FEATURE)
238+# undef HEDLEY_GCC_HAS_FEATURE
239+#endif
240+#if defined(__has_feature)
241+# define HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
242+#else
243+# define HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
244+#endif
245+
246+#if defined(HEDLEY_GCC_HAS_EXTENSION)
247+# undef HEDLEY_GCC_HAS_EXTENSION
248+#endif
249+#if defined(__has_extension)
250+# define HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
251+#else
252+# define HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
253+#endif
254+
255+#if defined(HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
256+# undef HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
257+#endif
258+#if defined(__has_declspec_attribute)
259+# define HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(declspec_attribute,major,minor,patch) __has_declspec_attribute(declspec_attribute)
260+#else
261+# define HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(declspec_attribute,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
262+#endif
263+
264+#if defined(HEDLEY_GCC_HAS_WARNING)
265+# undef HEDLEY_GCC_HAS_WARNING
266+#endif
267+#if defined(__has_warning)
268+# define HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
269+#else
270+# define HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
271+#endif
272+
273+#if defined(HEDLEY_DIAGNOSTIC_PUSH)
274+# undef HEDLEY_DIAGNOSTIC_PUSH
275+#endif
276+#if defined(HEDLEY_DIAGNOSTIC_POP)
277+# undef HEDLEY_DIAGNOSTIC_POP
278+#endif
279+#if defined(__clang__)
280+# define HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
281+# define HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
282+#elif HEDLEY_INTEL_VERSION_CHECK(16,0,0)
283+# define HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
284+# define HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
285+#elif HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(4,6,0)
286+# define HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
287+# define HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
288+#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
289+# define HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
290+# define HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
291+#else
292+# define HEDLEY_DIAGNOSTIC_PUSH
293+# define HEDLEY_DIAGNOSTIC_POP
294+#endif
295+
296+#if defined(HEDLEY_DEPRECATED)
297+# undef HEDLEY_DEPRECATED
298+#endif
299+#if defined(HEDLEY_DEPRECATED_FOR)
300+# undef HEDLEY_DEPRECATED_FOR
301+#endif
302+#if defined(__cplusplus) && (__cplusplus >= 201402L)
303+# define HEDLEY_DEPRECATED(since) [[deprecated("Since " #since)]]
304+# define HEDLEY_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
305+#elif \
306+ HEDLEY_GCC_HAS_EXTENSION(attribute_deprecated_with_message,4,5,0) || \
307+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
308+ HEDLEY_ARM_VERSION_CHECK(5,6,0)
309+# define HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
310+# define HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
311+#elif \
312+ HEDLEY_GCC_HAS_ATTRIBUTE(deprcated,4,0,0) || \
313+ HEDLEY_ARM_VERSION_CHECK(4,1,0)
314+# define HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
315+# define HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
316+#elif HEDLEY_MSVC_VERSION_CHECK(14,0,0)
317+# define HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
318+# define HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
319+#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
320+# define HEDLEY_DEPRECATED(since) _declspec(deprecated)
321+# define HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
322+#else
323+# define HEDLEY_DEPRECATED(since)
324+# define HEDLEY_DEPRECATED_FOR(since, replacement)
325+#endif
326+
327+#if defined(HEDLEY_UNAVAILABLE)
328+# undef HEDLEY_UNAVAILABLE
329+#endif
330+#if \
331+ HEDLEY_GCC_HAS_ATTRIBUTE(warning,4,3,0) || \
332+ HEDLEY_INTEL_VERSION_CHECK(16,0,0)
333+# define HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
334+#else
335+# define HEDLEY_UNAVAILABLE(available_since)
336+#endif
337+
338+#if defined(HEDLEY_WARN_UNUSED_RESULT)
339+# undef HEDLEY_WARN_UNUSED_RESULT
340+#endif
341+#if defined(__cplusplus) && (__cplusplus >= 201703L)
342+# define HEDLEY_WARN_UNUSED_RESULT [[nodiscard]]
343+#elif HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)
344+# define HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
345+#elif \
346+ HEDLEY_GCC_HAS_ATTRIBUTE(warn_unused_result,3,4,0) || \
347+ HEDLEY_INTEL_VERSION_CHECK(16,0,0)
348+# define HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
349+#elif defined(_Check_return_) /* SAL */
350+# define HEDLEY_WARN_UNUSED_RESULT _Check_return_
351+#else
352+# define HEDLEY_WARN_UNUSED_RESULT
353+#endif
354+
355+#if defined(HEDLEY_SENTINEL)
356+# undef HEDLEY_SENTINEL
357+#endif
358+#if \
359+ HEDLEY_GCC_HAS_ATTRIBUTE(sentinel,4,0,0) || \
360+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
361+ HEDLEY_ARM_VERSION_CHECK(5,4,0)
362+# define HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
363+#else
364+# define HEDLEY_SENTINEL(position)
365+#endif
366+
367+#if defined(HEDLEY_NO_RETURN)
368+# undef HEDLEY_NO_RETURN
369+#endif
370+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
371+# define HEDLEY_NO_RETURN _Noreturn
372+#elif defined(__cplusplus) && (__cplusplus >= 201103L)
373+# define HEDLEY_NO_RETURN [[noreturn]]
374+#elif \
375+ HEDLEY_GCC_HAS_ATTRIBUTE(noreturn,3,2,0) || \
376+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
377+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
378+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
379+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
380+# define HEDLEY_NO_RETURN __attribute__((__noreturn__))
381+#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
382+# define HEDLEY_NO_RETURN __declspec(noreturn)
383+#else
384+# define HEDLEY_NO_RETURN
385+#endif
386+
387+#if defined(HEDLEY_UNREACHABLE)
388+# undef HEDLEY_UNREACHABLE
389+#endif
390+#if (HEDLEY_GCC_HAS_BUILTIN(__builtin_unreachable,4,5,0) && !defined(__CC_ARM)) || HEDLEY_INTEL_VERSION_CHECK(16,0,0)
391+# define HEDLEY_UNREACHABLE() __builtin_unreachable()
392+#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
393+# define HEDLEY_UNREACHABLE() __assume(0)
394+#elif defined(EXIT_FAILURE)
395+# define HEDLEY_UNREACHABLE() abort()
396+#else
397+# define HEDLEY_UNREACHABLE()
398+#endif
399+
400+HEDLEY_DIAGNOSTIC_PUSH
401+#if HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
402+# if defined(__clang__)
403+# pragma clang diagnostic ignored "-Wvariadic-macros"
404+# elif defined(__GNUC__)
405+# pragma GCC diagnostic ignored "-Wvariadic-macros"
406+# endif
407+#endif
408+#if defined(HEDLEY_NON_NULL)
409+# undef HEDLEY_NON_NULL
410+#endif
411+#if HEDLEY_GCC_HAS_ATTRIBUTE(nonnull,3,3,0) || HEDLEY_INTEL_VERSION_CHECK(16,0,0) || HEDLEY_ARM_VERSION_CHECK(4,1,0)
412+# define HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
413+#else
414+# define HEDLEY_NON_NULL(...)
415+#endif
416+HEDLEY_DIAGNOSTIC_POP
417+
418+#if defined(HEDLEY_PRINTF_FORMAT)
419+# undef HEDLEY_PRINTF_FORMAT
420+#endif
421+#if defined(__MINGW32__) && HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
422+# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
423+#elif defined(__MINGW32__) && HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
424+# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
425+#elif \
426+ HEDLEY_GCC_HAS_ATTRIBUTE(format,3,1,0) || \
427+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
428+ HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
429+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
430+# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
431+#else
432+# define HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
433+#endif
434+
435+#if defined(HEDLEY_LIKELY)
436+# undef HEDLEY_LIKELY
437+#endif
438+#if defined(HEDLEY_UNLIKELY)
439+# undef HEDLEY_UNLIKELY
440+#endif
441+#if \
442+ HEDLEY_GCC_HAS_BUILTIN(__builtin_expect,3,0,0) || \
443+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
444+ HEDLEY_SUNPRO_VERSION_CHECK(5,12,0) || \
445+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
446+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
447+# define HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
448+# define HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
449+#else
450+# define HEDLEY_LIKELY(expr) (!!(expr))
451+# define HEDLEY_UNLIKELY(expr) (!!(expr))
452+#endif
453+
454+#if defined(HEDLEY_MALLOC)
455+# undef HEDLEY_MALLOC
456+#endif
457+#if \
458+ HEDLEY_GCC_HAS_ATTRIBUTE(malloc, 3, 1, 0) || \
459+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
460+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
461+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
462+ HEDLEY_IBM_VERSION_CHECK(12,1,0)
463+# define HEDLEY_MALLOC __attribute__((__malloc__))
464+#elif HEDLEY_MSVC_VERSION_CHECK(14, 0, 0)
465+# define HEDLEY_MALLOC __declspec(restrict)
466+#else
467+# define HEDLEY_MALLOC
468+#endif
469+
470+#if defined(HEDLEY_PURE)
471+# undef HEDLEY_PURE
472+#endif
473+#if \
474+ HEDLEY_GCC_HAS_ATTRIBUTE(pure, 2, 96, 0) || \
475+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
476+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
477+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
478+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
479+# define HEDLEY_PURE __attribute__((__pure__))
480+#elif HEDLEY_MSVC_VERSION_CHECK(14, 0, 0)
481+# define HEDLEY_PURE __declspec(noalias)
482+#else
483+# define HEDLEY_PURE
484+#endif
485+
486+#if defined(HEDLEY_CONST)
487+# undef HEDLEY_CONST
488+#endif
489+#if HEDLEY_GCC_HAS_ATTRIBUTE(const, 2, 5, 0) || \
490+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
491+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
492+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
493+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
494+# define HEDLEY_CONST __attribute__((__const__))
495+#else
496+# define HEDLEY_CONST HEDLEY_PURE
497+#endif
498+
499+#if defined(HEDLEY_RESTRICT)
500+# undef HEDLEY_RESTRICT
501+#endif
502+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
503+# define HEDLEY_RESTRICT restrict
504+#elif \
505+ HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
506+ HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
507+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
508+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
509+ HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
510+ HEDLEY_PGI_VERSION_CHECK(17,10,0)
511+# define HEDLEY_RESTRICT __restrict
512+#elif HEDLEY_SUNPRO_VERSION_CHECK(5,8,0)
513+# define HEDLEY_RESTRICT _Restrict
514+#endif
515+
516+#if defined(HEDLEY_INLINE)
517+# undef HEDLEY_INLINE
518+#endif
519+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
520+# define HEDLEY_INLINE inline
521+#elif defined(__GNUC_STDC_INLINE__) || HEDLEY_ARM_VERSION_CHECK(6,2,0)
522+# define HEDLEY_INLINE __inline__
523+#elif HEDLEY_MSVC_VERSION_CHECK(12,0,0) || HEDLEY_ARM_VERSION_CHECK(4,1,0)
524+# define HEDLEY_INLINE __inline
525+#else
526+# define HEDLEY_INLINE
527+#endif
528+
529+#if defined(HEDLEY_ALWAYS_INLINE)
530+# undef HEDLEY_ALWAYS_INLINE
531+#endif
532+#if \
533+ HEDLEY_GCC_HAS_ATTRIBUTE(always_inline,4,0,0) || \
534+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
535+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
536+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
537+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
538+# define HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__))
539+#elif HEDLEY_MSVC_VERSION_CHECK(12,0,0)
540+# define HEDLEY_ALWAYS_INLINE __forceinline
541+#else
542+# define HEDLEY_ALWAYS_INLINE HEDLEY_INLINE
543+#endif
544+
545+#if defined(HEDLEY_NEVER_INLINE)
546+# undef HEDLEY_NEVER_INLINE
547+#endif
548+#if \
549+ HEDLEY_GCC_HAS_ATTRIBUTE(never_inline,4,0,0) || \
550+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
551+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
552+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
553+ HEDLEY_IBM_VERSION_CHECK(10,1,0)
554+# define HEDLEY_NEVER_INLINE __attribute__((__noinline__))
555+#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
556+# define HEDLEY_NEVER_INLINE __declspec(noinline)
557+#else
558+# define HEDLEY_NEVER_INLINE HEDLEY_INLINE
559+#endif
560+
561+#if defined(HEDLEY_PRIVATE)
562+# undef HEDLEY_PRIVATE
563+#endif
564+#if defined(HEDLEY_PUBLIC)
565+# undef HEDLEY_PUBLIC
566+#endif
567+#if defined(HEDLEY_IMPORT)
568+# undef HEDLEY_IMPORT
569+#endif
570+#if defined(_WIN32) || defined(__CYGWIN__)
571+# if \
572+ HEDLEY_GCC_VERSION_CHECK(4,2,0) || \
573+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
574+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
575+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
576+ HEDLEY_IBM_VERSION_CHECK(13,1,0)
577+# define HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
578+# else
579+# define HEDLEY_PRIVATE
580+# endif
581+# define HEDLEY_PUBLIC __declspec(dllexport)
582+# define HEDLEY_IMPORT __declspec(dllimport)
583+#else
584+# if \
585+ HEDLEY_GCC_VERSION_CHECK(4,2,0) || \
586+ HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
587+ HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
588+ HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
589+ HEDLEY_IBM_VERSION_CHECK(13,1,0)
590+# define HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
591+# define HEDLEY_PUBLIC __attribute__((__visibility__("default")))
592+# else
593+# define HEDLEY_PRIVATE
594+# define HEDLEY_PUBLIC
595+# endif
596+# define HEDLEY_IMPORT extern
597+#endif
598+
599+#if defined(HEDLEY_NO_THROW)
600+# undef HEDLEY_NO_THROW
601+#endif
602+#if \
603+ HEDLEY_GCC_HAS_ATTRIBUTE(nothrow,3,3,0) || \
604+ HEDLEY_INTEL_VERSION_CHECK(16,0,0)
605+# define HEDLEY_NO_THROW __attribute__((__nothrow__))
606+#elif \
607+ HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
608+ HEDLEY_ARM_VERSION_CHECK(4,1,0)
609+# define HEDLEY_NO_THROW __declspec(nothrow)
610+#else
611+# define HEDLEY_NO_THROW
612+#endif
613+
614+#if defined(HEDLEY_FALL_THROUGH)
615+# undef HEDLEY_FALL_THROUGH
616+#endif
617+#if defined(__cplusplus) && (__cplusplus >= 201703L)
618+# define HEDLEY_FALL_THROUGH [[fallthrough]]
619+#elif defined(__cplusplus) && (__cplusplus >= 201103L) && HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(clang::fallthrough)
620+# define HEDLEY_FALL_THROUGH [[clang::fallthrough]]
621+#elif defined(__cplusplus) && (__cplusplus >= 201103L) && HEDLEY_GCC_HAS_CPP_ATTRIBUTE(gnu::fallthrough,7,0,0)
622+# define HEDLEY_FALL_THROUGH [[gnu::fallthrough]]
623+#elif HEDLEY_GCC_HAS_ATTRIBUTE(fallthrough,7,0,0)
624+# define HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
625+#elif defined(__fallthrough) /* SAL */
626+# define HEDLEY_FALL_THROUGH __fallthrough
627+#else
628+# define HEDLEY_FALL_THROUGH
629+#endif
630+
631+#if defined(HEDLEY_RETURNS_NON_NULL)
632+# undef HEDLEY_RETURNS_NON_NULL
633+#endif
634+#if \
635+ HEDLEY_GCC_HAS_ATTRIBUTE(returns_nonnull,4,9,0) || \
636+ HEDLEY_INTEL_VERSION_CHECK(16,0,0)
637+# define HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
638+#elif defined(_Ret_notnull_) /* SAL */
639+# define HEDLEY_RETURNS_NON_NULL _Ret_notnull_
640+#else
641+# define HEDLEY_RETURNS_NON_NULL
642+#endif
643+
644+#if defined(HEDLEY_ARRAY_PARAM)
645+# undef HEDLEY_ARRAY_PARAM
646+#endif
647+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus) && !defined(__PGI)
648+# define HEDLEY_ARRAY_PARAM(name) (name)
649+#else
650+# define HEDLEY_ARRAY_PARAM(name)
651+#endif
652+
653+#if defined(HEDLEY_BEGIN_C_DECLS)
654+# undef HEDLEY_BEGIN_C_DECLS
655+#endif
656+#if defined(HEDLEY_END_C_DECLS)
657+# undef HEDLEY_END_C_DECLS
658+#endif
659+#if defined(HEDLEY_C_DECL)
660+# undef HEDLEY_C_DECL
661+#endif
662+#if defined(__cplusplus)
663+# define HEDLEY_BEGIN_C_DECLS extern "C" {
664+# define HEDLEY_END_C_DECLS }
665+# define HEDLEY_C_DECL extern "C"
666+#else
667+# define HEDLEY_BEGIN_C_DECLS
668+# define HEDLEY_END_C_DECLS
669+# define HEDLEY_C_DECL
670+#endif
671+
672+#if defined(HEDLEY_STATIC_ASSERT)
673+# undef HEDLEY_STATIC_ASSERT
674+#endif
675+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
676+# define HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
677+#elif defined(__cplusplus) && __cplusplus >= 201103L
678+# define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message)
679+#elif \
680+ HEDLEY_GCC_HAS_FEATURE(c_static_assert,4,6,0) || \
681+ HEDLEY_INTEL_VERSION_CHECK(16,0,0)
682+# define HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
683+#elif HEDLEY_MSVC_VERSION_CHECK(16,0,0)
684+# define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message)
685+#else
686+# define HEDLEY_STATIC_ASSERT(expr, message)
687+#endif
688+
689+#if defined(HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
690+# undef HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
691+#endif
692+#if HEDLEY_CLANG_HAS_WARNING("-Wdeprecated")
693+# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated\"")
694+#elif HEDLEY_INTEL_VERSION_CHECK(16,0,0)
695+# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478)")
696+#elif HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(4,3,0)
697+# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated\"")
698+#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
699+# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
700+#else
701+# define HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
702+#endif
703+
704+#endif /* !defined(HEDLEY_VERSION) || (HEDLEY_VERSION < X) */
Minclude/cutils/linked-list.h
@@ -1,6 +1,9 @@
11 #ifndef LINKED_LIST_H
22 #define LINKED_LIST_H
33
4+#include <cutils/common.h>
5+#include <stdlib.h>
6+
47 typedef struct SLnode
58 {
69 void* item;
@@ -16,26 +19,102 @@ typedef struct DLnode
1619
1720 typedef struct LinkedList
1821 {
19- void* head;
22+ void* head; /*SLnode if doubly_linked is false, otherwise DLnode*/
2023 void* tail;
2124 size_t length;
2225 bool_t doubly_linked;
26+ bool_t circularly_linked;
2327 } LinkedList;
2428
25-LinkedList* new_ll(bool_t doubly_linked);
26-void delete_ll(LinkedList* ll, void(*rmv) (void*));
29+LinkedList* new_ll(bool_t doubly_linked, bool_t circularly_linked);
30+
31+void delete_sll(LinkedList* ll, void(*rmv) (void*));
32+void delete_dll(LinkedList* ll, void(*rmv) (void*));
33+HEDLEY_INLINE
34+static void delete_ll(LinkedList* ll, void(*rmv) (void*))
35+{
36+ if(ll->doubly_linked)
37+ delete_dll(ll, rmv);
38+ else
39+ delete_sll(ll, rmv);
40+}
2741
28-void* ll_at(const LinkedList* ll, size_t index);
42+void* sll_at(const LinkedList* ll, size_t index);
43+void* dll_at(const LinkedList* ll, size_t index);
44+HEDLEY_INLINE
45+static void* ll_at(const LinkedList* ll, size_t index)
46+{
47+ if(ll->doubly_linked)
48+ return sll_at(ll, index);
49+ else
50+ return dll_at(ll, index);
51+}
2952 #define ll_pop(ll) ll_pop_at(ll, ll->length-1)
30-void* ll_pop_at(LinkedList* ll, size_t index);
53+void* sll_pop_at(LinkedList* ll, size_t index);
54+void* dll_pop_at(LinkedList* ll, size_t index);
55+HEDLEY_INLINE
56+static void* ll_pop_at(LinkedList* ll, size_t index)
57+{
58+ if(ll->doubly_linked)
59+ return sll_pop_at(ll, index);
60+ else
61+ return dll_pop_at(ll, index);
62+}
3163
32-bool_t ll_insert(LinkedList* ll, size_t index, void* item);
3364 #define ll_push(ll, item) ll_insert(ll, ll->length, item)
65+bool_t sll_insert(LinkedList* ll, size_t index, void* item);
66+bool_t dll_insert(LinkedList* ll, size_t index, void* item);
67+HEDLEY_INLINE
68+static bool_t ll_insert(LinkedList* ll, size_t index, void* item)
69+{
70+ if(ll->doubly_linked)
71+ return sll_insert(ll, index, item);
72+ else
73+ return dll_insert(ll, index, item);
74+}
3475
35-void ll_remove(LinkedList* ll, size_t index, void (*rmv)(void*));
36-void ll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*));
3776
38-size_t* ll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*));
77+void sll_remove(LinkedList* ll, size_t index, void (*rmv)(void*));
78+void dll_remove(LinkedList* ll, size_t index, void (*rmv)(void*));
79+HEDLEY_INLINE
80+static void ll_remove(LinkedList* ll, size_t index, void (*rmv)(void*))
81+{
82+ if(ll->doubly_linked)
83+ sll_remove(ll, index, rmv);
84+ else
85+ dll_remove(ll, index, rmv);
86+}
87+void sll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*));
88+void dll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*));
89+HEDLEY_INLINE
90+static void ll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*))
91+{
92+ if(ll->doubly_linked)
93+ sll_remove_range(ll, index, length, rmv);
94+ else
95+ dll_remove_range(ll, index, length, rmv);
96+}
97+
98+size_t* sll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*));
99+size_t* dll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*));
100+HEDLEY_INLINE
101+static size_t* ll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*))
102+{
103+ if(haystack->doubly_linked)
104+ return sll_find(haystack, needle, cmp);
105+ else
106+ return dll_find(haystack, needle, cmp);
107+}
108+void sll_swap(LinkedList* ll, size_t item1, size_t item2);
109+void dll_swap(LinkedList* ll, size_t item1, size_t item2);
110+HEDLEY_INLINE
111+static void ll_swap(LinkedList* ll, size_t item1, size_t item2)
112+{
113+ if(ll->doubly_linked)
114+ sll_swap(ll, item1, item2);
115+ else
116+ dll_swap(ll, item1, item2);
117+}
39118
40119
41120 #endif /* LINKED_LIST_H */
Minclude/cutils/misc.h
@@ -15,15 +15,34 @@
1515 #endif
1616
1717 #include <string.h>
18+#include <cutils/common.h>
1819
1920 #if __STDC_VERSION__ >= 199901L
2021 #include <stdint.h>
2122 typedef uintmax_t max_uint_t;
22- uint32_t ntoh32(uint32_t const net);
23+ #ifdef UINT32_MAX
24+ uint32_t ntoh32(uint32_t const net);
25+ #endif
2326 #else
2427 typedef unsigned long max_uint_t;
2528 #endif
2629
30+HEDLEY_INLINE
31+static void memswap(void* item1, void* item2, size_t length)
32+{
33+ char *item1_tmp, *item2_tmp;
34+ char tmp;
35+
36+ item1_tmp = item1;
37+ item2_tmp = item2;
38+
39+ while(length--)
40+ {
41+ tmp = *item1_tmp;
42+ *item1_tmp = *item2_tmp;
43+ *item2_tmp = tmp;
44+ }
45+}
2746
2847 #if __STDC_VERSION__ >= 201112L
2948 #include <time.h>
Minclude/cutils/vector.h
@@ -30,6 +30,7 @@ void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv
3030
3131 bool_t vector_adjust_size(Vector* vector, size_t size);
3232 bool_t vector_shrink(Vector* vector);
33+HEDLEY_NON_NULL(3)
3334 size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*));
3435
3536
Mmeson.build
@@ -1,6 +1,6 @@
11 project('cutils', 'c')
22
3-WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter']
3+WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter', '-Wno-unused-function']
44 CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC'] + WARNINGS
55
66 subdir('include')
Msrc/byte_array.c
@@ -73,7 +73,7 @@ bool_t bytearray_insert(Bytearray* bytearray, size_t index, const void* item)
7373
7474 memmove(bytearray->items+index*size+1*size, bytearray->items+index*size, (length*size-index*size)*sizeof(*bytearray->items));
7575 memcpy(bytearray->items+index*size, item, bytearray->element_size);
76- ++bytearray->length;
76+ bytearray->length++;
7777 return b_true;
7878 }
7979
@@ -87,7 +87,7 @@ void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*))
8787 rmv(&bytearray->items[index*elsize]);
8888
8989 memmove(bytearray->items+index*elsize, bytearray->items+index*elsize+1*elsize, (length*elsize-index*elsize-1*elsize)*sizeof(*bytearray->items));
90- --bytearray->length;
90+ bytearray->length--;
9191 }
9292 }
9393
Msrc/dyn_string.c
@@ -22,7 +22,7 @@ void string_remove(String* string, size_t index)
2222 if(index < string->length)
2323 {
2424 memmove(string->chars+index, string->chars+index+1, (string->length-index-1)*sizeof(*string->chars));
25- --string->length;
25+ string->length--;
2626 }
2727 }
2828
@@ -77,8 +77,8 @@ bool_t string_concat(String* string, const String* other)
7777
7878 size_t* string_find_char(const String* haystack, const char needle)
7979 {
80- size_t* ret;
81- size_t i;
80+ size_t i, *ret;
81+
8282 for(i = 0; i < haystack->length; i++)
8383 {
8484 if(haystack->chars[i] == needle)
@@ -91,6 +91,27 @@ size_t* string_find_char(const String* haystack, const char needle)
9191 return NULL;
9292 }
9393
94+size_t* string_find_str(const String* haystack, const String* needle)
95+{
96+ size_t *ret, i, j, tmp;
97+
98+ for(i = 0; i < haystack->length; i++)
99+ {
100+ for(j = 0, tmp = i; j < needle->length; j++, i++)
101+ {
102+ if(haystack->chars[i] != needle->chars[j])
103+ {
104+ break;
105+ } else if(j == needle->length-1){
106+ ret = malloc(sizeof(*ret));
107+ *ret = tmp;
108+ return ret;
109+ }
110+ }
111+ }
112+ return NULL;
113+}
114+
94115 bool_t string_adjust_size(String* string, size_t size)
95116 {
96117 while(string->capacity < size)
Asrc/linked-list.c
@@ -0,0 +1,373 @@
1+#include <cutils/linked-list.h>
2+#include <cutils/misc.h>
3+
4+LinkedList* new_ll(bool_t doubly_linked, bool_t circularly_linked)
5+{
6+ LinkedList* ret = malloc(sizeof(*ret));
7+ ret->head = NULL;
8+ ret->tail = NULL;
9+ ret->length = 0;
10+ ret->doubly_linked = doubly_linked;
11+ ret->circularly_linked = circularly_linked;
12+ return ret;
13+}
14+
15+void delete_sll(LinkedList* ll, void(*rmv) (void*))
16+{
17+ size_t i;
18+ SLnode *current, *tmp;
19+
20+ current = ll->head;
21+
22+ for(i = 0; i < ll->length; i++)
23+ {
24+
25+ if(rmv)
26+ rmv(current->item);
27+ tmp = current;
28+ current = current->next;
29+ free(tmp);
30+
31+ }
32+}
33+
34+void delete_dll(LinkedList* ll, void(*rmv) (void*))
35+{
36+ size_t i;
37+ DLnode *current, *tmp;
38+
39+ current = ll->head;
40+
41+ for(i = 0; i < ll->length; i++)
42+ {
43+
44+ if(rmv)
45+ rmv(current->item);
46+ tmp = current;
47+ current = current->next;
48+ free(tmp);
49+
50+ }
51+}
52+
53+void* sll_at(const LinkedList* ll, size_t index)
54+{
55+ size_t i;
56+ SLnode* current;
57+
58+ current = ll->head;
59+
60+ for(i = 0; i < index; i++)
61+ {
62+ current = current->next;
63+ }
64+
65+ return current->item;
66+}
67+
68+void* dll_at(const LinkedList* ll, size_t index)
69+{
70+ size_t i;
71+ DLnode* current;
72+
73+ current = ll->head;
74+
75+ for(i = 0; i < index; i++)
76+ {
77+ current = current->next;
78+ }
79+
80+ return current->item;
81+}
82+
83+void* sll_pop_at(LinkedList* ll, size_t index)
84+{
85+ size_t i;
86+ SLnode* current;
87+ void* tmp;
88+
89+ current = ll->head;
90+
91+ for(i = 0; i < index; i++)
92+ {
93+ current = current->next;
94+ }
95+
96+ tmp = current->item;
97+ ll_remove(ll, index, NULL);
98+ return tmp;
99+}
100+
101+void* dll_pop_at(LinkedList* ll, size_t index)
102+{
103+ size_t i;
104+ DLnode* current;
105+ void* tmp;
106+
107+ current = ll->head;
108+
109+ for(i = 0; i < index; i++)
110+ {
111+ current = current->next;
112+ }
113+
114+ tmp = current->item;
115+ ll_remove(ll, index, NULL);
116+ return tmp;
117+}
118+
119+bool_t sll_insert(LinkedList* ll, size_t index, void* item)
120+{
121+ size_t i;
122+ SLnode *current, *prev, *newnode = malloc(sizeof(*newnode));
123+
124+ if(!newnode)
125+ return b_false;
126+
127+ newnode->item =item;
128+ prev = NULL;
129+ current = ll->head;
130+
131+ for(i = 0; i < index; i++)
132+ {
133+ prev = current;
134+ current = current->next;
135+ }
136+
137+ if(current)
138+ {
139+ newnode->next = current;
140+ if(prev)
141+ prev->next = newnode;
142+ else
143+ ll->head = newnode;
144+ } else {
145+ ll->tail = newnode;
146+ if(index == 0)
147+ ll->head = newnode;
148+ newnode->next = NULL;
149+ }
150+
151+ ll->length++;
152+ return b_true;
153+}
154+
155+bool_t dll_insert(LinkedList* ll, size_t index, void* item)
156+{
157+ size_t i;
158+ DLnode *current, *newnode = malloc(sizeof(*newnode));
159+
160+ if(!newnode)
161+ return b_false;
162+
163+ newnode->item =item;
164+ current = ll->head;
165+
166+ for(i = 0; i < index; i++)
167+ {
168+ current = current->next;
169+ }
170+
171+ if(current)
172+ {
173+ newnode->next = current;
174+ if(current->prev)
175+ {
176+ current->prev->next = newnode;
177+ newnode->prev =current;
178+ } else {
179+ ll->head = newnode;
180+ newnode->prev = NULL;
181+ }
182+ } else {
183+ ll->tail = newnode;
184+ if(index == 0)
185+ ll->head = newnode;
186+ newnode->next = NULL;
187+ newnode->prev = NULL;
188+ }
189+
190+ ll->length++;
191+ return b_true;
192+}
193+
194+void sll_remove(LinkedList* ll, size_t index, void (*rmv)(void*))
195+{
196+ size_t i;
197+ SLnode *current, *prev;
198+
199+ prev = NULL;
200+ current = ll->head;
201+
202+ for(i = 0; i < index; i++)
203+ {
204+ prev = current;
205+ current = current->next;
206+ }
207+
208+ if(rmv)
209+ rmv(current->item);
210+
211+ prev->next = current->next;
212+ free(current);
213+}
214+
215+void dll_remove(LinkedList* ll, size_t index, void (*rmv)(void*))
216+{
217+ size_t i;
218+ DLnode* current;
219+
220+ current = ll->head;
221+
222+ for(i = 0; i < index; i++)
223+ {
224+ current = current->next;
225+ }
226+
227+ if(rmv)
228+ rmv(current->item);
229+
230+ current->prev->next = current->next;
231+ current->next->prev = current->prev;
232+ free(current);
233+}
234+
235+void sll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*))
236+{
237+ size_t i;
238+ SLnode *current, *tmp, *prev;
239+
240+ prev = NULL;
241+ current = ll->head;
242+
243+ for(i = 0; i < index; i++)
244+ {
245+ prev = current;
246+ current = current->next;
247+ }
248+
249+ for(i = 0; i < length; i++)
250+ {
251+ if(rmv)
252+ rmv(current->item);
253+ tmp = current;
254+ free(current);
255+ current = tmp->next;
256+ }
257+
258+ prev->next = current;
259+}
260+
261+void dll_remove_range(LinkedList* ll, size_t index, size_t length, void (*rmv)(void*))
262+{
263+ size_t i;
264+ DLnode *current, *prev;
265+
266+ prev = NULL;
267+ current = ll->head;
268+
269+ for(i = 0; i < index; i++)
270+ {
271+ prev = current;
272+ current = current->next;
273+ }
274+
275+ for(i = 0; i < length; i++)
276+ {
277+ if(rmv)
278+ rmv(current->item);
279+ current = current->next;
280+ free(current->prev);
281+ }
282+
283+ prev->next = current;
284+ current->prev = prev;
285+}
286+
287+size_t* sll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*))
288+{
289+ size_t i, *ret;
290+ SLnode* current;
291+
292+ current = haystack->head;
293+
294+ for(i = 0; i < haystack->length; i++)
295+ {
296+ if(cmp(current->item, needle) == 0)
297+ {
298+ ret = malloc(sizeof(*ret));
299+ *ret = i;
300+ return ret;
301+ }
302+ current = current->next;
303+ }
304+ return NULL;
305+}
306+
307+size_t* dll_find(const LinkedList* haystack, const void* needle, int (*cmp)(const void*, const void*))
308+{
309+ size_t i, *ret;
310+ DLnode* current;
311+
312+ current = haystack->head;
313+
314+ for(i = 0; i < haystack->length; i++)
315+ {
316+ if(cmp(current->item, needle) == 0)
317+ {
318+ ret = malloc(sizeof(*ret));
319+ *ret = i;
320+ return ret;
321+ }
322+ current = current->next;
323+ }
324+ return NULL;
325+}
326+
327+void sll_swap(LinkedList* ll, size_t item1, size_t item2)
328+{
329+ size_t i, first_index, second_index;
330+ SLnode *first, *second;
331+
332+ first_index = item1 <= item2 ? item1 : item2;
333+ second_index = item1 <= item2 ? item2 : item1;
334+
335+ first = NULL;
336+ second = ll->head;
337+
338+ for(i = 0; i < second_index; i++)
339+ {
340+ if(i == first_index)
341+ first = second->next;
342+ second = second->next;
343+ }
344+
345+ if(!first)
346+ first = second;
347+
348+ memswap(&first->item, &second->item, sizeof(first->item));
349+}
350+
351+void dll_swap(LinkedList* ll, size_t item1, size_t item2)
352+{
353+ size_t i, first_index, second_index;
354+ DLnode *first, *second;
355+
356+ first_index = item1 <= item2 ? item1 : item2;
357+ second_index = item1 <= item2 ? item2 : item1;
358+
359+ first = NULL;
360+ second = ll->head;
361+
362+ for(i = 0; i < second_index; i++)
363+ {
364+ if(i == first_index)
365+ first = second->next;
366+ second = second->next;
367+ }
368+
369+ if(!first)
370+ first = second;
371+
372+ memswap(&first->item, &second->item, sizeof(first->item));
373+}
Msrc/meson.build
@@ -1,4 +1,4 @@
1-src = ['vector.c', 'dyn_string.c', 'misc.c', 'byte_array.c']
1+src = ['vector.c', 'dyn_string.c', 'misc.c', 'byte_array.c', 'linked-list.c']
22 srctest = src + ['test.c']
33
44 cutils_lib = library('cutils', src, include_directories : inc, install: true)
Msrc/misc.c
@@ -11,6 +11,7 @@ void sleep_ms(unsigned int milliseconds)
1111 }
1212
1313 #if __STDC_VERSION__ >= 199901L
14+#ifdef UINT32_MAX
1415 uint32_t ntoh32(uint32_t const net)
1516 {
1617 uint8_t data[4];
@@ -22,6 +23,7 @@ uint32_t ntoh32(uint32_t const net)
2223 | ((uint32_t) data[0] << 24);
2324 }
2425 #endif
26+#endif
2527
2628 #if __STDC_VERSION__ >= 201112L
2729 struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
Msrc/test.c
@@ -70,6 +70,7 @@ static void test2(void)
7070 String* string;
7171 String* string2;
7272 char* cstring;
73+ size_t* tmp;
7374
7475 string = new_string();
7576
@@ -103,6 +104,15 @@ static void test2(void)
103104
104105 delete_string(string2);
105106 delete_string(string);
107+
108+ string = from_cstring("abcd");
109+ string2 = from_cstring("cd");
110+ tmp = string_find_str(string, string2);
111+ assert(*tmp == 2);
112+ free(tmp);
113+
114+ delete_string(string2);
115+ delete_string(string);
106116 }
107117
108118 static void test3(void)
Msrc/vector.c
@@ -45,7 +45,7 @@ void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
4545 if(rmv)
4646 rmv(vector->items[index]);
4747 memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items));
48- --vector->length;
48+ vector->length--;
4949 }
5050 }
5151
@@ -60,8 +60,7 @@ void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv
6060
6161 size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*))
6262 {
63- size_t* ret;
64- size_t i;
63+ size_t i, *ret;
6564
6665 for(i = 0; i < haystack->length; i++)
6766 {
@@ -82,7 +81,7 @@ bool_t vector_insert(Vector* vector, size_t index, void* item)
8281
8382 memmove(vector->items+index+1, vector->items+index, (vector->length-index)*sizeof(*vector->items));
8483 vector->items[index] = item;
85- ++vector->length;
84+ vector->length++;
8685 return b_true;
8786 }
8887