Main Page | Class List | Directories | File List | Class Members | File Members

gmem.c File Reference

#include <stdlib.h>
#include <string.h>
#include "glib.h"

Go to the source code of this file.

Classes

struct  _GFreeAtom
struct  _GMemArea
struct  _GRealMemChunk
struct  _GAllocator

Defines

#define MEM_PROFILE_TABLE_SIZE   8192
#define ENTER_MEM_CHUNK_ROUTINE()
#define LEAVE_MEM_CHUNK_ROUTINE()
#define MEM_AREA_SIZE   4L
#define MEM_ALIGN   SIZEOF_LONG

Typedefs

typedef _GFreeAtom GFreeAtom
typedef _GMemArea GMemArea
typedef _GRealMemChunk GRealMemChunk

Functions

static gulong g_mem_chunk_compute_size (gulong size, gulong min_size)
static gint g_mem_chunk_area_compare (GMemArea *a, GMemArea *b)
static gint g_mem_chunk_area_search (GMemArea *a, gchar *addr)
gpointer g_malloc (gulong size)
gpointer g_malloc0 (gulong size)
gpointer g_realloc (gpointer mem, gulong size)
void g_free (gpointer mem)
void g_mem_profile (void)
void g_mem_check (gpointer mem)
GMemChunkg_mem_chunk_new (gchar *name, gint atom_size, gulong area_size, gint type)
void g_mem_chunk_destroy (GMemChunk *mem_chunk)
gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk)
gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
void g_mem_chunk_free (GMemChunk *mem_chunk, gpointer mem)
void g_mem_chunk_clean (GMemChunk *mem_chunk)
void g_mem_chunk_reset (GMemChunk *mem_chunk)
void g_mem_chunk_print (GMemChunk *mem_chunk)
void g_mem_chunk_info (void)
void g_blow_chunks (void)
GAllocatorg_allocator_new (const gchar *name, guint n_preallocs)
void g_allocator_free (GAllocator *allocator)
void g_mem_init (void)

Variables

static GMutexmem_chunks_lock = NULL
static GRealMemChunkmem_chunks = NULL


Define Documentation

 
#define ENTER_MEM_CHUNK_ROUTINE  ) 
 

Definition at line 69 of file gmem.c.

Referenced by g_mem_chunk_alloc(), g_mem_chunk_destroy(), g_mem_chunk_free(), and g_mem_chunk_new().

 
#define LEAVE_MEM_CHUNK_ROUTINE  ) 
 

Definition at line 70 of file gmem.c.

Referenced by g_mem_chunk_alloc(), g_mem_chunk_destroy(), g_mem_chunk_free(), and g_mem_chunk_new().

#define MEM_ALIGN   SIZEOF_LONG
 

Definition at line 79 of file gmem.c.

Referenced by g_mem_chunk_new().

#define MEM_AREA_SIZE   4L
 

Definition at line 74 of file gmem.c.

Referenced by g_mem_chunk_alloc(), and g_mem_chunk_new().

#define MEM_PROFILE_TABLE_SIZE   8192
 

Definition at line 42 of file gmem.c.

Referenced by g_malloc(), g_malloc0(), g_mem_profile(), and g_realloc().


Typedef Documentation

typedef struct _GFreeAtom GFreeAtom
 

Definition at line 83 of file gmem.c.

typedef struct _GMemArea GMemArea
 

Definition at line 84 of file gmem.c.

typedef struct _GRealMemChunk GRealMemChunk
 

Definition at line 85 of file gmem.c.


Function Documentation

void g_allocator_free GAllocator allocator  ) 
 

Definition at line 988 of file gmem.c.

References g_free(), g_mem_chunk_destroy(), g_return_if_fail, _GAllocator::is_unused, _GAllocator::mem_chunk, _GAllocator::name, NULL, and TRUE.

00989 {
00990   g_return_if_fail (allocator != NULL);
00991   g_return_if_fail (allocator->is_unused == TRUE);
00992 
00993   g_free (allocator->name);
00994   if (allocator->mem_chunk)
00995     g_mem_chunk_destroy (allocator->mem_chunk);
00996 
00997   g_free (allocator);
00998 }

GAllocator* g_allocator_new const gchar name,
guint  n_preallocs
 

Definition at line 968 of file gmem.c.

References CLAMP, _GAllocator::dummy, g_new0, g_return_val_if_fail, g_strdup(), _GAllocator::is_unused, _GAllocator::last, _GAllocator::mem_chunk, _GAllocator::n_preallocs, _GAllocator::name, NULL, TRUE, and _GAllocator::type.

