diff options
author | pacien | 2017-12-02 01:07:20 +0100 |
---|---|---|
committer | pacien | 2017-12-02 01:07:20 +0100 |
commit | c65669a785fba9b1f0f7539f47a677035ea06229 (patch) | |
tree | 8ba94389479b1bef7bd2bd07fddbc38bedfad569 | |
parent | 1c75b9dca56f4879798d9c9a5b9c48428e117448 (diff) | |
download | morpher-c65669a785fba9b1f0f7539f47a677035ea06229.tar.gz |
Add some common utility functions
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r-- | include/common/error.h | 24 | ||||
-rw-r--r-- | include/common/mem.h | 23 | ||||
-rw-r--r-- | src/common/error.c | 8 | ||||
-rw-r--r-- | src/common/mem.c | 12 |
4 files changed, 67 insertions, 0 deletions
diff --git a/include/common/error.h b/include/common/error.h new file mode 100644 index 0000000..522d646 --- /dev/null +++ b/include/common/error.h | |||
@@ -0,0 +1,24 @@ | |||
1 | #ifndef UPEM_MORPHING_ERROR | ||
2 | #define UPEM_MORPHING_ERROR | ||
3 | |||
4 | /** | ||
5 | * File: error.h | ||
6 | */ | ||
7 | |||
8 | /** | ||
9 | * Constants: Common errors | ||
10 | * | ||
11 | * OUT_OF_MEMORY_ERROR - when memory cannot be allocated for the program | ||
12 | */ | ||
13 | #define OUT_OF_MEMORY_ERROR "Out of memory" | ||
14 | |||
15 | /** | ||
16 | * Function: rage quit | ||
17 | * Logs the supplied error message to stderr before exiting the program with an error status code. | ||
18 | * | ||
19 | * Parameters; | ||
20 | * *msg - error message to log | ||
21 | */ | ||
22 | void rage_quit(const char *msg); | ||
23 | |||
24 | #endif | ||
diff --git a/include/common/mem.h b/include/common/mem.h new file mode 100644 index 0000000..1c46778 --- /dev/null +++ b/include/common/mem.h | |||
@@ -0,0 +1,23 @@ | |||
1 | #ifndef UPEM_MORPHING_MEM | ||
2 | #define UPEM_MORPHING_MEM | ||
3 | |||
4 | /** | ||
5 | * File: mem.h | ||
6 | */ | ||
7 | |||
8 | #include <stddef.h> | ||
9 | |||
10 | /** | ||
11 | * Function: malloc_or_die | ||
12 | * Allocates a memory chunk of the requested size. | ||
13 | * Interrupts the execution of the program in case of an error. | ||
14 | * | ||
15 | * Parameters: | ||
16 | * size - size of the chunk to allocate | ||
17 | * | ||
18 | * Returns: | ||
19 | * A non-NULL pointer to the allocated chunk | ||
20 | */ | ||
21 | void *malloc_or_die(size_t size); | ||
22 | |||
23 | #endif | ||
diff --git a/src/common/error.c b/src/common/error.c new file mode 100644 index 0000000..f28a6cd --- /dev/null +++ b/src/common/error.c | |||
@@ -0,0 +1,8 @@ | |||
1 | #include "common/error.h" | ||
2 | #include <stdlib.h> | ||
3 | #include <stdio.h> | ||
4 | |||
5 | void rage_quit(const char *msg) { | ||
6 | fprintf(stderr, "FATAL ERROR: %s\n", msg); | ||
7 | exit(1); | ||
8 | } | ||
diff --git a/src/common/mem.c b/src/common/mem.c new file mode 100644 index 0000000..855e010 --- /dev/null +++ b/src/common/mem.c | |||
@@ -0,0 +1,12 @@ | |||
1 | #include "common/mem.h" | ||
2 | #include <stdlib.h> | ||
3 | #include "common/error.h" | ||
4 | |||
5 | void *malloc_or_die(size_t size) { | ||
6 | void *ptr = malloc(size); | ||
7 | |||
8 | if (ptr == NULL) | ||
9 | rage_quit(OUT_OF_MEMORY_ERROR); | ||
10 | |||
11 | return ptr; | ||
12 | } | ||