mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 02:38:31 +00:00
Introduce libc/mem/tinymalloc.inc
This allocator shaves ~20kb off single-threaded tool programs and is slightly faster than proper malloc for simple non-demanding programs
This commit is contained in:
parent
3bf95ae7ec
commit
a6ecbb747d
17 changed files with 201 additions and 37 deletions
|
@ -259,6 +259,8 @@ static Elf64_Xword notesize;
|
|||
|
||||
static char *r_off32_e_lfanew;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static wontreturn void Die(const char *thing, const char *reason) {
|
||||
tinyprint(2, thing, ": ", reason, "\n", NULL);
|
||||
exit(1);
|
||||
|
|
|
@ -67,6 +67,8 @@
|
|||
#define FORMAT_MACHO 2
|
||||
#define FORMAT_PE 3
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static int g_arch;
|
||||
static int g_format;
|
||||
static bool g_force;
|
||||
|
|
|
@ -226,6 +226,8 @@ const char *const kSafeEnv[] = {
|
|||
"SYSTEMROOT", // needed by socket()
|
||||
};
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
void OnAlrm(int sig) {
|
||||
++gotalrm;
|
||||
}
|
||||
|
|
|
@ -69,6 +69,8 @@ char linkbuf[PATH_MAX];
|
|||
|
||||
void Cp(char *, char *);
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
bool IsDirectory(const char *path) {
|
||||
int e;
|
||||
bool res;
|
||||
|
|
|
@ -158,6 +158,8 @@ static const char *stubpath;
|
|||
static long FLAG_SizeOfStackCommit = 64 * 1024;
|
||||
static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static wontreturn void Die(const char *thing, const char *reason) {
|
||||
tinyprint(2, thing, ": ", reason, "\n", NULL);
|
||||
exit(1);
|
||||
|
|
|
@ -67,6 +67,8 @@ static Elf64_Ehdr *elf;
|
|||
static const char *epath;
|
||||
static Elf64_Xword symcount;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static wontreturn void Die(const char *reason) {
|
||||
tinyprint(2, epath, ": ", reason, "\n", NULL);
|
||||
exit(1);
|
||||
|
|
|
@ -71,6 +71,8 @@ const char *prog;
|
|||
char databuf[32768];
|
||||
char pathbuf[PATH_MAX];
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
wontreturn void PrintUsage(int rc, FILE *f) {
|
||||
fputs("usage: ", f);
|
||||
fputs(prog, f);
|
||||
|
|
|
@ -50,6 +50,8 @@ static const char *prog;
|
|||
static char16_t **filters;
|
||||
static uint32_t pids[10000];
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static wontreturn void PrintUsage(int rc, FILE *f) {
|
||||
fprintf(f,
|
||||
"Usage: %s [-nshv] NAME...\n"
|
||||
|
|
|
@ -146,6 +146,8 @@ static const char *buildroot;
|
|||
static const char *genroot;
|
||||
static const char *outpath;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static inline bool IsBlank(int c) {
|
||||
return c == ' ' || c == '\t';
|
||||
}
|
||||
|
|
|
@ -62,6 +62,8 @@ char linkbuf[PATH_MAX];
|
|||
|
||||
void Mv(char *, char *);
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
wontreturn void Die(const char *path, const char *reason) {
|
||||
tinyprint(2, path, ": ", reason, "\n", NULL);
|
||||
exit(1);
|
||||
|
|
|
@ -43,8 +43,6 @@
|
|||
#include "third_party/xed/x86.h"
|
||||
#include "tool/build/lib/getargs.h"
|
||||
|
||||
__static_yoink("realloc");
|
||||
|
||||
/**
|
||||
* @fileoverview Build Package Script.
|
||||
*
|
||||
|
@ -153,6 +151,8 @@ struct Relas {
|
|||
} *p;
|
||||
} prtu;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static wontreturn void Die(const char *path, const char *reason) {
|
||||
tinyprint(2, path, ": ", reason, "\n", NULL);
|
||||
exit(1);
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/nt/struct/imageimportbyname.internal.h"
|
||||
#include "libc/nt/struct/imageimportdescriptor.internal.h"
|
||||
|
@ -25,6 +24,7 @@
|
|||
#include "libc/nt/struct/imageoptionalheader.internal.h"
|
||||
#include "libc/nt/struct/imagesectionheader.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/elf/struct/shdr.h"
|
||||
#include "libc/elf/struct/sym.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/stdio/sysparam.h"
|
||||
|
@ -32,6 +33,8 @@ const char *FLAG_prefix;
|
|||
const char *FLAG_suffix;
|
||||
const char *path;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
wontreturn void PrintUsage(int fd, int exitcode) {
|
||||
tinyprint(fd, "\n\
|
||||
NAME\n\
|
||||
|
@ -74,42 +77,17 @@ wontreturn void DieOom(void) {
|
|||
Die("out of memory");
|
||||
}
|
||||
|
||||
struct {
|
||||
char *last;
|
||||
size_t used;
|
||||
union {
|
||||
char memory[1024 * 1024 * 1024];
|
||||
size_t align;
|
||||
};
|
||||
} heap;
|
||||
|
||||
void *Malloc(size_t need) {
|
||||
if (need <= sizeof(heap.memory)) {
|
||||
int align = sizeof(size_t);
|
||||
size_t base = heap.used;
|
||||
base += align - 1;
|
||||
base &= -align;
|
||||
size_t toto = base + sizeof(size_t) + need;
|
||||
if (toto >= heap.used && toto <= sizeof(heap.memory)) {
|
||||
char *res = heap.memory + base;
|
||||
*(size_t *)res = need;
|
||||
heap.used = toto;
|
||||
return res + sizeof(size_t);
|
||||
}
|
||||
}
|
||||
DieOom();
|
||||
static void *Malloc(size_t n) {
|
||||
void *p;
|
||||
if (!(p = malloc(n)))
|
||||
DieOom();
|
||||
return p;
|
||||
}
|
||||
|
||||
void *Realloc(void *ptr, size_t need) {
|
||||
if (ptr == heap.last) {
|
||||
heap.used = (char *)ptr - heap.memory;
|
||||
return Malloc(need);
|
||||
} else {
|
||||
void *res = Malloc(need);
|
||||
size_t size = *(size_t *)((char *)ptr - sizeof(size_t));
|
||||
memcpy(res, ptr, MIN(need, size));
|
||||
return res;
|
||||
}
|
||||
static void *Realloc(void *p, size_t n) {
|
||||
if (!(p = realloc(p, n)))
|
||||
DieOom();
|
||||
return p;
|
||||
}
|
||||
|
||||
void ProcessFile(void) {
|
||||
|
|
|
@ -48,6 +48,8 @@ static bool recursive;
|
|||
static bool doemptydirs;
|
||||
static const char *prog;
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
static wontreturn void PrintUsage(int rc, int fd) {
|
||||
tinyprint(fd, "USAGE\n\n ", prog, USAGE, NULL);
|
||||
exit(rc);
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
* @fileoverview elf to symbol table file dump tool
|
||||
*/
|
||||
|
||||
#include "libc/mem/tinymalloc.inc"
|
||||
|
||||
void PrintUsage(FILE *f) {
|
||||
fprintf(f, "%s%s%s\n", "usage: ", program_invocation_name,
|
||||
" [-?h] -o PATH COMDBG");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue