10Sstevel@tonic-gate/* This file is automatically generated. DO NOT EDIT! */ 20Sstevel@tonic-gate/* Generated from: NetBSD: mknative-gdb,v 1.16 2023/07/31 17:09:59 christos Exp */ 30Sstevel@tonic-gate/* Generated from: NetBSD: mknative.common,v 1.16 2018/04/15 15:13:37 christos Exp */ 40Sstevel@tonic-gate 511913SRoger.Faulkner@Sun.COM/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ 611913SRoger.Faulkner@Sun.COM/* Variable-sized buffer with on-stack default allocation. 70Sstevel@tonic-gate Copyright (C) 2015-2022 Free Software Foundation, Inc. 80Sstevel@tonic-gate This file is part of the GNU C Library. 90Sstevel@tonic-gate 100Sstevel@tonic-gate The GNU C Library is free software; you can redistribute it and/or 110Sstevel@tonic-gate modify it under the terms of the GNU Lesser General Public 120Sstevel@tonic-gate License as published by the Free Software Foundation; either 130Sstevel@tonic-gate version 2.1 of the License, or (at your option) any later version. 140Sstevel@tonic-gate 150Sstevel@tonic-gate The GNU C Library is distributed in the hope that it will be useful, 160Sstevel@tonic-gate but WITHOUT ANY WARRANTY; without even the implied warranty of 170Sstevel@tonic-gate MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 180Sstevel@tonic-gate Lesser General Public License for more details. 190Sstevel@tonic-gate 200Sstevel@tonic-gate You should have received a copy of the GNU Lesser General Public 2111913SRoger.Faulkner@Sun.COM License along with the GNU C Library; if not, see 220Sstevel@tonic-gate <https://www.gnu.org/licenses/>. */ 2311913SRoger.Faulkner@Sun.COM 240Sstevel@tonic-gate#ifndef _SCRATCH_BUFFER_H 250Sstevel@tonic-gate#define _SCRATCH_BUFFER_H 260Sstevel@tonic-gate 270Sstevel@tonic-gate/* Scratch buffers with a default stack allocation and fallback to 280Sstevel@tonic-gate heap allocation. It is expected that this function is used in this 290Sstevel@tonic-gate way: 300Sstevel@tonic-gate 310Sstevel@tonic-gate struct scratch_buffer tmpbuf; 320Sstevel@tonic-gate scratch_buffer_init (&tmpbuf); 330Sstevel@tonic-gate 340Sstevel@tonic-gate while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length)) 350Sstevel@tonic-gate if (!scratch_buffer_grow (&tmpbuf)) 360Sstevel@tonic-gate return -1; 370Sstevel@tonic-gate 380Sstevel@tonic-gate scratch_buffer_free (&tmpbuf); 390Sstevel@tonic-gate return 0; 400Sstevel@tonic-gate 410Sstevel@tonic-gate The allocation functions (scratch_buffer_grow, 420Sstevel@tonic-gate scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make 430Sstevel@tonic-gate sure that the heap allocation, if any, is freed, so that the code 440Sstevel@tonic-gate above does not have a memory leak. The buffer still remains in a 450Sstevel@tonic-gate state that can be deallocated using scratch_buffer_free, so a loop 460Sstevel@tonic-gate like this is valid as well: 470Sstevel@tonic-gate 480Sstevel@tonic-gate struct scratch_buffer tmpbuf; 490Sstevel@tonic-gate scratch_buffer_init (&tmpbuf); 500Sstevel@tonic-gate 510Sstevel@tonic-gate while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length)) 520Sstevel@tonic-gate if (!scratch_buffer_grow (&tmpbuf)) 530Sstevel@tonic-gate break; 540Sstevel@tonic-gate 550Sstevel@tonic-gate scratch_buffer_free (&tmpbuf); 560Sstevel@tonic-gate 570Sstevel@tonic-gate scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed 580Sstevel@tonic-gate to grow the buffer by at least 512 bytes. This means that when 590Sstevel@tonic-gate using the scratch buffer as a backing store for a non-character 600Sstevel@tonic-gate array whose element size, in bytes, is 512 or smaller, the scratch 610Sstevel@tonic-gate buffer only has to grow once to make room for at least one more 620Sstevel@tonic-gate element. 630Sstevel@tonic-gate*/ 640Sstevel@tonic-gate 650Sstevel@tonic-gate#include <stdbool.h> 660Sstevel@tonic-gate#include <stddef.h> 670Sstevel@tonic-gate#include <stdlib.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate/* Scratch buffer. Must be initialized with scratch_buffer_init 700Sstevel@tonic-gate before its use. */ 710Sstevel@tonic-gatestruct scratch_buffer { 720Sstevel@tonic-gate void *data; /* Pointer to the beginning of the scratch area. */ 730Sstevel@tonic-gate size_t length; /* Allocated space at the data pointer, in bytes. */ 740Sstevel@tonic-gate union { max_align_t __align; char __c[1024]; } __space; 750Sstevel@tonic-gate}; 760Sstevel@tonic-gate 770Sstevel@tonic-gate/* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space 780Sstevel@tonic-gate and BUFFER->length reflects the available space. */ 790Sstevel@tonic-gatestatic inline void 800Sstevel@tonic-gatescratch_buffer_init (struct scratch_buffer *buffer) 810Sstevel@tonic-gate{ 820Sstevel@tonic-gate buffer->data = buffer->__space.__c; 830Sstevel@tonic-gate buffer->length = sizeof (buffer->__space); 840Sstevel@tonic-gate} 850Sstevel@tonic-gate 860Sstevel@tonic-gate/* Deallocates *BUFFER (if it was heap-allocated). */ 870Sstevel@tonic-gatestatic inline void 880Sstevel@tonic-gatescratch_buffer_free (struct scratch_buffer *buffer) 890Sstevel@tonic-gate{ 900Sstevel@tonic-gate if (buffer->data != buffer->__space.__c) 910Sstevel@tonic-gate free (buffer->data); 920Sstevel@tonic-gate} 930Sstevel@tonic-gate 940Sstevel@tonic-gate/* Grow *BUFFER by some arbitrary amount. The buffer contents is NOT 950Sstevel@tonic-gate preserved. Return true on success, false on allocation failure (in 960Sstevel@tonic-gate which case the old buffer is freed). On success, the new buffer is 970Sstevel@tonic-gate larger than the previous size. On failure, *BUFFER is deallocated, 980Sstevel@tonic-gate but remains in a free-able state, and errno is set. */ 990Sstevel@tonic-gatebool __libc_scratch_buffer_grow (struct scratch_buffer *buffer); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate/* Alias for __libc_scratch_buffer_grow. */ 1020Sstevel@tonic-gatestatic inline _GL_ATTRIBUTE_ALWAYS_INLINE bool 1030Sstevel@tonic-gatescratch_buffer_grow (struct scratch_buffer *buffer) 1040Sstevel@tonic-gate{ 1050Sstevel@tonic-gate return _GL_LIKELY (__libc_scratch_buffer_grow (buffer)); 1060Sstevel@tonic-gate} 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate/* Like __libc_scratch_buffer_grow, but preserve the old buffer 1090Sstevel@tonic-gate contents on success, as a prefix of the new buffer. */ 1100Sstevel@tonic-gatebool __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate/* Alias for __libc_scratch_buffer_grow_preserve. */ 1130Sstevel@tonic-gatestatic inline _GL_ATTRIBUTE_ALWAYS_INLINE bool 1140Sstevel@tonic-gatescratch_buffer_grow_preserve (struct scratch_buffer *buffer) 1150Sstevel@tonic-gate{ 1160Sstevel@tonic-gate return _GL_LIKELY (__libc_scratch_buffer_grow_preserve (buffer)); 1170Sstevel@tonic-gate} 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate/* Grow *BUFFER so that it can store at least NELEM elements of SIZE 1200Sstevel@tonic-gate bytes. The buffer contents are NOT preserved. Both NELEM and SIZE 1210Sstevel@tonic-gate can be zero. Return true on success, false on allocation failure 1220Sstevel@tonic-gate (in which case the old buffer is freed, but *BUFFER remains in a 1230Sstevel@tonic-gate free-able state, and errno is set). It is unspecified whether this 1240Sstevel@tonic-gate function can reduce the array size. */ 1250Sstevel@tonic-gatebool __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer, 1260Sstevel@tonic-gate size_t nelem, size_t size); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate/* Alias for __libc_scratch_set_array_size. */ 1290Sstevel@tonic-gatestatic inline _GL_ATTRIBUTE_ALWAYS_INLINE bool 1300Sstevel@tonic-gatescratch_buffer_set_array_size (struct scratch_buffer *buffer, 1310Sstevel@tonic-gate size_t nelem, size_t size) 1320Sstevel@tonic-gate{ 1330Sstevel@tonic-gate return _GL_LIKELY (__libc_scratch_buffer_set_array_size 1340Sstevel@tonic-gate (buffer, nelem, size)); 1350Sstevel@tonic-gate} 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate/* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block, 1380Sstevel@tonic-gate deallocating *BUFFER if it was heap-allocated. SIZE must be at 1390Sstevel@tonic-gate most *BUFFER's size. Return NULL (setting errno) on memory 1400Sstevel@tonic-gate exhaustion. */ 1410Sstevel@tonic-gatevoid *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, 1420Sstevel@tonic-gate size_t size); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate/* Alias for __libc_scratch_dupfree. */ 1450Sstevel@tonic-gatestatic inline _GL_ATTRIBUTE_ALWAYS_INLINE void * 1460Sstevel@tonic-gatescratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size) 1470Sstevel@tonic-gate{ 1480Sstevel@tonic-gate void *r = __libc_scratch_buffer_dupfree (buffer, size); 1490Sstevel@tonic-gate return _GL_LIKELY (r != NULL) ? r : NULL; 1500Sstevel@tonic-gate} 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate#endif /* _SCRATCH_BUFFER_H */ 1530Sstevel@tonic-gate