OLD | NEW |
| (Empty) |
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | |
2 /* This Source Code Form is subject to the terms of the Mozilla Public | |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
5 | |
6 #ifndef plarena_h___ | |
7 #define plarena_h___ | |
8 /* | |
9 * Lifetime-based fast allocation, inspired by much prior art, including | |
10 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" | |
11 * David R. Hanson, Software -- Practice and Experience, Vol. 20(1). | |
12 * | |
13 * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE). | |
14 */ | |
15 #include "prtypes.h" | |
16 #include "plarenas.h" | |
17 | |
18 PR_BEGIN_EXTERN_C | |
19 | |
20 typedef struct PLArena PLArena; | |
21 | |
22 struct PLArena { | |
23 PLArena *next; /* next arena for this lifetime */ | |
24 PRUword base; /* aligned base address, follows this header */ | |
25 PRUword limit; /* one beyond last byte in arena */ | |
26 PRUword avail; /* points to next available byte */ | |
27 }; | |
28 | |
29 #ifdef PL_ARENAMETER | |
30 typedef struct PLArenaStats PLArenaStats; | |
31 | |
32 struct PLArenaStats { | |
33 PLArenaStats *next; /* next in arenaStats list */ | |
34 char *name; /* name for debugging */ | |
35 PRUint32 narenas; /* number of arenas in pool */ | |
36 PRUint32 nallocs; /* number of PL_ARENA_ALLOCATE() calls */ | |
37 PRUint32 nreclaims; /* number of reclaims from freeArenas */ | |
38 PRUint32 nmallocs; /* number of malloc() calls */ | |
39 PRUint32 ndeallocs; /* number of lifetime deallocations */ | |
40 PRUint32 ngrows; /* number of PL_ARENA_GROW() calls */ | |
41 PRUint32 ninplace; /* number of in-place growths */ | |
42 PRUint32 nreleases; /* number of PL_ARENA_RELEASE() calls */ | |
43 PRUint32 nfastrels; /* number of "fast path" releases */ | |
44 PRUint32 nbytes; /* total bytes allocated */ | |
45 PRUint32 maxalloc; /* maximum allocation size in bytes */ | |
46 PRFloat64 variance; /* size variance accumulator */ | |
47 }; | |
48 #endif | |
49 | |
50 struct PLArenaPool { | |
51 PLArena first; /* first arena in pool list */ | |
52 PLArena *current; /* arena from which to allocate space */ | |
53 PRUint32 arenasize; /* net exact size of a new arena */ | |
54 PRUword mask; /* alignment mask (power-of-2 - 1) */ | |
55 #ifdef PL_ARENAMETER | |
56 PLArenaStats stats; | |
57 #endif | |
58 }; | |
59 | |
60 /* | |
61 * If the including .c file uses only one power-of-2 alignment, it may define | |
62 * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions | |
63 * per ALLOCATE and GROW. | |
64 */ | |
65 #ifdef PL_ARENA_CONST_ALIGN_MASK | |
66 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \ | |
67 & ~PL_ARENA_CONST_ALIGN_MASK) | |
68 | |
69 #define PL_INIT_ARENA_POOL(pool, name, size) \ | |
70 PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1) | |
71 #else | |
72 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask) | |
73 #endif | |
74 | |
75 #define PL_ARENA_ALLOCATE(p, pool, nb) \ | |
76 PR_BEGIN_MACRO \ | |
77 PLArena *_a = (pool)->current; \ | |
78 PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \ | |
79 PRUword _p = _a->avail; \ | |
80 PRUword _q = _p + _nb; \ | |
81 if (_q > _a->limit) \ | |
82 _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ | |
83 else \ | |
84 _a->avail = _q; \ | |
85 p = (void *)_p; \ | |
86 PL_ArenaCountAllocation(pool, nb); \ | |
87 PR_END_MACRO | |
88 | |
89 #define PL_ARENA_GROW(p, pool, size, incr) \ | |
90 PR_BEGIN_MACRO \ | |
91 PLArena *_a = (pool)->current; \ | |
92 PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \ | |
93 PRUword _p = _a->avail; \ | |
94 PRUword _q = _p + _incr; \ | |
95 if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ | |
96 _q <= _a->limit) { \ | |
97 _a->avail = _q; \ | |
98 PL_ArenaCountInplaceGrowth(pool, size, incr); \ | |
99 } else { \ | |
100 p = PL_ArenaGrow(pool, p, size, incr); \ | |
101 } \ | |
102 PL_ArenaCountGrowth(pool, size, incr); \ | |
103 PR_END_MACRO | |
104 | |
105 #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) | |
106 #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q)) | |
107 | |
108 #define PL_CLEAR_UNUSED_PATTERN(a, pattern) \ | |
109 (PR_ASSERT((a)->avail <= (a)->limit), \ | |
110 memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail)) | |
111 #ifdef DEBUG | |
112 #define PL_FREE_PATTERN 0xDA | |
113 #define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN) | |
114 #define PL_CLEAR_ARENA(a) memset((void*)(a), PL_FREE_PATTERN, \ | |
115 (a)->limit - (PRUword)(a)) | |
116 #else | |
117 #define PL_CLEAR_UNUSED(a) | |
118 #define PL_CLEAR_ARENA(a) | |
119 #endif | |
120 | |
121 #define PL_ARENA_RELEASE(pool, mark) \ | |
122 PR_BEGIN_MACRO \ | |
123 char *_m = (char *)(mark); \ | |
124 PLArena *_a = (pool)->current; \ | |
125 if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \ | |
126 _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \ | |
127 PL_CLEAR_UNUSED(_a); \ | |
128 PL_ArenaCountRetract(pool, _m); \ | |
129 } else { \ | |
130 PL_ArenaRelease(pool, _m); \ | |
131 } \ | |
132 PL_ArenaCountRelease(pool, _m); \ | |
133 PR_END_MACRO | |
134 | |
135 #ifdef PL_ARENAMETER | |
136 #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op) | |
137 #else | |
138 #define PL_COUNT_ARENA(pool,op) | |
139 #endif | |
140 | |
141 #define PL_ARENA_DESTROY(pool, a, pnext) \ | |
142 PR_BEGIN_MACRO \ | |
143 PL_COUNT_ARENA(pool,--); \ | |
144 if ((pool)->current == (a)) (pool)->current = &(pool)->first; \ | |
145 *(pnext) = (a)->next; \ | |
146 PL_CLEAR_ARENA(a); \ | |
147 free(a); \ | |
148 (a) = 0; \ | |
149 PR_END_MACRO | |
150 | |
151 #ifdef PL_ARENAMETER | |
152 | |
153 #include <stdio.h> | |
154 | |
155 PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb); | |
156 | |
157 PR_EXTERN(void) PL_ArenaCountInplaceGrowth( | |
158 PLArenaPool *pool, PRUint32 size, PRUint32 incr); | |
159 | |
160 PR_EXTERN(void) PL_ArenaCountGrowth( | |
161 PLArenaPool *pool, PRUint32 size, PRUint32 incr); | |
162 | |
163 PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark); | |
164 | |
165 PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark); | |
166 | |
167 PR_EXTERN(void) PL_DumpArenaStats(FILE *fp); | |
168 | |
169 #else /* !PL_ARENAMETER */ | |
170 | |
171 #define PL_ArenaCountAllocation(ap, nb) /* nothing */ | |
172 #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */ | |
173 #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */ | |
174 #define PL_ArenaCountRelease(ap, mark) /* nothing */ | |
175 #define PL_ArenaCountRetract(ap, mark) /* nothing */ | |
176 | |
177 #endif /* !PL_ARENAMETER */ | |
178 | |
179 PR_END_EXTERN_C | |
180 | |
181 #endif /* plarena_h___ */ | |
OLD | NEW |