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 |
|
---|
51 | PR_BEGIN_EXTERN_C
|
---|
52 |
|
---|
53 | typedef struct PLArena PLArena;
|
---|
54 |
|
---|
55 | struct 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 | struct PLArenaPool {
|
---|
63 | PLArena first; /* first arena in pool list */
|
---|
64 | PLArena *current; /* arena from which to allocate space */
|
---|
65 | PRUint32 arenasize; /* net exact size of a new arena */
|
---|
66 | PRUword mask; /* alignment mask (power-of-2 - 1) */
|
---|
67 | };
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * If the including .c file uses only one power-of-2 alignment, it may define
|
---|
71 | * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
|
---|
72 | * per ALLOCATE and GROW.
|
---|
73 | */
|
---|
74 | #ifdef PL_ARENA_CONST_ALIGN_MASK
|
---|
75 | #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \
|
---|
76 | & ~PL_ARENA_CONST_ALIGN_MASK)
|
---|
77 |
|
---|
78 | #define PL_INIT_ARENA_POOL(pool, name, size) \
|
---|
79 | PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1)
|
---|
80 | #else
|
---|
81 | #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask)
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | #define PL_ARENA_ALLOCATE(p, pool, nb) \
|
---|
85 | PR_BEGIN_MACRO \
|
---|
86 | PLArena *_a = (pool)->current; \
|
---|
87 | PRUint32 _nb = PL_ARENA_ALIGN(pool, (PRUint32)nb); \
|
---|
88 | PRUword _p = _a->avail; \
|
---|
89 | if (_nb < (PRUint32)nb) { \
|
---|
90 | _p = 0; \
|
---|
91 | } else if (_nb > (_a->limit - _a->avail)) { \
|
---|
92 | _p = (PRUword)PL_ArenaAllocate(pool, _nb); \
|
---|
93 | } else { \
|
---|
94 | _a->avail += _nb; \
|
---|
95 | } \
|
---|
96 | p = (void *)_p; \
|
---|
97 | if (p) { \
|
---|
98 | PL_ArenaCountAllocation(pool, nb); \
|
---|
99 | } \
|
---|
100 | PR_END_MACRO
|
---|
101 |
|
---|
102 | #define PL_ARENA_GROW(p, pool, size, incr) \
|
---|
103 | PR_BEGIN_MACRO \
|
---|
104 | PLArena *_a = (pool)->current; \
|
---|
105 | PRUint32 _incr = PL_ARENA_ALIGN(pool, (PRUint32)incr); \
|
---|
106 | if (_incr < (PRUint32)incr) { \
|
---|
107 | p = NULL; \
|
---|
108 | } else if (_a->avail == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \
|
---|
109 | _incr <= (_a->limit - _a->avail)) { \
|
---|
110 | _a->avail = _incr; \
|
---|
111 | PL_ArenaCountInplaceGrowth(pool, size, (RTUint32)incr); \
|
---|
112 | } else { \
|
---|
113 | p = PL_ArenaGrow(pool, p, size, (PRUint32)incr); \
|
---|
114 | } \
|
---|
115 | if (p) { \
|
---|
116 | PL_ArenaCountGrowth(pool, size, (PRUint32)incr); \
|
---|
117 | } \
|
---|
118 | PR_END_MACRO
|
---|
119 |
|
---|
120 | #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail)
|
---|
121 | #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q))
|
---|
122 |
|
---|
123 | #ifdef DEBUG
|
---|
124 | #define PL_FREE_PATTERN 0xDA
|
---|
125 | #define PL_CLEAR_UNUSED(a) Assert((a)->avail <= (a)->limit); \
|
---|
126 | memset((void*)(a)->avail, PL_FREE_PATTERN, \
|
---|
127 | (a)->limit - (a)->avail)
|
---|
128 | #define PL_CLEAR_ARENA(a) memset((void*)(a), PL_FREE_PATTERN, \
|
---|
129 | (a)->limit - (PRUword)(a))
|
---|
130 | #else
|
---|
131 | #define PL_CLEAR_UNUSED(a)
|
---|
132 | #define PL_CLEAR_ARENA(a)
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | #define PL_ARENA_RELEASE(pool, mark) \
|
---|
136 | PR_BEGIN_MACRO \
|
---|
137 | char *_m = (char *)(mark); \
|
---|
138 | PLArena *_a = (pool)->current; \
|
---|
139 | if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \
|
---|
140 | _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \
|
---|
141 | PL_CLEAR_UNUSED(_a); \
|
---|
142 | PL_ArenaCountRetract(pool, _m); \
|
---|
143 | } else { \
|
---|
144 | PL_ArenaRelease(pool, _m); \
|
---|
145 | } \
|
---|
146 | PL_ArenaCountRelease(pool, _m); \
|
---|
147 | PR_END_MACRO
|
---|
148 |
|
---|
149 | #define PL_COUNT_ARENA(pool,op)
|
---|
150 |
|
---|
151 | #define PL_ARENA_DESTROY(pool, a, pnext) \
|
---|
152 | PR_BEGIN_MACRO \
|
---|
153 | PL_COUNT_ARENA(pool,--); \
|
---|
154 | if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
|
---|
155 | *(pnext) = (a)->next; \
|
---|
156 | PL_CLEAR_ARENA(a); \
|
---|
157 | free(a); \
|
---|
158 | (a) = 0; \
|
---|
159 | PR_END_MACRO
|
---|
160 |
|
---|
161 | #define PL_ArenaCountAllocation(ap, nb) /* nothing */
|
---|
162 | #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */
|
---|
163 | #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */
|
---|
164 | #define PL_ArenaCountRelease(ap, mark) /* nothing */
|
---|
165 | #define PL_ArenaCountRetract(ap, mark) /* nothing */
|
---|
166 |
|
---|
167 | PR_END_EXTERN_C
|
---|
168 |
|
---|
169 | #endif /* plarena_h___ */
|
---|