Blame glib/glib/gmem.c

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
/* 
Packit db3073
 * MT safe
Packit db3073
 */
Packit db3073
Packit db3073
#include "config.h"
Packit db3073
Packit db3073
#include "gmem.h"
Packit db3073
Packit db3073
#include <stdlib.h>
Packit db3073
#include <string.h>
Packit db3073
#include <signal.h>
Packit db3073
Packit db3073
#include "glib-init.h"
Packit db3073
Packit db3073
#include "gslice.h"
Packit db3073
#include "gbacktrace.h"
Packit db3073
#include "gtestutils.h"
Packit db3073
#include "gthread.h"
Packit db3073
#include "glib_trace.h"
Packit db3073
Packit db3073
#define MEM_PROFILE_TABLE_SIZE 4096
Packit db3073
Packit db3073
Packit db3073
/* notes on macros:
Packit db3073
 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
Packit db3073
 * g_mem_profile().
Packit db3073
 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
Packit db3073
 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
Packit db3073
 * match the corresponding GLib prototypes, keep configure.ac and gmem.h in sync here.
Packit db3073
 * g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
Packit db3073
 */
Packit db3073
Packit db3073
/* --- malloc wrappers --- */
Packit db3073
#ifndef	REALLOC_0_WORKS
Packit db3073
static gpointer
Packit db3073
standard_realloc (gpointer mem,
Packit db3073
		  gsize    n_bytes)
Packit db3073
{
Packit db3073
  if (!mem)
Packit db3073
    return malloc (n_bytes);
Packit db3073
  else
Packit db3073
    return realloc (mem, n_bytes);
Packit db3073
}
Packit db3073
#endif	/* !REALLOC_0_WORKS */
Packit db3073
Packit db3073
#ifdef SANE_MALLOC_PROTOS
Packit db3073
#  define standard_malloc	malloc
Packit db3073
#  ifdef REALLOC_0_WORKS
Packit db3073
#    define standard_realloc	realloc
Packit db3073
#  endif /* REALLOC_0_WORKS */
Packit db3073
#  define standard_free		free
Packit db3073
#  define standard_calloc	calloc
Packit db3073
#  define standard_try_malloc	malloc
Packit db3073
#  define standard_try_realloc	realloc
Packit db3073
#else	/* !SANE_MALLOC_PROTOS */
Packit db3073
static gpointer
Packit db3073
standard_malloc (gsize n_bytes)
Packit db3073
{
Packit db3073
  return malloc (n_bytes);
Packit db3073
}
Packit db3073
#  ifdef REALLOC_0_WORKS
Packit db3073
static gpointer
Packit db3073
standard_realloc (gpointer mem,
Packit db3073
		  gsize    n_bytes)
Packit db3073
{
Packit db3073
  return realloc (mem, n_bytes);
Packit db3073
}
Packit db3073
#  endif /* REALLOC_0_WORKS */
Packit db3073
static void
Packit db3073
standard_free (gpointer mem)
Packit db3073
{
Packit db3073
  free (mem);
Packit db3073
}
Packit db3073
static gpointer
Packit db3073
standard_calloc (gsize n_blocks,
Packit db3073
		 gsize n_bytes)
Packit db3073
{
Packit db3073
  return calloc (n_blocks, n_bytes);
Packit db3073
}
Packit db3073
#define	standard_try_malloc	standard_malloc
Packit db3073
#define	standard_try_realloc	standard_realloc
Packit db3073
#endif	/* !SANE_MALLOC_PROTOS */
Packit db3073
Packit db3073
Packit db3073
/* --- variables --- */
Packit db3073
static GMemVTable glib_mem_vtable = {
Packit db3073
  standard_malloc,
Packit db3073
  standard_realloc,
Packit db3073
  standard_free,
Packit db3073
  standard_calloc,
Packit db3073
  standard_try_malloc,
Packit db3073
  standard_try_realloc,
Packit db3073
};
Packit db3073
Packit db3073
/**
Packit db3073
 * SECTION:memory
Packit db3073
 * @Short_Description: general memory-handling
Packit db3073
 * @Title: Memory Allocation
Packit db3073
 * 
Packit db3073
 * These functions provide support for allocating and freeing memory.
Packit db3073
 * 
Packit db3073
 * <note>
Packit db3073
 * If any call to allocate memory fails, the application is terminated.
Packit db3073
 * This also means that there is no need to check if the call succeeded.
Packit db3073
 * </note>
Packit db3073
 * 
Packit db3073
 * <note>
Packit db3073
 * It's important to match g_malloc() with g_free(), plain malloc() with free(),
Packit db3073
 * and (if you're using C++) new with delete and new[] with delete[]. Otherwise
Packit db3073
 * bad things can happen, since these allocators may use different memory
Packit db3073
 * pools (and new/delete call constructors and destructors). See also
Packit db3073
 * g_mem_set_vtable().
Packit db3073
 * </note>
Packit db3073
 */