Referenced by g_list_alloc(), g_node_new(), and g_slist_alloc().

00970 {
00971   GAllocator *allocator;
00972 
00973   g_return_val_if_fail (name != NULL, NULL);
00974 
00975   allocator = g_new0 (GAllocator, 1);
00976   allocator->name = g_strdup (name);
00977   allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
00978   allocator->is_unused = TRUE;
00979   allocator->type = 0;
00980   allocator->last = NULL;
00981   allocator->mem_chunk = NULL;
00982   allocator->dummy = NULL;
00983 
00984   return allocator;
00985 }

void g_blow_chunks void   ) 
 

Definition at line 895 of file gmem.c.

References g_mem_chunk_clean(), g_mutex_lock, g_mutex_unlock, and mem_chunks_lock.

00896 {
00897   GRealMemChunk *mem_chunk;
00898   
00899   g_mutex_lock (mem_chunks_lock);
00900   mem_chunk = mem_chunks;
00901   g_mutex_unlock (mem_chunks_lock);
00902   while (mem_chunk)
00903     {
00904       g_mem_chunk_clean ((GMemChunk*) mem_chunk);
00905       mem_chunk = mem_chunk->next;
00906     }
00907 }

void g_free gpointer  mem  ) 
 

Definition at line 382 of file gmem.c.

References g_mutex_lock, g_mutex_unlock, and g_warning().

Referenced by g_allocator_free(), g_array_free(), g_cache_destroy(), g_completion_add_items(), g_completion_clear_items(), g_completion_complete(), g_completion_free(), g_cond_free_posix_impl(), g_cond_free_solaris_impl(), g_date_free(), g_date_prepare_to_parse(), g_get_any_init(), g_get_current_dir(), g_hash_table_destroy(), g_hash_table_resize(), g_io_unix_destroy(), g_io_unix_free(), g_io_win32_destroy(), g_io_win32_free(), g_io_win32_pipe_destroy(), g_log_domain_check_free(), g_log_remove_handler(), g_main_destroy(), g_main_poll(), g_mem_chunk_alloc(), g_mem_chunk_clean(), g_mem_chunk_destroy(), g_mem_chunk_reset(), g_module_close(), g_module_open(), g_module_set_error(), g_module_symbol(), g_mutex_free_posix_impl(), g_node_test(), g_parse_debug_string(), g_print(), g_printerr(), g_ptr_array_free(), g_realloc(), g_relation_destroy(), g_scanner_destroy(), g_scanner_destroy_symbol_table_entry(), g_scanner_error(), g_scanner_free_value(), g_scanner_get_token_ll(), g_scanner_input_text(), g_scanner_lookup_internal(), g_scanner_scope_remove_symbol(), g_scanner_stat_mode(), g_scanner_unexp_token(), g_scanner_warn(), g_set_prgname(), g_snprintf(), g_strerror(), g_strfreev(), g_string_chunk_free(), g_string_free(), g_string_sprintfa_int(), g_strsignal(), g_strtod(), g_timer_destroy(), g_tree_destroy(), g_tuples_destroy(), g_vsnprintf(), main(), node_build_string(), private_destructor(), and second_hash_test().

00383 {
00384   if (mem)
00385     {
00386 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00387       gulong *t;
00388       gulong size;
00389 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00390       
00391 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00392       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
00393       size = *t;
00394 #ifdef ENABLE_MEM_PROFILE     
00395       g_mutex_lock (mem_profile_lock);
00396       freed_mem += size;
00397       g_mutex_unlock (mem_profile_lock);
00398 #endif /* ENABLE_MEM_PROFILE */
00399       mem = t;
00400 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00401       
00402 #ifdef ENABLE_MEM_CHECK
00403       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
00404       if (*t >= 1)
00405         g_warning ("freeing previously freed (%lu times) memory\n", *t);
00406       *t += 1;
00407       mem = t;
00408       
00409       memset ((guchar*) mem + 2 * SIZEOF_LONG, 0, size);
00410 #else /* ENABLE_MEM_CHECK */
00411       free (mem);
00412 #endif /* ENABLE_MEM_CHECK */
00413     }
00414 }

gpointer g_malloc gulong  size  ) 
 

Definition at line 154 of file gmem.c.

References g_error(), g_mutex_lock, g_mutex_unlock, MEM_PROFILE_TABLE_SIZE, and NULL.

Referenced by g_get_any_init(), g_getenv(), g_mem_chunk_alloc(), g_memdup(), and g_relation_select().

