summaryrefslogtreecommitdiff
path: root/src/gfreelist.h
blob: 16073d26e625efeb78c5cecdfd1541bea185e211 (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
/* 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.
 */

#ifndef __G_FREELIST_H__
#define __G_FREELIST_H__

#include <glib/gtypes.h>
#include <glib/gutils.h>

G_BEGIN_DECLS

typedef struct _GFreeList GFreeList;
typedef struct _GFreeListAtom GFreeListAtom;
typedef struct _GFreeListBlock GFreeListBlock;

struct _GFreeListAtom
{
  GFreeListAtom *next;    
};

struct _GFreeList
{
  /*< private >*/
  gboolean		allocated;
  gsize			atom_size;
  gint			n_prealloc;
  GFreeListAtom *	atoms;
  GFreeListBlock *	blocks;
};

#define G_FREE_LIST_NAME(name)						\
  g__ ## name ## __free_list

#define G_FREE_LIST_EXTERN(name)					\
  extern GFreeList G_FREE_LIST_NAME (name)

/* Defining free lists */
#define G_FREE_LIST_DEFINE(name, atom_size, n_prealloc)			\
  GFreeList G_FREE_LIST_NAME (name) =					\
  {									\
    FALSE,					/* allocated */		\
    MAX (atom_size, sizeof (GFreeListAtom)),	/* atom_size */		\
    n_prealloc,					/* n_prealloc */	\
    NULL,					/* atoms */		\
    NULL					/* blocks */		\
  }

#define G_FREE_LIST_DEFINE_STATIC(name, atom_size, n_prealloc)		\
  static G_FREE_LIST_DEFINE (name, atom_size, n_prealloc)

/* Macros to allocate and free atoms */
#define G_FREE_LIST_ALLOC(name)						\
  g_free_list_alloc (&G_FREE_LIST_NAME (name))

#define G_FREE_LIST_FREE(name, mem)					\
  g_free_list_free (&G_FREE_LIST_NAME (name), mem)


/* functions */
GFreeList *   g_free_list_new        (gsize      atom_size,
				      gint       n_prealloc);
void          g_free_list_destroy    (GFreeList *free_list);

G_INLINE_FUNC
gpointer      g_free_list_alloc      (GFreeList *list);

G_INLINE_FUNC
void          g_free_list_free       (GFreeList *list,
				      gpointer   mem);

/* internal functions */
gpointer g_free_list_alloc_internal (GFreeList *flist);
void     g_free_list_free_internal  (GFreeList *flist,
				     gpointer   mem);

/* inline functions */

#if defined (G_CAN_INLINE) || defined (__G_FREELIST_C__)

G_INLINE_FUNC gpointer
g_free_list_alloc (GFreeList *flist)
{
  GFreeListAtom *result = flist->atoms;

  if (G_LIKELY (result))
    flist->atoms = result->next;
  else
    result = g_free_list_alloc_internal (flist);

  return result;
}

G_INLINE_FUNC void
g_free_list_free (GFreeList *flist,
		  gpointer   mem)
{
  ((GFreeListAtom *)mem)->next = flist->atoms;
  flist->atoms = mem;
}

#endif /* G_CAN_INLINE || __G_FREELIST_C__ */

G_END_DECLS

#endif /* __G_FREELIST_H__ */