2022-06-09 06:33:31 -07:00
|
|
|
/* clang-format off */
|
|
|
|
/* clang-format off */
|
|
|
|
/* alloc.c -- Default memory allocation routines.
|
|
|
|
|
|
|
|
(c) 1998-2006 (W3C) MIT, ERCIM, Keio University
|
|
|
|
See tidy.h for the copyright notice.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG_MEMORY very NOISY extra DEBUG of memory allocation, reallocation and free */
|
|
|
|
|
|
|
|
#include "third_party/tidy/tidy.h"
|
|
|
|
#include "third_party/tidy/forward.h"
|
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/assert.h"
|
Unbloat build config
- 10.5% reduction of o//depend dependency graph
- 8.8% reduction in latency of make command
- Fix issue with temporary file cleanup
There's a new -w option in compile.com that turns off the recent
Landlock output path workaround for "good commands" which do not
unlink() the output file like GNU tooling does.
Our new GNU Make unveil sandboxing appears to have zero overhead
in the grand scheme of things. Full builds are pretty fast since
the only thing that's actually slowed us down is probably libcxx
make -j16 MODE=rel
RL: took 85,732,063µs wall time
RL: ballooned to 323,612kb in size
RL: needed 828,560,521µs cpu (11% kernel)
RL: caused 39,080,670 page faults (99% memcpy)
RL: 350,073 context switches (72% consensual)
RL: performed 0 reads and 11,494,960 write i/o operations
pledge() and unveil() no longer consider ENOSYS to be an error.
These functions have also been added to Python's cosmo module.
This change also removes some WIN32 APIs and System Five magnums
which we're not using and it's doubtful anyone else would be too
2022-08-10 01:32:17 -07:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/mem/mem.h"
|
2022-06-09 06:33:31 -07:00
|
|
|
#include "third_party/tidy/sprtf.h"
|
|
|
|
|
|
|
|
static TidyMalloc g_malloc = NULL;
|
|
|
|
static TidyRealloc g_realloc = NULL;
|
|
|
|
static TidyFree g_free = NULL;
|
|
|
|
static TidyPanic g_panic = NULL;
|
|
|
|
|
|
|
|
#if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
|
|
|
|
static int alloccnt = 0;
|
|
|
|
static int realloccnt = 0;
|
|
|
|
static int freecnt = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
Bool tidySetMallocCall( TidyMalloc fmalloc )
|
|
|
|
{
|
|
|
|
g_malloc = fmalloc;
|
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
Bool tidySetReallocCall( TidyRealloc frealloc )
|
|
|
|
{
|
|
|
|
g_realloc = frealloc;
|
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
Bool tidySetFreeCall( TidyFree ffree )
|
|
|
|
{
|
|
|
|
g_free = ffree;
|
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
Bool tidySetPanicCall( TidyPanic fpanic )
|
|
|
|
{
|
|
|
|
g_panic = fpanic;
|
|
|
|
return yes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultPanic( TidyAllocator* ARG_UNUSED(allocator), ctmbstr msg )
|
|
|
|
{
|
|
|
|
if ( g_panic )
|
|
|
|
g_panic( msg );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* 2 signifies a serious error */
|
|
|
|
fprintf( stderr, "Fatal error: %s\n", msg );
|
|
|
|
#ifdef _DEBUG
|
|
|
|
assert(0);
|
|
|
|
#endif
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* defaultAlloc( TidyAllocator* allocator, size_t size )
|
|
|
|
{
|
|
|
|
void *p = ( g_malloc ? g_malloc(size) : malloc(size) );
|
|
|
|
if ( !p )
|
|
|
|
defaultPanic( allocator,"Out of memory!");
|
|
|
|
#if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
|
|
|
|
alloccnt++;
|
|
|
|
SPRTF("%d: alloc MEM %p, size %d\n", alloccnt, p, (int)size );
|
|
|
|
if (size == 0) {
|
|
|
|
SPRTF("NOTE: An allocation of ZERO bytes!!!!!!\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* defaultRealloc( TidyAllocator* allocator, void* mem, size_t newsize )
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
if ( mem == NULL )
|
|
|
|
return defaultAlloc( allocator, newsize );
|
|
|
|
|
|
|
|
p = ( g_realloc ? g_realloc(mem, newsize) : realloc(mem, newsize) );
|
|
|
|
if (!p)
|
|
|
|
defaultPanic( allocator, "Out of memory!");
|
|
|
|
#if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
|
|
|
|
realloccnt++;
|
|
|
|
SPRTF("%d: realloc MEM %p, size %d\n", realloccnt, p, (int)newsize );
|
|
|
|
#endif
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultFree( TidyAllocator* ARG_UNUSED(allocator), void* mem )
|
|
|
|
{
|
|
|
|
if ( mem )
|
|
|
|
{
|
|
|
|
#if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
|
|
|
|
freecnt++;
|
|
|
|
SPRTF("%d: free MEM %p\n", freecnt, mem );
|
|
|
|
#endif
|
|
|
|
if ( g_free )
|
|
|
|
g_free( mem );
|
|
|
|
else
|
|
|
|
free( mem );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TidyAllocatorVtbl defaultVtbl = {
|
|
|
|
defaultAlloc,
|
|
|
|
defaultRealloc,
|
|
|
|
defaultFree,
|
|
|
|
defaultPanic
|
|
|
|
};
|
|
|
|
|
|
|
|
TidyAllocator TY_(g_default_allocator) = {
|
|
|
|
&defaultVtbl
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* local variables:
|
|
|
|
* mode: c
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* end:
|
|
|
|
*/
|