1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 * if ENABLE_GC_FRIENDLY is defined, freed memory should be 0-wiped.
41 #define MEM_PROFILE_TABLE_SIZE 4096
43 #define MEM_AREA_SIZE 4L
45 static guint mem_chunk_recursion
= 0;
46 # define MEM_CHUNK_ROUTINE_COUNT() (mem_chunk_recursion)
47 # define ENTER_MEM_CHUNK_ROUTINE() (mem_chunk_recursion = MEM_CHUNK_ROUTINE_COUNT () + 1)
48 # define LEAVE_MEM_CHUNK_ROUTINE() (mem_chunk_recursion = MEM_CHUNK_ROUTINE_COUNT () - 1)
50 /* --- old memchunk prototypes --- */
51 GMemChunk
* old_mem_chunk_new (const gchar
*name
,
55 void old_mem_chunk_destroy (GMemChunk
*mem_chunk
);
56 gpointer
old_mem_chunk_alloc (GMemChunk
*mem_chunk
);
57 gpointer
old_mem_chunk_alloc0 (GMemChunk
*mem_chunk
);
58 void old_mem_chunk_free (GMemChunk
*mem_chunk
,
60 void old_mem_chunk_clean (GMemChunk
*mem_chunk
);
61 void old_mem_chunk_reset (GMemChunk
*mem_chunk
);
62 void old_mem_chunk_print (GMemChunk
*mem_chunk
);
63 void old_mem_chunk_info (void);
66 /* --- MemChunks --- */
67 #ifndef G_ALLOC_AND_FREE
68 typedef struct _GAllocator GAllocator
;
69 typedef struct _GMemChunk GMemChunk
;
70 #define G_ALLOC_ONLY 1
71 #define G_ALLOC_AND_FREE 2
74 typedef struct _GFreeAtom GFreeAtom
;
75 typedef struct _GMemArea GMemArea
;
84 GMemArea
*next
; /* the next mem area */
85 GMemArea
*prev
; /* the previous mem area */
86 gulong index
; /* the current index into the "mem" array */
87 gulong free
; /* the number of free bytes in this mem area */
88 gulong allocated
; /* the number of atoms allocated from this area */
89 gulong mark
; /* is this mem area marked for deletion */
90 gchar mem
[MEM_AREA_SIZE
]; /* the mem array from which atoms get allocated
91 * the actual size of this array is determined by
92 * the mem chunk "area_size". ANSI says that it
93 * must be declared to be the maximum size it
94 * can possibly be (even though the actual size
101 const gchar
*name
; /* name of this MemChunk...used for debugging output */
102 gint type
; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
103 gint num_mem_areas
; /* the number of memory areas */
104 gint num_marked_areas
; /* the number of areas marked for deletion */
105 guint atom_size
; /* the size of an atom */
106 gulong area_size
; /* the size of a memory area */
107 GMemArea
*mem_area
; /* the current memory area */
108 GMemArea
*mem_areas
; /* a list of all the mem areas owned by this chunk */
109 GMemArea
*free_mem_area
; /* the free area...which is about to be destroyed */
110 GFreeAtom
*free_atoms
; /* the free atoms list */
111 GTree
*mem_tree
; /* tree of mem areas sorted by memory address */
112 GMemChunk
*next
; /* pointer to the next chunk */
113 GMemChunk
*prev
; /* pointer to the previous chunk */
117 static gulong
old_mem_chunk_compute_size (gulong size
,
118 gulong min_size
) G_GNUC_CONST
;
119 static gint
old_mem_chunk_area_compare (GMemArea
*a
,
121 static gint
old_mem_chunk_area_search (GMemArea
*a
,
124 /* here we can't use StaticMutexes, as they depend upon a working
125 * g_malloc, the same holds true for StaticPrivate
127 static GMutex mem_chunks_lock
;
128 static GMemChunk
*mem_chunks
= NULL
;
131 old_mem_chunk_new (const gchar
*name
,
136 GMemChunk
*mem_chunk
;
139 g_return_val_if_fail (atom_size
> 0, NULL
);
140 g_return_val_if_fail (area_size
>= atom_size
, NULL
);
142 ENTER_MEM_CHUNK_ROUTINE ();
144 area_size
= (area_size
+ atom_size
- 1) / atom_size
;
145 area_size
*= atom_size
;
147 mem_chunk
= g_new (GMemChunk
, 1);
148 mem_chunk
->name
= name
;
149 mem_chunk
->type
= type
;
150 mem_chunk
->num_mem_areas
= 0;
151 mem_chunk
->num_marked_areas
= 0;
152 mem_chunk
->mem_area
= NULL
;
153 mem_chunk
->free_mem_area
= NULL
;
154 mem_chunk
->free_atoms
= NULL
;
155 mem_chunk
->mem_tree
= NULL
;
156 mem_chunk
->mem_areas
= NULL
;
157 mem_chunk
->atom_size
= atom_size
;
159 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
160 mem_chunk
->mem_tree
= g_tree_new ((GCompareFunc
) old_mem_chunk_area_compare
);
162 if (mem_chunk
->atom_size
% G_MEM_ALIGN
)
163 mem_chunk
->atom_size
+= G_MEM_ALIGN
- (mem_chunk
->atom_size
% G_MEM_ALIGN
);
165 rarea_size
= area_size
+ sizeof (GMemArea
) - MEM_AREA_SIZE
;
166 rarea_size
= old_mem_chunk_compute_size (rarea_size
, atom_size
+ sizeof (GMemArea
) - MEM_AREA_SIZE
);
167 mem_chunk
->area_size
= rarea_size
- (sizeof (GMemArea
) - MEM_AREA_SIZE
);
169 g_mutex_lock (&mem_chunks_lock
);
170 mem_chunk
->next
= mem_chunks
;
171 mem_chunk
->prev
= NULL
;
173 mem_chunks
->prev
= mem_chunk
;
174 mem_chunks
= mem_chunk
;
175 g_mutex_unlock (&mem_chunks_lock
);
177 LEAVE_MEM_CHUNK_ROUTINE ();
183 old_mem_chunk_destroy (GMemChunk
*mem_chunk
)
188 g_return_if_fail (mem_chunk
!= NULL
);
190 ENTER_MEM_CHUNK_ROUTINE ();
192 mem_areas
= mem_chunk
->mem_areas
;
195 temp_area
= mem_areas
;
196 mem_areas
= mem_areas
->next
;
200 g_mutex_lock (&mem_chunks_lock
);
202 mem_chunk
->next
->prev
= mem_chunk
->prev
;
204 mem_chunk
->prev
->next
= mem_chunk
->next
;
206 if (mem_chunk
== mem_chunks
)
207 mem_chunks
= mem_chunks
->next
;
208 g_mutex_unlock (&mem_chunks_lock
);
210 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
211 g_tree_destroy (mem_chunk
->mem_tree
);
215 LEAVE_MEM_CHUNK_ROUTINE ();
219 old_mem_chunk_alloc (GMemChunk
*mem_chunk
)
224 ENTER_MEM_CHUNK_ROUTINE ();
226 g_return_val_if_fail (mem_chunk
!= NULL
, NULL
);
228 while (mem_chunk
->free_atoms
)
230 /* Get the first piece of memory on the "free_atoms" list.
231 * We can go ahead and destroy the list node we used to keep
232 * track of it with and to update the "free_atoms" list to
233 * point to its next element.
235 mem
= mem_chunk
->free_atoms
;
236 mem_chunk
->free_atoms
= mem_chunk
->free_atoms
->next
;
238 /* Determine which area this piece of memory is allocated from */
239 temp_area
= g_tree_search (mem_chunk
->mem_tree
,
240 (GCompareFunc
) old_mem_chunk_area_search
,
243 /* If the area has been marked, then it is being destroyed.
244 * (ie marked to be destroyed).
245 * We check to see if all of the segments on the free list that
246 * reference this area have been removed. This occurs when
247 * the amount of free memory is less than the allocatable size.
248 * If the chunk should be freed, then we place it in the "free_mem_area".
249 * This is so we make sure not to free the mem area here and then
250 * allocate it again a few lines down.
251 * If we don't allocate a chunk a few lines down then the "free_mem_area"
253 * If there is already a "free_mem_area" then we'll just free this mem area.
257 /* Update the "free" memory available in that area */
258 temp_area
->free
+= mem_chunk
->atom_size
;
260 if (temp_area
->free
== mem_chunk
->area_size
)
262 if (temp_area
== mem_chunk
->mem_area
)
263 mem_chunk
->mem_area
= NULL
;
265 if (mem_chunk
->free_mem_area
)
267 mem_chunk
->num_mem_areas
-= 1;
270 temp_area
->next
->prev
= temp_area
->prev
;
272 temp_area
->prev
->next
= temp_area
->next
;
273 if (temp_area
== mem_chunk
->mem_areas
)
274 mem_chunk
->mem_areas
= mem_chunk
->mem_areas
->next
;
276 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
277 g_tree_remove (mem_chunk
->mem_tree
, temp_area
);
281 mem_chunk
->free_mem_area
= temp_area
;
283 mem_chunk
->num_marked_areas
-= 1;
288 /* Update the number of allocated atoms count.
290 temp_area
->allocated
+= 1;
292 /* The area wasn't marked...return the memory
298 /* If there isn't a current mem area or the current mem area is out of space
299 * then allocate a new mem area. We'll first check and see if we can use
300 * the "free_mem_area". Otherwise we'll just malloc the mem area.
302 if ((!mem_chunk
->mem_area
) ||
303 ((mem_chunk
->mem_area
->index
+ mem_chunk
->atom_size
) > mem_chunk
->area_size
))
305 if (mem_chunk
->free_mem_area
)
307 mem_chunk
->mem_area
= mem_chunk
->free_mem_area
;
308 mem_chunk
->free_mem_area
= NULL
;
312 #ifdef ENABLE_GC_FRIENDLY
313 mem_chunk
->mem_area
= (GMemArea
*) g_malloc0 (sizeof (GMemArea
) -
315 mem_chunk
->area_size
);
316 #else /* !ENABLE_GC_FRIENDLY */
317 mem_chunk
->mem_area
= (GMemArea
*) g_malloc (sizeof (GMemArea
) -
319 mem_chunk
->area_size
);
320 #endif /* ENABLE_GC_FRIENDLY */
322 mem_chunk
->num_mem_areas
+= 1;
323 mem_chunk
->mem_area
->next
= mem_chunk
->mem_areas
;
324 mem_chunk
->mem_area
->prev
= NULL
;
326 if (mem_chunk
->mem_areas
)
327 mem_chunk
->mem_areas
->prev
= mem_chunk
->mem_area
;
328 mem_chunk
->mem_areas
= mem_chunk
->mem_area
;
330 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
331 g_tree_insert (mem_chunk
->mem_tree
, mem_chunk
->mem_area
, mem_chunk
->mem_area
);
334 mem_chunk
->mem_area
->index
= 0;
335 mem_chunk
->mem_area
->free
= mem_chunk
->area_size
;
336 mem_chunk
->mem_area
->allocated
= 0;
337 mem_chunk
->mem_area
->mark
= 0;
340 /* Get the memory and modify the state variables appropriately.
342 mem
= (gpointer
) &mem_chunk
->mem_area
->mem
[mem_chunk
->mem_area
->index
];
343 mem_chunk
->mem_area
->index
+= mem_chunk
->atom_size
;
344 mem_chunk
->mem_area
->free
-= mem_chunk
->atom_size
;
345 mem_chunk
->mem_area
->allocated
+= 1;
349 LEAVE_MEM_CHUNK_ROUTINE ();
355 old_mem_chunk_alloc0 (GMemChunk
*mem_chunk
)
359 mem
= old_mem_chunk_alloc (mem_chunk
);
362 memset (mem
, 0, mem_chunk
->atom_size
);
369 old_mem_chunk_free (GMemChunk
*mem_chunk
,
373 GFreeAtom
*free_atom
;
375 g_return_if_fail (mem_chunk
!= NULL
);
376 g_return_if_fail (mem
!= NULL
);
378 ENTER_MEM_CHUNK_ROUTINE ();
380 #ifdef ENABLE_GC_FRIENDLY
381 memset (mem
, 0, mem_chunk
->atom_size
);
382 #endif /* ENABLE_GC_FRIENDLY */
384 /* Don't do anything if this is an ALLOC_ONLY chunk
386 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
388 /* Place the memory on the "free_atoms" list
390 free_atom
= (GFreeAtom
*) mem
;
391 free_atom
->next
= mem_chunk
->free_atoms
;
392 mem_chunk
->free_atoms
= free_atom
;
394 temp_area
= g_tree_search (mem_chunk
->mem_tree
,
395 (GCompareFunc
) old_mem_chunk_area_search
,
398 temp_area
->allocated
-= 1;
400 if (temp_area
->allocated
== 0)
403 mem_chunk
->num_marked_areas
+= 1;
407 LEAVE_MEM_CHUNK_ROUTINE ();
410 /* This doesn't free the free_area if there is one */
412 old_mem_chunk_clean (GMemChunk
*mem_chunk
)
415 GFreeAtom
*prev_free_atom
;
416 GFreeAtom
*temp_free_atom
;
419 g_return_if_fail (mem_chunk
!= NULL
);
421 ENTER_MEM_CHUNK_ROUTINE ();
423 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
425 prev_free_atom
= NULL
;
426 temp_free_atom
= mem_chunk
->free_atoms
;
428 while (temp_free_atom
)
430 mem
= (gpointer
) temp_free_atom
;
432 mem_area
= g_tree_search (mem_chunk
->mem_tree
,
433 (GCompareFunc
) old_mem_chunk_area_search
,
436 /* If this mem area is marked for destruction then delete the
437 * area and list node and decrement the free mem.
442 prev_free_atom
->next
= temp_free_atom
->next
;
444 mem_chunk
->free_atoms
= temp_free_atom
->next
;
445 temp_free_atom
= temp_free_atom
->next
;
447 mem_area
->free
+= mem_chunk
->atom_size
;
448 if (mem_area
->free
== mem_chunk
->area_size
)
450 mem_chunk
->num_mem_areas
-= 1;
451 mem_chunk
->num_marked_areas
-= 1;
454 mem_area
->next
->prev
= mem_area
->prev
;
456 mem_area
->prev
->next
= mem_area
->next
;
457 if (mem_area
== mem_chunk
->mem_areas
)
458 mem_chunk
->mem_areas
= mem_chunk
->mem_areas
->next
;
459 if (mem_area
== mem_chunk
->mem_area
)
460 mem_chunk
->mem_area
= NULL
;
462 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
463 g_tree_remove (mem_chunk
->mem_tree
, mem_area
);
469 prev_free_atom
= temp_free_atom
;
470 temp_free_atom
= temp_free_atom
->next
;
474 LEAVE_MEM_CHUNK_ROUTINE ();
478 old_mem_chunk_reset (GMemChunk
*mem_chunk
)
483 g_return_if_fail (mem_chunk
!= NULL
);
485 ENTER_MEM_CHUNK_ROUTINE ();
487 mem_areas
= mem_chunk
->mem_areas
;
488 mem_chunk
->num_mem_areas
= 0;
489 mem_chunk
->mem_areas
= NULL
;
490 mem_chunk
->mem_area
= NULL
;
494 temp_area
= mem_areas
;
495 mem_areas
= mem_areas
->next
;
499 mem_chunk
->free_atoms
= NULL
;
501 if (mem_chunk
->mem_tree
)
503 g_tree_destroy (mem_chunk
->mem_tree
);
504 mem_chunk
->mem_tree
= g_tree_new ((GCompareFunc
) old_mem_chunk_area_compare
);
507 LEAVE_MEM_CHUNK_ROUTINE ();
511 old_mem_chunk_print (GMemChunk
*mem_chunk
)
516 g_return_if_fail (mem_chunk
!= NULL
);
518 mem_areas
= mem_chunk
->mem_areas
;
523 mem
+= mem_chunk
->area_size
- mem_areas
->free
;
524 mem_areas
= mem_areas
->next
;
527 g_log (G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
,
528 "%s: %ld bytes using %d mem areas",
529 mem_chunk
->name
, mem
, mem_chunk
->num_mem_areas
);
533 old_mem_chunk_info (void)
535 GMemChunk
*mem_chunk
;
539 g_mutex_lock (&mem_chunks_lock
);
540 mem_chunk
= mem_chunks
;
544 mem_chunk
= mem_chunk
->next
;
546 g_mutex_unlock (&mem_chunks_lock
);
548 g_log (G_LOG_DOMAIN
, G_LOG_LEVEL_INFO
, "%d mem chunks", count
);
550 g_mutex_lock (&mem_chunks_lock
);
551 mem_chunk
= mem_chunks
;
552 g_mutex_unlock (&mem_chunks_lock
);
556 old_mem_chunk_print ((GMemChunk
*) mem_chunk
);
557 mem_chunk
= mem_chunk
->next
;
562 old_mem_chunk_compute_size (gulong size
,
569 while (power_of_2
< size
)
572 lower
= power_of_2
>> 1;
575 if (size
- lower
< upper
- size
&& lower
>= min_size
)
582 old_mem_chunk_area_compare (GMemArea
*a
,
587 else if (a
->mem
< b
->mem
)
593 old_mem_chunk_area_search (GMemArea
*a
,
598 if (addr
< &a
->mem
[a
->index
])