00155 {
00156   gpointer p;
00157   
00158   
00159 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00160   gulong *t;
00161 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00162   
00163   
00164   if (size == 0)
00165     return NULL;
00166   
00167   
00168 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00169   size += SIZEOF_LONG;
00170 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00171   
00172 #ifdef ENABLE_MEM_CHECK
00173   size += SIZEOF_LONG;
00174 #endif /* ENABLE_MEM_CHECK */
00175   
00176   
00177   p = (gpointer) malloc (size);
00178   if (!p)
00179     g_error ("could not allocate %ld bytes", size);
00180   
00181   
00182 #ifdef ENABLE_MEM_CHECK
00183   size -= SIZEOF_LONG;
00184   
00185   t = p;
00186   p = ((guchar*) p + SIZEOF_LONG);
00187   *t = 0;
00188 #endif /* ENABLE_MEM_CHECK */
00189   
00190 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00191   size -= SIZEOF_LONG;
00192   
00193   t = p;
00194   p = ((guchar*) p + SIZEOF_LONG);
00195   *t = size;
00196   
00197 #ifdef ENABLE_MEM_PROFILE
00198   g_mutex_lock (mem_profile_lock);
00199 #  ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
00200   if(!IS_IN_MEM_CHUNK_ROUTINE()) {
00201 #  endif
00202     if (size <= MEM_PROFILE_TABLE_SIZE - 1)
00203       allocations[size-1] += 1;
00204     else
00205       allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
00206     allocated_mem += size;
00207 #  ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
00208   }
00209 #  endif
00210   g_mutex_unlock (mem_profile_lock);
00211 #endif /* ENABLE_MEM_PROFILE */
00212 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00213   
00214   
00215   return p;
00216 }

gpointer g_malloc0 gulong  size  ) 
 

Definition at line 219 of file gmem.c.

References g_error(), g_mutex_lock, g_mutex_unlock, MEM_PROFILE_TABLE_SIZE, and NULL.

00220 {
00221   gpointer p;
00222   
00223   
00224 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00225   gulong *t;
00226 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00227   
00228   
00229   if (size == 0)
00230     return NULL;
00231   
00232   
00233 #if defined (ENABLE_MEM_PROFILE) || defined (ENABLE_MEM_CHECK)
00234   size += SIZEOF_LONG;
00235 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00236   
00237 #ifdef ENABLE_MEM_CHECK
00238   size += SIZEOF_LONG;
00239 #endif /* ENABLE_MEM_CHECK */
00240   
00241   
00242   p = (gpointer) calloc (size, 1);
00243   if (!p)
00244     g_error ("could not allocate %ld bytes", size);
00245   
00246   
00247 #ifdef ENABLE_MEM_CHECK
00248   size -= SIZEOF_LONG;
00249   
00250   t = p;
00251   p = ((guchar*) p + SIZEOF_LONG);
00252   *t = 0;
00253 #endif /* ENABLE_MEM_CHECK */
00254   
00255 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
00256   size -= SIZEOF_LONG;
00257   
00258   t = p;
00259   p = ((guchar*) p + SIZEOF_LONG);
00260   *t = size;
00261   
00262 #  ifdef ENABLE_MEM_PROFILE
00263   g_mutex_lock (mem_profile_lock);
00264 #    ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
00265   if(!IS_IN_MEM_CHUNK_ROUTINE()) {
00266 #    endif
00267     if (size <= (MEM_PROFILE_TABLE_SIZE - 1))
00268       allocations[size-1] += 1;
00269     else
00270       allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
00271     allocated_mem += size;
00272 #    ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
00273   }
00274 #    endif
00275   g_mutex_unlock (mem_profile_lock);
00276 #  endif /* ENABLE_MEM_PROFILE */
00277 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
00278   
00279   
00280   return p;
00281 }

void g_mem_check gpointer  mem  ) 
 

Definition at line 451 of file gmem.c.

References g_warning().

00452 {
00453 #ifdef ENABLE_MEM_CHECK
00454   gulong *t;
00455   
00456   t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
00457   
00458   if (*t >= 1)
00459     g_warning ("mem: 0x%08lx has been freed %lu times\n", (gulong) mem, *t);
00460 #endif /* ENABLE_MEM_CHECK */
00461 }

gpointer g_mem_chunk_alloc GMemChunk mem_chunk  ) 
 

Definition at line 555 of file gmem.c.