Packit db3073
Packit db3073
/* --- functions --- */
Packit db3073
/**
Packit db3073
 * g_malloc:
Packit db3073
 * @n_bytes: the number of bytes to allocate
Packit db3073
 * 
Packit db3073
 * Allocates @n_bytes bytes of memory.
Packit db3073
 * If @n_bytes is 0 it returns %NULL.
Packit db3073
 * 
Packit db3073
 * Returns: a pointer to the allocated memory
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_malloc (gsize n_bytes)
Packit db3073
{
Packit db3073
  if (G_LIKELY (n_bytes))
Packit db3073
    {
Packit db3073
      gpointer mem;
Packit db3073
Packit db3073
      mem = glib_mem_vtable.malloc (n_bytes);
Packit db3073
      TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 0));
Packit db3073
      if (mem)
Packit db3073
	return mem;
Packit db3073
Packit db3073
      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
Packit db3073
               G_STRLOC, n_bytes);
Packit db3073
    }
Packit db3073
Packit db3073
  TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 0, 0));
Packit db3073
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_malloc0:
Packit db3073
 * @n_bytes: the number of bytes to allocate
Packit db3073
 * 
Packit db3073
 * Allocates @n_bytes bytes of memory, initialized to 0's.
Packit db3073
 * If @n_bytes is 0 it returns %NULL.
Packit db3073
 * 
Packit db3073
 * Returns: a pointer to the allocated memory
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_malloc0 (gsize n_bytes)
Packit db3073
{
Packit db3073
  if (G_LIKELY (n_bytes))
Packit db3073
    {
Packit db3073
      gpointer mem;
Packit db3073
Packit db3073
      mem = glib_mem_vtable.calloc (1, n_bytes);
Packit db3073
      TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 1, 0));
Packit db3073
      if (mem)
Packit db3073
	return mem;
Packit db3073
Packit db3073
      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
Packit db3073
               G_STRLOC, n_bytes);
Packit db3073
    }
Packit db3073
Packit db3073
  TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 1, 0));
Packit db3073
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_realloc:
Packit db3073
 * @mem: the memory to reallocate
Packit db3073
 * @n_bytes: new size of the memory in bytes
Packit db3073
 * 
Packit db3073
 * Reallocates the memory pointed to by @mem, so that it now has space for
Packit db3073
 * @n_bytes bytes of memory. It returns the new address of the memory, which may
Packit db3073
 * have been moved. @mem may be %NULL, in which case it's considered to
Packit db3073
 * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
Packit db3073
 * and @mem will be freed unless it is %NULL.
Packit db3073
 * 
Packit db3073
 * Returns: the new address of the allocated memory
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_realloc (gpointer mem,
Packit db3073
	   gsize    n_bytes)
Packit db3073
{
Packit db3073
  gpointer newmem;
Packit db3073
Packit db3073
  if (G_LIKELY (n_bytes))
Packit db3073
    {
Packit db3073
      newmem = glib_mem_vtable.realloc (mem, n_bytes);
Packit db3073
      TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 0));
Packit db3073
      if (newmem)
Packit db3073
	return newmem;
Packit db3073
Packit db3073
      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
Packit db3073
               G_STRLOC, n_bytes);
Packit db3073
    }
Packit db3073
Packit db3073
  if (mem)
Packit db3073
    glib_mem_vtable.free (mem);
Packit db3073
Packit db3073
  TRACE (GLIB_MEM_REALLOC((void*) NULL, (void*)mem, 0, 0));
Packit db3073
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_free:
Packit db3073
 * @mem: the memory to free
Packit db3073
 * 
Packit db3073
 * Frees the memory pointed to by @mem.
Packit db3073
 * If @mem is %NULL it simply returns.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_free (gpointer mem)
Packit db3073
{
Packit db3073
  if (G_LIKELY (mem))
Packit db3073
    glib_mem_vtable.free (mem);
Packit db3073
  TRACE(GLIB_MEM_FREE((void*) mem));
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_try_malloc:
Packit db3073
 * @n_bytes: number of bytes to allocate.
Packit db3073
 * 
Packit db3073
 * Attempts to allocate @n_bytes, and returns %NULL on failure.
Packit db3073
 * Contrast with g_malloc(), which aborts the program on failure.
Packit db3073
 * 
Packit db3073
 * Returns: the allocated memory, or %NULL.
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_try_malloc (gsize n_bytes)
Packit db3073
{
Packit db3073
  gpointer mem;
Packit db3073
Packit db3073
  if (G_LIKELY (n_bytes))
Packit db3073
    mem = glib_mem_vtable.try_malloc (n_bytes);
Packit db3073
  else
Packit db3073
    mem = NULL;
Packit db3073
Packit db3073
  TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));
Packit db3073
Packit db3073
  return mem;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_try_malloc0:
Packit db3073
 * @n_bytes: number of bytes to allocate
Packit db3073
 * 
Packit db3073
 * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
Packit db3073
 * failure. Contrast with g_malloc0(), which aborts the program on failure.
Packit db3073
 * 
Packit db3073
 * Since: 2.8
Packit db3073
 * Returns: the allocated memory, or %NULL
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_try_malloc0 (gsize n_bytes)
Packit db3073
{
Packit db3073
  gpointer mem;
Packit db3073
Packit db3073
  if (G_LIKELY (n_bytes))
Packit db3073
    mem = glib_mem_vtable.try_malloc (n_bytes);
Packit db3073
  else
Packit db3073
    mem = NULL;
Packit db3073
Packit db3073
  if (mem)
Packit db3073
    memset (mem, 0, n_bytes);
Packit db3073
Packit db3073
  return mem;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_try_realloc:
Packit db3073
 * @mem: (allow-none): previously-allocated memory, or %NULL.
Packit db3073
 * @n_bytes: number of bytes to allocate.
Packit db3073
 * 
Packit db3073
 * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
Packit db3073
 * on failure. Contrast with g_realloc(), which aborts the program
Packit db3073
 * on failure. If @mem is %NULL, behaves the same as g_try_malloc().
Packit db3073
 * 
Packit db3073
 * Returns: the allocated memory, or %NULL.
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_try_realloc (gpointer mem,
Packit db3073
	       gsize    n_bytes)
Packit db3073
{
Packit db3073
  gpointer newmem;
Packit db3073
Packit db3073
  if (G_LIKELY (n_bytes))
Packit db3073
    newmem = glib_mem_vtable.try_realloc (mem, n_bytes);
Packit db3073
  else
Packit db3073
    {
Packit db3073
      newmem = NULL;
Packit db3073
      if (mem)
Packit db3073
	glib_mem_vtable.free (mem);
Packit db3073
    }
Packit db3073
Packit db3073
  TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 1));
Packit db3073
Packit db3073
  return newmem;
Packit db3073
}
Packit db3073
Packit db3073
Packit db3073
#define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXSIZE / (b)))
Packit db3073
Packit db3073
/**
Packit db3073
 * g_malloc_n:
Packit db3073
 * @n_blocks: the number of blocks to allocate
Packit db3073
 * @n_block_bytes: the size of each block in bytes
Packit db3073
 * 
Packit db3073
 * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
Packit db3073
 * but care is taken to detect possible overflow during multiplication.
Packit db3073
 * 
Packit db3073
 * Since: 2.24
Packit db3073
 * Returns: a pointer to the allocated memory
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_malloc_n (gsize n_blocks,
Packit db3073
	    gsize n_block_bytes)
Packit db3073
{
Packit db3073
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
Packit db3073
    {
Packit db3073
      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
Packit db3073
               G_STRLOC, n_blocks, n_block_bytes);
Packit db3073
    }
Packit db3073
Packit db3073
  return g_malloc (n_blocks * n_block_bytes);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_malloc0_n:
Packit db3073
 * @n_blocks: the number of blocks to allocate
Packit db3073
 * @n_block_bytes: the size of each block in bytes
Packit db3073
 * 
Packit db3073
 * This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
Packit db3073
 * but care is taken to detect possible overflow during multiplication.
Packit db3073
 * 
Packit db3073
 * Since: 2.24
Packit db3073
 * Returns: a pointer to the allocated memory
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_malloc0_n (gsize n_blocks,
Packit db3073
	     gsize n_block_bytes)
Packit db3073
{
Packit db3073
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
Packit db3073
    {
Packit db3073
      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
Packit db3073
               G_STRLOC, n_blocks, n_block_bytes);
Packit db3073
    }
Packit db3073
Packit db3073
  return g_malloc0 (n_blocks * n_block_bytes);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_realloc_n:
Packit db3073
 * @mem: the memory to reallocate
Packit db3073
 * @n_blocks: the number of blocks to allocate
Packit db3073
 * @n_block_bytes: the size of each block in bytes
Packit db3073
 * 
Packit db3073
 * This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
Packit db3073
 * but care is taken to detect possible overflow during multiplication.
Packit db3073
 * 
Packit db3073
 * Since: 2.24
Packit db3073
 * Returns: the new address of the allocated memory
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_realloc_n (gpointer mem,
Packit db3073
	     gsize    n_blocks,
Packit db3073
	     gsize    n_block_bytes)
Packit db3073
{
Packit db3073
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
Packit db3073
    {
Packit db3073
      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
Packit db3073
               G_STRLOC, n_blocks, n_block_bytes);
Packit db3073
    }
Packit db3073
Packit db3073
  return g_realloc (mem, n_blocks * n_block_bytes);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_try_malloc_n:
Packit db3073
 * @n_blocks: the number of blocks to allocate
Packit db3073
 * @n_block_bytes: the size of each block in bytes
Packit db3073
 * 
Packit db3073
 * This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
Packit db3073
 * but care is taken to detect possible overflow during multiplication.
Packit db3073
 * 
Packit db3073
 * Since: 2.24
Packit db3073
 * Returns: the allocated memory, or %NULL.
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_try_malloc_n (gsize n_blocks,
Packit db3073
		gsize n_block_bytes)
Packit db3073
{
Packit db3073
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
Packit db3073
    return NULL;
Packit db3073
Packit db3073
  return g_try_malloc (n_blocks * n_block_bytes);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_try_malloc0_n:
Packit db3073
 * @n_blocks: the number of blocks to allocate
Packit db3073
 * @n_block_bytes: the size of each block in bytes
Packit db3073
 * 
Packit db3073
 * This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
Packit db3073
 * but care is taken to detect possible overflow during multiplication.
Packit db3073
 * 
Packit db3073
 * Since: 2.24
Packit db3073
 * Returns: the allocated memory, or %NULL
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_try_malloc0_n (gsize n_blocks,
Packit db3073
		 gsize n_block_bytes)
Packit db3073
{
Packit db3073
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
Packit db3073
    return NULL;
Packit db3073
Packit db3073
  return g_try_malloc0 (n_blocks * n_block_bytes);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_try_realloc_n:
Packit db3073
 * @mem: (allow-none): previously-allocated memory, or %NULL.
Packit db3073
 * @n_blocks: the number of blocks to allocate
Packit db3073
 * @n_block_bytes: the size of each block in bytes
Packit db3073
 * 
Packit db3073
 * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
Packit db3073
 * but care is taken to detect possible overflow during multiplication.
Packit db3073
 * 
Packit db3073
 * Since: 2.24
Packit db3073
 * Returns: the allocated memory, or %NULL.
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_try_realloc_n (gpointer mem,
Packit db3073
		 gsize    n_blocks,
Packit db3073
		 gsize    n_block_bytes)
Packit db3073
{
Packit db3073
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
Packit db3073
    return NULL;
Packit db3073
Packit db3073
  return g_try_realloc (mem, n_blocks * n_block_bytes);
Packit db3073
}
Packit db3073
Packit db3073
Packit db3073
Packit db3073
static gpointer
Packit db3073
fallback_calloc (gsize n_blocks,
Packit db3073
		 gsize n_block_bytes)
Packit db3073
{
Packit db3073
  gsize l = n_blocks * n_block_bytes;
Packit db3073
  gpointer mem = glib_mem_vtable.malloc (l);
Packit db3073
Packit db3073
  if (mem)
Packit db3073
    memset (mem, 0, l);
Packit db3073
Packit db3073
  return mem;
Packit db3073
}
Packit db3073
Packit db3073
static gboolean vtable_set = FALSE;
Packit db3073
Packit db3073
/**
Packit db3073
 * g_mem_is_system_malloc:
Packit db3073
 * 
Packit db3073
 * Checks whether the allocator used by g_malloc() is the system's
Packit db3073
 * malloc implementation. If it returns %TRUE memory allocated with
Packit db3073
 * malloc() can be used interchangeable with memory allocated using g_malloc().
Packit db3073
 * This function is useful for avoiding an extra copy of allocated memory returned
Packit db3073
 * by a non-GLib-based API.
Packit db3073
 *
Packit db3073
 * A different allocator can be set using g_mem_set_vtable().
Packit db3073
 *
Packit db3073
 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
Packit db3073
 **/
