summaryrefslogtreecommitdiff
path: root/apps/plugins/puzzles/src/malloc.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2017-04-29 18:21:56 -0400
committerFranklin Wei <git@fwei.tk>2017-04-29 18:24:42 -0400
commit881746789a489fad85aae8317555f73dbe261556 (patch)
treecec2946362c4698c8db3c10f3242ef546c2c22dd /apps/plugins/puzzles/src/malloc.c
parent03dd4b92be7dcd5c8ab06da3810887060e06abd5 (diff)
downloadrockbox-881746789a489fad85aae8317555f73dbe261556.tar.gz
rockbox-881746789a489fad85aae8317555f73dbe261556.zip
puzzles: refactor and resync with upstream
This brings puzzles up-to-date with upstream revision 2d333750272c3967cfd5cd3677572cddeaad5932, though certain changes made by me, including cursor-only Untangle and some compilation fixes remain. Upstream code has been moved to its separate subdirectory and future syncs can be done by simply copying over the new sources. Change-Id: Ia6506ca5f78c3627165ea6791d38db414ace0804
Diffstat (limited to 'apps/plugins/puzzles/src/malloc.c')
-rw-r--r--apps/plugins/puzzles/src/malloc.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/apps/plugins/puzzles/src/malloc.c b/apps/plugins/puzzles/src/malloc.c
new file mode 100644
index 0000000000..a7fa7c5adc
--- /dev/null
+++ b/apps/plugins/puzzles/src/malloc.c
@@ -0,0 +1,53 @@
1/*
2 * malloc.c: safe wrappers around malloc, realloc, free, strdup
3 */
4
5#include <stdlib.h>
6#include <string.h>
7#include "puzzles.h"
8
9/*
10 * smalloc should guarantee to return a useful pointer - Halibut
11 * can do nothing except die when it's out of memory anyway.
12 */
13void *smalloc(size_t size) {
14 void *p;
15 p = malloc(size);
16 if (!p)
17 fatal("out of memory");
18 return p;
19}
20
21/*
22 * sfree should guaranteeably deal gracefully with freeing NULL
23 */
24void sfree(void *p) {
25 if (p) {
26 free(p);
27 }
28}
29
30/*
31 * srealloc should guaranteeably be able to realloc NULL
32 */
33void *srealloc(void *p, size_t size) {
34 void *q;
35 if (p) {
36 q = realloc(p, size);
37 } else {
38 q = malloc(size);
39 }
40 if (!q)
41 fatal("out of memory");
42 return q;
43}
44
45/*
46 * dupstr is like strdup, but with the never-return-NULL property
47 * of smalloc (and also reliably defined in all environments :-)
48 */
49char *dupstr(const char *s) {
50 char *r = smalloc(1+strlen(s));
51 strcpy(r,s);
52 return r;
53}