File: mpl.c

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (340 lines) | stat: -rw-r--r-- 6,728 bytes parent folder | download | duplicates (2)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 *  mpl.c
 *
 *  $Id: mpl.c,v 1.1.1.1 2006/04/11 17:56:16 source Exp $
 *
 *  Mempory Pool Primitives
 *  Derived from obstack
 *  
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *  
 *  Copyright (C) 1998-2006 OpenLink Software
 *  
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *  
 *  This program 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
 *  General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *  
 *  
*/

#include "libutil.h"
#include "mpl.h"

#define MPL_CHUNK_SIZE	4096
#define MPL_ALIGNMENT	16

#define MPL_RNDUP(SZ,AL)	((((SZ) + AL - 1) / AL) * AL)
#define MPL_ALIGN(X)		MPL_RNDUP (X, MPL_ALIGNMENT)
#define MPL_ALIGNPTR(PTR)	((memptr_t) MPL_ALIGN ((memsz_t) (PTR)))
#define MPL_MCBASE(MC)		MPL_ALIGNPTR (((memptr_t) MC) + sizeof (MPC))

#ifdef MEMORY_DEBUG
# define MPL_DEBUG
#endif


memptr_t
getcore (memsz_t size)
{
  memptr_t mem;

  mem = (memptr_t) calloc (1, size);

#ifndef NO_LOG
  if (mem == (memptr_t) 0)
    {
# ifdef MPL_DEBUG
      fprintf (stderr, "get_core: out of memory\n");
      abort ();
# endif
      log (L_ERR, "out of memory");
      terminate (1);
    }
#endif

#ifdef MPL_DEBUG
  /* init to garbage, to trap uninit use */
  memset (mem, 0xFA, size);
#endif

  return mem;
}


void
freecore (memptr_t mem)
{
  free (mem);
}


void
mpl_newchunk (MPL * mp, memsz_t length)
{
  register memsz_t obj_size;
  MPC *mc;
  memsz_t new_size;
  memptr_t new_base;

  /* Old data */
  obj_size = mp->mp_next - mp->mp_base;

  /* Compute size for new chunk.  */
  new_size = (obj_size + length) + (obj_size >> 3) + 100;
  new_size = MPL_RNDUP (new_size, MPL_CHUNK_SIZE);

  /* Allocate new block and copy old data */
  mc = (MPC *) getcore (new_size);
  new_base = MPL_MCBASE (mc);
  memcpy (new_base, mp->mp_base, obj_size);

  /* If old chunk has no other data, free that */
  if (mp->mp_base == MPL_MCBASE (mp->mp_chunk))
    {
      mc->mc_prev = mp->mp_chunk->mc_prev;
      freecore ((memptr_t) mp->mp_chunk);
    }
  else
    mc->mc_prev = mp->mp_chunk;

  mp->mp_limit = mc->mc_limit = (memptr_t) mc + new_size;
  mp->mp_chunk = mc;
  mp->mp_base = new_base;
  mp->mp_next = new_base + obj_size;
}


void
mpl_init (MPL * mp)
{
  memset (mp, 0, sizeof (MPL));
}


void
mpl_destroy (MPL * mp)
{
  MPC *prev;
  MPC *p;

  for (p = mp->mp_chunk; p; p = prev)
    {
      prev = p->mc_prev;
      freecore ((memptr_t) p);
    }
  memset (mp, 0, sizeof (MPL));
  mpl_init (mp);
}


void
mpl_free (MPL * mp, memptr_t ptr)
{
  MPC *p;
  MPC *prev;

  if (ptr)
    {
      for (p = mp->mp_chunk; p; p = prev)
	{
	  if (MPL_MCBASE (p) <= ptr && ptr < p->mc_limit)
	    {
	      mp->mp_base = mp->mp_next = ptr;
	      mp->mp_chunk = p;
	      mp->mp_limit = mp->mp_chunk->mc_limit;
	      return;
	    }
	  prev = p->mc_prev;
	  freecore ((memptr_t) p);
	}

#ifdef MPL_DEBUG
      fprintf (stderr, "mpl_free: bad address\n");
#endif
      mpl_init (mp);
    }
  else
    mp->mp_next = mp->mp_base;
}


memsz_t
mpl_object_size (MPL * mp)
{
  return mp->mp_next - mp->mp_base;
}