Packit db3073
gboolean
Packit db3073
g_mem_is_system_malloc (void)
Packit db3073
{
Packit db3073
  return !vtable_set;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_mem_set_vtable:
Packit db3073
 * @vtable: table of memory allocation routines.
Packit db3073
 * 
Packit db3073
 * Sets the #GMemVTable to use for memory allocation. You can use this to provide
Packit db3073
 * custom memory allocation routines. <emphasis>This function must be called
Packit db3073
 * before using any other GLib functions.</emphasis> The @vtable only needs to
Packit db3073
 * provide malloc(), realloc(), and free() functions; GLib can provide default
Packit db3073
 * implementations of the others. The malloc() and realloc() implementations
Packit db3073
 * should return %NULL on failure, GLib will handle error-checking for you.
Packit db3073
 * @vtable is copied, so need not persist after this function has been called.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_mem_set_vtable (GMemVTable *vtable)
Packit db3073
{
Packit db3073
  if (!vtable_set)
Packit db3073
    {
Packit db3073
      if (vtable->malloc && vtable->realloc && vtable->free)
Packit db3073
	{
Packit db3073
	  glib_mem_vtable.malloc = vtable->malloc;
Packit db3073
	  glib_mem_vtable.realloc = vtable->realloc;
Packit db3073
	  glib_mem_vtable.free = vtable->free;
Packit db3073
	  glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
Packit db3073
	  glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
Packit db3073
	  glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
Packit db3073
	  vtable_set = TRUE;
Packit db3073
	}
Packit db3073
      else
Packit db3073
	g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
Packit db3073
    }
Packit db3073
  else
Packit db3073
    g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
Packit db3073
}
Packit db3073
Packit db3073
Packit db3073
/* --- memory profiling and checking --- */
Packit db3073
#ifdef	G_DISABLE_CHECKS
Packit db3073
/**
Packit db3073
 * glib_mem_profiler_table:
Packit db3073
 * 
Packit db3073
 * A #GMemVTable containing profiling variants of the memory
Packit db3073
 * allocation functions. Use them together with g_mem_profile()
Packit db3073
 * in order to get information about the memory allocation pattern
Packit db3073
 * of your program.
Packit db3073
 */
Packit db3073
GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
Packit db3073
void
Packit db3073
g_mem_profile (void)
Packit db3073
{
Packit db3073
}
Packit db3073
#else	/* !G_DISABLE_CHECKS */
Packit db3073
typedef enum {
Packit db3073
  PROFILER_FREE		= 0,
Packit db3073
  PROFILER_ALLOC	= 1,
Packit db3073
  PROFILER_RELOC	= 2,
Packit db3073
  PROFILER_ZINIT	= 4
Packit db3073
} ProfilerJob;
Packit db3073
static guint *profile_data = NULL;
Packit db3073
static gsize profile_allocs = 0;
Packit db3073
static gsize profile_zinit = 0;
Packit db3073
static gsize profile_frees = 0;
Packit db3073
static GMutex gmem_profile_mutex;
Packit db3073
#ifdef  G_ENABLE_DEBUG
Packit db3073
static volatile gsize g_trap_free_size = 0;
Packit db3073
static volatile gsize g_trap_realloc_size = 0;
Packit db3073
static volatile gsize g_trap_malloc_size = 0;
Packit db3073
#endif  /* G_ENABLE_DEBUG */
Packit db3073
Packit db3073
#define	PROFILE_TABLE(f1,f2,f3)   ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
Packit db3073
Packit db3073
static void
Packit db3073
profiler_log (ProfilerJob job,
Packit db3073
	      gsize       n_bytes,
Packit db3073
	      gboolean    success)
Packit db3073
{
Packit db3073
  g_mutex_lock (&gmem_profile_mutex);
Packit db3073
  if (!profile_data)
Packit db3073
    {
Packit db3073
      profile_data = standard_calloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8, 
Packit db3073
                                      sizeof (profile_data[0]));
Packit db3073
      if (!profile_data)	/* memory system kiddin' me, eh? */
Packit db3073
	{
Packit db3073
	  g_mutex_unlock (&gmem_profile_mutex);
Packit db3073
	  return;
Packit db3073
	}
Packit db3073
    }
Packit db3073
Packit db3073
  if (n_bytes < MEM_PROFILE_TABLE_SIZE)
Packit db3073
    profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
Packit db3073
                                          (job & PROFILER_RELOC) != 0,
Packit db3073
                                          success != 0)] += 1;
