Mercurial > molko
annotate libcore/core/util.h @ 136:30b68089ae70
core: rework actions and a bit of drawables, closes #2492
In the effort of having as less as possible memory allocation in libcore, the
usage of actions and drawable no longer copy the original source parameter to
let user pass a heap allocated variable or a static storage one.
Update predefined drawable and actions to match these new needs.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 13 Oct 2020 09:38:44 +0200 |
parents | 789b23e01f52 |
children | eb0a7ab71023 |
rev | line source |
---|---|
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * util.h -- utilities |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 #ifndef MOLKO_UTIL_H |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 #define MOLKO_UTIL_H |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
21 |
70
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
22 /** |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
23 * \file util.h |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
24 * \brief Utilities. |
72
6203e1ac9b18
doc: improve doxygen documentation a lot
David Demelier <markand@malikania.fr>
parents:
70
diff
changeset
|
25 * \ingroup basics |
70
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
26 * |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
27 * This file contains several utilities. |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
28 * |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
29 * \note In contrast to other files, identifiers are not prefixed with `util_` |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
30 * for convenience. |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
31 */ |
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
32 |
95
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
33 #include <stdarg.h> |
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
34 #include <stddef.h> |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
35 |
95
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
36 #include "plat.h" |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
37 |
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
38 /** |
47
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
39 * Get the number of elements in a static array. |
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
40 * |
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
41 * \param x the array |
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
42 * \return the number of elements |
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
43 */ |
70
53b217afe122
doc: improve doxygen documentation
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
44 #define NELEM(x) sizeof ((x)) / sizeof ((x)[0]) |
47
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
45 |
f053a9f38c0e
core: implement basic scrolling, closes #2459
David Demelier <markand@malikania.fr>
parents:
39
diff
changeset
|
46 /** |
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
47 * Wrapper around malloc(3) that exits on allocation failure. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
48 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
49 * \param size the size |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 * \return a pointer |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
51 * \post returned pointer will never be NULL |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
52 */ |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
53 void * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
54 emalloc(size_t size); |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
55 |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
56 /** |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
57 * Wrapper around calloc(3) that exits on allocation failure. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
58 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
59 * \param n the number of objects to allocate |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
60 * \param size the size per n |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
61 * \return a pointer |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 * \post returned pointer will never be NULL |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
63 */ |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
64 void * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
65 ecalloc(size_t n, size_t size); |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
66 |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
67 /** |
65
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
68 * Copy the region specified by ptr. |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
69 * |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
70 * \pre ptr != NULL |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
71 * \param ptr the pointer |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
72 * \param size the size of the memory to copy |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
73 * \return a pointer |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
74 * \post returned pointer will never be NULL |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
75 */ |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
76 void * |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
77 ememdup(const void *ptr, size_t size); |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
78 |
80a913d25aa9
core: implement script, closes #2465 @2h
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
79 /** |
95
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
80 * Create a dynamically allocated string in the printf(3) format string. |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
81 * |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
82 * \pre fmt != NULL |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
83 * \return The heap allocated string. |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
84 * \post Returned string will never be NULL. |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
85 */ |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
86 char * |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
87 eprintf(const char *fmt, ...) PLAT_PRINTF(1, 2); |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
88 |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
89 /** |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
90 * Similar to \ref eprintf with arguments pointer. |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
91 * |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
92 * \copydoc eprintf |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
93 */ |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
94 char * |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
95 evprintf(const char *fmt, va_list ap) PLAT_PRINTF(1, 0); |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
96 |
e82eca4f8606
core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents:
72
diff
changeset
|
97 /** |
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
98 * Put the thread to sleep for a given amount of milliseconds. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
99 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
100 * \param ms the number of milliseconds to wait |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
101 */ |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
102 void |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
103 delay(unsigned int ms); |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
104 |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
105 #endif /* !MOLKO_UTIL_H */ |