comparison src/core/util.h @ 59:52792b863ff7

misc: separate core from game
author David Demelier <markand@malikania.fr>
date Tue, 21 Jan 2020 12:42:33 +0100
parents src/util.h@f053a9f38c0e
children 80a913d25aa9
comparison
equal deleted inserted replaced
58:d7d88ac30611 59:52792b863ff7
1 /*
2 * util.h -- utilities
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef MOLKO_UTIL_H
20 #define MOLKO_UTIL_H
21
22 #include <stddef.h>
23
24 /**
25 * Get the number of elements in a static array.
26 *
27 * \param x the array
28 * \return the number of elements
29 */
30 #define nelem(x) sizeof ((x)) / sizeof ((x)[0])
31
32 /**
33 * Wrapper around malloc(3) that exits on allocation failure.
34 *
35 * \param size the size
36 * \return a pointer
37 * \post returned pointer will never be NULL
38 */
39 void *
40 emalloc(size_t size);
41
42 /**
43 * Wrapper around calloc(3) that exits on allocation failure.
44 *
45 * \param n the number of objects to allocate
46 * \param size the size per n
47 * \return a pointer
48 * \post returned pointer will never be NULL
49 */
50 void *
51 ecalloc(size_t n, size_t size);
52
53 /**
54 * Put the thread to sleep for a given amount of milliseconds.
55 *
56 * \param ms the number of milliseconds to wait
57 */
58 void
59 delay(unsigned int ms);
60
61 #endif /* !MOLKO_UTIL_H */