Packit db3073
  else
Packit db3073
    profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
Packit db3073
                                                         (job & PROFILER_RELOC) != 0,
Packit db3073
                                                         success != 0)] += 1;
Packit db3073
  if (success)
Packit db3073
    {
Packit db3073
      if (job & PROFILER_ALLOC)
Packit db3073
        {
Packit db3073
          profile_allocs += n_bytes;
Packit db3073
          if (job & PROFILER_ZINIT)
Packit db3073
            profile_zinit += n_bytes;
Packit db3073
        }
Packit db3073
      else
Packit db3073
        profile_frees += n_bytes;
Packit db3073
    }
Packit db3073
  g_mutex_unlock (&gmem_profile_mutex);
Packit db3073
}
Packit db3073
Packit db3073
static void
Packit db3073
profile_print_locked (guint   *local_data,
Packit db3073
		      gboolean success)
Packit db3073
{
Packit db3073
  gboolean need_header = TRUE;
Packit db3073
  guint i;
Packit db3073
Packit db3073
  for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
Packit db3073
    {
Packit db3073
      glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
Packit db3073
      glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
Packit db3073
      glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
Packit db3073
      glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
Packit db3073
      
Packit db3073
      if (!t_malloc && !t_realloc && !t_free && !t_refree)
Packit db3073
	continue;
Packit db3073
      else if (need_header)
Packit db3073
	{
Packit db3073
	  need_header = FALSE;
Packit db3073
	  g_print (" blocks of | allocated  | freed      | allocated  | freed      | n_bytes   \n");
Packit db3073
	  g_print ("  n_bytes  | n_times by | n_times by | n_times by | n_times by | remaining \n");
Packit db3073
	  g_print ("           | malloc()   | free()     | realloc()  | realloc()  |           \n");
Packit db3073
	  g_print ("===========|============|============|============|============|===========\n");
Packit db3073
	}
Packit db3073
      if (i < MEM_PROFILE_TABLE_SIZE)
Packit db3073
	g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
Packit db3073
		 i, t_malloc, t_free, t_realloc, t_refree,
Packit db3073
		 (t_malloc - t_free + t_realloc - t_refree) * i);
Packit db3073
      else if (i >= MEM_PROFILE_TABLE_SIZE)
Packit db3073
	g_print ("   >%6u | %10ld | %10ld | %10ld | %10ld |        ***\n",
Packit db3073
		 i, t_malloc, t_free, t_realloc, t_refree);
Packit db3073
    }
