/src/nspr/lib/ds/plarena.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  | /*  | 
7  |  |  * Lifetime-based fast allocation, inspired by much prior art, including  | 
8  |  |  * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"  | 
9  |  |  * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).  | 
10  |  |  */  | 
11  |  | #include <stdlib.h>  | 
12  |  | #include <string.h>  | 
13  |  | #include "plarena.h"  | 
14  |  | #include "prmem.h"  | 
15  |  | #include "prbit.h"  | 
16  |  | #include "prlog.h"  | 
17  |  | #include "prlock.h"  | 
18  |  | #include "prinit.h"  | 
19  |  |  | 
20  |  | #ifdef PL_ARENAMETER  | 
21  |  | static PLArenaStats* arena_stats_list;  | 
22  |  |  | 
23  |  | #  define COUNT(pool, what) (pool)->stats.what++  | 
24  |  | #else  | 
25  |  | #  define COUNT(pool, what) /* nothing */  | 
26  |  | #endif  | 
27  |  |  | 
28  | 0  | #define PL_ARENA_DEFAULT_ALIGN sizeof(double)  | 
29  |  |  | 
30  |  | PR_IMPLEMENT(void)  | 
31  |  | PL_InitArenaPool(PLArenaPool* pool, const char* name, PRUint32 size,  | 
32  | 94.0k  |                  PRUint32 align) { | 
33  |  |   /*  | 
34  |  |    * Look-up table of PR_BITMASK(PR_CeilingLog2(align)) values for  | 
35  |  |    * align = 1 to 32.  | 
36  |  |    */  | 
37  | 94.0k  |   static const PRUint8 pmasks[33] = { | 
38  | 94.0k  |       0, /*  not used */  | 
39  | 94.0k  |       0,  1,  3,  3,  7,  7,  7,  7,  | 
40  | 94.0k  |       15, 15, 15, 15, 15, 15, 15, 15, /*  1 ... 16 */  | 
41  | 94.0k  |       31, 31, 31, 31, 31, 31, 31, 31,  | 
42  | 94.0k  |       31, 31, 31, 31, 31, 31, 31, 31 /* 17 ... 32 */  | 
43  | 94.0k  |   };  | 
44  |  |  | 
45  | 94.0k  |   if (align == 0) { | 
46  | 0  |     align = PL_ARENA_DEFAULT_ALIGN;  | 
47  | 0  |   }  | 
48  |  |  | 
49  | 94.0k  |   if (align < sizeof(pmasks) / sizeof(pmasks[0])) { | 
50  | 94.0k  |     pool->mask = pmasks[align];  | 
51  | 94.0k  |   } else { | 
52  | 0  |     pool->mask = PR_BITMASK(PR_CeilingLog2(align));  | 
53  | 0  |   }  | 
54  |  |  | 
55  | 94.0k  |   pool->first.next = NULL;  | 
56  |  |   /* Set all three addresses in pool->first to the same dummy value.  | 
57  |  |    * These addresses are only compared with each other, but never  | 
58  |  |    * dereferenced. */  | 
59  | 94.0k  |   pool->first.base = pool->first.avail = pool->first.limit =  | 
60  | 94.0k  |       (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1);  | 
61  | 94.0k  |   pool->current = &pool->first;  | 
62  |  |   /*  | 
63  |  |    * Compute the net size so that each arena's gross size is |size|.  | 
64  |  |    * sizeof(PLArena) + pool->mask is the header and alignment slop  | 
65  |  |    * that PL_ArenaAllocate adds to the net size.  | 
66  |  |    */  | 
67  | 94.0k  |   if (size > sizeof(PLArena) + pool->mask) { | 
68  | 94.0k  |     pool->arenasize = size - (sizeof(PLArena) + pool->mask);  | 
69  | 94.0k  |   } else { | 
70  | 0  |     pool->arenasize = size;  | 
71  | 0  |   }  | 
72  |  | #ifdef PL_ARENAMETER  | 
73  |  |   memset(&pool->stats, 0, sizeof pool->stats);  | 
74  |  |   pool->stats.name = strdup(name);  | 
75  |  |   pool->stats.next = arena_stats_list;  | 
76  |  |   arena_stats_list = &pool->stats;  | 
77  |  | #endif  | 
78  | 94.0k  | }  | 
79  |  |  | 
80  |  | /*  | 
81  |  | ** PL_ArenaAllocate() -- allocate space from an arena pool  | 
82  |  | **  | 
83  |  | ** Description: PL_ArenaAllocate() allocates space from an arena  | 
84  |  | ** pool.  | 
85  |  | **  | 
86  |  | ** First, try to satisfy the request from arenas starting at  | 
87  |  | ** pool->current. Then try to allocate a new arena from the heap.  | 
88  |  | **  | 
89  |  | ** Returns: pointer to allocated space or NULL  | 
90  |  | **  | 
91  |  | ** Notes: The original implementation had some difficult to  | 
92  |  | ** solve bugs; the code was difficult to read. Sometimes it's  | 
93  |  | ** just easier to rewrite it. I did that. larryh.  | 
94  |  | **  | 
95  |  | ** See also: bugzilla: 45343.  | 
96  |  | **  | 
97  |  | */  | 
98  |  |  | 
99  | 46.6k  | PR_IMPLEMENT(void*) PL_ArenaAllocate(PLArenaPool* pool, PRUint32 nb) { | 
100  | 46.6k  |   PLArena* a;  | 
101  | 46.6k  |   char* rp; /* returned pointer */  | 
102  | 46.6k  |   PRUint32 nbOld;  | 
103  |  |  | 
104  | 46.6k  |   PR_ASSERT((nb & pool->mask) == 0);  | 
105  |  |  | 
106  | 46.6k  |   nbOld = nb;  | 
107  | 46.6k  |   nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */  | 
108  | 46.6k  |   if (nb < nbOld) { | 
109  | 0  |     return NULL;  | 
110  | 0  |   }  | 
111  |  |  | 
112  |  |   /* attempt to allocate from arenas at pool->current */  | 
113  | 46.6k  |   { | 
114  | 46.6k  |     a = pool->current;  | 
115  | 46.6k  |     do { | 
116  | 46.6k  |       if (nb <= a->limit - a->avail) { | 
117  | 0  |         pool->current = a;  | 
118  | 0  |         rp = (char*)a->avail;  | 
119  | 0  |         a->avail += nb;  | 
120  | 0  |         return rp;  | 
121  | 0  |       }  | 
122  | 46.6k  |     } while (NULL != (a = a->next));  | 
123  | 46.6k  |   }  | 
124  |  |  | 
125  |  |   /* attempt to allocate from the heap */  | 
126  | 46.6k  |   { | 
127  | 46.6k  |     PRUint32 sz = PR_MAX(pool->arenasize, nb);  | 
128  | 46.6k  |     if (PR_UINT32_MAX - sz < sizeof *a + pool->mask) { | 
129  | 0  |       a = NULL;  | 
130  | 46.6k  |     } else { | 
131  | 46.6k  |       sz += sizeof *a + pool->mask; /* header and alignment slop */  | 
132  | 46.6k  |       a = (PLArena*)PR_MALLOC(sz);  | 
133  | 46.6k  |     }  | 
134  | 46.6k  |     if (NULL != a) { | 
135  | 46.6k  |       a->limit = (PRUword)a + sz;  | 
136  | 46.6k  |       a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1);  | 
137  | 46.6k  |       PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail);  | 
138  | 46.6k  |       rp = (char*)a->avail;  | 
139  | 46.6k  |       a->avail += nb;  | 
140  | 46.6k  |       PR_ASSERT(a->avail <= a->limit);  | 
141  |  |       /* the newly allocated arena is linked after pool->current  | 
142  |  |        *  and becomes pool->current */  | 
143  | 46.6k  |       a->next = pool->current->next;  | 
144  | 46.6k  |       pool->current->next = a;  | 
145  | 46.6k  |       pool->current = a;  | 
146  | 46.6k  |       if (NULL == pool->first.next) { | 
147  | 0  |         pool->first.next = a;  | 
148  | 0  |       }  | 
149  | 46.6k  |       PL_COUNT_ARENA(pool, ++);  | 
150  | 46.6k  |       COUNT(pool, nmallocs);  | 
151  | 46.6k  |       return (rp);  | 
152  | 46.6k  |     }  | 
153  | 46.6k  |   }  | 
154  |  |  | 
155  |  |   /* we got to here, and there's no memory to allocate */  | 
156  | 0  |   return (NULL);  | 
157  | 46.6k  | } /* --- end PL_ArenaAllocate() --- */  | 
158  |  |  | 
159  |  | PR_IMPLEMENT(void*)  | 
160  | 48.6k  | PL_ArenaGrow(PLArenaPool* pool, void* p, PRUint32 size, PRUint32 incr) { | 
161  | 48.6k  |   void* newp;  | 
162  |  |  | 
163  | 48.6k  |   if (PR_UINT32_MAX - size < incr) { | 
164  | 0  |     return NULL;  | 
165  | 0  |   }  | 
166  | 48.6k  |   PL_ARENA_ALLOCATE(newp, pool, size + incr);  | 
167  | 48.6k  |   if (newp) { | 
168  | 48.6k  |     memcpy(newp, p, size);  | 
169  | 48.6k  |   }  | 
170  | 48.6k  |   return newp;  | 
171  | 48.6k  | }  | 
172  |  |  | 
173  | 0  | PR_IMPLEMENT(void) PL_ClearArenaPool(PLArenaPool* pool, PRInt32 pattern) { | 
174  | 0  |   PLArena* a;  | 
175  |  | 
  | 
176  | 0  |   for (a = pool->first.next; a; a = a->next) { | 
177  | 0  |     PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);  | 
178  | 0  |     a->avail = a->base;  | 
179  | 0  |     PL_CLEAR_UNUSED_PATTERN(a, pattern);  | 
180  | 0  |     PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail);  | 
181  | 0  |   }  | 
182  | 0  | }  | 
183  |  |  | 
184  |  | /*  | 
185  |  |  * Free tail arenas linked after head, which may not be the true list head.  | 
186  |  |  * Reset pool->current to point to head in case it pointed at a tail arena.  | 
187  |  |  */  | 
188  | 94.0k  | static void FreeArenaList(PLArenaPool* pool, PLArena* head) { | 
189  | 94.0k  |   PLArena* a = head->next;  | 
190  | 94.0k  |   if (!a) { | 
191  | 85.2k  |     return;  | 
192  | 85.2k  |   }  | 
193  |  |  | 
194  | 8.80k  |   head->next = NULL;  | 
195  |  |  | 
196  | 46.6k  |   do { | 
197  | 46.6k  |     PLArena* tmp = a;  | 
198  | 46.6k  |     a = a->next;  | 
199  | 46.6k  |     PL_CLEAR_ARENA(tmp);  | 
200  | 46.6k  |     PL_COUNT_ARENA(pool, --);  | 
201  | 46.6k  |     PR_DELETE(tmp);  | 
202  | 46.6k  |   } while (a);  | 
203  |  |  | 
204  | 8.80k  |   pool->current = head;  | 
205  | 8.80k  | }  | 
206  |  |  | 
207  | 0  | PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool* pool, char* mark) { | 
208  | 0  |   PLArena* a;  | 
209  |  | 
  | 
210  | 0  |   for (a = &pool->first; a; a = a->next) { | 
211  | 0  |     if (PR_UPTRDIFF(mark, a->base) <= PR_UPTRDIFF(a->avail, a->base)) { | 
212  | 0  |       a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark);  | 
213  | 0  |       FreeArenaList(pool, a);  | 
214  | 0  |       return;  | 
215  | 0  |     }  | 
216  | 0  |   }  | 
217  | 0  | }  | 
218  |  |  | 
219  | 94.0k  | PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool* pool) { | 
220  | 94.0k  |   FreeArenaList(pool, &pool->first);  | 
221  | 94.0k  |   COUNT(pool, ndeallocs);  | 
222  | 94.0k  | }  | 
223  |  |  | 
224  | 0  | PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool* pool) { | 
225  | 0  |   FreeArenaList(pool, &pool->first);  | 
226  |  | #ifdef PL_ARENAMETER  | 
227  |  |   { | 
228  |  |     PLArenaStats *stats, **statsp;  | 
229  |  |  | 
230  |  |     if (pool->stats.name) { | 
231  |  |       PR_DELETE(pool->stats.name);  | 
232  |  |     }  | 
233  |  |     for (statsp = &arena_stats_list; (stats = *statsp) != 0;  | 
234  |  |          statsp = &stats->next) { | 
235  |  |       if (stats == &pool->stats) { | 
236  |  |         *statsp = stats->next;  | 
237  |  |         return;  | 
238  |  |       }  | 
239  |  |     }  | 
240  |  |   }  | 
241  |  | #endif  | 
242  | 0  | }  | 
243  |  |  | 
244  | 0  | PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool* ap) {} | 
245  |  |  | 
246  | 0  | PR_IMPLEMENT(void) PL_ArenaFinish(void) {} | 
247  |  |  | 
248  |  | PR_IMPLEMENT(size_t)  | 
249  |  | PL_SizeOfArenaPoolExcludingPool(const PLArenaPool* pool,  | 
250  | 0  |                                 PLMallocSizeFn mallocSizeOf) { | 
251  |  |   /*  | 
252  |  |    * The first PLArena is within |pool|, so don't measure it.  Subsequent  | 
253  |  |    * PLArenas are separate and must be measured.  | 
254  |  |    */  | 
255  | 0  |   size_t size = 0;  | 
256  | 0  |   const PLArena* arena = pool->first.next;  | 
257  | 0  |   while (arena) { | 
258  | 0  |     size += mallocSizeOf(arena);  | 
259  | 0  |     arena = arena->next;  | 
260  | 0  |   }  | 
261  | 0  |   return size;  | 
262  | 0  | }  | 
263  |  |  | 
264  |  | #ifdef PL_ARENAMETER  | 
265  |  | PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool* pool, PRUint32 nb) { | 
266  |  |   pool->stats.nallocs++;  | 
267  |  |   pool->stats.nbytes += nb;  | 
268  |  |   if (nb > pool->stats.maxalloc) { | 
269  |  |     pool->stats.maxalloc = nb;  | 
270  |  |   }  | 
271  |  |   pool->stats.variance += nb * nb;  | 
272  |  | }  | 
273  |  |  | 
274  |  | PR_IMPLEMENT(void)  | 
275  |  | PL_ArenaCountInplaceGrowth(PLArenaPool* pool, PRUint32 size, PRUint32 incr) { | 
276  |  |   pool->stats.ninplace++;  | 
277  |  | }  | 
278  |  |  | 
279  |  | PR_IMPLEMENT(void)  | 
280  |  | PL_ArenaCountGrowth(PLArenaPool* pool, PRUint32 size, PRUint32 incr) { | 
281  |  |   pool->stats.ngrows++;  | 
282  |  |   pool->stats.nbytes += incr;  | 
283  |  |   pool->stats.variance -= size * size;  | 
284  |  |   size += incr;  | 
285  |  |   if (size > pool->stats.maxalloc) { | 
286  |  |     pool->stats.maxalloc = size;  | 
287  |  |   }  | 
288  |  |   pool->stats.variance += size * size;  | 
289  |  | }  | 
290  |  |  | 
291  |  | PR_IMPLEMENT(void) PL_ArenaCountRelease(PLArenaPool* pool, char* mark) { | 
292  |  |   pool->stats.nreleases++;  | 
293  |  | }  | 
294  |  |  | 
295  |  | PR_IMPLEMENT(void) PL_ArenaCountRetract(PLArenaPool* pool, char* mark) { | 
296  |  |   pool->stats.nfastrels++;  | 
297  |  | }  | 
298  |  |  | 
299  |  | #  include <math.h>  | 
300  |  | #  include <stdio.h>  | 
301  |  |  | 
302  |  | PR_IMPLEMENT(void) PL_DumpArenaStats(FILE* fp) { | 
303  |  |   PLArenaStats* stats;  | 
304  |  |   double mean, variance;  | 
305  |  |  | 
306  |  |   for (stats = arena_stats_list; stats; stats = stats->next) { | 
307  |  |     if (stats->nallocs != 0) { | 
308  |  |       mean = (double)stats->nbytes / stats->nallocs;  | 
309  |  |       variance = fabs(stats->variance / stats->nallocs - mean * mean);  | 
310  |  |     } else { | 
311  |  |       mean = variance = 0;  | 
312  |  |     }  | 
313  |  |  | 
314  |  |     fprintf(fp, "\n%s allocation statistics:\n", stats->name);  | 
315  |  |     fprintf(fp, "              number of arenas: %u\n", stats->narenas);  | 
316  |  |     fprintf(fp, "         number of allocations: %u\n", stats->nallocs);  | 
317  |  |     fprintf(fp, " number of free arena reclaims: %u\n", stats->nreclaims);  | 
318  |  |     fprintf(fp, "        number of malloc calls: %u\n", stats->nmallocs);  | 
319  |  |     fprintf(fp, "       number of deallocations: %u\n", stats->ndeallocs);  | 
320  |  |     fprintf(fp, "  number of allocation growths: %u\n", stats->ngrows);  | 
321  |  |     fprintf(fp, "    number of in-place growths: %u\n", stats->ninplace);  | 
322  |  |     fprintf(fp, "number of released allocations: %u\n", stats->nreleases);  | 
323  |  |     fprintf(fp, "       number of fast releases: %u\n", stats->nfastrels);  | 
324  |  |     fprintf(fp, "         total bytes allocated: %u\n", stats->nbytes);  | 
325  |  |     fprintf(fp, "          mean allocation size: %g\n", mean);  | 
326  |  |     fprintf(fp, "            standard deviation: %g\n", sqrt(variance));  | 
327  |  |     fprintf(fp, "       maximum allocation size: %u\n", stats->maxalloc);  | 
328  |  |   }  | 
329  |  | }  | 
330  |  | #endif /* PL_ARENAMETER */  |