1*6881a400Schristos /* Obstack wrapper for GDB. 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 2002-2023 Free Software Foundation, Inc. 4*6881a400Schristos 5*6881a400Schristos This file is part of GDB. 6*6881a400Schristos 7*6881a400Schristos This program is free software; you can redistribute it and/or modify 8*6881a400Schristos it under the terms of the GNU General Public License as published by 9*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 10*6881a400Schristos (at your option) any later version. 11*6881a400Schristos 12*6881a400Schristos This program is distributed in the hope that it will be useful, 13*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*6881a400Schristos GNU General Public License for more details. 16*6881a400Schristos 17*6881a400Schristos You should have received a copy of the GNU General Public License 18*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*6881a400Schristos 20*6881a400Schristos #if !defined (GDB_OBSTACK_H) 21*6881a400Schristos #define GDB_OBSTACK_H 1 22*6881a400Schristos 23*6881a400Schristos #include "obstack.h" 24*6881a400Schristos 25*6881a400Schristos /* Utility macros - wrap obstack alloc into something more robust. */ 26*6881a400Schristos 27*6881a400Schristos template <typename T> 28*6881a400Schristos static inline T* 29*6881a400Schristos obstack_zalloc (struct obstack *ob) 30*6881a400Schristos { 31*6881a400Schristos static_assert (IsMallocable<T>::value, "Trying to use OBSTACK_ZALLOC with a \ 32*6881a400Schristos non-POD data type. Use obstack_new instead."); 33*6881a400Schristos return ((T *) memset (obstack_alloc (ob, sizeof (T)), 0, sizeof (T))); 34*6881a400Schristos } 35*6881a400Schristos 36*6881a400Schristos #define OBSTACK_ZALLOC(OBSTACK,TYPE) obstack_zalloc<TYPE> ((OBSTACK)) 37*6881a400Schristos 38*6881a400Schristos template <typename T> 39*6881a400Schristos static inline T * 40*6881a400Schristos obstack_calloc (struct obstack *ob, size_t number) 41*6881a400Schristos { 42*6881a400Schristos static_assert (IsMallocable<T>::value, "Trying to use OBSTACK_CALLOC with a \ 43*6881a400Schristos non-POD data type. Use obstack_new instead."); 44*6881a400Schristos return ((T *) memset (obstack_alloc (ob, number * sizeof (T)), 0, 45*6881a400Schristos number * sizeof (T))); 46*6881a400Schristos } 47*6881a400Schristos 48*6881a400Schristos #define OBSTACK_CALLOC(OBSTACK,NUMBER,TYPE) \ 49*6881a400Schristos obstack_calloc<TYPE> ((OBSTACK), (NUMBER)) 50*6881a400Schristos 51*6881a400Schristos /* Allocate an object on OB and call its constructor. */ 52*6881a400Schristos 53*6881a400Schristos template <typename T, typename... Args> 54*6881a400Schristos static inline T* 55*6881a400Schristos obstack_new (struct obstack *ob, Args&&... args) 56*6881a400Schristos { 57*6881a400Schristos T* object = (T *) obstack_alloc (ob, sizeof (T)); 58*6881a400Schristos object = new (object) T (std::forward<Args> (args)...); 59*6881a400Schristos return object; 60*6881a400Schristos } 61*6881a400Schristos 62*6881a400Schristos /* Unless explicitly specified, GDB obstacks always use xmalloc() and 63*6881a400Schristos xfree(). */ 64*6881a400Schristos /* Note: ezannoni 2004-02-09: One could also specify the allocation 65*6881a400Schristos functions using a special init function for each obstack, 66*6881a400Schristos obstack_specify_allocation. However we just use obstack_init and 67*6881a400Schristos let these defines here do the job. While one could argue the 68*6881a400Schristos superiority of one approach over the other, we just chose one 69*6881a400Schristos throughout. */ 70*6881a400Schristos 71*6881a400Schristos #define obstack_chunk_alloc xmalloc 72*6881a400Schristos #define obstack_chunk_free xfree 73*6881a400Schristos 74*6881a400Schristos #define obstack_grow_str(OBSTACK,STRING) \ 75*6881a400Schristos obstack_grow (OBSTACK, STRING, strlen (STRING)) 76*6881a400Schristos #define obstack_grow_str0(OBSTACK,STRING) \ 77*6881a400Schristos obstack_grow0 (OBSTACK, STRING, strlen (STRING)) 78*6881a400Schristos 79*6881a400Schristos #define obstack_grow_wstr(OBSTACK, WSTRING) \ 80*6881a400Schristos obstack_grow (OBSTACK, WSTRING, sizeof (gdb_wchar_t) * gdb_wcslen (WSTRING)) 81*6881a400Schristos 82*6881a400Schristos /* Concatenate NULL terminated variable argument list of `const char 83*6881a400Schristos *' strings; return the new string. Space is found in the OBSTACKP. 84*6881a400Schristos Argument list must be terminated by a sentinel expression `(char *) 85*6881a400Schristos NULL'. */ 86*6881a400Schristos 87*6881a400Schristos extern char *obconcat (struct obstack *obstackp, ...) ATTRIBUTE_SENTINEL; 88*6881a400Schristos 89*6881a400Schristos /* Duplicate STRING, returning an equivalent string that's allocated on the 90*6881a400Schristos obstack OBSTACKP. */ 91*6881a400Schristos 92*6881a400Schristos static inline char * 93*6881a400Schristos obstack_strdup (struct obstack *obstackp, const char *string) 94*6881a400Schristos { 95*6881a400Schristos return (char *) obstack_copy0 (obstackp, string, strlen (string)); 96*6881a400Schristos } 97*6881a400Schristos 98*6881a400Schristos /* Duplicate STRING, returning an equivalent string that's allocated on the 99*6881a400Schristos obstack OBSTACKP. */ 100*6881a400Schristos 101*6881a400Schristos static inline char * 102*6881a400Schristos obstack_strdup (struct obstack *obstackp, const std::string &string) 103*6881a400Schristos { 104*6881a400Schristos return (char *) obstack_copy0 (obstackp, string.c_str (), 105*6881a400Schristos string.size ()); 106*6881a400Schristos } 107*6881a400Schristos 108*6881a400Schristos /* Duplicate the first N characters of STRING, returning a 109*6881a400Schristos \0-terminated string that's allocated on the obstack OBSTACKP. 110*6881a400Schristos Note that exactly N characters are copied, even if STRING is 111*6881a400Schristos shorter. */ 112*6881a400Schristos 113*6881a400Schristos static inline char * 114*6881a400Schristos obstack_strndup (struct obstack *obstackp, const char *string, size_t n) 115*6881a400Schristos { 116*6881a400Schristos return (char *) obstack_copy0 (obstackp, string, n); 117*6881a400Schristos } 118*6881a400Schristos 119*6881a400Schristos /* An obstack that frees itself on scope exit. */ 120*6881a400Schristos struct auto_obstack : obstack 121*6881a400Schristos { 122*6881a400Schristos auto_obstack () 123*6881a400Schristos { obstack_init (this); } 124*6881a400Schristos 125*6881a400Schristos ~auto_obstack () 126*6881a400Schristos { obstack_free (this, NULL); } 127*6881a400Schristos 128*6881a400Schristos DISABLE_COPY_AND_ASSIGN (auto_obstack); 129*6881a400Schristos 130*6881a400Schristos /* Free all memory in the obstack but leave it valid for further 131*6881a400Schristos allocation. */ 132*6881a400Schristos void clear () 133*6881a400Schristos { obstack_free (this, obstack_base (this)); } 134*6881a400Schristos }; 135*6881a400Schristos 136*6881a400Schristos /* Objects are allocated on obstack instead of heap. */ 137*6881a400Schristos 138*6881a400Schristos struct allocate_on_obstack 139*6881a400Schristos { 140*6881a400Schristos allocate_on_obstack () = default; 141*6881a400Schristos 142*6881a400Schristos void* operator new (size_t size, struct obstack *obstack) 143*6881a400Schristos { 144*6881a400Schristos return obstack_alloc (obstack, size); 145*6881a400Schristos } 146*6881a400Schristos 147*6881a400Schristos void* operator new[] (size_t size, struct obstack *obstack) 148*6881a400Schristos { 149*6881a400Schristos return obstack_alloc (obstack, size); 150*6881a400Schristos } 151*6881a400Schristos 152*6881a400Schristos void operator delete (void *memory) {} 153*6881a400Schristos void operator delete[] (void *memory) {} 154*6881a400Schristos }; 155*6881a400Schristos 156*6881a400Schristos #endif 157