|
Packit |
db3073 |
/* GLIB - Library of useful routines for C programming
|
|
Packit |
db3073 |
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
db3073 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
db3073 |
* License as published by the Free Software Foundation; either
|
|
Packit |
db3073 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
db3073 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
db3073 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
db3073 |
* Lesser General Public License for more details.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
db3073 |
* License along with this library; if not, write to the
|
|
Packit |
db3073 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
Packit |
db3073 |
* Boston, MA 02111-1307, USA.
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/*
|
|
Packit |
db3073 |
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
|
Packit |
db3073 |
* file for a list of people on the GLib Team. See the ChangeLog
|
|
Packit |
db3073 |
* files for a list of changes. These files are distributed with
|
|
Packit |
db3073 |
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
|
|
Packit |
db3073 |
#error "Only <glib.h> can be included directly."
|
|
Packit |
db3073 |
#endif
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#ifndef __G_MEM_H__
|
|
Packit |
db3073 |
#define __G_MEM_H__
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#include <glib/gtypes.h>
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
G_BEGIN_DECLS
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* GMemVTable:
|
|
Packit |
db3073 |
* @malloc: function to use for allocating memory.
|
|
Packit |
db3073 |
* @realloc: function to use for reallocating memory.
|
|
Packit |
db3073 |
* @free: function to use to free memory.
|
|
Packit |
db3073 |
* @calloc: function to use for allocating zero-filled memory.
|
|
Packit |
db3073 |
* @try_malloc: function to use for allocating memory without a default error handler.
|
|
Packit |
db3073 |
* @try_realloc: function to use for reallocating memory without a default error handler.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* A set of functions used to perform memory allocation. The same #GMemVTable must
|
|
Packit |
db3073 |
* be used for all allocations in the same program; a call to g_mem_set_vtable(),
|
|
Packit |
db3073 |
* if it exists, should be prior to any use of GLib.
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
typedef struct _GMemVTable GMemVTable;
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* G_MEM_ALIGN:
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Indicates the number of bytes to which memory will be aligned on the
|
|
Packit |
db3073 |
* current platform.
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
# define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
|
|
Packit |
db3073 |
#else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
|
|
Packit |
db3073 |
# define G_MEM_ALIGN GLIB_SIZEOF_LONG
|
|
Packit |
db3073 |
#endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/* Memory allocation functions
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
void g_free (gpointer mem);
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
|
|
Packit |
db3073 |
gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
|
|
Packit |
db3073 |
gpointer g_realloc (gpointer mem,
|
|
Packit |
db3073 |
gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
|
|
Packit |
db3073 |
gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
|
|
Packit |
db3073 |
gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
|
|
Packit |
db3073 |
gpointer g_try_realloc (gpointer mem,
|
|
Packit |
db3073 |
gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
gpointer g_malloc_n (gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
|
|
Packit |
db3073 |
gpointer g_malloc0_n (gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
|
|
Packit |
db3073 |
gpointer g_realloc_n (gpointer mem,
|
|
Packit |
db3073 |
gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
|
|
Packit |
db3073 |
gpointer g_try_malloc_n (gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
|
|
Packit |
db3073 |
gpointer g_try_malloc0_n (gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
|
|
Packit |
db3073 |
gpointer g_try_realloc_n (gpointer mem,
|
|
Packit |
db3073 |
gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/* Optimise: avoid the call to the (slower) _n function if we can
|
|
Packit |
db3073 |
* determine at compile-time that no overflow happens.
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
|
|
Packit |
db3073 |
# define _G_NEW(struct_type, n_structs, func) \
|
|
Packit |
db3073 |
(struct_type *) (G_GNUC_EXTENSION ({ \
|
|
Packit |
db3073 |
gsize __n = (gsize) (n_structs); \
|
|
Packit |
db3073 |
gsize __s = sizeof (struct_type); \
|
|
Packit |
db3073 |
gpointer __p; \
|
|
Packit |
db3073 |
if (__s == 1) \
|
|
Packit |
db3073 |
__p = g_##func (__n); \
|
|
Packit |
db3073 |
else if (__builtin_constant_p (__n) && \
|
|
Packit |
db3073 |
(__s == 0 || __n <= G_MAXSIZE / __s)) \
|
|
Packit |
db3073 |
__p = g_##func (__n * __s); \
|
|
Packit |
db3073 |
else \
|
|
Packit |
db3073 |
__p = g_##func##_n (__n, __s); \
|
|
Packit |
db3073 |
__p; \
|
|
Packit |
db3073 |
}))
|
|
Packit |
db3073 |
# define _G_RENEW(struct_type, mem, n_structs, func) \
|
|
Packit |
db3073 |
(struct_type *) (G_GNUC_EXTENSION ({ \
|
|
Packit |
db3073 |
gsize __n = (gsize) (n_structs); \
|
|
Packit |
db3073 |
gsize __s = sizeof (struct_type); \
|
|
Packit |
db3073 |
gpointer __p = (gpointer) (mem); \
|
|
Packit |
db3073 |
if (__s == 1) \
|
|
Packit |
db3073 |
__p = g_##func (__p, __n); \
|
|
Packit |
db3073 |
else if (__builtin_constant_p (__n) && \
|
|
Packit |
db3073 |
(__s == 0 || __n <= G_MAXSIZE / __s)) \
|
|
Packit |
db3073 |
__p = g_##func (__p, __n * __s); \
|
|
Packit |
db3073 |
else \
|
|
Packit |
db3073 |
__p = g_##func##_n (__p, __n, __s); \
|
|
Packit |
db3073 |
__p; \
|
|
Packit |
db3073 |
}))
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#else
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/* Unoptimised version: always call the _n() function. */
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#define _G_NEW(struct_type, n_structs, func) \
|
|
Packit |
db3073 |
((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
|
|
Packit |
db3073 |
#define _G_RENEW(struct_type, mem, n_structs, func) \
|
|
Packit |
db3073 |
((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#endif
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* g_new:
|
|
Packit |
db3073 |
* @struct_type: the type of the elements to allocate
|
|
Packit |
db3073 |
* @n_structs: the number of elements to allocate
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Allocates @n_structs elements of type @struct_type.
|
|
Packit |
db3073 |
* The returned pointer is cast to a pointer to the given type.
|
|
Packit |
db3073 |
* If @n_structs is 0 it returns %NULL.
|
|
Packit |
db3073 |
* Care is taken to avoid overflow when calculating the size of the allocated block.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Since the returned pointer is already casted to the right type,
|
|
Packit |
db3073 |
* it is normally unnecessary to cast it explicitly, and doing
|
|
Packit |
db3073 |
* so might hide memory allocation errors.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* g_new0:
|
|
Packit |
db3073 |
* @struct_type: the type of the elements to allocate.
|
|
Packit |
db3073 |
* @n_structs: the number of elements to allocate.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Allocates @n_structs elements of type @struct_type, initialized to 0's.
|
|
Packit |
db3073 |
* The returned pointer is cast to a pointer to the given type.
|
|
Packit |
db3073 |
* If @n_structs is 0 it returns %NULL.
|
|
Packit |
db3073 |
* Care is taken to avoid overflow when calculating the size of the allocated block.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Since the returned pointer is already casted to the right type,
|
|
Packit |
db3073 |
* it is normally unnecessary to cast it explicitly, and doing
|
|
Packit |
db3073 |
* so might hide memory allocation errors.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* g_renew:
|
|
Packit |
db3073 |
* @struct_type: the type of the elements to allocate
|
|
Packit |
db3073 |
* @mem: the currently allocated memory
|
|
Packit |
db3073 |
* @n_structs: the number of elements to allocate
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Reallocates the memory pointed to by @mem, so that it now has space for
|
|
Packit |
db3073 |
* @n_structs elements of type @struct_type. It returns the new address of
|
|
Packit |
db3073 |
* the memory, which may have been moved.
|
|
Packit |
db3073 |
* Care is taken to avoid overflow when calculating the size of the allocated block.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* g_try_new:
|
|
Packit |
db3073 |
* @struct_type: the type of the elements to allocate
|
|
Packit |
db3073 |
* @n_structs: the number of elements to allocate
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Attempts to allocate @n_structs elements of type @struct_type, and returns
|
|
Packit |
db3073 |
* %NULL on failure. Contrast with g_new(), which aborts the program on failure.
|
|
Packit |
db3073 |
* The returned pointer is cast to a pointer to the given type.
|
|
Packit |
db3073 |
* The function returns %NULL when @n_structs is 0 of if an overflow occurs.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Since: 2.8
|
|
Packit |
db3073 |
* Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* g_try_new0:
|
|
Packit |
db3073 |
* @struct_type: the type of the elements to allocate
|
|
Packit |
db3073 |
* @n_structs: the number of elements to allocate
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Attempts to allocate @n_structs elements of type @struct_type, initialized
|
|
Packit |
db3073 |
* to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
|
|
Packit |
db3073 |
* the program on failure.
|
|
Packit |
db3073 |
* The returned pointer is cast to a pointer to the given type.
|
|
Packit |
db3073 |
* The function returns %NULL when @n_structs is 0 of if an overflow occurs.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Since: 2.8
|
|
Packit |
db3073 |
* Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
|
|
Packit |
db3073 |
/**
|
|
Packit |
db3073 |
* g_try_renew:
|
|
Packit |
db3073 |
* @struct_type: the type of the elements to allocate
|
|
Packit |
db3073 |
* @mem: the currently allocated memory
|
|
Packit |
db3073 |
* @n_structs: the number of elements to allocate
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Attempts to reallocate the memory pointed to by @mem, so that it now has
|
|
Packit |
db3073 |
* space for @n_structs elements of type @struct_type, and returns %NULL on
|
|
Packit |
db3073 |
* failure. Contrast with g_renew(), which aborts the program on failure.
|
|
Packit |
db3073 |
* It returns the new address of the memory, which may have been moved.
|
|
Packit |
db3073 |
* The function returns %NULL if an overflow occurs.
|
|
Packit |
db3073 |
*
|
|
Packit |
db3073 |
* Since: 2.8
|
|
Packit |
db3073 |
* Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
#define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/* Memory allocation virtualization for debugging purposes
|
|
Packit |
db3073 |
* g_mem_set_vtable() has to be the very first GLib function called
|
|
Packit |
db3073 |
* if being used
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
struct _GMemVTable {
|
|
Packit |
db3073 |
gpointer (*malloc) (gsize n_bytes);
|
|
Packit |
db3073 |
gpointer (*realloc) (gpointer mem,
|
|
Packit |
db3073 |
gsize n_bytes);
|
|
Packit |
db3073 |
void (*free) (gpointer mem);
|
|
Packit |
db3073 |
/* optional; set to NULL if not used ! */
|
|
Packit |
db3073 |
gpointer (*calloc) (gsize n_blocks,
|
|
Packit |
db3073 |
gsize n_block_bytes);
|
|
Packit |
db3073 |
gpointer (*try_malloc) (gsize n_bytes);
|
|
Packit |
db3073 |
gpointer (*try_realloc) (gpointer mem,
|
|
Packit |
db3073 |
gsize n_bytes);
|
|
Packit |
db3073 |
};
|
|
Packit |
db3073 |
void g_mem_set_vtable (GMemVTable *vtable);
|
|
Packit |
db3073 |
gboolean g_mem_is_system_malloc (void);
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
GLIB_VAR gboolean g_mem_gc_friendly;
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
/* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
|
|
Packit |
db3073 |
*/
|
|
Packit |
db3073 |
GLIB_VAR GMemVTable *glib_mem_profiler_table;
|
|
Packit |
db3073 |
void g_mem_profile (void);
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
G_END_DECLS
|
|
Packit |
db3073 |
|
|
Packit |
db3073 |
#endif /* __G_MEM_H__ */
|