lt__alloc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* lt__alloc.c -- internal memory management interface
  2. Copyright (C) 2004, 2006-2007, 2011-2015 Free Software Foundation,
  3. Inc.
  4. Written by Gary V. Vaughan, 2004
  5. NOTE: The canonical source of this file is maintained with the
  6. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  7. GNU Libltdl is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. As a special exception to the GNU Lesser General Public License,
  12. if you distribute this file as part of a program or library that
  13. is built using GNU Libtool, you may include this file under the
  14. same distribution terms that you use for the rest of that program.
  15. GNU Libltdl is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  21. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  22. or obtained by writing to the Free Software Foundation, Inc.,
  23. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #include "lt__private.h"
  26. #include <stdio.h>
  27. #include "lt__alloc.h"
  28. static void alloc_die_default (void);
  29. void (*lt__alloc_die) (void) = alloc_die_default;
  30. /* Unless overridden, exit on memory failure. */
  31. static void
  32. alloc_die_default (void)
  33. {
  34. fprintf (stderr, "Out of memory.\n");
  35. exit (EXIT_FAILURE);
  36. }
  37. void *
  38. lt__malloc (size_t n)
  39. {
  40. void *mem;
  41. if (! (mem = malloc (n)))
  42. (*lt__alloc_die) ();
  43. return mem;
  44. }
  45. void *
  46. lt__zalloc (size_t n)
  47. {
  48. void *mem;
  49. if ((mem = lt__malloc (n)))
  50. memset (mem, 0, n);
  51. return mem;
  52. }
  53. void *
  54. lt__realloc (void *mem, size_t n)
  55. {
  56. if (! (mem = realloc (mem, n)))
  57. (*lt__alloc_die) ();
  58. return mem;
  59. }
  60. void *
  61. lt__memdup (void const *mem, size_t n)
  62. {
  63. void *newmem;
  64. if ((newmem = lt__malloc (n)))
  65. return memcpy (newmem, mem, n);
  66. return 0;
  67. }
  68. char *
  69. lt__strdup (const char *string)
  70. {
  71. return (char *) lt__memdup (string, strlen (string) +1);
  72. }