diff options
| author | Franklin Wei <frankhwei536@gmail.com> | 2016-11-24 15:38:20 -0500 |
|---|---|---|
| committer | Franklin Wei <frankhwei536@gmail.com> | 2016-11-24 15:38:20 -0500 |
| commit | 46ae5224a5c39ee2bc3945d5f5511d290a3b926e (patch) | |
| tree | a3d999bf49d8f4b35a8704977ea71c7262a00e0c /apps/plugins/puzzles/malloc.c | |
| parent | c1400ad87c5e8d02411f8add6aedd4da268590f3 (diff) | |
| parent | f311285f23cf7f6c664bc712fab2ea38d7cc2f8c (diff) | |
| download | rockbox-46ae5224a5c39ee2bc3945d5f5511d290a3b926e.zip rockbox-46ae5224a5c39ee2bc3945d5f5511d290a3b926e.tar.gz rockbox-46ae5224a5c39ee2bc3945d5f5511d290a3b926e.tar.bz2 rockbox-46ae5224a5c39ee2bc3945d5f5511d290a3b926e.tar.xz | |
Merge branch 'puzzles' into working
Change-Id: I66c3920266e141767b776203e0c4f0076260dadc
Diffstat (limited to 'apps/plugins/puzzles/malloc.c')
| -rw-r--r-- | apps/plugins/puzzles/malloc.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/apps/plugins/puzzles/malloc.c b/apps/plugins/puzzles/malloc.c new file mode 100644 index 0000000..d9943c6 --- /dev/null +++ b/apps/plugins/puzzles/malloc.c @@ -0,0 +1,61 @@ +/* + * malloc.c: safe wrappers around malloc, realloc, free, strdup + */ + +#include <stdlib.h> +#include <string.h> +#include "puzzles.h" + +/* + * smalloc should guarantee to return a useful pointer - Halibut + * can do nothing except die when it's out of memory anyway. + */ + +int allocs = 0; +int frees = 0; + +void *smalloc(size_t size) { + void *p; + p = malloc(size); + LOGF("allocs: %d", ++allocs); + if (!p) + fatal("out of memory"); + return p; +} + +/* + * sfree should guaranteeably deal gracefully with freeing NULL + */ +void sfree(void *p) { + if (p) { + ++frees; + LOGF("frees: %d, total outstanding: %d", frees, allocs - frees); + free(p); + } +} + +/* + * srealloc should guaranteeably be able to realloc NULL + */ +void *srealloc(void *p, size_t size) { + void *q; + if (p) { + q = realloc(p, size); + } else { + LOGF("allocs: %d", ++allocs); + q = malloc(size); + } + if (!q) + fatal("out of memory"); + return q; +} + +/* + * dupstr is like strdup, but with the never-return-NULL property + * of smalloc (and also reliably defined in all environments :-) + */ +char *dupstr(const char *s) { + char *r = smalloc(1+strlen(s)); + strcpy(r,s); + return r; +} |