Blame glib/gmem.h

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#ifndef __G_MEM_H__
Packit ae235b
#define __G_MEM_H__
Packit ae235b
Packit ae235b
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
Packit ae235b
#error "Only <glib.h> can be included directly."
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <glib/gutils.h>
Packit ae235b
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GMemVTable:
Packit ae235b
 * @malloc: function to use for allocating memory.
Packit ae235b
 * @realloc: function to use for reallocating memory.
Packit ae235b
 * @free: function to use to free memory.
Packit ae235b
 * @calloc: function to use for allocating zero-filled memory.
Packit ae235b
 * @try_malloc: function to use for allocating memory without a default error handler.
Packit ae235b
 * @try_realloc: function to use for reallocating memory without a default error handler.
Packit ae235b
 * 
Packit ae235b
 * A set of functions used to perform memory allocation. The same #GMemVTable must
Packit ae235b
 * be used for all allocations in the same program; a call to g_mem_set_vtable(),
Packit ae235b
 * if it exists, should be prior to any use of GLib.
Packit ae235b
 *
Packit ae235b
 * This functions related to this has been deprecated in 2.46, and no longer work.
Packit ae235b
 */
Packit ae235b
typedef struct _GMemVTable GMemVTable;
Packit ae235b
Packit ae235b
Packit ae235b
#if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
Packit ae235b
/**
Packit ae235b
 * G_MEM_ALIGN:
Packit ae235b
 *
Packit ae235b
 * Indicates the number of bytes to which memory will be aligned on the
Packit ae235b
 * current platform.
Packit ae235b
 */
