mi_alloc.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* This file is part of the GNU libxmi package. Copyright (C) 1998, 1999,
  2. 2000, 2005, Free Software Foundation, Inc.
  3. The GNU libxmi package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU libxmi package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* Wrappers for standard storage allocation functions. The tests for zero
  16. size, etc., are necessitated by the way in which the original X11
  17. scan-conversion code was written. */
  18. #include "sys-defines.h"
  19. #include "extern.h"
  20. #include "xmi.h"
  21. #include "mi_spans.h"
  22. #include "mi_api.h"
  23. /* wrapper for malloc() */
  24. void *
  25. mi_xmalloc (size_t size)
  26. {
  27. void * p;
  28. if (size == 0)
  29. return (void *)NULL;
  30. p = (void *) malloc (size);
  31. if (p == (void *)NULL)
  32. {
  33. fprintf (stderr, "libxmi: ");
  34. perror ("out of memory");
  35. exit (EXIT_FAILURE);
  36. }
  37. return p;
  38. }
  39. /* wrapper for calloc() */
  40. void *
  41. mi_xcalloc (size_t nmemb, size_t size)
  42. {
  43. void * p;
  44. if (size == 0)
  45. return (void *)NULL;
  46. p = (void *) calloc (nmemb, size);
  47. if (p == (void *)NULL)
  48. {
  49. fprintf (stderr, "libxmi: ");
  50. perror ("out of memory");
  51. exit (EXIT_FAILURE);
  52. }
  53. return p;
  54. }
  55. /* wrapper for realloc() */
  56. void *
  57. mi_xrealloc (void * p, size_t size)
  58. {
  59. if (!p)
  60. return mi_xmalloc (size);
  61. else
  62. {
  63. if (size == 0)
  64. {
  65. free (p);
  66. return (void *)NULL;
  67. }
  68. p = (void *) realloc (p, size);
  69. if (p == (void *)NULL)
  70. {
  71. fprintf (stderr, "libxmi: ");
  72. perror ("out of memory");
  73. exit (EXIT_FAILURE);
  74. }
  75. return p;
  76. }
  77. }