VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/lib/ds/plarena.c@ 108413

Last change on this file since 108413 was 101946, checked in by vboxsync, 19 months ago

libs/xpcom: Convert plarena.c to use IPRT, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
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 "prbit.h"
48
49#include <iprt/assert.h>
50#include <iprt/errcore.h>
51#include <iprt/mem.h>
52#include <iprt/once.h>
53#include <iprt/semaphore.h>
54
55static PLArena *arena_freelist;
56
57#define PL_ARENA_DEFAULT_ALIGN sizeof(double)
58
59static RTSEMFASTMUTEX g_hMtxArena = NIL_RTSEMFASTMUTEX;
60static RTONCE g_rtOnce = RTONCE_INITIALIZER;
61
62/*
63** InitializeArenas() -- Initialize arena operations.
64**
65** InitializeArenas() is called exactly once and only once from
66** LockArena(). This function creates the arena protection
67** lock: arenaLock.
68**
69** Note: If the arenaLock cannot be created, InitializeArenas()
70** fails quietly, returning only PR_FAILURE. This percolates up
71** to the application using the Arena API. He gets no arena
72** from PL_ArenaAllocate(). It's up to him to fail gracefully
73** or recover.
74**
75*/
76static DECLCALLBACK(int) InitializeArenas(void *pvUser)
77{
78 RT_NOREF(pvUser);
79 Assert(g_hMtxArena == NIL_RTSEMFASTMUTEX);
80
81 return RTSemFastMutexCreate(&g_hMtxArena);
82} /* end ArenaInitialize() */
83
84static PRStatus LockArena( void )
85{
86 int vrc = RTOnce(&g_rtOnce, InitializeArenas, NULL /*pvUser*/);
87 if (RT_FAILURE(vrc))
88 return PR_FAILURE;
89
90 RTSemFastMutexRequest(g_hMtxArena);
91 return PR_SUCCESS;
92} /* end LockArena() */
93
94static void UnlockArena( void )
95{
96 RTSemFastMutexRelease(g_hMtxArena);
97 return;
98} /* end UnlockArena() */
99
100PR_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
143PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb)
144{
145 PLArena *a;
146 char *rp; /* returned pointer */
147 PRUint32 nbOld;
148
149 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*)RTMemAlloc(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 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 return(rp);
220 }
221 }
222
223 /* we got to here, and there's no memory to allocate */
224 return(NULL);
225} /* --- end PL_ArenaAllocate() --- */
226
227PR_IMPLEMENT(void *) PL_ArenaGrow(
228 PLArenaPool *pool, void *p, PRUint32 size, PRUint32 incr)
229{
230 void *newp;
231
232 if (PR_UINT32_MAX - size < incr)
233 return NULL;
234 PL_ARENA_ALLOCATE(newp, pool, size + incr);
235 if (newp)
236 memcpy(newp, p, size);
237 return newp;
238}
239
240/*
241 * Free tail arenas linked after head, which may not be the true list head.
242 * Reset pool->current to point to head in case it pointed at a tail arena.
243 */
244static void FreeArenaList(PLArenaPool *pool, PLArena *head, PRBool reallyFree)
245{
246 PLArena **ap, *a;
247
248 ap = &head->next;
249 a = *ap;
250 if (!a)
251 return;
252
253#ifdef DEBUG
254 do {
255 Assert(a->base <= a->avail && a->avail <= a->limit);
256 a->avail = a->base;
257 PL_CLEAR_UNUSED(a);
258 } while ((a = a->next) != 0);
259 a = *ap;
260#endif
261
262 if (reallyFree) {
263 do {
264 *ap = a->next;
265 PL_CLEAR_ARENA(a);
266 PL_COUNT_ARENA(pool,--);
267 RTMemFree(a);
268 } while ((a = *ap) != 0);
269 } else {
270 /* Insert the whole arena chain at the front of the freelist. */
271 do {
272 ap = &(*ap)->next;
273 } while (*ap);
274 LockArena();
275 *ap = arena_freelist;
276 arena_freelist = a;
277 head->next = 0;
278 UnlockArena();
279 }
280
281 pool->current = head;
282}
283
284PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool *pool, char *mark)
285{
286 PLArena *a;
287
288 for (a = pool->first.next; a; a = a->next) {
289 if (PR_UPTRDIFF(mark, a->base) < PR_UPTRDIFF(a->avail, a->base)) {
290 a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark);
291 FreeArenaList(pool, a, PR_FALSE);
292 return;
293 }
294 }
295}
296
297PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool *pool)
298{
299 FreeArenaList(pool, &pool->first, PR_FALSE);
300}
301
302PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool *pool)
303{
304 FreeArenaList(pool, &pool->first, PR_TRUE);
305}
306
307PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool *ap)
308{
309}
310
311PR_IMPLEMENT(void) PL_ArenaFinish(void)
312{
313 PLArena *a, *next;
314
315 for (a = arena_freelist; a; a = next) {
316 next = a->next;
317 RTMemFree(a);
318 }
319 arena_freelist = NULL;
320
321 if (g_hMtxArena != NIL_RTSEMFASTMUTEX) {
322 RTSemFastMutexDestroy(g_hMtxArena);
323 g_hMtxArena = NIL_RTSEMFASTMUTEX;
324 }
325}
326
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette