diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index e7a113c8cd..40bf665249 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.158 2014/04/23 15:07:27 tedu Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.159 2014/05/01 04:08:13 tedu Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -61,7 +61,7 @@ | |||
61 | 61 | ||
62 | #define MALLOC_MAXCHUNK (1 << MALLOC_MAXSHIFT) | 62 | #define MALLOC_MAXCHUNK (1 << MALLOC_MAXSHIFT) |
63 | #define MALLOC_MAXCACHE 256 | 63 | #define MALLOC_MAXCACHE 256 |
64 | #define MALLOC_DELAYED_CHUNKS 15 /* max of getrnibble() */ | 64 | #define MALLOC_DELAYED_CHUNK_MASK 15 |
65 | #define MALLOC_INITIAL_REGIONS 512 | 65 | #define MALLOC_INITIAL_REGIONS 512 |
66 | #define MALLOC_DEFAULT_CACHE 64 | 66 | #define MALLOC_DEFAULT_CACHE 64 |
67 | 67 | ||
@@ -115,7 +115,7 @@ struct dir_info { | |||
115 | /* free pages cache */ | 115 | /* free pages cache */ |
116 | struct region_info free_regions[MALLOC_MAXCACHE]; | 116 | struct region_info free_regions[MALLOC_MAXCACHE]; |
117 | /* delayed free chunk slots */ | 117 | /* delayed free chunk slots */ |
118 | void *delayed_chunks[MALLOC_DELAYED_CHUNKS + 1]; | 118 | void *delayed_chunks[MALLOC_DELAYED_CHUNK_MASK + 1]; |
119 | u_short chunk_start; | 119 | u_short chunk_start; |
120 | #ifdef MALLOC_STATS | 120 | #ifdef MALLOC_STATS |
121 | size_t inserts; | 121 | size_t inserts; |
@@ -191,9 +191,9 @@ static int malloc_active; /* status of malloc */ | |||
191 | static size_t malloc_guarded; /* bytes used for guards */ | 191 | static size_t malloc_guarded; /* bytes used for guards */ |
192 | static size_t malloc_used; /* bytes allocated */ | 192 | static size_t malloc_used; /* bytes allocated */ |
193 | 193 | ||
194 | static size_t rnibblesused; /* random nibbles used */ | 194 | static size_t rbytesused; /* random bytes used */ |
195 | static u_char rbytes[512]; /* random bytes */ | 195 | static u_char rbytes[512]; /* random bytes */ |
196 | static u_char getrnibble(void); | 196 | static u_char getrbyte(void); |
197 | 197 | ||
198 | extern char *__progname; | 198 | extern char *__progname; |
199 | 199 | ||
@@ -273,18 +273,18 @@ static void | |||
273 | rbytes_init(void) | 273 | rbytes_init(void) |
274 | { | 274 | { |
275 | arc4random_buf(rbytes, sizeof(rbytes)); | 275 | arc4random_buf(rbytes, sizeof(rbytes)); |
276 | rnibblesused = 0; | 276 | rbytesused = 0; |
277 | } | 277 | } |
278 | 278 | ||
279 | static inline u_char | 279 | static inline u_char |
280 | getrnibble(void) | 280 | getrbyte(void) |
281 | { | 281 | { |
282 | u_char x; | 282 | u_char x; |
283 | 283 | ||
284 | if (rnibblesused >= 2 * sizeof(rbytes)) | 284 | if (rbytesused >= sizeof(rbytes)) |
285 | rbytes_init(); | 285 | rbytes_init(); |
286 | x = rbytes[rnibblesused++ / 2]; | 286 | x = rbytes[rbytesused++]; |
287 | return (rnibblesused & 1 ? x & 0xf : x >> 4); | 287 | return x; |
288 | } | 288 | } |
289 | 289 | ||
290 | /* | 290 | /* |
@@ -317,7 +317,7 @@ unmap(struct dir_info *d, void *p, size_t sz) | |||
317 | rsz = mopts.malloc_cache - d->free_regions_size; | 317 | rsz = mopts.malloc_cache - d->free_regions_size; |
318 | if (psz > rsz) | 318 | if (psz > rsz) |
319 | tounmap = psz - rsz; | 319 | tounmap = psz - rsz; |
320 | offset = getrnibble() + (getrnibble() << 4); | 320 | offset = getrbyte(); |
321 | for (i = 0; tounmap > 0 && i < mopts.malloc_cache; i++) { | 321 | for (i = 0; tounmap > 0 && i < mopts.malloc_cache; i++) { |
322 | r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; | 322 | r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; |
323 | if (r->p != NULL) { | 323 | if (r->p != NULL) { |
@@ -398,7 +398,7 @@ map(struct dir_info *d, size_t sz, int zero_fill) | |||
398 | /* zero fill not needed */ | 398 | /* zero fill not needed */ |
399 | return p; | 399 | return p; |
400 | } | 400 | } |
401 | offset = getrnibble() + (getrnibble() << 4); | 401 | offset = getrbyte(); |
402 | for (i = 0; i < mopts.malloc_cache; i++) { | 402 | for (i = 0; i < mopts.malloc_cache; i++) { |
403 | r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; | 403 | r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; |
404 | if (r->p != NULL) { | 404 | if (r->p != NULL) { |
@@ -920,7 +920,7 @@ malloc_bytes(struct dir_info *d, size_t size, void *f) | |||
920 | 920 | ||
921 | i = d->chunk_start; | 921 | i = d->chunk_start; |
922 | if (bp->free > 1) | 922 | if (bp->free > 1) |
923 | i += getrnibble(); | 923 | i += getrbyte(); |
924 | if (i >= bp->total) | 924 | if (i >= bp->total) |
925 | i &= bp->total - 1; | 925 | i &= bp->total - 1; |
926 | for (;;) { | 926 | for (;;) { |
@@ -1200,7 +1200,7 @@ ofree(void *p) | |||
1200 | if (mopts.malloc_junk && sz > 0) | 1200 | if (mopts.malloc_junk && sz > 0) |
1201 | memset(p, SOME_FREEJUNK, sz); | 1201 | memset(p, SOME_FREEJUNK, sz); |
1202 | if (!mopts.malloc_freenow) { | 1202 | if (!mopts.malloc_freenow) { |
1203 | i = getrnibble(); | 1203 | i = getrbyte() & MALLOC_DELAYED_CHUNK_MASK; |
1204 | tmp = p; | 1204 | tmp = p; |
1205 | p = g_pool->delayed_chunks[i]; | 1205 | p = g_pool->delayed_chunks[i]; |
1206 | g_pool->delayed_chunks[i] = tmp; | 1206 | g_pool->delayed_chunks[i] = tmp; |