(root)/
glib-2.79.0/
glib/
galloca.h
       1  /* GLIB - Library of useful routines for C programming
       2   * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       3   *
       4   * SPDX-License-Identifier: LGPL-2.1-or-later
       5   *
       6   * This library is free software; you can redistribute it and/or
       7   * modify it under the terms of the GNU Lesser General Public
       8   * License as published by the Free Software Foundation; either
       9   * version 2.1 of the License, or (at your option) any later version.
      10   *
      11   * This library is distributed in the hope that it will be useful,
      12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14   * Lesser General Public License for more details.
      15   *
      16   * You should have received a copy of the GNU Lesser General Public
      17   * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18   */
      19  
      20  /*
      21   * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
      22   * file for a list of people on the GLib Team.  See the ChangeLog
      23   * files for a list of changes.  These files are distributed with
      24   * GLib at ftp://ftp.gtk.org/pub/gtk/.
      25   */
      26  
      27  #ifndef __G_ALLOCA_H__
      28  #define __G_ALLOCA_H__
      29  
      30  #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
      31  #error "Only <glib.h> can be included directly."
      32  #endif
      33  
      34  #include <glib/gtypes.h>
      35  #include <string.h>
      36  
      37  #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
      38  # include <alloca.h>
      39  #elif defined(__GNUC__)
      40  /* GCC does the right thing */
      41  # undef alloca
      42  # define alloca(size)   __builtin_alloca (size)
      43  #elif defined (GLIB_HAVE_ALLOCA_H)
      44  /* a native and working alloca.h is there */ 
      45  # include <alloca.h>
      46  #else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
      47  # if defined(_MSC_VER) || defined(__DMC__)
      48  #  include <malloc.h>
      49  #  define alloca _alloca
      50  # else /* !_MSC_VER && !__DMC__ */
      51  #  ifdef _AIX
      52  #   pragma alloca
      53  #  else /* !_AIX */
      54  #   ifndef alloca /* predefined by HP cc +Olibcalls */
      55  G_BEGIN_DECLS
      56  char *alloca ();
      57  G_END_DECLS
      58  #   endif /* !alloca */
      59  #  endif /* !_AIX */
      60  # endif /* !_MSC_VER && !__DMC__ */
      61  #endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
      62  
      63  /**
      64   * g_alloca:
      65   * @size: number of bytes to allocate.
      66   * 
      67   * Allocates @size bytes on the stack; these bytes will be freed when the current
      68   * stack frame is cleaned up. This macro essentially just wraps the alloca()
      69   * function present on most UNIX variants.
      70   * Thus it provides the same advantages and pitfalls as alloca():
      71   *
      72   * - alloca() is very fast, as on most systems it's implemented by just adjusting
      73   *   the stack pointer register.
      74   *
      75   * - It doesn't cause any memory fragmentation, within its scope, separate alloca()
      76   *   blocks just build up and are released together at function end.
      77   *
      78   * - Allocation sizes have to fit into the current stack frame. For instance in a
      79   *   threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
      80   *   so be sparse with alloca() uses.
      81   *
      82   * - Allocation failure due to insufficient stack space is not indicated with a %NULL
      83   *   return like e.g. with malloc(). Instead, most systems probably handle it the same
      84   *   way as out of stack space situations from infinite function recursion, i.e.
      85   *   with a segmentation fault.
      86   *
      87   * - Allowing @size to be specified by an untrusted party would allow for them
      88   *   to trigger a segmentation fault by specifying a large size, leading to a
      89   *   denial of service vulnerability. @size must always be entirely under the
      90   *   control of the program.
      91   *
      92   * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
      93   *   Stack space allocated with alloca() in the same scope as a variable sized array
      94   *   will be freed together with the variable sized array upon exit of that scope, and
      95   *   not upon exit of the enclosing function scope.
      96   * 
      97   * Returns: space for @size bytes, allocated on the stack
      98   */
      99  #define g_alloca(size)		 alloca (size)
     100  
     101  /**
     102   * g_alloca0:
     103   * @size: number of bytes to allocate.
     104   *
     105   * Wraps g_alloca() and initializes allocated memory to zeroes.
     106   * If @size is `0` it returns %NULL.
     107   *
     108   * Note that the @size argument will be evaluated multiple times.
     109   *
     110   * Returns: (nullable) (transfer full): space for @size bytes, allocated on the stack
     111   *
     112   * Since: 2.72
     113   */
     114  #define g_alloca0(size)  ((size) == 0 ? NULL : memset (g_alloca (size), 0, (size)))
     115  
     116  /**
     117   * g_newa:
     118   * @struct_type: Type of memory chunks to be allocated
     119   * @n_structs: Number of chunks to be allocated
     120   * 
     121   * Wraps g_alloca() in a more typesafe manner.
     122   * 
     123   * As mentioned in the documentation for g_alloca(), @n_structs must always be
     124   * entirely under the control of the program, or you may introduce a denial of
     125   * service vulnerability. In addition, the multiplication of @struct_type by
     126   * @n_structs is not checked, so an overflow may lead to a remote code execution
     127   * vulnerability.
     128   *
     129   * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
     130   */
     131  #define g_newa(struct_type, n_structs)	((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
     132  
     133  /**
     134   * g_newa0:
     135   * @struct_type: the type of the elements to allocate.
     136   * @n_structs: the number of elements to allocate.
     137   *
     138   * Wraps g_alloca0() in a more typesafe manner.
     139   *
     140   * Returns: (nullable) (transfer full): Pointer to stack space for @n_structs
     141   *   chunks of type @struct_type
     142   *
     143   * Since: 2.72
     144   */
     145  #define g_newa0(struct_type, n_structs)  ((struct_type*) g_alloca0 (sizeof (struct_type) * (gsize) (n_structs)))
     146  
     147  #endif /* __G_ALLOCA_H__ */