diff options
author | otto <> | 2023-12-19 06:59:28 +0000 |
---|---|---|
committer | otto <> | 2023-12-19 06:59:28 +0000 |
commit | dce6821928859539be40a9a42b4c7cc1a7c9bb46 (patch) | |
tree | 92b2586938285ed23e1b2b3d8d38d0fcc54dcef0 | |
parent | b94f27252e5d924442c45f66956492249d0a6d26 (diff) | |
download | openbsd-dce6821928859539be40a9a42b4c7cc1a7c9bb46.tar.gz openbsd-dce6821928859539be40a9a42b4c7cc1a7c9bb46.tar.bz2 openbsd-dce6821928859539be40a9a42b4c7cc1a7c9bb46.zip |
A small cleanup of malloc_bytes(), getting rid of a goto and a tiny
bit of optimization; ok tb@ asou@
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 56 |
1 files changed, 27 insertions, 29 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 7c226657df..b5eb212227 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.294 2023/12/04 07:01:45 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.295 2023/12/19 06:59:28 otto Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -288,6 +288,7 @@ caller(struct dir_info *d) | |||
288 | { | 288 | { |
289 | struct btnode p; | 289 | struct btnode p; |
290 | int level = DO_STATS; | 290 | int level = DO_STATS; |
291 | |||
291 | if (level == 0) | 292 | if (level == 0) |
292 | return NULL; | 293 | return NULL; |
293 | 294 | ||
@@ -1165,8 +1166,7 @@ fill_canary(char *ptr, size_t sz, size_t allocated) | |||
1165 | static void * | 1166 | static void * |
1166 | malloc_bytes(struct dir_info *d, size_t size) | 1167 | malloc_bytes(struct dir_info *d, size_t size) |
1167 | { | 1168 | { |
1168 | u_int i, k, r, bucket, listnum; | 1169 | u_int i, j, k, r, bucket, listnum; |
1169 | int j; | ||
1170 | u_short *lp; | 1170 | u_short *lp; |
1171 | struct chunk_info *bp; | 1171 | struct chunk_info *bp; |
1172 | void *p; | 1172 | void *p; |
@@ -1177,7 +1177,7 @@ malloc_bytes(struct dir_info *d, size_t size) | |||
1177 | 1177 | ||
1178 | bucket = find_bucket(size); | 1178 | bucket = find_bucket(size); |
1179 | 1179 | ||
1180 | r = ((u_int)getrbyte(d) << 8) | getrbyte(d); | 1180 | r = getrbyte(d); |
1181 | listnum = r % MALLOC_CHUNK_LISTS; | 1181 | listnum = r % MALLOC_CHUNK_LISTS; |
1182 | 1182 | ||
1183 | /* If it's empty, make a page more of that size chunks */ | 1183 | /* If it's empty, make a page more of that size chunks */ |
@@ -1190,39 +1190,39 @@ malloc_bytes(struct dir_info *d, size_t size) | |||
1190 | if (bp->canary != (u_short)d->canary1 || bucket != bp->bucket) | 1190 | if (bp->canary != (u_short)d->canary1 || bucket != bp->bucket) |
1191 | wrterror(d, "chunk info corrupted"); | 1191 | wrterror(d, "chunk info corrupted"); |
1192 | 1192 | ||
1193 | r /= MALLOC_CHUNK_LISTS; | ||
1194 | /* do we need more random bits? */ | ||
1195 | if (bp->total > 256 / MALLOC_CHUNK_LISTS) | ||
1196 | r = r << 8 | getrbyte(d); | ||
1193 | /* bias, as bp->total is not a power of 2 */ | 1197 | /* bias, as bp->total is not a power of 2 */ |
1194 | i = (r / MALLOC_CHUNK_LISTS) % bp->total; | 1198 | i = r % bp->total; |
1195 | 1199 | ||
1196 | /* potentially start somewhere in a short */ | 1200 | j = i % MALLOC_BITS; |
1197 | lp = &bp->bits[i / MALLOC_BITS]; | ||
1198 | j = i % MALLOC_BITS; /* j must be signed */ | ||
1199 | if (*lp >> j) { | ||
1200 | k = ffs(*lp >> j); | ||
1201 | if (k != 0) { | ||
1202 | k += j - 1; | ||
1203 | goto found; | ||
1204 | } | ||
1205 | } | ||
1206 | /* no bit halfway, go to next full short */ | ||
1207 | i /= MALLOC_BITS; | 1201 | i /= MALLOC_BITS; |
1208 | for (;;) { | 1202 | lp = &bp->bits[i]; |
1209 | if (++i >= bp->offset) | 1203 | /* potentially start somewhere in a short */ |
1210 | i = 0; | 1204 | if (j > 0 && *lp >> j) |
1211 | lp = &bp->bits[i]; | 1205 | k = ffs(*lp >> j) + j; |
1212 | if (*lp) { | 1206 | else { |
1213 | k = ffs(*lp) - 1; | 1207 | /* no bit halfway, go to next full short */ |
1214 | break; | 1208 | for (;;) { |
1209 | if (*lp) { | ||
1210 | k = ffs(*lp); | ||
1211 | break; | ||
1212 | } | ||
1213 | if (++i >= bp->offset) | ||
1214 | i = 0; | ||
1215 | lp = &bp->bits[i]; | ||
1215 | } | 1216 | } |
1216 | } | 1217 | } |
1217 | found: | 1218 | *lp ^= 1 << --k; |
1218 | *lp ^= 1 << k; | ||
1219 | 1219 | ||
1220 | /* If there are no more free, remove from free-list */ | 1220 | /* If there are no more free, remove from free-list */ |
1221 | if (--bp->free == 0) | 1221 | if (--bp->free == 0) |
1222 | LIST_REMOVE(bp, entries); | 1222 | LIST_REMOVE(bp, entries); |
1223 | 1223 | ||
1224 | /* Adjust to the real offset of that chunk */ | 1224 | /* Adjust to the real offset of that chunk */ |
1225 | k += (lp - bp->bits) * MALLOC_BITS; | 1225 | k += i * MALLOC_BITS; |
1226 | 1226 | ||
1227 | if (mopts.chunk_canaries && size > 0) | 1227 | if (mopts.chunk_canaries && size > 0) |
1228 | bp->bits[bp->offset + k] = size; | 1228 | bp->bits[bp->offset + k] = size; |
@@ -1232,9 +1232,7 @@ found: | |||
1232 | STATS_SETFN(r, k, d->caller); | 1232 | STATS_SETFN(r, k, d->caller); |
1233 | } | 1233 | } |
1234 | 1234 | ||
1235 | k *= B2ALLOC(bucket); | 1235 | p = (char *)bp->page + k * B2ALLOC(bucket); |
1236 | |||
1237 | p = (char *)bp->page + k; | ||
1238 | if (bucket > 0) { | 1236 | if (bucket > 0) { |
1239 | validate_junk(d, p, B2SIZE(bucket)); | 1237 | validate_junk(d, p, B2SIZE(bucket)); |
1240 | if (mopts.chunk_canaries) | 1238 | if (mopts.chunk_canaries) |