Packit ae235b
#  define G_MEM_ALIGN	GLIB_SIZEOF_VOID_P
Packit ae235b
#else	/* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
Packit ae235b
#  define G_MEM_ALIGN	GLIB_SIZEOF_LONG
Packit ae235b
#endif	/* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
Packit ae235b
Packit ae235b
Packit ae235b
/* Memory allocation functions
Packit ae235b
 */
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	 g_free	          (gpointer	 mem);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_2_34
Packit ae235b
void     g_clear_pointer  (gpointer      *pp,
Packit ae235b
                           GDestroyNotify destroy);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_malloc         (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_malloc0        (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_realloc        (gpointer	 mem,
Packit ae235b
			   gsize	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_try_malloc     (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_try_malloc0    (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_try_realloc    (gpointer	 mem,
Packit ae235b
			   gsize	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_malloc_n       (gsize	 n_blocks,
Packit ae235b
			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_malloc0_n      (gsize	 n_blocks,
Packit ae235b
			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_realloc_n      (gpointer	 mem,
Packit ae235b
			   gsize	 n_blocks,
Packit ae235b
			   gsize	 n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_try_malloc_n   (gsize	 n_blocks,
Packit ae235b
			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_try_malloc0_n  (gsize	 n_blocks,
Packit ae235b
			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
gpointer g_try_realloc_n  (gpointer	 mem,
Packit ae235b
			   gsize	 n_blocks,
Packit ae235b
			   gsize	 n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
Packit ae235b
Packit ae235b
#define g_clear_pointer(pp, destroy) \
Packit ae235b
  G_STMT_START {                                                               \
Packit ae235b
    G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
Packit ae235b
    /* Only one access, please; work around type aliasing */                   \
Packit ae235b
    union { char *in; gpointer *out; } _pp;                                    \
Packit ae235b
    gpointer _p;                                                               \
Packit ae235b
    /* This assignment is needed to avoid a gcc warning */                     \
Packit ae235b
    GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
Packit ae235b
                                                                               \
Packit ae235b
    _pp.in = (char *) (pp);                                                    \
Packit ae235b
    _p = *_pp.out;                                                             \
Packit ae235b
    if (_p) 								       \
Packit ae235b
      { 								       \
Packit ae235b
        *_pp.out = NULL;                                                       \
Packit ae235b
        _destroy (_p);                                                         \
Packit ae235b
      }                                                                        \
Packit ae235b
  } G_STMT_END
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_steal_pointer:
Packit ae235b
 * @pp: (not nullable): a pointer to a pointer
Packit ae235b
 *
Packit ae235b
 * Sets @pp to %NULL, returning the value that was there before.
Packit ae235b
 *
Packit ae235b
 * Conceptually, this transfers the ownership of the pointer from the
Packit ae235b
 * referenced variable to the "caller" of the macro (ie: "steals" the
Packit ae235b
 * reference).
Packit ae235b
 *
Packit ae235b
 * The return value will be properly typed, according to the type of
Packit ae235b
 * @pp.
Packit ae235b
 *
Packit ae235b
 * This can be very useful when combined with g_autoptr() to prevent the
Packit ae235b
 * return value of a function from being automatically freed.  Consider
Packit ae235b
 * the following example (which only works on GCC and clang):
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * GObject *
Packit ae235b
 * create_object (void)
Packit ae235b
 * {
Packit ae235b
 *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
Packit ae235b
 *
Packit ae235b
 *   if (early_error_case)
Packit ae235b
 *     return NULL;
Packit ae235b
 *
Packit ae235b
 *   return g_steal_pointer (&obj);
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * It can also be used in similar ways for 'out' parameters and is
Packit ae235b
 * particularly useful for dealing with optional out parameters:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * gboolean
Packit ae235b
 * get_object (GObject **obj_out)
Packit ae235b
 * {
Packit ae235b
 *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
Packit ae235b
 *
Packit ae235b
 *   if (early_error_case)
Packit ae235b
 *     return FALSE;
Packit ae235b
 *
Packit ae235b
 *   if (obj_out)
Packit ae235b
 *     *obj_out = g_steal_pointer (&obj);
Packit ae235b
 *
Packit ae235b
 *   return TRUE;
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * In the above example, the object will be automatically freed in the
Packit ae235b
 * early error case and also in the case that %NULL was given for
Packit ae235b
 * @obj_out.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
static inline gpointer
Packit ae235b
g_steal_pointer (gpointer pp)
Packit ae235b
{
Packit ae235b
  gpointer *ptr = (gpointer *) pp;
Packit ae235b
  gpointer ref;
Packit ae235b
Packit ae235b
  ref = *ptr;
Packit ae235b
  *ptr = NULL;
Packit ae235b
Packit ae235b
  return ref;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* type safety */
Packit ae235b
#define g_steal_pointer(pp) \
Packit ae235b
  (0 ? (*(pp)) : (g_steal_pointer) (pp))
Packit ae235b
Packit ae235b
/* Optimise: avoid the call to the (slower) _n function if we can
Packit ae235b
 * determine at compile-time that no overflow happens.
Packit ae235b
 */
Packit ae235b
#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
Packit ae235b
#  define _G_NEW(struct_type, n_structs, func) \
Packit ae235b
	(struct_type *) (G_GNUC_EXTENSION ({			\
Packit ae235b
	  gsize __n = (gsize) (n_structs);			\
Packit ae235b
	  gsize __s = sizeof (struct_type);			\
Packit ae235b
	  gpointer __p;						\
Packit ae235b
	  if (__s == 1)						\
Packit ae235b
	    __p = g_##func (__n);				\
Packit ae235b
	  else if (__builtin_constant_p (__n) &&		\
Packit ae235b
	           (__s == 0 || __n <= G_MAXSIZE / __s))	\
Packit ae235b
	    __p = g_##func (__n * __s);				\
Packit ae235b
	  else							\
Packit ae235b
	    __p = g_##func##_n (__n, __s);			\
Packit ae235b
	  __p;							\
Packit ae235b
	}))
Packit ae235b
#  define _G_RENEW(struct_type, mem, n_structs, func) \
Packit ae235b
	(struct_type *) (G_GNUC_EXTENSION ({			\
Packit ae235b
	  gsize __n = (gsize) (n_structs);			\
Packit ae235b
	  gsize __s = sizeof (struct_type);			\
Packit ae235b
	  gpointer __p = (gpointer) (mem);			\
Packit ae235b
	  if (__s == 1)						\
Packit ae235b
	    __p = g_##func (__p, __n);				\
Packit ae235b
	  else if (__builtin_constant_p (__n) &&		\
Packit ae235b
	           (__s == 0 || __n <= G_MAXSIZE / __s))	\
Packit ae235b
	    __p = g_##func (__p, __n * __s);			\
Packit ae235b
	  else							\
Packit ae235b
	    __p = g_##func##_n (__p, __n, __s);			\
Packit ae235b
	  __p;							\
Packit ae235b
	}))
Packit ae235b
Packit ae235b
#else
Packit ae235b
Packit ae235b
/* Unoptimised version: always call the _n() function. */
Packit ae235b
Packit ae235b
#define _G_NEW(struct_type, n_structs, func) \
Packit ae235b
        ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
Packit ae235b
#define _G_RENEW(struct_type, mem, n_structs, func) \
Packit ae235b
        ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_new:
Packit ae235b
 * @struct_type: the type of the elements to allocate
Packit ae235b
 * @n_structs: the number of elements to allocate
Packit ae235b
 * 
Packit ae235b
 * Allocates @n_structs elements of type @struct_type.
Packit ae235b
 * The returned pointer is cast to a pointer to the given type.
Packit ae235b
 * If @n_structs is 0 it returns %NULL.
Packit ae235b
 * Care is taken to avoid overflow when calculating the size of the allocated block.
Packit ae235b
 * 
Packit ae235b
 * Since the returned pointer is already casted to the right type,
Packit ae235b
 * it is normally unnecessary to cast it explicitly, and doing
Packit ae235b
 * so might hide memory allocation errors.
Packit ae235b
 * 
Packit ae235b
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
Packit ae235b
 */
Packit ae235b
#define g_new(struct_type, n_structs)			_G_NEW (struct_type, n_structs, malloc)
Packit ae235b
/**
Packit ae235b
 * g_new0:
Packit ae235b
 * @struct_type: the type of the elements to allocate.
Packit ae235b
 * @n_structs: the number of elements to allocate.
Packit ae235b
 * 
Packit ae235b
 * Allocates @n_structs elements of type @struct_type, initialized to 0's.
Packit ae235b
 * The returned pointer is cast to a pointer to the given type.
Packit ae235b
 * If @n_structs is 0 it returns %NULL.
Packit ae235b
 * Care is taken to avoid overflow when calculating the size of the allocated block.
Packit ae235b
 * 
Packit ae235b
 * Since the returned pointer is already casted to the right type,
Packit ae235b
 * it is normally unnecessary to cast it explicitly, and doing
Packit ae235b
 * so might hide memory allocation errors.
Packit ae235b
 * 
Packit ae235b
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
Packit ae235b
 */
Packit ae235b
#define g_new0(struct_type, n_structs)			_G_NEW (struct_type, n_structs, malloc0)
Packit ae235b
/**
Packit ae235b
 * g_renew:
Packit ae235b
 * @struct_type: the type of the elements to allocate
Packit ae235b
 * @mem: the currently allocated memory
Packit ae235b
 * @n_structs: the number of elements to allocate
Packit ae235b
 * 
Packit ae235b
 * Reallocates the memory pointed to by @mem, so that it now has space for
Packit ae235b
 * @n_structs elements of type @struct_type. It returns the new address of
Packit ae235b
 * the memory, which may have been moved.
Packit ae235b
 * Care is taken to avoid overflow when calculating the size of the allocated block.
Packit ae235b
 * 
Packit ae235b
 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
Packit ae235b
 */
Packit ae235b
#define g_renew(struct_type, mem, n_structs)		_G_RENEW (struct_type, mem, n_structs, realloc)
Packit ae235b
/**
Packit ae235b
 * g_try_new:
Packit ae235b
 * @struct_type: the type of the elements to allocate
Packit ae235b
 * @n_structs: the number of elements to allocate
Packit ae235b
 * 
Packit ae235b
 * Attempts to allocate @n_structs elements of type @struct_type, and returns
Packit ae235b
 * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
Packit ae235b
 * The returned pointer is cast to a pointer to the given type.
Packit ae235b
 * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
Packit ae235b
 */
Packit ae235b
#define g_try_new(struct_type, n_structs)		_G_NEW (struct_type, n_structs, try_malloc)
Packit ae235b
/**
Packit ae235b
 * g_try_new0:
Packit ae235b
 * @struct_type: the type of the elements to allocate
Packit ae235b
 * @n_structs: the number of elements to allocate
Packit ae235b
 * 
Packit ae235b
 * Attempts to allocate @n_structs elements of type @struct_type, initialized
Packit ae235b
 * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
Packit ae235b
 * the program on failure.
Packit ae235b
 * The returned pointer is cast to a pointer to the given type.
Packit ae235b
 * The function returns %NULL when @n_structs is 0 or if an overflow occurs.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
Packit ae235b
 */
Packit ae235b
#define g_try_new0(struct_type, n_structs)		_G_NEW (struct_type, n_structs, try_malloc0)
Packit ae235b
/**
Packit ae235b
 * g_try_renew:
Packit ae235b
 * @struct_type: the type of the elements to allocate
Packit ae235b
 * @mem: the currently allocated memory
Packit ae235b
 * @n_structs: the number of elements to allocate
Packit ae235b
 * 
Packit ae235b
 * Attempts to reallocate the memory pointed to by @mem, so that it now has
Packit ae235b
 * space for @n_structs elements of type @struct_type, and returns %NULL on
Packit ae235b
 * failure. Contrast with g_renew(), which aborts the program on failure.
Packit ae235b
 * It returns the new address of the memory, which may have been moved.
Packit ae235b
 * The function returns %NULL if an overflow occurs.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
Packit ae235b
 */
Packit ae235b
#define g_try_renew(struct_type, mem, n_structs)	_G_RENEW (struct_type, mem, n_structs, try_realloc)
Packit ae235b
Packit ae235b
Packit ae235b
/* Memory allocation virtualization for debugging purposes
Packit ae235b
 * g_mem_set_vtable() has to be the very first GLib function called
Packit ae235b
 * if being used
Packit ae235b
 */
Packit ae235b
struct _GMemVTable {
Packit ae235b
  gpointer (*malloc)      (gsize    n_bytes);
Packit ae235b
  gpointer (*realloc)     (gpointer mem,
Packit ae235b
			   gsize    n_bytes);
Packit ae235b
  void     (*free)        (gpointer mem);
Packit ae235b
  /* optional; set to NULL if not used ! */
Packit ae235b
  gpointer (*calloc)      (gsize    n_blocks,
Packit ae235b
			   gsize    n_block_bytes);
Packit ae235b
  gpointer (*try_malloc)  (gsize    n_bytes);
Packit ae235b
  gpointer (*try_realloc) (gpointer mem,
Packit ae235b
			   gsize    n_bytes);
Packit ae235b
};
Packit ae235b
GLIB_DEPRECATED_IN_2_46
Packit ae235b
void	 g_mem_set_vtable (GMemVTable	*vtable);
Packit ae235b
GLIB_DEPRECATED_IN_2_46
Packit ae235b
gboolean g_mem_is_system_malloc (void);
Packit ae235b
Packit ae235b
GLIB_VAR gboolean g_mem_gc_friendly;
Packit ae235b
Packit ae235b
/* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
Packit ae235b
 */
Packit ae235b
GLIB_VAR GMemVTable	*glib_mem_profiler_table;
Packit ae235b
GLIB_DEPRECATED_IN_2_46
Packit ae235b
void	g_mem_profile	(void);
Packit ae235b
Packit ae235b
G_END_DECLS
Packit ae235b
Packit ae235b
#endif /* __G_MEM_H__ */