malloc.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * malloc.h: safe wrappers around malloc, realloc, free, strdup
  3. */
  4. #ifndef UMLWRAP_MALLOC_H
  5. #define UMLWRAP_MALLOC_H
  6. #include <stddef.h>
  7. /*
  8. * smalloc should guarantee to return a useful pointer - Halibut
  9. * can do nothing except die when it's out of memory anyway.
  10. */
  11. void *smalloc(size_t size);
  12. /*
  13. * srealloc should guaranteeably be able to realloc NULL
  14. */
  15. void *srealloc(void *p, size_t size);
  16. /*
  17. * sfree should guaranteeably deal gracefully with freeing NULL
  18. */
  19. void sfree(void *p);
  20. /*
  21. * dupstr is like strdup, but with the never-return-NULL property
  22. * of smalloc (and also reliably defined in all environments :-)
  23. */
  24. char *dupstr(const char *s);
  25. /*
  26. * snew allocates one instance of a given type, and casts the
  27. * result so as to type-check that you're assigning it to the
  28. * right kind of pointer. Protects against allocation bugs
  29. * involving allocating the wrong size of thing.
  30. */
  31. #define snew(type) \
  32. ( (type *) smalloc (sizeof (type)) )
  33. /*
  34. * snewn allocates n instances of a given type, for arrays.
  35. */
  36. #define snewn(number, type) \
  37. ( (type *) smalloc ((number) * sizeof (type)) )
  38. /*
  39. * sresize wraps realloc so that you specify the new number of
  40. * elements and the type of the element, with the same type-
  41. * checking advantages. Also type-checks the input pointer.
  42. */
  43. #define sresize(array, number, type) \
  44. ( (void)sizeof((array)-(type *)0), \
  45. (type *) srealloc ((array), (number) * sizeof (type)) )
  46. #endif /* UMLWRAP_MALLOC_H */