VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/lib/ds/plarena.h@ 95259

Last change on this file since 95259 was 58734, checked in by vboxsync, 9 years ago

libs/xpcom: fix (just in case) for sloppy code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 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#ifndef plarena_h___
40#define plarena_h___
41/*
42 * Lifetime-based fast allocation, inspired by much prior art, including
43 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
44 * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
45 *
46 * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE).
47 */
48#include "prtypes.h"
49#include "plarenas.h"
50
51PR_BEGIN_EXTERN_C
52
53typedef struct PLArena PLArena;
54
55struct PLArena {
56 PLArena *next; /* next arena for this lifetime */
57 PRUword base; /* aligned base address, follows this header */
58 PRUword limit; /* one beyond last byte in arena */
59 PRUword avail; /* points to next available byte */
60};
61
62#ifdef PL_ARENAMETER
63typedef struct PLArenaStats PLArenaStats;
64
65struct PLArenaStats {
66 PLArenaStats *next; /* next in arenaStats list */
67 char *name; /* name for debugging */
68 PRUint32 narenas; /* number of arenas in pool */
69 PRUint32 nallocs; /* number of PL_ARENA_ALLOCATE() calls */
70 PRUint32 nreclaims; /* number of reclaims from freeArenas */
71 PRUint32 nmallocs; /* number of malloc() calls */
72 PRUint32 ndeallocs; /* number of lifetime deallocations */
73 PRUint32 ngrows; /* number of PL_ARENA_GROW() calls */
74 PRUint32 ninplace; /* number of in-place growths */
75 PRUint32 nreleases; /* number of PL_ARENA_RELEASE() calls */
76 PRUint32 nfastrels; /* number of "fast path" releases */
77 PRUint32 nbytes; /* total bytes allocated */
78 PRUint32 maxalloc; /* maximum allocation size in bytes */
79 PRFloat64 variance; /* size variance accumulator */
80};
81#endif
82
83struct PLArenaPool {
84 PLArena first; /* first arena in pool list */
85 PLArena *current; /* arena from which to allocate space */
86 PRUint32 arenasize; /* net exact size of a new arena */
87 PRUword mask; /* alignment mask (power-of-2 - 1) */
88#ifdef PL_ARENAMETER
89 PLArenaStats stats;
90#endif
91};
92
93/*
94 * If the including .c file uses only one power-of-2 alignment, it may define
95 * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
96 * per ALLOCATE and GROW.
97 */
98#ifdef PL_ARENA_CONST_ALIGN_MASK
99#define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \
100 & ~PL_ARENA_CONST_ALIGN_MASK)
101
102#define PL_INIT_ARENA_POOL(pool, name, size) \
103 PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1)
104#else
105#define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask)
106#endif
107
108#define PL_ARENA_ALLOCATE(p, pool, nb) \
109 PR_BEGIN_MACRO \
110 PLArena *_a = (pool)->current; \
111 PRUint32 _nb = PL_ARENA_ALIGN(pool, (PRUint32)nb); \
112 PRUword _p = _a->avail; \
113 if (_nb < (PRUint32)nb) { \
114 _p = 0; \
115 } else if (_nb > (_a->limit - _a->avail)) { \
116 _p = (PRUword)PL_ArenaAllocate(pool, _nb); \
117 } else { \
118 _a->avail += _nb; \
119 } \
120 p = (void *)_p; \
121 if (p) { \
122 PL_ArenaCountAllocation(pool, nb); \
123 } \
124 PR_END_MACRO
125
126#define PL_ARENA_GROW(p, pool, size, incr) \
127 PR_BEGIN_MACRO \
128 PLArena *_a = (pool)->current; \
129 PRUint32 _incr = PL_ARENA_ALIGN(pool, (PRUint32)incr); \
130 if (_incr < (PRUint32)incr) { \
131 p = NULL; \
132 } else if (_a->avail == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \
133 _incr <= (_a->limit - _a->avail)) { \
134 _a->avail = _incr; \
135 PL_ArenaCountInplaceGrowth(pool, size, (RTUint32)incr); \
136 } else { \
137 p = PL_ArenaGrow(pool, p, size, (PRUint32)incr); \
138 } \
139 if (p) { \
140 PL_ArenaCountGrowth(pool, size, (PRUint32)incr); \
141 } \
142 PR_END_MACRO
143
144#define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail)
145#define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q))
146
147#ifdef DEBUG
148#define PL_FREE_PATTERN 0xDA
149#define PL_CLEAR_UNUSED(a) (PR_ASSERT((a)->avail <= (a)->limit), \
150 memset((void*)(a)->avail, PL_FREE_PATTERN, \
151 (a)->limit - (a)->avail))
152#define PL_CLEAR_ARENA(a) memset((void*)(a), PL_FREE_PATTERN, \
153 (a)->limit - (PRUword)(a))
154#else
155#define PL_CLEAR_UNUSED(a)
156#define PL_CLEAR_ARENA(a)
157#endif
158
159#define PL_ARENA_RELEASE(pool, mark) \
160 PR_BEGIN_MACRO \
161 char *_m = (char *)(mark); \
162 PLArena *_a = (pool)->current; \
163 if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \
164 _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \
165 PL_CLEAR_UNUSED(_a); \
166 PL_ArenaCountRetract(pool, _m); \
167 } else { \
168 PL_ArenaRelease(pool, _m); \
169 } \
170 PL_ArenaCountRelease(pool, _m); \
171 PR_END_MACRO
172
173#ifdef PL_ARENAMETER
174#define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
175#else
176#define PL_COUNT_ARENA(pool,op)
177#endif
178
179#define PL_ARENA_DESTROY(pool, a, pnext) \
180 PR_BEGIN_MACRO \
181 PL_COUNT_ARENA(pool,--); \
182 if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
183 *(pnext) = (a)->next; \
184 PL_CLEAR_ARENA(a); \
185 free(a); \
186 (a) = 0; \
187 PR_END_MACRO
188
189#ifdef PL_ARENAMETER
190
191#include <stdio.h>
192
193PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb);
194
195PR_EXTERN(void) PL_ArenaCountInplaceGrowth(
196 PLArenaPool *pool, PRUint32 size, PRUint32 incr);
197
198PR_EXTERN(void) PL_ArenaCountGrowth(
199 PLArenaPool *pool, PRUint32 size, PRUint32 incr);
200
201PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark);
202
203PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark);
204
205PR_EXTERN(void) PL_DumpArenaStats(FILE *fp);
206
207#else /* !PL_ARENAMETER */
208
209#define PL_ArenaCountAllocation(ap, nb) /* nothing */
210#define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */
211#define PL_ArenaCountGrowth(ap, size, incr) /* nothing */
212#define PL_ArenaCountRelease(ap, mark) /* nothing */
213#define PL_ArenaCountRetract(ap, mark) /* nothing */
214
215#endif /* !PL_ARENAMETER */
216
217PR_END_EXTERN_C
218
219#endif /* plarena_h___ */
Note: See TracBrowser for help on using the repository browser.

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