References _GMemArea::allocated, _GRealMemChunk::area_size, _GRealMemChunk::atom_size, ENTER_MEM_CHUNK_ROUTINE, _GMemArea::free, _GRealMemChunk::free_atoms, _GRealMemChunk::free_mem_area, G_ALLOC_AND_FREE, g_free(), g_malloc(), g_mem_chunk_area_search(), g_return_val_if_fail, g_tree_insert(), g_tree_remove(), g_tree_search(), _GMemArea::index, LEAVE_MEM_CHUNK_ROUTINE, _GMemArea::mark, _GMemArea::mem, _GRealMemChunk::mem_area, MEM_AREA_SIZE, _GRealMemChunk::mem_areas, _GRealMemChunk::mem_tree, _GMemArea::next, _GFreeAtom::next, NULL, _GRealMemChunk::num_marked_areas, _GRealMemChunk::num_mem_areas, _GMemArea::prev, and _GRealMemChunk::type.

Referenced by g_mem_chunk_alloc0().

00556 {
00557   GRealMemChunk *rmem_chunk;
00558   GMemArea *temp_area;
00559   gpointer mem;
00560 
00561   ENTER_MEM_CHUNK_ROUTINE();
00562 
00563   g_return_val_if_fail (mem_chunk != NULL, NULL);
00564   
00565   rmem_chunk = (GRealMemChunk*) mem_chunk;
00566   
00567   while (rmem_chunk->free_atoms)
00568     {
00569       /* Get the first piece of memory on the "free_atoms" list.
00570        * We can go ahead and destroy the list node we used to keep
00571        *  track of it with and to update the "free_atoms" list to
00572        *  point to its next element.
00573        */
00574       mem = rmem_chunk->free_atoms;
00575       rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
00576       
00577       /* Determine which area this piece of memory is allocated from */
00578       temp_area = g_tree_search (rmem_chunk->mem_tree,
00579                                  (GSearchFunc) g_mem_chunk_area_search,
00580                                  mem);
00581       
00582       /* If the area has been marked, then it is being destroyed.
00583        *  (ie marked to be destroyed).
00584        * We check to see if all of the segments on the free list that
00585        *  reference this area have been removed. This occurs when
00586        *  the ammount of free memory is less than the allocatable size.
00587        * If the chunk should be freed, then we place it in the "free_mem_area".
00588        * This is so we make sure not to free the mem area here and then
00589        *  allocate it again a few lines down.
00590        * If we don't allocate a chunk a few lines down then the "free_mem_area"
00591        *  will be freed.
00592        * If there is already a "free_mem_area" then we'll just free this mem area.
00593        */
00594       if (temp_area->mark)
00595         {
00596           /* Update the "free" memory available in that area */
00597           temp_area->free += rmem_chunk->atom_size;
00598           
00599           if (temp_area->free == rmem_chunk->area_size)
00600             {
00601               if (temp_area == rmem_chunk->mem_area)
00602                 rmem_chunk->mem_area = NULL;
00603               
00604               if (rmem_chunk->free_mem_area)
00605                 {
00606                   rmem_chunk->num_mem_areas -= 1;
00607                   
00608                   if (temp_area->next)
00609                     temp_area->next->prev = temp_area->prev;
00610                   if (temp_area->prev)
00611                     temp_area->prev->next = temp_area->next;
00612                   if (temp_area == rmem_chunk->mem_areas)
00613                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
00614                   
00615                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
00616                     g_tree_remove (rmem_chunk->mem_tree, temp_area);
00617                   g_free (temp_area);
00618                 }
00619               else
00620                 rmem_chunk->free_mem_area = temp_area;
00621               
00622               rmem_chunk->num_marked_areas -= 1;
00623             }
00624         }
00625       else
00626         {
00627           /* Update the number of allocated atoms count.
00628            */
00629           temp_area->allocated += 1;
00630           
00631           /* The area wasn't marked...return the memory
00632            */
00633           goto outa_here;
00634         }
00635     }
00636   
00637   /* If there isn't a current mem area or the current mem area is out of space
00638    *  then allocate a new mem area. We'll first check and see if we can use
00639    *  the "free_mem_area". Otherwise we'll just malloc the mem area.
00640    */
00641   if ((!rmem_chunk->mem_area) ||
00642       ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
00643     {
00644       if (rmem_chunk->free_mem_area)
00645         {
00646           rmem_chunk->mem_area = rmem_chunk->free_mem_area;
00647           rmem_chunk->free_mem_area = NULL;
00648         }
00649       else
00650         {
00651           rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
00652                                                        MEM_AREA_SIZE +
00653                                                        rmem_chunk->area_size);
00654           
00655           rmem_chunk->num_mem_areas += 1;
00656           rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
00657           rmem_chunk->mem_area->prev = NULL;
00658           
00659           if (rmem_chunk->mem_areas)
00660             rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
00661           rmem_chunk->mem_areas = rmem_chunk->mem_area;
00662           
00663           if (rmem_chunk->type == G_ALLOC_AND_FREE)
00664             g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
00665         }
00666       
00667       rmem_chunk->mem_area->index = 0;
00668       rmem_chunk->mem_area->free = rmem_chunk->area_size;
00669       rmem_chunk->mem_area->allocated = 0;
00670       rmem_chunk->mem_area->mark = 0;
00671     }
00672   
00673   /* Get the memory and modify the state variables appropriately.
00674    */
00675   mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
00676   rmem_chunk->mem_area->index += rmem_chunk->atom_size;
00677   rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
00678   rmem_chunk->mem_area->allocated += 1;
00679 
00680 outa_here:
00681 
00682   LEAVE_MEM_CHUNK_ROUTINE();
00683 
00684   return mem;
00685 }

gpointer g_mem_chunk_alloc0 GMemChunk mem_chunk  ) 
 

Definition at line 688 of file gmem.c.

References _GRealMemChunk::atom_size, and g_mem_chunk_alloc().

00689 {
00690   gpointer mem;
00691 
00692   mem = g_mem_chunk_alloc (mem_chunk);
00693   if (mem)
00694     {
00695       GRealMemChunk *rmem_chunk = (GRealMemChunk*) mem_chunk;
00696 
00697       memset (mem, 0, rmem_chunk->atom_size);
00698     }
00699 
00700   return mem;
00701 }

static gint g_mem_chunk_area_compare GMemArea a,
GMemArea b
[static]
 

Definition at line 931 of file gmem.c.

References _GMemArea::mem.

Referenced by g_mem_chunk_new(), and g_mem_chunk_reset().

00933 {
00934   if (a->mem > b->mem)
00935         return 1;
00936   else if (a->mem < b->mem)
00937         return -1;
00938   return 0;
00939 }

static gint g_mem_chunk_area_search GMemArea a,
gchar addr
[static]
 

Definition at line 942 of file gmem.c.

References _GMemArea::index, and _GMemArea::mem.

Referenced by g_mem_chunk_alloc(), g_mem_chunk_clean(), and g_mem_chunk_free().

00944 {
00945   if (a->mem <= addr)
00946     {
00947       if (addr < &a->mem[a->index])
00948         return 0;
00949       return 1;
00950     }
00951   return -1;
00952 }

void g_mem_chunk_clean GMemChunk mem_chunk  ) 
 

Definition at line 746 of file gmem.c.

References _GRealMemChunk::area_size, _GRealMemChunk::atom_size, _GMemArea::free, _GRealMemChunk::free_atoms, G_ALLOC_AND_FREE, g_free(), g_mem_chunk_area_search(), g_return_if_fail, g_tree_remove(), g_tree_search(), _GMemArea::mark, _GRealMemChunk::mem_area, _GRealMemChunk::mem_areas, _GRealMemChunk::mem_tree, _GMemArea::next, _GFreeAtom::next, NULL, _GRealMemChunk::num_marked_areas, _GRealMemChunk::num_mem_areas, _GMemArea::prev, and _GRealMemChunk::type.

Referenced by g_blow_chunks().

00747 {
00748   GRealMemChunk *rmem_chunk;
00749   GMemArea *mem_area;
00750   GFreeAtom *prev_free_atom;
00751   GFreeAtom *temp_free_atom;
00752   gpointer mem;
00753   
00754   g_return_if_fail (mem_chunk != NULL);
00755   
00756   rmem_chunk = (GRealMemChunk*) mem_chunk;
00757   
00758   if (rmem_chunk->type == G_ALLOC_AND_FREE)
00759     {
00760       prev_free_atom = NULL;
00761       temp_free_atom = rmem_chunk->free_atoms;
00762       
00763       while (temp_free_atom)
00764         {
00765           mem = (gpointer) temp_free_atom;
00766           
00767           mem_area = g_tree_search (rmem_chunk->mem_tree,
00768                                     (GSearchFunc) g_mem_chunk_area_search,
00769                                     mem);
00770           
00771           /* If this mem area is marked for destruction then delete the
00772            *  area and list node and decrement the free mem.
00773            */
00774           if (mem_area->mark)
00775             {
00776               if (prev_free_atom)
00777                 prev_free_atom->next = temp_free_atom->next;
00778               else
00779                 rmem_chunk->free_atoms = temp_free_atom->next;
00780               temp_free_atom = temp_free_atom->next;
00781               
00782               mem_area->free += rmem_chunk->atom_size;
00783               if (mem_area->free == rmem_chunk->area_size)
00784                 {
00785                   rmem_chunk->num_mem_areas -= 1;
00786                   rmem_chunk->num_marked_areas -= 1;
00787                   
00788                   if (mem_area->next)
00789                     mem_area->next->prev = mem_area->prev;
00790                   if (mem_area->prev)
00791                     mem_area->prev->next = mem_area->next;
00792                   if (mem_area == rmem_chunk->mem_areas)
00793                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
00794                   if (mem_area == rmem_chunk->mem_area)
00795                     rmem_chunk->mem_area = NULL;
00796                   
00797                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
00798                     g_tree_remove (rmem_chunk->mem_tree, mem_area);
00799                   g_free (mem_area);
00800                 }
00801             }
00802           else
00803             {
00804               prev_free_atom = temp_free_atom;
00805               temp_free_atom = temp_free_atom->next;
00806             }
00807         }
00808     }
00809 }

