summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordlg <>2017-06-19 03:06:26 +0000
committerdlg <>2017-06-19 03:06:26 +0000
commitf1f441c618072739c74120d8ff8f866e7244bd19 (patch)
tree419209ebdf4f7df90e5b9d1c7187fd778efc4503 /src
parent4c96d4ebbcd7cdce018cbbe6a0b245eda24c9737 (diff)
downloadopenbsd-f1f441c618072739c74120d8ff8f866e7244bd19.tar.gz
openbsd-f1f441c618072739c74120d8ff8f866e7244bd19.tar.bz2
openbsd-f1f441c618072739c74120d8ff8f866e7244bd19.zip
port the RBT code to userland by making it part of libc.
src/lib/libc/gen/tree.c is a copy of src/sys/kern/subr_tree.c, but with annotations for symbol visibility. changes to one should be reflected in the other. the malloc debug code that uses RB code is ported to RBT. because libc provides the RBT code, procmap doesn't have to reach into the kernel and build subr_tree.c itself now. mild enthusiasm from many ok guenther@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/malloc.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 999da6c1e9..eaa97f88bb 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.225 2017/05/13 07:11:29 otto Exp $ */ 1/* $OpenBSD: malloc.c,v 1.226 2017/06/19 03:06:26 dlg Exp $ */
2/* 2/*
3 * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net>
4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> 4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@@ -2085,18 +2085,19 @@ struct malloc_leak {
2085}; 2085};
2086 2086
2087struct leaknode { 2087struct leaknode {
2088 RB_ENTRY(leaknode) entry; 2088 RBT_ENTRY(leaknode) entry;
2089 struct malloc_leak d; 2089 struct malloc_leak d;
2090}; 2090};
2091 2091
2092static int 2092static inline int
2093leakcmp(struct leaknode *e1, struct leaknode *e2) 2093leakcmp(const struct leaknode *e1, const struct leaknode *e2)
2094{ 2094{
2095 return e1->d.f < e2->d.f ? -1 : e1->d.f > e2->d.f; 2095 return e1->d.f < e2->d.f ? -1 : e1->d.f > e2->d.f;
2096} 2096}
2097 2097
2098static RB_HEAD(leaktree, leaknode) leakhead; 2098static RBT_HEAD(leaktree, leaknode) leakhead;
2099RB_GENERATE_STATIC(leaktree, leaknode, entry, leakcmp) 2099RBT_PROTOTYPE(leaktree, leaknode, entry, leakcmp);
2100RBT_GENERATE(leaktree, leaknode, entry, leakcmp);
2100 2101
2101static void 2102static void
2102putleakinfo(void *f, size_t sz, int cnt) 2103putleakinfo(void *f, size_t sz, int cnt)
@@ -2109,7 +2110,7 @@ putleakinfo(void *f, size_t sz, int cnt)
2109 return; 2110 return;
2110 2111
2111 key.d.f = f; 2112 key.d.f = f;
2112 p = RB_FIND(leaktree, &leakhead, &key); 2113 p = RBT_FIND(leaktree, &leakhead, &key);
2113 if (p == NULL) { 2114 if (p == NULL) {
2114 if (page == NULL || 2115 if (page == NULL ||
2115 used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) { 2116 used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) {
@@ -2122,7 +2123,7 @@ putleakinfo(void *f, size_t sz, int cnt)
2122 p->d.f = f; 2123 p->d.f = f;
2123 p->d.total_size = sz * cnt; 2124 p->d.total_size = sz * cnt;
2124 p->d.count = cnt; 2125 p->d.count = cnt;
2125 RB_INSERT(leaktree, &leakhead, p); 2126 RBT_INSERT(leaktree, &leakhead, p);
2126 } else { 2127 } else {
2127 p->d.total_size += sz * cnt; 2128 p->d.total_size += sz * cnt;
2128 p->d.count += cnt; 2129 p->d.count += cnt;
@@ -2151,7 +2152,7 @@ dump_leaks(int fd)
2151 malloc_leaks = MMAP(MALLOC_PAGESIZE); 2152 malloc_leaks = MMAP(MALLOC_PAGESIZE);
2152 if (malloc_leaks != MAP_FAILED) 2153 if (malloc_leaks != MAP_FAILED)
2153 memset(malloc_leaks, 0, MALLOC_PAGESIZE); 2154 memset(malloc_leaks, 0, MALLOC_PAGESIZE);
2154 RB_FOREACH(p, leaktree, &leakhead) { 2155 RBT_FOREACH(p, leaktree, &leakhead) {
2155 snprintf(buf, sizeof(buf), "%18p %7zu %6u %6zu\n", p->d.f, 2156 snprintf(buf, sizeof(buf), "%18p %7zu %6u %6zu\n", p->d.f,
2156 p->d.total_size, p->d.count, p->d.total_size / p->d.count); 2157 p->d.total_size, p->d.count, p->d.total_size / p->d.count);
2157 write(fd, buf, strlen(buf)); 2158 write(fd, buf, strlen(buf));
@@ -2316,7 +2317,7 @@ malloc_dump(int fd, int poolno, struct dir_info *pool)
2316 pool->delayed_chunks[i] = NULL; 2317 pool->delayed_chunks[i] = NULL;
2317 } 2318 }
2318 /* XXX leak when run multiple times */ 2319 /* XXX leak when run multiple times */
2319 RB_INIT(&leakhead); 2320 RBT_INIT(leaktree, &leakhead);
2320 malloc_dump1(fd, poolno, pool); 2321 malloc_dump1(fd, poolno, pool);
2321 errno = saved_errno; 2322 errno = saved_errno;
2322} 2323}