Пример #1
0
/**
 * g_realloc:
 * @mem: the memory to reallocate
 * @n_bytes: new size of the memory in bytes
 * 
 * Reallocates the memory pointed to by @mem, so that it now has space for
 * @n_bytes bytes of memory. It returns the new address of the memory, which may
 * have been moved. @mem may be %NULL, in which case it's considered to
 * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
 * and @mem will be freed unless it is %NULL.
 * 
 * Returns: the new address of the allocated memory
 */
gpointer
g_realloc (gpointer mem,
	   gsize    n_bytes)
{
  gpointer newmem;

  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    {
      newmem = glib_mem_vtable.realloc (mem, n_bytes);
      TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 0));
      if (newmem)
	return newmem;

      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
               G_STRLOC, n_bytes);
    }

  if (mem)
    glib_mem_vtable.free (mem);

  TRACE (GLIB_MEM_REALLOC((void*) NULL, (void*)mem, 0, 0));

  return NULL;
}
Пример #2
0
void
g_free (gpointer mem)
{
    if (G_UNLIKELY (!g_mem_initialized))
        g_mem_init_nomessage();
    if (G_LIKELY (mem))
        glib_mem_vtable.free (mem);
}
Пример #3
0
/**
 * g_free:
 * @mem: the memory to free
 * 
 * Frees the memory pointed to by @mem.
 * If @mem is %NULL it simply returns.
 */
void
g_free (gpointer mem)
{
  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (mem))
    glib_mem_vtable.free (mem);
  TRACE(GLIB_MEM_FREE((void*) mem));
}
Пример #4
0
gpointer
g_try_malloc (gsize n_bytes)
{
    if (G_UNLIKELY (!g_mem_initialized))
        g_mem_init_nomessage();
    if (G_LIKELY (n_bytes))
        return glib_mem_vtable.try_malloc (n_bytes);
    else
        return NULL;
}
Пример #5
0
void
_g_mem_thread_init_noprivate_nomessage (void)
{
    /* we may only create mutexes here, locking/
     * unlocking a mutex does not yet work.
     */
    g_mem_init_nomessage();
#ifndef G_DISABLE_CHECKS
    gmem_profile_mutex = g_mutex_new ();
#endif
}
Пример #6
0
gpointer
g_try_realloc (gpointer mem,
               gsize    n_bytes)
{
    if (G_UNLIKELY (!g_mem_initialized))
        g_mem_init_nomessage();
    if (G_LIKELY (n_bytes))
        return glib_mem_vtable.try_realloc (mem, n_bytes);

    if (mem)
        glib_mem_vtable.free (mem);

    return NULL;
}
Пример #7
0
gpointer
g_malloc0_n (gsize n_blocks,
	     gsize n_block_bytes)
{
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
    {
      if (G_UNLIKELY (!g_mem_initialized))
	g_mem_init_nomessage();

      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
               G_STRLOC, n_blocks, n_block_bytes);
    }

  return g_malloc0 (n_blocks * n_block_bytes);
}
Пример #8
0
/**
 * g_try_malloc:
 * @n_bytes: number of bytes to allocate.
 * 
 * Attempts to allocate @n_bytes, and returns %NULL on failure.
 * Contrast with g_malloc(), which aborts the program on failure.
 * 
 * Returns: the allocated memory, or %NULL.
 */
gpointer
g_try_malloc (gsize n_bytes)
{
  gpointer mem;

  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    mem = glib_mem_vtable.try_malloc (n_bytes);
  else
    mem = NULL;

  TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));

  return mem;
}
Пример #9
0
/**
 * g_try_malloc0:
 * @n_bytes: number of bytes to allocate
 * 
 * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
 * failure. Contrast with g_malloc0(), which aborts the program on failure.
 * 
 * Since: 2.8
 * Returns: the allocated memory, or %NULL
 */
gpointer
g_try_malloc0 (gsize n_bytes)
{
  gpointer mem;

  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    mem = glib_mem_vtable.try_malloc (n_bytes);
  else
    mem = NULL;

  if (mem)
    memset (mem, 0, n_bytes);

  return mem;
}
Пример #10
0
void
g_mem_profile (void)
{
    guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
    gsize local_allocs;
    gsize local_zinit;
    gsize local_frees;

    if (G_UNLIKELY (!g_mem_initialized))
        g_mem_init_nomessage();

    g_mutex_lock (gmem_profile_mutex);

    local_allocs = profile_allocs;
    local_zinit = profile_zinit;
    local_frees = profile_frees;

    if (!profile_data)
    {
        g_mutex_unlock (gmem_profile_mutex);
        return;
    }

    memcpy (local_data, profile_data,
            (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));

    g_mutex_unlock (gmem_profile_mutex);

    g_print ("GLib Memory statistics (successful operations):\n");
    profile_print_locked (local_data, TRUE);
    g_print ("GLib Memory statistics (failing operations):\n");
    profile_print_locked (local_data, FALSE);
    g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT", "
             "zero-initialized=%"G_GSIZE_FORMAT" (%.2f%%), "
             "freed=%"G_GSIZE_FORMAT" (%.2f%%), "
             "remaining=%"G_GSIZE_FORMAT"\n",
             local_allocs,
             local_zinit,
             ((gdouble) local_zinit) / local_allocs * 100.0,
             local_frees,
             ((gdouble) local_frees) / local_allocs * 100.0,
             local_allocs - local_frees);
}
Пример #11
0
gpointer
g_malloc0 (gsize n_bytes)
{
  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    {
      gpointer mem;

      mem = glib_mem_vtable.calloc (1, n_bytes);
      if (mem)
	return mem;

      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
               G_STRLOC, n_bytes);
    }

  return NULL;
}
Пример #12
0
/* --- functions --- */
gpointer
g_malloc (gsize n_bytes)
{
    if (G_UNLIKELY (!g_mem_initialized))
        g_mem_init_nomessage();
    if (G_LIKELY (n_bytes))
    {
        gpointer mem;

        mem = glib_mem_vtable.malloc (n_bytes);
        if (mem)
            return mem;

#if NOT_NEEDED_FOR_NAVIT
        g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
                 G_STRLOC, n_bytes);
#endif /* NOT_NEEDED_FOR_NAVIT */
    }

    return NULL;
}
Пример #13
0
gpointer
g_realloc (gpointer mem,
	   gsize    n_bytes)
{
  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    {
      mem = glib_mem_vtable.realloc (mem, n_bytes);
      if (mem)
	return mem;

      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
               G_STRLOC, n_bytes);
    }

  if (mem)
    glib_mem_vtable.free (mem);

  return NULL;
}
Пример #14
0
/**
 * g_try_realloc:
 * @mem: previously-allocated memory, or %NULL.
 * @n_bytes: number of bytes to allocate.
 * 
 * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
 * on failure. Contrast with g_realloc(), which aborts the program
 * on failure. If @mem is %NULL, behaves the same as g_try_malloc().
 * 
 * Returns: the allocated memory, or %NULL.
 */
gpointer
g_try_realloc (gpointer mem,
	       gsize    n_bytes)
{
  gpointer newmem;

  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    newmem = glib_mem_vtable.try_realloc (mem, n_bytes);
  else
    {
      newmem = NULL;
      if (mem)
	glib_mem_vtable.free (mem);
    }

  TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 1));

  return newmem;
}
Пример #15
0
/**
 * g_malloc0:
 * @n_bytes: the number of bytes to allocate
 * 
 * Allocates @n_bytes bytes of memory, initialized to 0's.
 * If @n_bytes is 0 it returns %NULL.
 * 
 * Returns: a pointer to the allocated memory
 */
gpointer
g_malloc0 (gsize n_bytes)
{
  if (G_UNLIKELY (!g_mem_initialized))
    g_mem_init_nomessage();
  if (G_LIKELY (n_bytes))
    {
      gpointer mem;

      mem = glib_mem_vtable.calloc (1, n_bytes);
      TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 1, 0));
      if (mem)
	return mem;

      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
               G_STRLOC, n_bytes);
    }

  TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 1, 0));

  return NULL;
}