/*
 *  Reserve memory in the pool.
 *  Can be used for structures, since data is aligned.
 *  Assumes mpl is aligned on input.
 */
memptr_t
mpl_alloc (MPL * mp, memsz_t size)
{
  memptr_t base = mp->mp_next;
  if (base + size >= mp->mp_limit)
    {
      mpl_newchunk (mp, size);
      base = mp->mp_next;
    }
  mp->mp_next = MPL_ALIGNPTR (base + size);
  return base;
}


void
mpl_align (MPL * mp)
{
  mp->mp_next = MPL_ALIGNPTR (mp->mp_next);
}


/*
 *  Finished memory growth on current chunk and returns a pointer
 *  to the first byte.
 *  mpl is aligned on output.
 */
memptr_t
mpl_finish (MPL * mp)
{
  memptr_t base = mp->mp_base;
  mp->mp_base = mp->mp_next = MPL_ALIGNPTR (mp->mp_next);
  return base;
}


memptr_t
mpl_finish2 (MPL * mp, memsz_t *size)
{
  memptr_t base = mp->mp_base;
  *size = mp->mp_next - base;
  mp->mp_base = mp->mp_next = MPL_ALIGNPTR (mp->mp_next);
  return base;
}


memptr_t
mpl_getmem (MPL * mp, memsz_t size)
{
  mpl_alloc (mp, size);
  return mpl_finish (mp);
}


/*
 *  Copy data to the pool.
 *  Do not use with structures, because the memory is not necessarily aligned
 *
 *  If mpl_alloc or mpl_getmem is required after an mpl_grow (mpl_1grow),
 *  call mpl_align or mpl_finish first.
 *
 *  Note: the returned pointer is only valid until the pool is reallocated.
 *        It's only intended use is an immediate memcpy.
 */
memptr_t
mpl_grow (MPL * mp, memptr_t addr, memsz_t len)
{
  memptr_t base;

  if (mp->mp_next + len >= mp->mp_limit)
    mpl_newchunk (mp, len);
  base = mp->mp_next;
  memcpy (base, addr, len);
  mp->mp_next += len;

  return base;
}


#ifdef MPL_DEBUG
#include <assert.h>

void
mpl_dump (MPL * mp, char *where)
{
  memsz_t total;
  MPC *p;
  int i = 0;

  puts (where);
  total = 0;
  for (p = mp->mp_chunk; p; p = p->mc_prev)
    {
      memsz_t size = p->mc_limit - MPL_MCBASE (p);
      total += size;
      assert (size < MPL_CHUNK_SIZE);
      i++;
    }

  printf ("Stored %lu bytes in %d chunks\n", (unsigned long) total, i);
  printf ("Current mp: base=%p, next=%p limit=%p chunk=%p\n",
      mp->mp_base, mp->mp_next, mp->mp_limit, mp->mp_chunk);
  printf ("            stored=%ld remaining=%ld size=%ld\n",
      (long) (mp->mp_next - mp->mp_base),
      (long) (mp->mp_limit - mp->mp_next),
      (long) (mp->mp_limit - mp->mp_base));
}
#endif


#ifdef MPL_TESTCODE
char *pp[2000];

int
main ()
{
  MPL xx;
  int i, j, k;
  char *cp;

  mpl_init (&xx);

  for (k = 0; k < 100; k++)
    {
      for (j = 0; j < 200; j++)
	{
	  for (i = 0; i < 100; i++)
	    mpl_grow (&xx, "ABCDEFGHIJ", 10);
	  mpl_1grow (&xx, 0);
	  pp[j] = cp = mpl_finish (&xx);
	}
      fflush (stdout);
      mpl_free (&xx, pp[k % 10]);
    }
  mpl_dump (&xx, "test1");
  mpl_free (&xx, pp[0]);
  for (j = 0; j < 2000; j++)
    {
      pp[j] = cp = mpl_getmem (&xx, 200);
      for (k = 0; k < 200; k++)
	cp[k] = j & 0x7f;
    }
  for (j = 0; j < 2000; j++)
    {
      cp = pp[j];
      for (k = 0; k < 200; k++)
	assert (cp[k] == (j & 0x7f));
    }
  mpl_dump (&xx, "test2");

  mpl_destroy (&xx);

  mpl_1grow (&xx, 0);
  mpl_align (&xx);
  mpl_dump (&xx, "test3");

  return 0;
}
#endif