static gulong g_mem_chunk_compute_size gulong  size,
gulong  min_size
[static]
 

Definition at line 911 of file gmem.c.

Referenced by g_mem_chunk_new().

00913 {
00914   gulong power_of_2;
00915   gulong lower, upper;
00916   
00917   power_of_2 = 16;
00918   while (power_of_2 < size)
00919     power_of_2 <<= 1;
00920   
00921   lower = power_of_2 >> 1;
00922   upper = power_of_2;
00923   
00924   if (size - lower < upper - size && lower >= min_size)
00925     return lower;
00926   else
00927     return upper;
00928 }

void g_mem_chunk_destroy GMemChunk mem_chunk  ) 
 

Definition at line 516 of file gmem.c.

References ENTER_MEM_CHUNK_ROUTINE, G_ALLOC_AND_FREE, g_free(), g_mutex_lock, g_mutex_unlock, g_return_if_fail, g_tree_destroy(), LEAVE_MEM_CHUNK_ROUTINE, _GRealMemChunk::mem_areas, mem_chunks_lock, _GRealMemChunk::mem_tree, _GRealMemChunk::next, _GMemArea::next, NULL, _GRealMemChunk::prev, and _GRealMemChunk::type.

Referenced by g_allocator_free(), g_hook_list_clear(), g_hook_unref(), g_list_validate_allocator(), g_node_validate_allocator(), g_relation_destroy(), and g_slist_validate_allocator().

00517 {
00518   GRealMemChunk *rmem_chunk;
00519   GMemArea *mem_areas;
00520   GMemArea *temp_area;
00521   
00522   g_return_if_fail (mem_chunk != NULL);
00523 
00524   ENTER_MEM_CHUNK_ROUTINE();
00525 
00526   rmem_chunk = (GRealMemChunk*) mem_chunk;
00527   
00528   mem_areas = rmem_chunk->mem_areas;
00529   while (mem_areas)
00530     {
00531       temp_area = mem_areas;
00532       mem_areas = mem_areas->next;
00533       g_free (temp_area);
00534     }
00535   
00536   if (rmem_chunk->next)
00537     rmem_chunk->next->prev = rmem_chunk->prev;
00538   if (rmem_chunk->prev)
00539     rmem_chunk->prev->next = rmem_chunk->next;
00540   
00541   g_mutex_lock (mem_chunks_lock);
00542   if (rmem_chunk == mem_chunks)
00543     mem_chunks = mem_chunks->next;
00544   g_mutex_unlock (mem_chunks_lock);
00545   
00546   if (rmem_chunk->type == G_ALLOC_AND_FREE)
00547     g_tree_destroy (rmem_chunk->mem_tree);
00548   
00549   g_free (rmem_chunk);
00550 
00551   LEAVE_MEM_CHUNK_ROUTINE();
00552 }

void g_mem_chunk_free GMemChunk mem_chunk,
gpointer  mem
 

Definition at line 704 of file gmem.c.

References _GMemArea::allocated, ENTER_MEM_CHUNK_ROUTINE, _GRealMemChunk::free_atoms, G_ALLOC_AND_FREE, g_mem_chunk_area_search(), g_return_if_fail, g_tree_search(), LEAVE_MEM_CHUNK_ROUTINE, _GMemArea::mark, _GRealMemChunk::mem_tree, _GFreeAtom::next, NULL, _GRealMemChunk::num_marked_areas, and _GRealMemChunk::type.

