Imported Upstream version 0.19.5
[platform/upstream/fribidi.git] / lib / fribidi-mem.c
1 /* FriBidi
2  * fribidi-mem.c - memory manipulation routines
3  *
4  * $Id: fribidi-mem.c,v 1.8 2006-01-31 03:23:13 behdad Exp $
5  * $Author: behdad $
6  * $Date: 2006-01-31 03:23:13 $
7  * $Revision: 1.8 $
8  * $Source: /home/behdad/src/fdo/fribidi/togit/git/../fribidi/fribidi2/lib/fribidi-mem.c,v $
9  *
10  * Authors:
11  *   Behdad Esfahbod, 2001, 2002, 2004
12  *
13  * Copyright (C) 2004 Sharif FarsiWeb, Inc
14  * Copyright (C) 2001,2002 Behdad Esfahbod
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with this library, in a file named COPYING; if not, write to the
28  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29  * Boston, MA 02110-1301, USA
30  * 
31  * For licensing issues, contact <license@farsiweb.info>.
32  */
33
34 #include "common.h"
35
36 #include "mem.h"
37
38 #if FRIBIDI_USE_GLIB+0
39 #else
40 #if USE_SIMPLE_MALLOC+0
41 #else
42
43 struct _FriBidiMemChunk
44 {
45   int atom_size;
46   int area_size;
47   int empty_size;
48   void *chunk;
49 };
50
51 FriBidiMemChunk *
52 fribidi_mem_chunk_new (
53   /* input */
54   const char *name,
55   int atom_size,
56   unsigned long area_size,
57   int alloc_type
58 )
59 {
60   register FriBidiMemChunk *m;
61
62   fribidi_assert (area_size >= atom_size * 8);
63
64   m = (FriBidiMemChunk *) fribidi_malloc (sizeof (FriBidiMemChunk));
65   if LIKELY
66     (m)
67     {
68       m->atom_size = atom_size;
69       m->area_size = area_size;
70       m->empty_size = 0;
71       m->chunk = NULL;
72     }
73
74   return m;
75 }
76
77 void *
78 fribidi_mem_chunk_alloc (
79   /* input */
80   FriBidiMemChunk *mem_chunk
81 )
82 {
83   fribidi_assert (mem_chunk);
84
85   if UNLIKELY
86     (mem_chunk->empty_size < mem_chunk->atom_size)
87     {
88       register void *chunk = fribidi_malloc (mem_chunk->area_size);
89       if LIKELY
90         (chunk)
91         {
92           if (mem_chunk->chunk)
93             *(void **) chunk =
94               (char *) mem_chunk->chunk + mem_chunk->empty_size -
95               mem_chunk->area_size;
96           chunk = (char *) chunk + mem_chunk->atom_size;
97           mem_chunk->chunk = chunk;
98           mem_chunk->empty_size = mem_chunk->area_size - mem_chunk->atom_size;
99         }
100       else
101         return NULL;
102     }
103
104   {
105     register void *m = mem_chunk->chunk;
106     mem_chunk->chunk = (char *) mem_chunk->chunk + mem_chunk->atom_size;
107     mem_chunk->empty_size -= mem_chunk->atom_size;
108
109     return m;
110   }
111 }
112
113 void
114 fribidi_mem_chunk_destroy (
115   /* input */
116   FriBidiMemChunk *mem_chunk
117 )
118 {
119   register void *chunk;
120
121   fribidi_assert (mem_chunk);
122
123   chunk =
124     (char *) mem_chunk->chunk + mem_chunk->empty_size - mem_chunk->area_size;
125   while LIKELY
126     (chunk)
127     {
128       register void *tofree = chunk;
129       chunk = *(void **) chunk;
130       fribidi_free (tofree);
131     }
132   fribidi_free (mem_chunk);
133 }
134
135 #endif /* !USE_SIMPLE_MALLOC */
136 #endif /* !FRIBIDI_USE_GLIB */
137
138 /* Editor directions:
139  * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent
140  */