summaryrefslogtreecommitdiff
path: root/src/gfreelist.c
blob: e86b260475e0f131f73235fa0ca2592186d6b224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* GLIB - Library of useful routines for C programming
 *
 * Copyright (C) 2003 Soeren Sandmann (sandmann@daimi.au.dk)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#define G_IMPLEMENT_INLINES 1
#define __G_FREELIST_C__

#include "gfreelist.h"

#include <glib.h>

struct _GFreeListBlock
{
  GFreeListBlock *next;
  gint8 *data;
};
     
GFreeList *
g_free_list_new (gsize atom_size,
		 gint n_prealloc)
{
  GFreeList *list;

  g_return_val_if_fail (atom_size > 0, NULL);
  g_return_val_if_fail (n_prealloc > 0, NULL);

  list = g_new0 (GFreeList, 1);

  list->allocated = TRUE;
  list->atom_size = MAX (atom_size, sizeof (GFreeListAtom));
  list->n_prealloc = n_prealloc;
  list->atoms = NULL;
  list->blocks = NULL;
  
  return list;
}

static void
g_free_list_ensure_atoms (GFreeList *list)
{
  if (!list->atoms)
    {
      gint i;
#ifdef DISABLE_MEM_POOLS
      const int n_atoms = 1;
#else
      const int n_atoms = list->n_prealloc;
#endif
      
      GFreeListBlock *block = g_new (GFreeListBlock, 1);
      block->data = g_malloc (list->atom_size * n_atoms);
      block->next = list->blocks;
      list->blocks = block;

      for (i = n_atoms - 1; i >= 0; --i)
	{
	  GFreeListAtom *atom =
	    (GFreeListAtom *)(block->data + i * list->atom_size);

	  atom->next = list->atoms;
	  list->atoms = atom;
	}
    }
}

gpointer
g_free_list_alloc_internal (GFreeList *list)
{
  gpointer result;

  g_return_val_if_fail (list != NULL, NULL);
  
  if (!list->atoms)
    g_free_list_ensure_atoms (list);

  result = list->atoms;
  list->atoms = list->atoms->next;

  return result;
}

void
g_free_list_free_internal (GFreeList *list,
			   gpointer   mem)
{
  GFreeListAtom *atom;

  g_return_if_fail (list != NULL);

  if (!mem)
    return;
  
  atom = mem;
  
  atom->next = list->atoms;
  list->atoms = atom;
}

static void
g_free_list_free_all (GFreeList *list)
{
  GFreeListBlock *block;

  g_return_if_fail (list != NULL);

  block = list->blocks;
  while (block)
    {
      GFreeListBlock *next = block->next;

      g_free (block->data);
      g_free (block);
      
      block = next;
    }
  
  list->atoms = NULL;
  list->blocks = NULL;
}

void
g_free_list_destroy (GFreeList *list)
{
  g_return_if_fail (list != NULL);
  g_return_if_fail (list->allocated);

  g_free_list_free_all (list);
  g_free (list);
}