1237611Sadrian/* This file is automatically generated. DO NOT EDIT! */ 2237611Sadrian/* Generated from: NetBSD: mknative-gdb,v 1.16 2023/07/31 17:09:59 christos Exp */ 3237611Sadrian/* Generated from: NetBSD: mknative.common,v 1.16 2018/04/15 15:13:37 christos Exp */ 4237611Sadrian 5237611Sadrian/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ 6237611Sadrian/* Variable-sized buffer with on-stack default allocation. 7237611Sadrian Copyright (C) 2015-2022 Free Software Foundation, Inc. 8237611Sadrian This file is part of the GNU C Library. 9237611Sadrian 10237611Sadrian The GNU C Library is free software; you can redistribute it and/or 11237611Sadrian modify it under the terms of the GNU Lesser General Public 12237611Sadrian License as published by the Free Software Foundation; either 13237611Sadrian version 2.1 of the License, or (at your option) any later version. 14237611Sadrian 15237611Sadrian The GNU C Library is distributed in the hope that it will be useful, 16237611Sadrian but WITHOUT ANY WARRANTY; without even the implied warranty of 17237611Sadrian MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18237611Sadrian Lesser General Public License for more details. 19237611Sadrian 20237611Sadrian You should have received a copy of the GNU Lesser General Public 21237611Sadrian License along with the GNU C Library; if not, see 22237611Sadrian <https://www.gnu.org/licenses/>. */ 23237611Sadrian 24237611Sadrian#ifndef _SCRATCH_BUFFER_H 25237611Sadrian#define _SCRATCH_BUFFER_H 26237611Sadrian 27243841Sadrian/* Scratch buffers with a default stack allocation and fallback to 28237611Sadrian heap allocation. It is expected that this function is used in this 29237611Sadrian way: 30237611Sadrian 31237611Sadrian struct scratch_buffer tmpbuf; 32237611Sadrian scratch_buffer_init (&tmpbuf); 33237611Sadrian 34237611Sadrian while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length)) 35237611Sadrian if (!scratch_buffer_grow (&tmpbuf)) 36237611Sadrian return -1; 37237611Sadrian 38237611Sadrian scratch_buffer_free (&tmpbuf); 39237611Sadrian return 0; 40237611Sadrian 41237611Sadrian The allocation functions (scratch_buffer_grow, 42237611Sadrian scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make 43237611Sadrian sure that the heap allocation, if any, is freed, so that the code 44237611Sadrian above does not have a memory leak. The buffer still remains in a 45237611Sadrian state that can be deallocated using scratch_buffer_free, so a loop 46237611Sadrian like this is valid as well: 47237611Sadrian 48237611Sadrian struct scratch_buffer tmpbuf; 49237611Sadrian scratch_buffer_init (&tmpbuf); 50243841Sadrian 51243841Sadrian while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length)) 52243841Sadrian if (!scratch_buffer_grow (&tmpbuf)) 53243841Sadrian break; 54243841Sadrian 55243841Sadrian scratch_buffer_free (&tmpbuf); 56243841Sadrian 57237611Sadrian scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed 58243841Sadrian to grow the buffer by at least 512 bytes. This means that when 59243841Sadrian using the scratch buffer as a backing store for a non-character 60243841Sadrian array whose element size, in bytes, is 512 or smaller, the scratch 61237611Sadrian buffer only has to grow once to make room for at least one more 62243841Sadrian element. 63243841Sadrian*/ 64243841Sadrian 65243841Sadrian#include <stdbool.h> 66243841Sadrian#include <stddef.h> 67243841Sadrian#include <stdlib.h> 68243841Sadrian 69243841Sadrian/* Scratch buffer. Must be initialized with scratch_buffer_init 70237611Sadrian before its use. */ 71243841Sadrianstruct scratch_buffer { 72243841Sadrian void *data; /* Pointer to the beginning of the scratch area. */ 73243841Sadrian size_t length; /* Allocated space at the data pointer, in bytes. */ 74237611Sadrian union { max_align_t __align; char __c[1024]; } __space; 75243841Sadrian}; 76243841Sadrian 77243841Sadrian/* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space 78243841Sadrian and BUFFER->length reflects the available space. */ 79243841Sadrianstatic inline void 80243841Sadrianscratch_buffer_init (struct scratch_buffer *buffer) 81243841Sadrian{ 82243841Sadrian buffer->data = buffer->__space.__c; 83243841Sadrian buffer->length = sizeof (buffer->__space); 84243841Sadrian} 85243841Sadrian 86237611Sadrian/* Deallocates *BUFFER (if it was heap-allocated). */ 87243841Sadrianstatic inline void 88243841Sadrianscratch_buffer_free (struct scratch_buffer *buffer) 89243841Sadrian{ 90237611Sadrian if (buffer->data != buffer->__space.__c) 91243841Sadrian free (buffer->data); 92243841Sadrian} 93243841Sadrian 94243841Sadrian/* Grow *BUFFER by some arbitrary amount. The buffer contents is NOT 95243841Sadrian preserved. Return true on success, false on allocation failure (in 96243841Sadrian which case the old buffer is freed). On success, the new buffer is 97243841Sadrian larger than the previous size. On failure, *BUFFER is deallocated, 98237611Sadrian but remains in a free-able state, and errno is set. */ 99243841Sadrianbool __libc_scratch_buffer_grow (struct scratch_buffer *buffer); 100243841Sadrian 101243841Sadrian/* Alias for __libc_scratch_buffer_grow. */ 102243841Sadrianstatic inline _GL_ATTRIBUTE_ALWAYS_INLINE bool 103243841Sadrianscratch_buffer_grow (struct scratch_buffer *buffer) 104243841Sadrian{ 105243841Sadrian return _GL_LIKELY (__libc_scratch_buffer_grow (buffer)); 106237611Sadrian} 107243841Sadrian 108243841Sadrian/* Like __libc_scratch_buffer_grow, but preserve the old buffer 109243841Sadrian contents on success, as a prefix of the new buffer. */ 110243841Sadrianbool __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer); 111243841Sadrian 112243841Sadrian/* Alias for __libc_scratch_buffer_grow_preserve. */ 113237611Sadrianstatic inline _GL_ATTRIBUTE_ALWAYS_INLINE bool 114243841Sadrianscratch_buffer_grow_preserve (struct scratch_buffer *buffer) 115243841Sadrian{ 116243841Sadrian return _GL_LIKELY (__libc_scratch_buffer_grow_preserve (buffer)); 117243841Sadrian} 118243841Sadrian 119237611Sadrian/* Grow *BUFFER so that it can store at least NELEM elements of SIZE 120237611Sadrian bytes. The buffer contents are NOT preserved. Both NELEM and SIZE 121237611Sadrian can be zero. Return true on success, false on allocation failure 122237611Sadrian (in which case the old buffer is freed, but *BUFFER remains in a 123237611Sadrian free-able state, and errno is set). It is unspecified whether this 124237611Sadrian function can reduce the array size. */ 125237611Sadrianbool __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer, 126237611Sadrian size_t nelem, size_t size); 127237611Sadrian 128237611Sadrian/* Alias for __libc_scratch_set_array_size. */ 129237611Sadrianstatic inline _GL_ATTRIBUTE_ALWAYS_INLINE bool 130237611Sadrianscratch_buffer_set_array_size (struct scratch_buffer *buffer, 131237611Sadrian size_t nelem, size_t size) 132237611Sadrian{ 133237611Sadrian return _GL_LIKELY (__libc_scratch_buffer_set_array_size 134237611Sadrian (buffer, nelem, size)); 135237611Sadrian} 136237611Sadrian 137237611Sadrian/* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block, 138237611Sadrian deallocating *BUFFER if it was heap-allocated. SIZE must be at 139237611Sadrian most *BUFFER's size. Return NULL (setting errno) on memory 140237611Sadrian exhaustion. */ 141237611Sadrianvoid *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, 142237611Sadrian size_t size); 143237611Sadrian 144237611Sadrian/* Alias for __libc_scratch_dupfree. */ 145237611Sadrianstatic inline _GL_ATTRIBUTE_ALWAYS_INLINE void * 146237611Sadrianscratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size) 147{ 148 void *r = __libc_scratch_buffer_dupfree (buffer, size); 149 return _GL_LIKELY (r != NULL) ? r : NULL; 150} 151 152#endif /* _SCRATCH_BUFFER_H */ 153