Files
puzzles/malloc.c
Simon Tatham 50edaa578b Miscellaneous fixes from James Harvey's PalmOS porting work:
- fixed numerous memory leaks (not Palm-specific)
 - corrected a couple of 32-bit-int assumptions (vital for Palm but
   generally a good thing anyway)
 - lifted a few function pointer types into explicit typedefs
   (neutral for me but convenient for the source-munging Perl
   scripts he uses to deal with Palm code segment rules)
 - lifted a few function-level static arrays into global static
   arrays (neutral for me but apparently works round a Palm tools
   bug)
 - a couple more presets in Rectangles (so that Palm, or any other
   slow platform which can't handle the larger sizes easily, can
   still have some variety available)
 - in Solo, arranged a means of sharing scratch space between calls
   to nsolve to prevent a lot of redundant malloc/frees (gives a 10%
   speed increase even on existing platforms)

[originally from svn r5897]
2005-06-01 17:47:56 +00:00

54 lines
991 B
C

/*
* 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.
*/
void *smalloc(size_t size) {
void *p;
p = malloc(size);
if (!p)
fatal("out of memory");
return p;
}
/*
* sfree should guaranteeably deal gracefully with freeing NULL
*/
void sfree(void *p) {
if (p) {
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 {
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;
}