aboutsummaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.c62
-rw-r--r--src/util/util.h59
2 files changed, 121 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c
new file mode 100644
index 0000000..7419d0c
--- /dev/null
+++ b/src/util/util.c
@@ -0,0 +1,62 @@
1/* See LICENSE file for copyright and license details. */
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "util.h"
9
10/*
11 * die()
12 * Function to print an error message and terminate the program.
13 * Takes a formatted string (`fmt`) and additional arguments (as in `printf`).
14 * This function uses variable arguments and will print the formatted error
15 * message to `stderr` and then exit the program with a non-zero status
16 * (typically 1).
17 *
18 * Usage:
19 * die("Error occurred in function %s", __func__);
20 */
21void die(const char *format_string, ...) {
22 va_list argument_list;
23 va_start(argument_list, format_string);
24 vfprintf(stderr, format_string, argument_list);
25 va_end(argument_list);
26
27 if (format_string[0] && format_string[strlen(format_string) - 1] == ':') {
28 fputc(' ', stderr);
29 perror(NULL);
30 } else
31 fputc('\n', stderr);
32
33 exit(1);
34}
35
36/*
37 * ecalloc()
38 * Function to allocate memory for an array of elements, initializing the memory
39 * to zero. This function works like `calloc()`, but it adds error handling. If
40 * memory allocation fails, it will call the `die()` function to print an error
41 * message and terminate the program.
42 *
43 * Parameters:
44 * - `num_elements`: The number of elements to allocate memory for.
45 * - `element_size`: The size of each element in bytes.
46 *
47 * Returns:
48 * - A pointer to the allocated memory (of type `void *`).
49 * - If the allocation fails, the program will terminate via `die()`.
50 *
51 * Usage:
52 * int *array = ecalloc(10, sizeof(int)); // Allocate memory for an array of
53 * 10 integers.
54 */
55void *ecalloc(size_t num_elements, size_t element_size) {
56 void *allocated_memory;
57
58 if (!(allocated_memory = calloc(num_elements, element_size)))
59 die("calloc:");
60
61 return allocated_memory;
62}
diff --git a/src/util/util.h b/src/util/util.h
new file mode 100644
index 0000000..6ef3c86
--- /dev/null
+++ b/src/util/util.h
@@ -0,0 +1,59 @@
1/* See LICENSE file for copyright and license details. */
2
3/*
4 * MAX(A, B)
5 * Macro to compute the maximum of two values.
6 * It returns the greater of the two values, A or B.
7 * Usage: MAX(3, 5) -> 5
8 */
9#define MAX(A, B) ((A) > (B) ? (A) : (B))
10
11/*
12 * MIN(A, B)
13 * Macro to compute the minimum of two values.
14 * It returns the lesser of the two values, A or B.
15 * Usage: MIN(3, 5) -> 3
16 */
17#define MIN(A, B) ((A) < (B) ? (A) : (B))
18
19/*
20 * BETWEEN(X, A, B)
21 * Macro to check if a value X is between two values A and B (inclusive).
22 * It returns true (1) if X is between A and B, and false (0) otherwise.
23 * Usage: BETWEEN(4, 1, 5) -> 1 (true), BETWEEN(6, 1, 5) -> 0 (false)
24 */
25#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
26
27/*
28 * die()
29 * Function to print an error message and terminate the program.
30 * Takes a formatted string (`fmt`) and additional arguments (as in `printf`).
31 * This function uses variable arguments and will print the formatted error
32 * message to `stderr` and then exit the program with a non-zero status
33 * (typically 1).
34 *
35 * Usage:
36 * die("Error occurred in function %s", __func__);
37 */
38void die(const char *fmt, ...);
39
40/*
41 * ecalloc()
42 * Function to allocate memory for an array of elements, initializing the memory
43 * to zero. This function works like `calloc()`, but it adds error handling. If
44 * memory allocation fails, it will call the `die()` function to print an error
45 * message and terminate the program.
46 *
47 * Parameters:
48 * - `nmemb`: The number of elements to allocate memory for.
49 * - `size`: The size of each element in bytes.
50 *
51 * Returns:
52 * - A pointer to the allocated memory (of type `void *`).
53 * - If the allocation fails, the program will terminate via `die()`.
54 *
55 * Usage:
56 * int *arr = ecalloc(10, sizeof(int)); // Allocate memory for an array of 10
57 * integers.
58 */
59void *ecalloc(size_t nmemb, size_t size);