alloc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Shared allocation functions for GDB, the GNU debugger.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* This file is unusual.
  15. Because both libiberty and readline define xmalloc and friends, the
  16. functions in this file can't appear in a library -- that will cause
  17. link errors.
  18. And, because we want to turn the common code into a library, this
  19. file can't live there.
  20. So, it lives in gdb and is built separately by gdb and gdbserver.
  21. Please be aware of this when modifying it.
  22. This also explains why this file includes common-defs.h and not
  23. defs.h or server.h -- we'd prefer to avoid depending on the
  24. GDBSERVER define when possible, and for this file it seemed
  25. simple to do so. */
  26. #include "gdbsupport/common-defs.h"
  27. #include "libiberty.h"
  28. #include "gdbsupport/errors.h"
  29. /* The xmalloc() (libiberty.h) family of memory management routines.
  30. These are like the ISO-C malloc() family except that they implement
  31. consistent semantics and guard against typical memory management
  32. problems. */
  33. /* NOTE: These are declared using PTR to ensure consistency with
  34. "libiberty.h". xfree() is GDB local. */
  35. PTR /* ARI: PTR */
  36. xmalloc (size_t size)
  37. {
  38. void *val;
  39. /* See libiberty/xmalloc.c. This function need's to match that's
  40. semantics. It never returns NULL. */
  41. if (size == 0)
  42. size = 1;
  43. val = malloc (size); /* ARI: malloc */
  44. if (val == NULL)
  45. malloc_failure (size);
  46. return val;
  47. }
  48. PTR /* ARI: PTR */
  49. xrealloc (PTR ptr, size_t size) /* ARI: PTR */
  50. {
  51. void *val;
  52. /* See libiberty/xmalloc.c. This function need's to match that's
  53. semantics. It never returns NULL. */
  54. if (size == 0)
  55. size = 1;
  56. if (ptr != NULL)
  57. val = realloc (ptr, size); /* ARI: realloc */
  58. else
  59. val = malloc (size); /* ARI: malloc */
  60. if (val == NULL)
  61. malloc_failure (size);
  62. return val;
  63. }
  64. PTR /* ARI: PTR */
  65. xcalloc (size_t number, size_t size)
  66. {
  67. void *mem;
  68. /* See libiberty/xmalloc.c. This function need's to match that's
  69. semantics. It never returns NULL. */
  70. if (number == 0 || size == 0)
  71. {
  72. number = 1;
  73. size = 1;
  74. }
  75. mem = calloc (number, size); /* ARI: xcalloc */
  76. if (mem == NULL)
  77. malloc_failure (number * size);
  78. return mem;
  79. }
  80. void
  81. xmalloc_failed (size_t size)
  82. {
  83. malloc_failure (size);
  84. }