mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-20 09:30:31 +00:00
Introduce ctl::set and ctl::map
We now have a C++ red-black tree implementation that implements standard template library compatible APIs while compiling 10x faster than libcxx. It's not as beautiful as the red-black tree implementation in Plinko but this will get the job done and the test proves it upholds all invariants This change also restores CheckForMemoryLeaks() support and fixes a real actual bug I discovered with Doug Lea's dlmalloc_inspect_all() function.
This commit is contained in:
parent
388e236360
commit
c4c812c154
45 changed files with 2358 additions and 135 deletions
94
libc/mem/leaks.c
Normal file
94
libc/mem/leaks.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/cxxabi.h"
|
||||
#include "libc/intrin/cxaatexit.internal.h"
|
||||
#include "libc/intrin/dll.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/typedef/imagetlscallback.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
#define LEAK_CONTAINER(e) DLL_CONTAINER(struct Leak, elem, e)
|
||||
|
||||
struct Leak {
|
||||
void *alloc;
|
||||
struct Dll elem;
|
||||
};
|
||||
|
||||
static int leak_count;
|
||||
static struct Dll *leaks;
|
||||
static struct Dll *freaks;
|
||||
static pthread_mutex_t lock;
|
||||
|
||||
void __may_leak(void *alloc) {
|
||||
if (!alloc)
|
||||
return;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (dll_is_empty(freaks)) {
|
||||
int g = __granularity();
|
||||
struct Leak *p = _mapanon(g);
|
||||
int n = g / sizeof(struct Leak);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
dll_init(&p[i].elem);
|
||||
dll_make_first(&freaks, &p[i].elem);
|
||||
}
|
||||
}
|
||||
struct Dll *e = dll_first(freaks);
|
||||
LEAK_CONTAINER(e)->alloc = alloc;
|
||||
dll_remove(&freaks, e);
|
||||
dll_make_first(&leaks, e);
|
||||
pthread_mutex_unlock(&lock);
|
||||
}
|
||||
|
||||
static void visitor(void *start, void *end, size_t used_bytes, void *arg) {
|
||||
if (!used_bytes)
|
||||
return;
|
||||
for (struct Dll *e = dll_first(leaks); e; e = dll_next(leaks, e))
|
||||
if (start == LEAK_CONTAINER(e)->alloc)
|
||||
return;
|
||||
kprintf("error: leaked %'zu byte allocation at %p\n", used_bytes, start);
|
||||
++leak_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for memory leaks.
|
||||
*/
|
||||
void CheckForMemoryLeaks(void) {
|
||||
|
||||
// validate usage of this api
|
||||
if (_weaken(_pthread_decimate))
|
||||
_weaken(_pthread_decimate)();
|
||||
if (!pthread_orphan_np())
|
||||
kprintf("warning: called CheckForMemoryLeaks() from non-orphaned thread\n");
|
||||
|
||||
// call dynamic global destructors
|
||||
__cxa_thread_finalize();
|
||||
__cxa_finalize(0);
|
||||
|
||||
// check for leaks
|
||||
malloc_inspect_all(visitor, 0);
|
||||
if (leak_count) {
|
||||
kprintf("loser: you forgot to call free %'d time%s\n", leak_count,
|
||||
leak_count == 1 ? "" : "s");
|
||||
_exit(73);
|
||||
}
|
||||
}
|
22
libc/mem/leaks.h
Normal file
22
libc/mem/leaks.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_MEM_LEAKS_H_
|
||||
#define COSMOPOLITAN_LIBC_MEM_LEAKS_H_
|
||||
#include "libc/intrin/weaken.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
void CheckForMemoryLeaks(void) libcesque;
|
||||
|
||||
/**
|
||||
* Declares that allocation needn't be freed.
|
||||
*
|
||||
* This function does nothing if CheckForMemoryLeaks() hasn't been
|
||||
* linked into the binary.
|
||||
*/
|
||||
forceinline void *may_leak(void *__p) {
|
||||
void __may_leak(void *) libcesque;
|
||||
if (_weaken(__may_leak))
|
||||
_weaken(__may_leak)(__p);
|
||||
return __p;
|
||||
}
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_LIBC_MEM_LEAKS_H_ */
|
|
@ -17,10 +17,10 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/getenv.internal.h"
|
||||
#include "libc/intrin/leaky.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/internal.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
@ -42,7 +42,7 @@ static char **__growenv(char **a) {
|
|||
a = environ;
|
||||
n = a ? __lenenv(a) : 0;
|
||||
c = MAX(8ul, n) << 1;
|
||||
if ((b = malloc(c * sizeof(char *)))) {
|
||||
if ((b = may_leak(malloc(c * sizeof(char *))))) {
|
||||
if (a) {
|
||||
for (p = b; *a;) {
|
||||
*p++ = *a++;
|
||||
|
@ -59,8 +59,6 @@ static char **__growenv(char **a) {
|
|||
}
|
||||
}
|
||||
|
||||
IGNORE_LEAKS(__growenv)
|
||||
|
||||
int __putenv(char *s, bool overwrite) {
|
||||
char **p;
|
||||
struct Env e;
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/leaky.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/mem/internal.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -39,7 +39,8 @@ int setenv(const char *name, const char *value, int overwrite) {
|
|||
size_t n, m;
|
||||
if (!name || !*name || !value || strchr(name, '='))
|
||||
return einval();
|
||||
if ((s = malloc((n = strlen(name)) + 1 + (m = strlen(value)) + 1))) {
|
||||
if ((s = may_leak(
|
||||
malloc((n = strlen(name)) + 1 + (m = strlen(value)) + 1)))) {
|
||||
memcpy(mempcpy(mempcpy(s, name, n), "=", 1), value, m + 1);
|
||||
rc = __putenv(s, overwrite);
|
||||
} else {
|
||||
|
@ -48,5 +49,3 @@ int setenv(const char *name, const char *value, int overwrite) {
|
|||
STRACE("setenv(%#s, %#s, %hhhd) → %d% m", name, value, overwrite, rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
IGNORE_LEAKS(setenv)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue