emalloc.c 845 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*====================================================================*
  2. *
  3. * void * emalloc (size_t length)
  4. *
  5. * error.h
  6. *
  7. * attempt to allocate memory using malloc(); return the memory address
  8. * on success; print an error message on stderr and then terminate the
  9. * program on failure;
  10. *
  11. * Motley Tools by Charles Maier;
  12. * Copyright (c) 2001-2006 by Charles Maier Associates;
  13. * Licensed under the Internet Software Consortium License;
  14. *
  15. *--------------------------------------------------------------------*/
  16. #ifndef EMALLOC_SOURCE
  17. #define EMALLOC_SOURCE
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include "../tools/error.h"
  22. void * emalloc (size_t length)
  23. {
  24. void * memory = malloc (length);
  25. if (!memory)
  26. {
  27. error (1, errno, "need %lu bytes", (long)(length));
  28. }
  29. return (memory);
  30. }
  31. #endif