Packit db3073
  if (need_header)
Packit db3073
    g_print (" --- none ---\n");
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_mem_profile:
Packit db3073
 * 
Packit db3073
 * Outputs a summary of memory usage.
Packit db3073
 * 
Packit db3073
 * It outputs the frequency of allocations of different sizes,
Packit db3073
 * the total number of bytes which have been allocated,
Packit db3073
 * the total number of bytes which have been freed,
Packit db3073
 * and the difference between the previous two values, i.e. the number of bytes
Packit db3073
 * still in use.
Packit db3073
 * 
Packit db3073
 * Note that this function will not output anything unless you have
Packit db3073
 * previously installed the #glib_mem_profiler_table with g_mem_set_vtable().
Packit db3073
 */
Packit db3073
Packit db3073
void
Packit db3073
g_mem_profile (void)
Packit db3073
{
Packit db3073
  guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
Packit db3073
  gsize local_allocs;
Packit db3073
  gsize local_zinit;
Packit db3073
  gsize local_frees;
Packit db3073
Packit db3073
  g_mutex_lock (&gmem_profile_mutex);
Packit db3073
Packit db3073
  local_allocs = profile_allocs;
Packit db3073
  local_zinit = profile_zinit;
Packit db3073
  local_frees = profile_frees;
Packit db3073
Packit db3073
  if (!profile_data)
Packit db3073
    {
Packit db3073
      g_mutex_unlock (&gmem_profile_mutex);
Packit db3073
      return;
Packit db3073
    }
Packit db3073
Packit db3073
  memcpy (local_data, profile_data, 
Packit db3073
	  (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
Packit db3073
  
Packit db3073
  g_mutex_unlock (&gmem_profile_mutex);
Packit db3073
Packit db3073
  g_print ("GLib Memory statistics (successful operations):\n");
Packit db3073
  profile_print_locked (local_data, TRUE);
Packit db3073
  g_print ("GLib Memory statistics (failing operations):\n");
Packit db3073
  profile_print_locked (local_data, FALSE);
Packit db3073
  g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT", "
Packit db3073
           "zero-initialized=%"G_GSIZE_FORMAT" (%.2f%%), "
Packit db3073
           "freed=%"G_GSIZE_FORMAT" (%.2f%%), "
Packit db3073
           "remaining=%"G_GSIZE_FORMAT"\n",
Packit db3073
	   local_allocs,
Packit db3073
	   local_zinit,
Packit db3073
	   ((gdouble) local_zinit) / local_allocs * 100.0,
Packit db3073
	   local_frees,
Packit db3073
	   ((gdouble) local_frees) / local_allocs * 100.0,
Packit db3073
	   local_allocs - local_frees);
Packit db3073
}
Packit db3073
Packit db3073
static gpointer
Packit db3073
profiler_try_malloc (gsize n_bytes)
Packit db3073
{
Packit db3073
  gsize *p;
Packit db3073
Packit db3073
#ifdef  G_ENABLE_DEBUG
Packit db3073
  if (g_trap_malloc_size == n_bytes)
Packit db3073
    G_BREAKPOINT ();
Packit db3073
#endif  /* G_ENABLE_DEBUG */
Packit db3073
Packit db3073
  p = standard_malloc (sizeof (gsize) * 2 + n_bytes);
Packit db3073
Packit db3073
  if (p)
Packit db3073
    {
Packit db3073
      p[0] = 0;		/* free count */
Packit db3073
      p[1] = n_bytes;	/* length */
Packit db3073
      profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
Packit db3073
      p += 2;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
Packit db3073
  
Packit db3073
  return p;
Packit db3073
}
Packit db3073
Packit db3073
static gpointer
Packit db3073
profiler_malloc (gsize n_bytes)
Packit db3073
{
Packit db3073
  gpointer mem = profiler_try_malloc (n_bytes);
Packit db3073
Packit db3073
  if (!mem)
Packit db3073
    g_mem_profile ();
Packit db3073
Packit db3073
  return mem;
Packit db3073
}
Packit db3073
Packit db3073
static gpointer
Packit db3073
profiler_calloc (gsize n_blocks,
Packit db3073
		 gsize n_block_bytes)
Packit db3073
{
Packit db3073
  gsize l = n_blocks * n_block_bytes;
Packit db3073
  gsize *p;
Packit db3073
Packit db3073
#ifdef  G_ENABLE_DEBUG
Packit db3073
  if (g_trap_malloc_size == l)
Packit db3073
    G_BREAKPOINT ();
Packit db3073
#endif  /* G_ENABLE_DEBUG */
Packit db3073
  
Packit db3073
  p = standard_calloc (1, sizeof (gsize) * 2 + l);
Packit db3073
Packit db3073
  if (p)
Packit db3073
    {
Packit db3073
      p[0] = 0;		/* free count */
Packit db3073
      p[1] = l;		/* length */
Packit db3073
      profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
Packit db3073
      p += 2;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
Packit db3073
      g_mem_profile ();
Packit db3073
    }
Packit db3073
Packit db3073
  return p;
Packit db3073
}
Packit db3073
Packit db3073
static void
Packit db3073
profiler_free (gpointer mem)
Packit db3073
{
Packit db3073
  gsize *p = mem;
Packit db3073
Packit db3073
  p -= 2;
Packit db3073
  if (p[0])	/* free count */
Packit db3073
    {
Packit db3073
      g_warning ("free(%p): memory has been freed %"G_GSIZE_FORMAT" times already",
Packit db3073
                 p + 2, p[0]);
Packit db3073
      profiler_log (PROFILER_FREE,
Packit db3073
		    p[1],	/* length */
Packit db3073
		    FALSE);
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
#ifdef  G_ENABLE_DEBUG
Packit db3073
      if (g_trap_free_size == p[1])
Packit db3073
	G_BREAKPOINT ();
Packit db3073
#endif  /* G_ENABLE_DEBUG */
Packit db3073
Packit db3073
      profiler_log (PROFILER_FREE,
Packit db3073
		    p[1],	/* length */
Packit db3073
		    TRUE);
Packit db3073
      memset (p + 2, 0xaa, p[1]);
Packit db3073
Packit db3073
      /* for all those that miss standard_free (p); in this place, yes,
Packit db3073
       * we do leak all memory when profiling, and that is intentional
Packit db3073
       * to catch double frees. patch submissions are futile.
Packit db3073
       */
Packit db3073
    }
Packit db3073
  p[0] += 1;
Packit db3073
}
Packit db3073
Packit db3073
static gpointer
Packit db3073
profiler_try_realloc (gpointer mem,
Packit db3073
		      gsize    n_bytes)
Packit db3073
{
Packit db3073
  gsize *p = mem;
Packit db3073
Packit db3073
  p -= 2;
Packit db3073
Packit db3073
#ifdef  G_ENABLE_DEBUG
Packit db3073
  if (g_trap_realloc_size == n_bytes)
Packit db3073
    G_BREAKPOINT ();
Packit db3073
#endif  /* G_ENABLE_DEBUG */
Packit db3073
  
Packit db3073
  if (mem && p[0])	/* free count */
Packit db3073
    {
Packit db3073
      g_warning ("realloc(%p, %"G_GSIZE_FORMAT"): "
Packit db3073
                 "memory has been freed %"G_GSIZE_FORMAT" times already",
Packit db3073
                 p + 2, (gsize) n_bytes, p[0]);
Packit db3073
      profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
Packit db3073
Packit db3073
      return NULL;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      p = standard_realloc (mem ? p : NULL, sizeof (gsize) * 2 + n_bytes);
Packit db3073
Packit db3073
      if (p)
Packit db3073
	{
Packit db3073
	  if (mem)
Packit db3073
	    profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
Packit db3073
	  p[0] = 0;
Packit db3073
	  p[1] = n_bytes;
Packit db3073
	  profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
Packit db3073
	  p += 2;
Packit db3073
	}
Packit db3073
      else
Packit db3073
	profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
Packit db3073
Packit db3073
      return p;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
static gpointer
Packit db3073
profiler_realloc (gpointer mem,
Packit db3073
		  gsize    n_bytes)
Packit db3073
{
Packit db3073
  mem = profiler_try_realloc (mem, n_bytes);
Packit db3073
Packit db3073
  if (!mem)
Packit db3073
    g_mem_profile ();
Packit db3073
Packit db3073
  return mem;
Packit db3073
}
Packit db3073
Packit db3073
static GMemVTable profiler_table = {
Packit db3073
  profiler_malloc,
Packit db3073
  profiler_realloc,
Packit db3073
  profiler_free,
Packit db3073
  profiler_calloc,
Packit db3073
  profiler_try_malloc,
Packit db3073
  profiler_try_realloc,
Packit db3073
};
Packit db3073
GMemVTable *glib_mem_profiler_table = &profiler_table;
Packit db3073
Packit db3073
#endif	/* !G_DISABLE_CHECKS */