gdb_obstack.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Obstack wrapper for GDB.
  2. Copyright (C) 2002-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. #if !defined (GDB_OBSTACK_H)
  15. #define GDB_OBSTACK_H 1
  16. #include "obstack.h"
  17. /* Utility macros - wrap obstack alloc into something more robust. */
  18. template <typename T>
  19. static inline T*
  20. obstack_zalloc (struct obstack *ob)
  21. {
  22. static_assert (IsMallocable<T>::value, "Trying to use OBSTACK_ZALLOC with a \
  23. non-POD data type. Use obstack_new instead.");
  24. return ((T *) memset (obstack_alloc (ob, sizeof (T)), 0, sizeof (T)));
  25. }
  26. #define OBSTACK_ZALLOC(OBSTACK,TYPE) obstack_zalloc<TYPE> ((OBSTACK))
  27. template <typename T>
  28. static inline T *
  29. obstack_calloc (struct obstack *ob, size_t number)
  30. {
  31. static_assert (IsMallocable<T>::value, "Trying to use OBSTACK_CALLOC with a \
  32. non-POD data type. Use obstack_new instead.");
  33. return ((T *) memset (obstack_alloc (ob, number * sizeof (T)), 0,
  34. number * sizeof (T)));
  35. }
  36. #define OBSTACK_CALLOC(OBSTACK,NUMBER,TYPE) \
  37. obstack_calloc<TYPE> ((OBSTACK), (NUMBER))
  38. /* Allocate an object on OB and call its constructor. */
  39. template <typename T, typename... Args>
  40. static inline T*
  41. obstack_new (struct obstack *ob, Args&&... args)
  42. {
  43. T* object = (T *) obstack_alloc (ob, sizeof (T));
  44. object = new (object) T (std::forward<Args> (args)...);
  45. return object;
  46. }
  47. /* Unless explicitly specified, GDB obstacks always use xmalloc() and
  48. xfree(). */
  49. /* Note: ezannoni 2004-02-09: One could also specify the allocation
  50. functions using a special init function for each obstack,
  51. obstack_specify_allocation. However we just use obstack_init and
  52. let these defines here do the job. While one could argue the
  53. superiority of one approach over the other, we just chose one
  54. throughout. */
  55. #define obstack_chunk_alloc xmalloc
  56. #define obstack_chunk_free xfree
  57. #define obstack_grow_str(OBSTACK,STRING) \
  58. obstack_grow (OBSTACK, STRING, strlen (STRING))
  59. #define obstack_grow_str0(OBSTACK,STRING) \
  60. obstack_grow0 (OBSTACK, STRING, strlen (STRING))
  61. #define obstack_grow_wstr(OBSTACK, WSTRING) \
  62. obstack_grow (OBSTACK, WSTRING, sizeof (gdb_wchar_t) * gdb_wcslen (WSTRING))
  63. /* Concatenate NULL terminated variable argument list of `const char
  64. *' strings; return the new string. Space is found in the OBSTACKP.
  65. Argument list must be terminated by a sentinel expression `(char *)
  66. NULL'. */
  67. extern char *obconcat (struct obstack *obstackp, ...) ATTRIBUTE_SENTINEL;
  68. /* Duplicate STRING, returning an equivalent string that's allocated on the
  69. obstack OBSTACKP. */
  70. static inline char *
  71. obstack_strdup (struct obstack *obstackp, const char *string)
  72. {
  73. return (char *) obstack_copy0 (obstackp, string, strlen (string));
  74. }
  75. /* Duplicate STRING, returning an equivalent string that's allocated on the
  76. obstack OBSTACKP. */
  77. static inline char *
  78. obstack_strdup (struct obstack *obstackp, const std::string &string)
  79. {
  80. return (char *) obstack_copy0 (obstackp, string.c_str (),
  81. string.size ());
  82. }
  83. /* Duplicate the first N characters of STRING, returning a
  84. \0-terminated string that's allocated on the obstack OBSTACKP.
  85. Note that exactly N characters are copied, even if STRING is
  86. shorter. */
  87. static inline char *
  88. obstack_strndup (struct obstack *obstackp, const char *string, size_t n)
  89. {
  90. return (char *) obstack_copy0 (obstackp, string, n);
  91. }
  92. /* An obstack that frees itself on scope exit. */
  93. struct auto_obstack : obstack
  94. {
  95. auto_obstack ()
  96. { obstack_init (this); }
  97. ~auto_obstack ()
  98. { obstack_free (this, NULL); }
  99. DISABLE_COPY_AND_ASSIGN (auto_obstack);
  100. /* Free all memory in the obstack but leave it valid for further
  101. allocation. */
  102. void clear ()
  103. { obstack_free (this, obstack_base (this)); }
  104. };
  105. /* Objects are allocated on obstack instead of heap. */
  106. struct allocate_on_obstack
  107. {
  108. allocate_on_obstack () = default;
  109. void* operator new (size_t size, struct obstack *obstack)
  110. {
  111. return obstack_alloc (obstack, size);
  112. }
  113. void* operator new[] (size_t size, struct obstack *obstack)
  114. {
  115. return obstack_alloc (obstack, size);
  116. }
  117. void operator delete (void *memory) {}
  118. void operator delete[] (void *memory) {}
  119. };
  120. #endif