1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is Netscape
|
---|
18 | * Communications Corporation. Portions created by Netscape are
|
---|
19 | * Copyright (C) 1998-2000 Netscape Communications Corporation. All
|
---|
20 | * Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK *****
|
---|
37 | */
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Lifetime-based fast allocation, inspired by much prior art, including
|
---|
41 | * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
|
---|
42 | * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
|
---|
43 | */
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include "plarena.h"
|
---|
47 | #include "prmem.h"
|
---|
48 | #include "prbit.h"
|
---|
49 | #include "prlog.h"
|
---|
50 | #include "prlock.h"
|
---|
51 | #include "prinit.h"
|
---|
52 |
|
---|
53 | static PLArena *arena_freelist;
|
---|
54 |
|
---|
55 | #define COUNT(pool,what)
|
---|
56 | #define PL_ARENA_DEFAULT_ALIGN sizeof(double)
|
---|
57 |
|
---|
58 | static PRLock *arenaLock;
|
---|
59 | static PRCallOnceType once;
|
---|
60 |
|
---|
61 | /*
|
---|
62 | ** InitializeArenas() -- Initialize arena operations.
|
---|
63 | **
|
---|
64 | ** InitializeArenas() is called exactly once and only once from
|
---|
65 | ** LockArena(). This function creates the arena protection
|
---|
66 | ** lock: arenaLock.
|
---|
67 | **
|
---|
68 | ** Note: If the arenaLock cannot be created, InitializeArenas()
|
---|
69 | ** fails quietly, returning only PR_FAILURE. This percolates up
|
---|
70 | ** to the application using the Arena API. He gets no arena
|
---|
71 | ** from PL_ArenaAllocate(). It's up to him to fail gracefully
|
---|
72 | ** or recover.
|
---|
73 | **
|
---|
74 | */
|
---|
75 | static PRStatus InitializeArenas( void )
|
---|
76 | {
|
---|
77 | PR_ASSERT( arenaLock == NULL );
|
---|
78 | arenaLock = PR_NewLock();
|
---|
79 | if ( arenaLock == NULL )
|
---|
80 | return PR_FAILURE;
|
---|
81 | else
|
---|
82 | return PR_SUCCESS;
|
---|
83 | } /* end ArenaInitialize() */
|
---|
84 |
|
---|
85 | static PRStatus LockArena( void )
|
---|
86 | {
|
---|
87 | PRStatus rc = PR_CallOnce( &once, InitializeArenas );
|
---|
88 |
|
---|
89 | if ( PR_FAILURE != rc )
|
---|
90 | PR_Lock( arenaLock );
|
---|
91 | return(rc);
|
---|
92 | } /* end LockArena() */
|
---|
93 |
|
---|
94 | static void UnlockArena( void )
|
---|
95 | {
|
---|
96 | PR_Unlock( arenaLock );
|
---|
97 | return;
|
---|
98 | } /* end UnlockArena() */
|
---|
99 |
|
---|
100 | PR_IMPLEMENT(void) PL_InitArenaPool(
|
---|
101 | PLArenaPool *pool, const char *name, PRUint32 size, PRUint32 align)
|
---|
102 | {
|
---|
103 | if (align == 0)
|
---|
104 | align = PL_ARENA_DEFAULT_ALIGN;
|
---|
105 | pool->mask = PR_BITMASK(PR_CeilingLog2(align));
|
---|
106 | pool->first.next = NULL;
|
---|
107 | /* Set all three addresses in pool->first to the same dummy value.
|
---|
108 | * These addresses are only compared with each other, but never
|
---|
109 | * dereferenced. */
|
---|
110 | pool->first.base = pool->first.avail = pool->first.limit =
|
---|
111 | (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1);
|
---|
112 | pool->current = &pool->first;
|
---|
113 | pool->arenasize = size;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /*
|
---|
118 | ** PL_ArenaAllocate() -- allocate space from an arena pool
|
---|
119 | **
|
---|
120 | ** Description: PL_ArenaAllocate() allocates space from an arena
|
---|
121 | ** pool.
|
---|
122 | **
|
---|
123 | ** First, try to satisfy the request from arenas starting at
|
---|
124 | ** pool->current.
|
---|
125 | **
|
---|
126 | ** If there is not enough space in the arena pool->current, try
|
---|
127 | ** to claim an arena, on a first fit basis, from the global
|
---|
128 | ** freelist (arena_freelist).
|
---|
129 | **
|
---|
130 | ** If no arena in arena_freelist is suitable, then try to
|
---|
131 | ** allocate a new arena from the heap.
|
---|
132 | **
|
---|
133 | ** Returns: pointer to allocated space or NULL
|
---|
134 | **
|
---|
135 | ** Notes: The original implementation had some difficult to
|
---|
136 | ** solve bugs; the code was difficult to read. Sometimes it's
|
---|
137 | ** just easier to rewrite it. I did that. larryh.
|
---|
138 | **
|
---|
139 | ** See also: bugzilla: 45343.
|
---|
140 | **
|
---|
141 | */
|
---|
142 |
|
---|
143 | PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb)
|
---|
144 | {
|
---|
145 | PLArena *a;
|
---|
146 | char *rp; /* returned pointer */
|
---|
147 | PRUint32 nbOld;
|
---|
148 |
|
---|
149 | PR_ASSERT((nb & pool->mask) == 0);
|
---|
150 |
|
---|
151 | nbOld = nb;
|
---|
152 | nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */
|
---|
153 | if (nb < nbOld)
|
---|
154 | return NULL;
|
---|
155 |
|
---|
156 | /* attempt to allocate from arenas at pool->current */
|
---|
157 | {
|
---|
158 | a = pool->current;
|
---|
159 | do {
|
---|
160 | if ( a->avail +nb <= a->limit ) {
|
---|
161 | pool->current = a;
|
---|
162 | rp = (char *)a->avail;
|
---|
163 | a->avail += nb;
|
---|
164 | return rp;
|
---|
165 | }
|
---|
166 | } while( NULL != (a = a->next) );
|
---|
167 | }
|
---|
168 |
|
---|
169 | /* attempt to allocate from arena_freelist */
|
---|
170 | {
|
---|
171 | PLArena *p; /* previous pointer, for unlinking from freelist */
|
---|
172 |
|
---|
173 | /* lock the arena_freelist. Make access to the freelist MT-Safe */
|
---|
174 | if ( PR_FAILURE == LockArena())
|
---|
175 | return(0);
|
---|
176 |
|
---|
177 | for ( a = p = arena_freelist; a != NULL ; p = a, a = a->next ) {
|
---|
178 | if ( a->base +nb <= a->limit ) {
|
---|
179 | if ( p == arena_freelist )
|
---|
180 | arena_freelist = a->next;
|
---|
181 | else
|
---|
182 | p->next = a->next;
|
---|
183 | UnlockArena();
|
---|
184 | a->avail = a->base;
|
---|
185 | rp = (char *)a->avail;
|
---|
186 | a->avail += nb;
|
---|
187 | /* the newly allocated arena is linked after pool->current
|
---|
188 | * and becomes pool->current */
|
---|
189 | a->next = pool->current->next;
|
---|
190 | pool->current->next = a;
|
---|
191 | pool->current = a;
|
---|
192 | if ( NULL == pool->first.next )
|
---|
193 | pool->first.next = a;
|
---|
194 | return(rp);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | UnlockArena();
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* attempt to allocate from the heap */
|
---|
201 | {
|
---|
202 | PRUint32 sz = PR_MAX(pool->arenasize, nb);
|
---|
203 | sz += sizeof *a + pool->mask; /* header and alignment slop */
|
---|
204 | a = (PLArena*)PR_MALLOC(sz);
|
---|
205 | if ( NULL != a ) {
|
---|
206 | a->limit = (PRUword)a + sz;
|
---|
207 | a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1);
|
---|
208 | rp = (char *)a->avail;
|
---|
209 | a->avail += nb;
|
---|
210 | PR_ASSERT(a->avail <= a->limit);
|
---|
211 | /* the newly allocated arena is linked after pool->current
|
---|
212 | * and becomes pool->current */
|
---|
213 | a->next = pool->current->next;
|
---|
214 | pool->current->next = a;
|
---|
215 | pool->current = a;
|
---|
216 | if ( NULL == pool->first.next )
|
---|
217 | pool->first.next = a;
|
---|
218 | PL_COUNT_ARENA(pool,++);
|
---|
219 | COUNT(pool, nmallocs);
|
---|
220 | return(rp);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | /* we got to here, and there's no memory to allocate */
|
---|
225 | return(NULL);
|
---|
226 | } /* --- end PL_ArenaAllocate() --- */
|
---|
227 |
|
---|
228 | PR_IMPLEMENT(void *) PL_ArenaGrow(
|
---|
229 | PLArenaPool *pool, void *p, PRUint32 size, PRUint32 incr)
|
---|
230 | {
|
---|
231 | void *newp;
|
---|
232 |
|
---|
233 | if (PR_UINT32_MAX - size < incr)
|
---|
234 | return NULL;
|
---|
235 | PL_ARENA_ALLOCATE(newp, pool, size + incr);
|
---|
236 | if (newp)
|
---|
237 | memcpy(newp, p, size);
|
---|
238 | return newp;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * Free tail arenas linked after head, which may not be the true list head.
|
---|
243 | * Reset pool->current to point to head in case it pointed at a tail arena.
|
---|
244 | */
|
---|
245 | static void FreeArenaList(PLArenaPool *pool, PLArena *head, PRBool reallyFree)
|
---|
246 | {
|
---|
247 | PLArena **ap, *a;
|
---|
248 |
|
---|
249 | ap = &head->next;
|
---|
250 | a = *ap;
|
---|
251 | if (!a)
|
---|
252 | return;
|
---|
253 |
|
---|
254 | #ifdef DEBUG
|
---|
255 | do {
|
---|
256 | PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
|
---|
257 | a->avail = a->base;
|
---|
258 | PL_CLEAR_UNUSED(a);
|
---|
259 | } while ((a = a->next) != 0);
|
---|
260 | a = *ap;
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | if (reallyFree) {
|
---|
264 | do {
|
---|
265 | *ap = a->next;
|
---|
266 | PL_CLEAR_ARENA(a);
|
---|
267 | PL_COUNT_ARENA(pool,--);
|
---|
268 | PR_DELETE(a);
|
---|
269 | } while ((a = *ap) != 0);
|
---|
270 | } else {
|
---|
271 | /* Insert the whole arena chain at the front of the freelist. */
|
---|
272 | do {
|
---|
273 | ap = &(*ap)->next;
|
---|
274 | } while (*ap);
|
---|
275 | LockArena();
|
---|
276 | *ap = arena_freelist;
|
---|
277 | arena_freelist = a;
|
---|
278 | head->next = 0;
|
---|
279 | UnlockArena();
|
---|
280 | }
|
---|
281 |
|
---|
282 | pool->current = head;
|
---|
283 | }
|
---|
284 |
|
---|
285 | PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool *pool, char *mark)
|
---|
286 | {
|
---|
287 | PLArena *a;
|
---|
288 |
|
---|
289 | for (a = pool->first.next; a; a = a->next) {
|
---|
290 | if (PR_UPTRDIFF(mark, a->base) < PR_UPTRDIFF(a->avail, a->base)) {
|
---|
291 | a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark);
|
---|
292 | FreeArenaList(pool, a, PR_FALSE);
|
---|
293 | return;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool *pool)
|
---|
299 | {
|
---|
300 | FreeArenaList(pool, &pool->first, PR_FALSE);
|
---|
301 | COUNT(pool, ndeallocs);
|
---|
302 | }
|
---|
303 |
|
---|
304 | PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool *pool)
|
---|
305 | {
|
---|
306 | FreeArenaList(pool, &pool->first, PR_TRUE);
|
---|
307 | }
|
---|
308 |
|
---|
309 | PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool *ap)
|
---|
310 | {
|
---|
311 | }
|
---|
312 |
|
---|
313 | PR_IMPLEMENT(void) PL_ArenaFinish(void)
|
---|
314 | {
|
---|
315 | PLArena *a, *next;
|
---|
316 |
|
---|
317 | for (a = arena_freelist; a; a = next) {
|
---|
318 | next = a->next;
|
---|
319 | PR_DELETE(a);
|
---|
320 | }
|
---|
321 | arena_freelist = NULL;
|
---|
322 |
|
---|
323 | if (arenaLock) {
|
---|
324 | PR_DestroyLock(arenaLock);
|
---|
325 | arenaLock = NULL;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|