|
|
|||
File indexing completed on 2026-05-06 08:41:16
0001 /* GLIB - Library of useful routines for C programming 0002 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-or-later 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Lesser General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2.1 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Lesser General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Lesser General Public 0017 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 /* 0021 * Modified by the GLib Team and others 1997-2000. See the AUTHORS 0022 * file for a list of people on the GLib Team. See the ChangeLog 0023 * files for a list of changes. These files are distributed with 0024 * GLib at ftp://ftp.gtk.org/pub/gtk/. 0025 */ 0026 0027 #ifndef __G_ALLOCA_H__ 0028 #define __G_ALLOCA_H__ 0029 0030 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) 0031 #error "Only <glib.h> can be included directly." 0032 #endif 0033 0034 #include <glib/gtypes.h> 0035 #include <string.h> 0036 0037 #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H) 0038 # include <alloca.h> 0039 #elif defined(__GNUC__) 0040 /* GCC does the right thing */ 0041 # undef alloca 0042 # define alloca(size) __builtin_alloca (size) 0043 #elif defined (GLIB_HAVE_ALLOCA_H) 0044 /* a native and working alloca.h is there */ 0045 # include <alloca.h> 0046 #else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */ 0047 # if defined(_MSC_VER) || defined(__DMC__) 0048 # include <malloc.h> 0049 # define alloca _alloca 0050 # else /* !_MSC_VER && !__DMC__ */ 0051 # ifdef _AIX 0052 # pragma alloca 0053 # else /* !_AIX */ 0054 # ifndef alloca /* predefined by HP cc +Olibcalls */ 0055 G_BEGIN_DECLS 0056 char *alloca (); 0057 G_END_DECLS 0058 # endif /* !alloca */ 0059 # endif /* !_AIX */ 0060 # endif /* !_MSC_VER && !__DMC__ */ 0061 #endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */ 0062 0063 /** 0064 * g_alloca: 0065 * @size: number of bytes to allocate. 0066 * 0067 * Allocates @size bytes on the stack; these bytes will be freed when the current 0068 * stack frame is cleaned up. This macro essentially just wraps the alloca() 0069 * function present on most UNIX variants. 0070 * Thus it provides the same advantages and pitfalls as alloca(): 0071 * 0072 * - alloca() is very fast, as on most systems it's implemented by just adjusting 0073 * the stack pointer register. 0074 * 0075 * - It doesn't cause any memory fragmentation, within its scope, separate alloca() 0076 * blocks just build up and are released together at function end. 0077 * 0078 * - Allocation sizes have to fit into the current stack frame. For instance in a 0079 * threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes, 0080 * so be sparse with alloca() uses. 0081 * 0082 * - Allocation failure due to insufficient stack space is not indicated with a %NULL 0083 * return like e.g. with malloc(). Instead, most systems probably handle it the same 0084 * way as out of stack space situations from infinite function recursion, i.e. 0085 * with a segmentation fault. 0086 * 0087 * - Allowing @size to be specified by an untrusted party would allow for them 0088 * to trigger a segmentation fault by specifying a large size, leading to a 0089 * denial of service vulnerability. @size must always be entirely under the 0090 * control of the program. 0091 * 0092 * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays. 0093 * Stack space allocated with alloca() in the same scope as a variable sized array 0094 * will be freed together with the variable sized array upon exit of that scope, and 0095 * not upon exit of the enclosing function scope. 0096 * 0097 * Returns: space for @size bytes, allocated on the stack 0098 */ 0099 #define g_alloca(size) alloca (size) 0100 0101 /** 0102 * g_alloca0: 0103 * @size: number of bytes to allocate. 0104 * 0105 * Wraps g_alloca() and initializes allocated memory to zeroes. 0106 * If @size is `0` it returns %NULL. 0107 * 0108 * Note that the @size argument will be evaluated multiple times. 0109 * 0110 * Returns: (nullable) (transfer full): space for @size bytes, allocated on the stack 0111 * 0112 * Since: 2.72 0113 */ 0114 #define g_alloca0(size) ((size) == 0 ? NULL : memset (g_alloca (size), 0, (size))) 0115 0116 /** 0117 * g_newa: 0118 * @struct_type: Type of memory chunks to be allocated 0119 * @n_structs: Number of chunks to be allocated 0120 * 0121 * Wraps g_alloca() in a more typesafe manner. 0122 * 0123 * As mentioned in the documentation for g_alloca(), @n_structs must always be 0124 * entirely under the control of the program, or you may introduce a denial of 0125 * service vulnerability. In addition, the multiplication of @struct_type by 0126 * @n_structs is not checked, so an overflow may lead to a remote code execution 0127 * vulnerability. 0128 * 0129 * Returns: Pointer to stack space for @n_structs chunks of type @struct_type 0130 */ 0131 #define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs))) 0132 0133 /** 0134 * g_newa0: 0135 * @struct_type: the type of the elements to allocate. 0136 * @n_structs: the number of elements to allocate. 0137 * 0138 * Wraps g_alloca0() in a more typesafe manner. 0139 * 0140 * Returns: (nullable) (transfer full): Pointer to stack space for @n_structs 0141 * chunks of type @struct_type 0142 * 0143 * Since: 2.72 0144 */ 0145 #define g_newa0(struct_type, n_structs) ((struct_type*) g_alloca0 (sizeof (struct_type) * (gsize) (n_structs))) 0146 0147 #endif /* __G_ALLOCA_H__ */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|