Referenced by g_array_free(), g_cache_node_destroy(), g_data_set_internal(), g_datalist_clear_i(), g_dataset_destroy_internal(), g_ptr_array_free(), g_relation_exists(), g_string_free(), and main().

00706 {
00707   GRealMemChunk *rmem_chunk;
00708   GMemArea *temp_area;
00709   GFreeAtom *free_atom;
00710   
00711   g_return_if_fail (mem_chunk != NULL);
00712   g_return_if_fail (mem != NULL);
00713 
00714   ENTER_MEM_CHUNK_ROUTINE();
00715 
00716   rmem_chunk = (GRealMemChunk*) mem_chunk;
00717   
00718   /* Don't do anything if this is an ALLOC_ONLY chunk
00719    */
00720   if (rmem_chunk->type == G_ALLOC_AND_FREE)
00721     {
00722       /* Place the memory on the "free_atoms" list
00723        */
00724       free_atom = (GFreeAtom*) mem;
00725       free_atom->next = rmem_chunk->free_atoms;
00726       rmem_chunk->free_atoms = free_atom;
00727       
00728       temp_area = g_tree_search (rmem_chunk->mem_tree,
00729                                  (GSearchFunc) g_mem_chunk_area_search,
00730                                  mem);
00731       
00732       temp_area->allocated -= 1;
00733       
00734       if (temp_area->allocated == 0)
00735         {
00736           temp_area->mark = 1;
00737           rmem_chunk->num_marked_areas += 1;
00738         }
00739     }
00740 
00741   LEAVE_MEM_CHUNK_ROUTINE();
00742 }

void g_mem_chunk_info void   ) 
 

Definition at line 866 of file gmem.c.

References g_log(), g_log_domain_glib, G_LOG_LEVEL_INFO, g_mem_chunk_print(), g_mutex_lock, g_mutex_unlock, mem_chunks_lock, and _GRealMemChunk::next.

00867 {
00868   GRealMemChunk *mem_chunk;
00869   gint count;
00870   
00871   count = 0;
00872   g_mutex_lock (mem_chunks_lock);
00873   mem_chunk = mem_chunks;
00874   while (mem_chunk)
00875     {
00876       count += 1;
00877       mem_chunk = mem_chunk->next;
00878     }
00879   g_mutex_unlock (mem_chunks_lock);
00880   
00881   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks", count);
00882   
00883   g_mutex_lock (mem_chunks_lock);
00884   mem_chunk = mem_chunks;
00885   g_mutex_unlock (mem_chunks_lock);
00886 
00887   while (mem_chunk)
00888     {
00889       g_mem_chunk_print ((GMemChunk*) mem_chunk);
00890       mem_chunk = mem_chunk->next;
00891     }  
00892 }

GMemChunk* g_mem_chunk_new gchar name,
gint  atom_size,
gulong  area_size,
gint  type
 

Definition at line 464 of file gmem.c.

References _GRealMemChunk::area_size, _GRealMemChunk::atom_size, ENTER_MEM_CHUNK_ROUTINE, _GRealMemChunk::free_atoms, _GRealMemChunk::free_mem_area, G_ALLOC_AND_FREE, g_mem_chunk_area_compare(), g_mem_chunk_compute_size(), g_mutex_lock, g_mutex_unlock, g_new, g_return_val_if_fail, g_tree_new(), LEAVE_MEM_CHUNK_ROUTINE, MEM_ALIGN, _GRealMemChunk::mem_area, MEM_AREA_SIZE, _GRealMemChunk::mem_areas, mem_chunks_lock, _GRealMemChunk::mem_tree, _GRealMemChunk::name, _GRealMemChunk::next, NULL, _GRealMemChunk::num_marked_areas, _GRealMemChunk::num_mem_areas, _GRealMemChunk::prev, and _GRealMemChunk::type.

Referenced by g_array_new(), g_cache_node_new(), g_data_initialize(), g_hash_node_new(), g_hook_list_init(), g_list_validate_allocator(), g_node_validate_allocator(), g_ptr_array_new(), g_relation_new(), g_slist_validate_allocator(), g_string_sized_new(), g_tree_node_new(), and main().

