summaryrefslogtreecommitdiff
path: root/camel/camel-mempool.c
blob: bc2fade38cacc00026aa6a1d5a57cf09cb5e2dcc (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "camel-mempool.h"

#include <string.h>

typedef struct _MemPoolNode {
	struct _MemPoolNode *next;

	gint free;
} MemPoolNode;

typedef struct _MemPoolThresholdNode {
	struct _MemPoolThresholdNode *next;
} MemPoolThresholdNode;

#define ALIGNED_SIZEOF(t)	((sizeof (t) + G_MEM_ALIGN - 1) & -G_MEM_ALIGN)

struct _CamelMemPool {
	gint blocksize;
	gint threshold;
	guint align;
	struct _MemPoolNode *blocks;
	struct _MemPoolThresholdNode *threshold_blocks;
};

/**
 * camel_mempool_new:
 * @blocksize: The base blocksize to use for all system alocations.
 * @threshold: If the allocation exceeds the threshold, then it is
 * allocated separately and stored in a separate list.
 * @flags: Alignment options: CAMEL_MEMPOOL_ALIGN_STRUCT uses native
 * struct alignment, CAMEL_MEMPOOL_ALIGN_WORD aligns to 16 bits (2 bytes),
 * and CAMEL_MEMPOOL_ALIGN_BYTE aligns to the nearest byte.  The default
 * is to align to native structures.
 *
 * Create a new mempool header.  Mempools can be used to efficiently
 * allocate data which can then be freed as a whole.
 *
 * Mempools can also be used to efficiently allocate arbitrarily
 * aligned data (such as strings) without incurring the space overhead
 * of aligning each allocation (which is not required for strings).
 *
 * However, each allocation cannot be freed individually, only all
 * or nothing.
 *
 * Returns:
 *
 * Since: 2.32
 **/
CamelMemPool *
camel_mempool_new (gint blocksize,
                   gint threshold,
                   CamelMemPoolFlags flags)
{
	CamelMemPool *pool;

	pool = g_slice_new0 (CamelMemPool);
	if (threshold >= blocksize)
		threshold = blocksize * 2 / 3;
	pool->blocksize = blocksize;
	pool->threshold = threshold;
	pool->blocks = NULL;
	pool->threshold_blocks = NULL;

	switch (flags & CAMEL_MEMPOOL_ALIGN_MASK) {
	case CAMEL_MEMPOOL_ALIGN_STRUCT:
	default:
		pool->align = G_MEM_ALIGN - 1;
		break;
	case CAMEL_MEMPOOL_ALIGN_WORD:
		pool->align = 2 - 1;
		break;
	case CAMEL_MEMPOOL_ALIGN_BYTE:
		pool->align = 1 - 1;
	}
	return pool;
}

/**
 * camel_mempool_alloc:
 * @pool: a #CamelMemPool
 * @size:
 *
 * Allocate a new data block in the mempool.  Size will
 * be rounded up to the mempool's alignment restrictions
 * before being used.
 *
 * Since: 2.32
 **/
gpointer
camel_mempool_alloc (CamelMemPool *pool,
                     register gint size)
{
	size = (size + pool->align) & (~(pool->align));
	if (size >= pool->threshold) {
		MemPoolThresholdNode *n;

		n = g_malloc (ALIGNED_SIZEOF (*n) + size);
		n->next = pool->threshold_blocks;
		pool->threshold_blocks = n;
		return (gchar *) n + ALIGNED_SIZEOF (*n);
	} else {
		register MemPoolNode *n;

		n = pool->blocks;
		if (n && n->free >= size) {
			n->free -= size;
			return (gchar *) n + ALIGNED_SIZEOF (*n) + n->free;
		}

		/* maybe we could do some sort of the free blocks based on size, but
		 * it doubt its worth it at all */

		n = g_malloc (ALIGNED_SIZEOF (*n) + pool->blocksize);
		n->next = pool->blocks;
		pool->blocks = n;
		n->free = pool->blocksize - size;
		return (gchar *) n + ALIGNED_SIZEOF (*n) + n->free;
	}
}

/**
 * camel_mempool_strdup:
 * @pool: a #CamelMemPool
 * @str:
 *
 * Since: 2.32
 **/
gchar *
camel_mempool_strdup (CamelMemPool *pool,
                      const gchar *str)
{
	gchar *out;
	gsize out_len;

	out_len = strlen (str) + 1;
	out = camel_mempool_alloc (pool, out_len);
	g_strlcpy (out, str, out_len);

	return out;
}

/**
 * camel_mempool_flush:
 * @pool: a #CamelMemPool
 * @freeall: free all system allocated blocks as well
 *
 * Flush used memory and mark allocated blocks as free.
 *
 * If @freeall is %TRUE, then all allocated blocks are free'd
 * as well.  Otherwise only blocks above the threshold are
 * actually freed, and the others are simply marked as empty.
 *
 * Since: 2.32
 **/
void
camel_mempool_flush (CamelMemPool *pool,
                     gint freeall)
{
	MemPoolThresholdNode *tn, *tw;
	MemPoolNode *pw, *pn;

	tw = pool->threshold_blocks;
	while (tw) {
		tn = tw->next;
		g_free (tw);
		tw = tn;
	}
	pool->threshold_blocks = NULL;

	if (freeall) {
		pw = pool->blocks;
		while (pw) {
			pn = pw->next;
			g_free (pw);
			pw = pn;
		}
		pool->blocks = NULL;
	} else {
		pw = pool->blocks;
		while (pw) {
			pw->free = pool->blocksize;
			pw = pw->next;
		}
	}
}

/**
 * camel_mempool_destroy:
 * @pool: a #CamelMemPool
 *
 * Free all memory associated with a mempool.
 *
 * Since: 2.32
 **/
void
camel_mempool_destroy (CamelMemPool *pool)
{
	if (pool) {
		camel_mempool_flush (pool, 1);
		g_slice_free (CamelMemPool, pool);
	}
}