]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_asn1/lib/plarena.c
2 * Copyright (c) 2003-2006,2008,2010-2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * The contents of this file are subject to the Mozilla Public
25 * License Version 1.1 (the "License"); you may not use this file
26 * except in compliance with the License. You may obtain a copy of
27 * the License at http://www.mozilla.org/MPL/
29 * Software distributed under the License is distributed on an "AS
30 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
31 * implied. See the License for the specific language governing
32 * rights and limitations under the License.
34 * The Original Code is the Netscape Portable Runtime (NSPR).
36 * The Initial Developer of the Original Code is Netscape
37 * Communications Corporation. Portions created by Netscape are
38 * Copyright (C) 1998-2000 Netscape Communications Corporation. All
43 * Alternatively, the contents of this file may be used under the
44 * terms of the GNU General Public License Version 2 or later (the
45 * "GPL"), in which case the provisions of the GPL are applicable
46 * instead of those above. If you wish to allow use of your
47 * version of this file only under the terms of the GPL and not to
48 * allow others to use your version of this file under the MPL,
49 * indicate your decision by deleting the provisions above and
50 * replace them with the notice and other provisions required by
51 * the GPL. If you do not delete the provisions above, a recipient
52 * may use your version of this file under either the MPL or the
57 * Lifetime-based fast allocation, inspired by much prior art, including
58 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
59 * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
70 static PLArenaStats
*arena_stats_list
;
72 #define COUNT(pool,what) (pool)->stats.what++
74 #define COUNT(pool,what) /* nothing */
77 #define PL_ARENA_DEFAULT_ALIGN sizeof(double)
79 PR_IMPLEMENT(void) PL_InitArenaPool(
80 PLArenaPool
*pool
, const char *name
, PRUint32 size
, PRUint32 align
)
82 #if !defined (__GNUC__)
87 align
= PL_ARENA_DEFAULT_ALIGN
;
88 pool
->mask
= PR_BITMASK(PR_CeilingLog2(align
));
89 pool
->first
.next
= NULL
;
90 pool
->first
.base
= pool
->first
.avail
= pool
->first
.limit
=
91 (PRUword
)PL_ARENA_ALIGN(pool
, &pool
->first
+ 1);
92 pool
->current
= &pool
->first
;
93 pool
->arenasize
= size
;
95 memset(&pool
->stats
, 0, sizeof pool
->stats
);
96 pool
->stats
.name
= strdup(name
);
97 pool
->stats
.next
= arena_stats_list
;
98 arena_stats_list
= &pool
->stats
;
103 #define MAX_SIZE (PR_UINT32_MAX >> 1)
107 ** PL_ArenaAllocate() -- allocate space from an arena pool
109 ** Description: PL_ArenaAllocate() allocates space from an arena
112 ** First, try to satisfy the request from arenas starting at
113 ** pool->current. Then try to allocate a new arena from the heap.
115 ** Returns: pointer to allocated space or NULL
117 ** Notes: The original implementation had some difficult to
118 ** solve bugs; the code was difficult to read. Sometimes it's
119 ** just easier to rewrite it. I did that. larryh.
121 ** See also: bugzilla: 45343.
125 PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool
*pool
, PRUint32 nb
)
128 char *rp
; /* returned pointer */
130 PR_ASSERT((nb
& pool
->mask
) == 0);
132 nb
= PL_ARENA_ALIGN(pool
, nb
); /* force alignment, cast is useless/causes warning. */
134 nb
= (PRUword
)PL_ARENA_ALIGN(pool
, nb
); /* force alignment */
137 /* attempt to allocate from arenas at pool->current */
141 if ( nb
<= a
->limit
- a
->avail
) {
143 rp
= (char *)a
->avail
;
147 } while( NULL
!= (a
= a
->next
) );
150 /* attempt to allocate from the heap */
152 PRUint32 sz
= PR_MAX(pool
->arenasize
, nb
);
153 if (PR_UINT32_MAX
- sz
< sizeof *a
+ pool
->mask
) {
156 sz
+= sizeof *a
+ pool
->mask
; /* header and alignment slop */
157 a
= (PLArena
*)PR_MALLOC(sz
);
160 // Check for integer overflow on a->avail += nb
161 PRUword a_avail_tmp
=(PRUword
)PL_ARENA_ALIGN(pool
, a
+ 1);
162 if (a_avail_tmp
+ nb
< a_avail_tmp
)
164 PR_FREEIF(a
); // Set a back to NULL
168 a
->limit
= (PRUword
)a
+ sz
;
170 a
->base
= a
->avail
= a_avail_tmp
;
172 a
->base
= a
->avail
= (PRUword
)PL_ARENA_ALIGN(pool
, a
+ 1);
174 rp
= (char *)a
->avail
;
176 /* the newly allocated arena is linked after pool->current
177 * and becomes pool->current */
178 a
->next
= pool
->current
->next
;
179 pool
->current
->next
= a
;
181 if ( NULL
== pool
->first
.next
)
182 pool
->first
.next
= a
;
183 PL_COUNT_ARENA(pool
,++);
184 COUNT(pool
, nmallocs
);
189 /* we got to here, and there's no memory to allocate */
191 } /* --- end PL_ArenaAllocate() --- */
194 * Grow, a.k.a. realloc. The PL_ARENA_GROW macro has already handled
195 * the possible grow-in-place action in which the current PLArena is the
196 * source of the incoming pointer, and there is room in that arena for
197 * the requested size.
199 PR_IMPLEMENT(void *) PL_ArenaGrow(
200 PLArenaPool
*pool
, void *p
, PRUint32 origSize
, PRUint32 incr
)
205 PRUint32 origAlignSize
; // bytes currently reserved for caller
206 PRUint32 newSize
; // bytes actually mallocd here
208 /* expand at least by 2x */
209 origAlignSize
= PL_ARENA_ALIGN(pool
, origSize
);
210 newSize
= PR_MAX(origAlignSize
+incr
, 2*origAlignSize
);
211 newSize
= PL_ARENA_ALIGN(pool
, newSize
);
213 // Enforce maximal size before any potential implicit truncation
214 if (newSize
>=MAX_SIZE
|| origSize
>=MAX_SIZE
|| incr
>=MAX_SIZE
) {
218 PL_ARENA_ALLOCATE(newp
, pool
, newSize
);
223 * Trim back the memory we just allocated to the amount our caller really
224 * needs, leaving the remainder for grow-in-place on subsequent calls
227 PRUint32 newAlignSize
= PL_ARENA_ALIGN(pool
, origSize
+incr
);
228 PR_ASSERT(pool
->current
->avail
== ((PRUword
)newp
+ newSize
));
229 pool
->current
->avail
= (PRUword
)newp
+ newAlignSize
;
230 PR_ASSERT(pool
->current
->avail
<= pool
->current
->limit
);
233 memcpy(newp
, p
, origSize
);
236 * Free old memory only if it's the entire outstanding allocated
237 * memory associated with one of our known PLArenas.
239 lastArena
= &pool
->first
; /* pool->first always empty */
240 thisArena
= lastArena
->next
; /* so, start here */
242 PRUword origPtr
= (PRUword
)p
;
243 while(thisArena
!= NULL
) {
244 if(origPtr
== thisArena
->base
) {
245 if((origPtr
+ origAlignSize
) == thisArena
->avail
) {
247 lastArena
->next
= thisArena
->next
;
250 PL_CLEAR_ARENA(thisArena
);
251 PL_COUNT_ARENA(pool
,--);
252 PR_DELETE(thisArena
);
256 lastArena
= thisArena
;
257 thisArena
= thisArena
->next
;
260 * Note: inability to free is not an error; it just causes a temporary leak
261 * of the old buffer (until the arena pool is freed, of course).
266 static void ClearArenaList(PLArena
*a
, PRInt32 pattern
)
269 for (; a
; a
= a
->next
) {
270 PR_ASSERT(a
->base
<= a
->avail
&& a
->avail
<= a
->limit
);
272 PL_CLEAR_UNUSED_PATTERN(a
, pattern
);
276 PR_IMPLEMENT(void) PL_ClearArenaPool(PLArenaPool
*pool
, PRInt32 pattern
)
278 ClearArenaList(pool
->first
.next
, pattern
);
282 * Free tail arenas linked after head, which may not be the true list head.
283 * Reset pool->current to point to head in case it pointed at a tail arena.
285 static void FreeArenaList(PLArenaPool
*pool
, PLArena
*head
, PRBool reallyFree
)
297 PL_COUNT_ARENA(pool
,--);
299 } while ((a
= *ap
) != 0);
301 pool
->current
= head
;
304 PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool
*pool
, char *mark
)
306 #if ARENA_MARK_ENABLE
309 for (a
= pool
->first
.next
; a
; a
= a
->next
) {
310 if (PR_UPTRDIFF(mark
, a
->base
) < PR_UPTRDIFF(a
->avail
, a
->base
)) {
311 a
->avail
= (PRUword
)PL_ARENA_ALIGN(pool
, mark
);
312 FreeArenaList(pool
, a
, PR_FALSE
);
316 #endif /* ARENA_MARK_ENABLE */
319 PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool
*pool
)
321 FreeArenaList(pool
, &pool
->first
, PR_FALSE
);
322 COUNT(pool
, ndeallocs
);
325 PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool
*pool
)
327 FreeArenaList(pool
, &pool
->first
, PR_TRUE
);
330 PLArenaStats
*stats
, **statsp
;
332 if (pool
->stats
.name
)
333 PR_DELETE(pool
->stats
.name
);
334 for (statsp
= &arena_stats_list
; (stats
= *statsp
) != 0;
335 statsp
= &stats
->next
) {
336 if (stats
== &pool
->stats
) {
337 *statsp
= stats
->next
;
345 PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool
*ap
)
349 PR_IMPLEMENT(void) PL_ArenaFinish(void)
354 PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool
*pool
, PRUint32 nb
)
356 pool
->stats
.nallocs
++;
357 pool
->stats
.nbytes
+= nb
;
358 if (nb
> pool
->stats
.maxalloc
)
359 pool
->stats
.maxalloc
= nb
;
360 pool
->stats
.variance
+= nb
* nb
;
363 PR_IMPLEMENT(void) PL_ArenaCountInplaceGrowth(
364 PLArenaPool
*pool
, PRUint32 size
, PRUint32 incr
)
366 pool
->stats
.ninplace
++;
369 PR_IMPLEMENT(void) PL_ArenaCountGrowth(
370 PLArenaPool
*pool
, PRUint32 size
, PRUint32 incr
)
372 pool
->stats
.ngrows
++;
373 pool
->stats
.nbytes
+= incr
;
374 pool
->stats
.variance
-= size
* size
;
376 if (size
> pool
->stats
.maxalloc
)
377 pool
->stats
.maxalloc
= size
;
378 pool
->stats
.variance
+= size
* size
;
381 PR_IMPLEMENT(void) PL_ArenaCountRelease(PLArenaPool
*pool
, char *mark
)
383 pool
->stats
.nreleases
++;
386 PR_IMPLEMENT(void) PL_ArenaCountRetract(PLArenaPool
*pool
, char *mark
)
388 pool
->stats
.nfastrels
++;
394 PR_IMPLEMENT(void) PL_DumpArenaStats(FILE *fp
)
397 double mean
, variance
;
399 for (stats
= arena_stats_list
; stats
; stats
= stats
->next
) {
400 if (stats
->nallocs
!= 0) {
401 mean
= (double)stats
->nbytes
/ stats
->nallocs
;
402 variance
= fabs(stats
->variance
/ stats
->nallocs
- mean
* mean
);
407 fprintf(fp
, "\n%s allocation statistics:\n", stats
->name
);
408 fprintf(fp
, " number of arenas: %u\n", stats
->narenas
);
409 fprintf(fp
, " number of allocations: %u\n", stats
->nallocs
);
410 fprintf(fp
, " number of free arena reclaims: %u\n", stats
->nreclaims
);
411 fprintf(fp
, " number of malloc calls: %u\n", stats
->nmallocs
);
412 fprintf(fp
, " number of deallocations: %u\n", stats
->ndeallocs
);
413 fprintf(fp
, " number of allocation growths: %u\n", stats
->ngrows
);
414 fprintf(fp
, " number of in-place growths: %u\n", stats
->ninplace
);
415 fprintf(fp
, "number of released allocations: %u\n", stats
->nreleases
);
416 fprintf(fp
, " number of fast releases: %u\n", stats
->nfastrels
);
417 fprintf(fp
, " total bytes allocated: %u\n", stats
->nbytes
);
418 fprintf(fp
, " mean allocation size: %g\n", mean
);
419 fprintf(fp
, " standard deviation: %g\n", sqrt(variance
));
420 fprintf(fp
, " maximum allocation size: %u\n", stats
->maxalloc
);
423 #endif /* PL_ARENAMETER */