00468 {
00469   GRealMemChunk *mem_chunk;
00470   gulong rarea_size;
00471 
00472   g_return_val_if_fail (atom_size > 0, NULL);
00473   g_return_val_if_fail (area_size >= atom_size, NULL);
00474 
00475   ENTER_MEM_CHUNK_ROUTINE();
00476 
00477   area_size = (area_size + atom_size - 1) / atom_size;
00478   area_size *= atom_size;
00479 
00480   mem_chunk = g_new (struct _GRealMemChunk, 1);
00481   mem_chunk->name = name;
00482   mem_chunk->type = type;
00483   mem_chunk->num_mem_areas = 0;
00484   mem_chunk->num_marked_areas = 0;
00485   mem_chunk->mem_area = NULL;
00486   mem_chunk->free_mem_area = NULL;
00487   mem_chunk->free_atoms = NULL;
00488   mem_chunk->mem_tree = NULL;
00489   mem_chunk->mem_areas = NULL;
00490   mem_chunk->atom_size = atom_size;
00491   
00492   if (mem_chunk->type == G_ALLOC_AND_FREE)
00493     mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
00494   
00495   if (mem_chunk->atom_size % MEM_ALIGN)
00496     mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
00497 
00498   rarea_size = area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
00499   rarea_size = g_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
00500   mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
00501 
00502   g_mutex_lock (mem_chunks_lock);
00503   mem_chunk->next = mem_chunks;
00504   mem_chunk->prev = NULL;
00505   if (mem_chunks)
00506     mem_chunks->prev = mem_chunk;
00507   mem_chunks = mem_chunk;
00508   g_mutex_unlock (mem_chunks_lock);
00509 
00510   LEAVE_MEM_CHUNK_ROUTINE();
00511 
00512   return ((GMemChunk*) mem_chunk);
00513 }

void g_mem_chunk_print GMemChunk mem_chunk  ) 
 

Definition at line 842 of file gmem.c.

References _GRealMemChunk::area_size, _GMemArea::free, g_log(), g_log_domain_glib, G_LOG_LEVEL_INFO, g_return_if_fail, _GRealMemChunk::mem_areas, _GRealMemChunk::name, _GMemArea::next, NULL, and _GRealMemChunk::num_mem_areas.

Referenced by g_mem_chunk_info().

00843 {
00844   GRealMemChunk *rmem_chunk;
00845   GMemArea *mem_areas;
00846   gulong mem;
00847   
00848   g_return_if_fail (mem_chunk != NULL);
00849   
00850   rmem_chunk = (GRealMemChunk*) mem_chunk;
00851   mem_areas = rmem_chunk->mem_areas;
00852   mem = 0;
00853   
00854   while (mem_areas)
00855     {
00856       mem += rmem_chunk->area_size - mem_areas->free;
00857       mem_areas = mem_areas->next;
00858     }
00859   
00860   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
00861          "%s: %ld bytes using %d mem areas",
00862          rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
00863 }

void g_mem_chunk_reset GMemChunk mem_chunk  ) 
 

Definition at line 812 of file gmem.c.

References _GRealMemChunk::free_atoms, g_free(), g_mem_chunk_area_compare(), g_return_if_fail, g_tree_destroy(), g_tree_new(), _GRealMemChunk::mem_area, _GRealMemChunk::mem_areas, _GRealMemChunk::mem_tree, _GMemArea::next, NULL, and _GRealMemChunk::num_mem_areas.

00813 {
00814   GRealMemChunk *rmem_chunk;
00815   GMemArea *mem_areas;
00816   GMemArea *temp_area;
00817   
00818   g_return_if_fail (mem_chunk != NULL);
00819   
00820   rmem_chunk = (GRealMemChunk*) mem_chunk;
00821   
00822   mem_areas = rmem_chunk->mem_areas;
00823   rmem_chunk->num_mem_areas = 0;
00824   rmem_chunk->mem_areas = NULL;
00825   rmem_chunk->mem_area = NULL;
00826   
00827   while (mem_areas)
00828     {
00829       temp_area = mem_areas;
00830       mem_areas = mem_areas->next;
00831       g_free (temp_area);
00832     }
00833   
00834   rmem_chunk->free_atoms = NULL;
00835   
00836   if (rmem_chunk->mem_tree)
00837     g_tree_destroy (rmem_chunk->mem_tree);
00838   rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
00839 }

void g_mem_init void   ) 
 

Definition at line 1001 of file gmem.c.

References g_mutex_new, g_private_new, mem_chunks_lock, and NULL.

Referenced by g_thread_init().

01002 {
01003   mem_chunks_lock = g_mutex_new();
01004 #ifdef ENABLE_MEM_PROFILE
01005   mem_profile_lock = g_mutex_new();
01006   allocating_for_mem_chunk = g_private_new(NULL);
01007 #endif
01008 }

void g_mem_profile void   )