memory.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* GNU Objective C Runtime Memory allocation functions
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 3, or (at your option) any
  8. later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file includes the standard functions for memory allocation and
  21. disposal. Users should use these functions in their ObjC programs
  22. so that they work properly with garbage collectors. */
  23. /* TODO: Turn these into macros or inline functions. */
  24. #include "objc-private/common.h"
  25. #include "objc-private/error.h"
  26. /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
  27. malloc, free, etc. on some platforms. It is unclear if we still
  28. need it, but it can't hurt. */
  29. #define __USE_FIXED_PROTOTYPES__
  30. #include <stdlib.h>
  31. #include "objc/runtime.h"
  32. #if OBJC_WITH_GC
  33. #include <gc/gc.h>
  34. void *
  35. objc_malloc (size_t size)
  36. {
  37. void *res = (void *)(GC_malloc (size));
  38. if (! res)
  39. _objc_abort ("Virtual memory exhausted\n");
  40. return res;
  41. }
  42. void *
  43. objc_atomic_malloc (size_t size)
  44. {
  45. void *res = (void *)(GC_malloc_atomic (size));
  46. if (! res)
  47. _objc_abort ("Virtual memory exhausted\n");
  48. return res;
  49. }
  50. void *
  51. objc_realloc (void *mem, size_t size)
  52. {
  53. void *res = (void *)(GC_realloc (mem, size));
  54. if (! res)
  55. _objc_abort ("Virtual memory exhausted\n");
  56. return res;
  57. }
  58. void *
  59. objc_calloc (size_t nelem, size_t size)
  60. {
  61. /* Note that GC_malloc returns cleared memory (see documentation) so
  62. there is no need to clear it. */
  63. void *res = (void *)(GC_malloc (nelem * size));
  64. if (! res)
  65. _objc_abort ("Virtual memory exhausted\n");
  66. return res;
  67. }
  68. void
  69. objc_free (void *mem __attribute__ ((__unused__)))
  70. {
  71. return;
  72. }
  73. #else
  74. void *
  75. objc_malloc (size_t size)
  76. {
  77. void *res = (void *)(malloc (size));
  78. if (! res)
  79. _objc_abort ("Virtual memory exhausted\n");
  80. return res;
  81. }
  82. void *
  83. objc_atomic_malloc (size_t size)
  84. {
  85. void *res = (void *)(malloc (size));
  86. if (! res)
  87. _objc_abort ("Virtual memory exhausted\n");
  88. return res;
  89. }
  90. void *
  91. objc_realloc (void *mem, size_t size)
  92. {
  93. void *res = (void *)(realloc (mem, size));
  94. if (! res)
  95. _objc_abort ("Virtual memory exhausted\n");
  96. return res;
  97. }
  98. void *
  99. objc_calloc (size_t nelem, size_t size)
  100. {
  101. void *res = (void *)(calloc (nelem, size));
  102. if (! res)
  103. _objc_abort ("Virtual memory exhausted\n");
  104. return res;
  105. }
  106. void
  107. objc_free (void *mem)
  108. {
  109. free (mem);
  110. }
  111. #endif /* !OBJC_WITH_GC */