Get LIBC_MEM and LIBC_STDIO building with aarch64

This commit is contained in:
Justine Tunney 2023-05-09 08:08:56 -07:00
parent ae0ee59614
commit d04430f4ef
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
81 changed files with 440 additions and 1064 deletions

View file

@ -27,7 +27,7 @@
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/hook/hook.internal.h"
#include "libc/mem/hook.internal.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,15 +16,21 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Frees and clears (sets to NULL) each non-null pointer in given array.
//
// This is twice as fast as freeing them one-by-one. If footers are
// used, pointers that have been allocated in different mspaces are
// not freed or cleared, and the count of all such pointers is returned.
// For large arrays of pointers with poor locality, it may be worthwhile
// to sort this array before calling bulk_free.
bulk_free:
jmp *hook_bulk_free(%rip)
.endfn bulk_free,globl
size_t (*hook_bulk_free)(void *[], size_t) = dlbulk_free;
/**
* Frees and clears (sets to NULL) each non-null pointer in given array.
*
* This is twice as fast as freeing them one-by-one. If footers are
* used, pointers that have been allocated in different mspaces are
* not freed or cleared, and the count of all such pointers is returned.
* For large arrays of pointers with poor locality, it may be worthwhile
* to sort this array before calling bulk_free.
*/
size_t bulk_free(void **p, size_t n) {
return hook_bulk_free(p, n);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,16 +16,22 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Allocates n * itemsize bytes, initialized to zero.
//
// @param rdi is number of items (n)
// @param rsi is size of each item (itemsize)
// @return rax is memory address, or NULL w/ errno
// @note overreliance on memalign is a sure way to fragment space
// @see dlcalloc()
// @threadsafe
calloc: jmp *hook_calloc(%rip)
.endfn calloc,globl
void *(*hook_calloc)(size_t, size_t) = dlcalloc;
/**
* Allocates n * itemsize bytes, initialized to zero.
*
* @param n is number of items
* @param itemsize is size of each item
* @return rax is memory address, or NULL w/ errno
* @note overreliance on memalign is a sure way to fragment space
* @see dlcalloc()
* @threadsafe
*/
void *calloc(size_t n, size_t itemsize) {
return hook_calloc(n, itemsize);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,18 +16,25 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Free memory returned by malloc() & co.
//
// Releases the chunk of memory pointed to by p, that had been
// previously allocated using malloc or a related routine such as
// realloc. It has no effect if p is null. If p was not malloced or
// already freed, free(p) will by default cause the current program to
// abort.
//
// @param rdi is allocation address, which may be NULL
// @see dlfree()
// @threadsafe
free: jmp *hook_free(%rip)
.endfn free,globl
void (*hook_free)(void *) = dlfree;
/**
* Free memory returned by malloc() & co.
*
* Releases the chunk of memory pointed to by p, that had been
* previously allocated using malloc or a related routine such as
* realloc. It has no effect if p is null. If p was not malloced or
* already freed, free(p) will by default cause the current program to
* abort.
*
* @param p is allocation address, which may be NULL
* @see dlfree()
* @threadsafe
*/
void free(void *p) {
hook_free(p);
}

View file

@ -25,6 +25,8 @@
#include "libc/str/str.h"
#include "libc/thread/tls.h"
#ifdef __x86_64__
forceinline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
struct StackFrame *parent,
void *ptr) {
@ -144,3 +146,5 @@ void *(_defer)(void *fn, void *arg) {
DeferFunction(frame->next, fn, arg);
return arg;
}
#endif /* __x86_64__ */

View file

@ -1,5 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_MEM_HOOK_HOOK_H_
#define COSMOPOLITAN_LIBC_MEM_HOOK_HOOK_H_
#ifndef COSMOPOLITAN_LIBC_MEM_HOOK_H_
#define COSMOPOLITAN_LIBC_MEM_HOOK_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -10,9 +10,9 @@ extern void *(*hook_memalign)(size_t, size_t);
extern void *(*hook_realloc)(void *, size_t);
extern void *(*hook_realloc_in_place)(void *, size_t);
extern int (*hook_malloc_trim)(size_t);
extern size_t (*hook_malloc_usable_size)(const void *);
extern size_t (*hook_malloc_usable_size)(void *);
extern size_t (*hook_bulk_free)(void *[], size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_MEM_HOOK_HOOK_H_ */
#endif /* COSMOPOLITAN_LIBC_MEM_HOOK_H_ */

View file

@ -1,31 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_bulk_free
hook_bulk_free:
.quad 0
.endobj hook_bulk_free,globl,hidden
.previous
.init.start 202,_init_bulk_free
.hidden dlbulk_free
ezlea dlbulk_free,ax
stosq
.init.end 202,_init_bulk_free

View file

@ -1,33 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_free
hook_free:
.quad 0
.endobj hook_free,globl,hidden
.previous
.init.start 202,_init_free
ezlea dlfree,ax
stosq
.yoink realloc
.init.end 202,_init_free
.hidden dlfree

View file

@ -1,32 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_malloc
hook_malloc:
.quad 0
.endobj hook_malloc,globl,hidden
.previous
.init.start 202,_init_malloc
.hidden dlmalloc
ezlea dlmalloc,ax
stosq
.yoink free
.init.end 202,_init_malloc

View file

@ -1,32 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_malloc_trim
hook_malloc_trim:
.quad 0
.endobj hook_malloc_trim,globl,hidden
.previous
.init.start 202,_init_malloc_trim
.hidden dlmalloc_trim
ezlea dlmalloc_trim,ax
stosq
.yoink free
.init.end 202,_init_malloc_trim

View file

@ -1,31 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_malloc_usable_size
hook_malloc_usable_size:
.quad 0
.endobj hook_malloc_usable_size,globl,hidden
.previous
.init.start 202,_init_malloc_usable_size
.hidden dlmalloc_usable_size
ezlea dlmalloc_usable_size,ax
stosq
.init.end 202,_init_malloc_usable_size

View file

@ -1,32 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_memalign
hook_memalign:
.quad 0
.endobj hook_memalign,globl,hidden
.previous
.init.start 202,_init_memalign
.hidden dlmemalign
ezlea dlmemalign,ax
stosq
.yoink free
.init.end 202,_init_memalign

View file

@ -1,32 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_realloc
hook_realloc:
.quad 0
.endobj hook_realloc,globl,hidden
.previous
.init.start 202,_init_realloc
.hidden dlrealloc
ezlea dlrealloc,ax
stosq
.yoink free
.init.end 202,_init_realloc

View file

@ -1,32 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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/macros.internal.h"
.initbss 202,_init_realloc_in_place
hook_realloc_in_place:
.quad 0
.endobj hook_realloc_in_place,globl,hidden
.previous
.init.start 202,_init_realloc_in_place
.hidden dlrealloc_in_place
ezlea dlrealloc_in_place,ax
stosq
.yoink free
.init.end 202,_init_realloc_in_place

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,23 +16,28 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Resizes the space allocated for p to size n, only if this can be
// done without moving p (i.e., only if there is adjacent space
// available if n is greater than p's current allocated size, or n
// is less than or equal to p's size). This may be used instead of
// plain realloc if an alternative allocation strategy is needed
// upon failure to expand space, for example, reallocation of a
// buffer that must be memory-aligned or cleared. You can use
// realloc_in_place to trigger these alternatives only when needed.
//
// @param rdi (p) is address of current allocation
// @param rsi (newsize) is number of bytes needed
// @return rax is result, or NULL w/ errno
// @see dlrealloc_in_place()
// @threadsafe
realloc_in_place:
jmp *hook_realloc_in_place(%rip)
.endfn realloc_in_place,globl
void *(*hook_realloc_in_place)(void *, size_t) = dlrealloc_in_place;
/**
* Resizes the space allocated for p to size n, only if this can be
* done without moving p (i.e., only if there is adjacent space
* available if n is greater than p's current allocated size, or n
* is less than or equal to p's size). This may be used instead of
* plain realloc if an alternative allocation strategy is needed
* upon failure to expand space, for example, reallocation of a
* buffer that must be memory-aligned or cleared. You can use
* realloc_in_place to trigger these alternatives only when needed.
*
* @param p is address of current allocation
* @param n is number of bytes needed
* @return rax is result, or NULL w/ errno
* @see dlrealloc_in_place()
* @threadsafe
*/
void *realloc_in_place(void *p, size_t n) {
return hook_realloc_in_place(p, n);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,23 +16,30 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Allocates uninitialized memory.
//
// Returns a pointer to a newly allocated chunk of at least n bytes, or
// null if no space is available, in which case errno is set to ENOMEM
// on ANSI C systems.
//
// If n is zero, malloc returns a minimum-sized chunk. (The minimum size
// is 32 bytes on 64bit systems.) Note that size_t is an unsigned type,
// so calls with arguments that would be negative if signed are
// interpreted as requests for huge amounts of space, which will often
// fail. The maximum supported value of n differs across systems, but is
// in all cases less than the maximum representable value of a size_t.
//
// @param rdi is number of bytes needed, coerced to 1+
// @return new memory, or NULL w/ errno
// @threadsafe
malloc: jmp *hook_malloc(%rip)
.endfn malloc,globl
void *(*hook_malloc)(size_t) = dlmalloc;
/**
* Allocates uninitialized memory.
*
* Returns a pointer to a newly allocated chunk of at least n bytes, or
* null if no space is available, in which case errno is set to ENOMEM
* on ANSI C systems.
*
* If n is zero, malloc returns a minimum-sized chunk. (The minimum size
* is 32 bytes on 64bit systems.) Note that size_t is an unsigned type,
* so calls with arguments that would be negative if signed are
* interpreted as requests for huge amounts of space, which will often
* fail. The maximum supported value of n differs across systems, but is
* in all cases less than the maximum representable value of a size_t.
*
* @param rdi is number of bytes needed, coerced to 1+
* @return new memory, or NULL w/ errno
* @threadsafe
*/
void *malloc(size_t n) {
return hook_malloc(n);
}

View file

@ -1,27 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.internal.h"
// Releases freed memory back to system.
//
// @param rdi specifies bytes of memory to leave available
// @return 1 if it actually released any memory, else 0
malloc_trim:
jmp *hook_malloc_trim(%rip)
.endfn malloc_trim,globl

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,17 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
.initbss 202,_init_calloc
hook_calloc:
.quad 0
.endobj hook_calloc,globl,hidden
.previous
int (*hook_malloc_trim)(size_t) = dlmalloc_trim;
.init.start 202,_init_calloc
.hidden dlcalloc
ezlea dlcalloc,ax
stosq
.yoink free
.init.end 202,_init_calloc
/**
* Releases freed memory back to system.
*
* @param n specifies bytes of memory to leave available
* @return 1 if it actually released any memory, else 0
*/
int malloc_trim(size_t n) {
return hook_malloc_trim(n);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,26 +16,31 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Returns the number of bytes you can actually use in
// an allocated chunk, which may be more than you requested
// (although often not) due to alignment and minimum size
// constraints.
//
// You can use this many bytes without worrying about overwriting
// other allocated objects. This is not a particularly great
// programming practice. malloc_usable_size can be more useful in
// debugging and assertions, for example:
//
// p = malloc(n)
// assert(malloc_usable_size(p) >= 256)
//
// @param rdi is address of allocation
// @return rax is total number of bytes
// @see dlmalloc_usable_size()
// @threadsafe
malloc_usable_size:
jmp *hook_malloc_usable_size(%rip)
.endfn malloc_usable_size,globl
size_t (*hook_malloc_usable_size)(void *) = dlmalloc_usable_size;
/**
* Returns the number of bytes you can actually use in
* an allocated chunk, which may be more than you requested
* (although often not) due to alignment and minimum size
* constraints.
*
* You can use this many bytes without worrying about overwriting
* other allocated objects. This is not a particularly great
* programming practice. malloc_usable_size can be more useful in
* debugging and assertions, for example:
*
* p = malloc(n)
* assert(malloc_usable_size(p) >= 256)
*
* @param p is address of allocation
* @return total number of bytes
* @see dlmalloc_usable_size()
* @threadsafe
*/
size_t malloc_usable_size(void *p) {
return hook_malloc_usable_size(p);
}

View file

@ -7,21 +7,12 @@ LIBC_MEM_ARTIFACTS += LIBC_MEM_A
LIBC_MEM = $(LIBC_MEM_A_DEPS) $(LIBC_MEM_A)
LIBC_MEM_A = o/$(MODE)/libc/mem/mem.a
LIBC_MEM_A_HDRS = $(filter %.h,$(LIBC_MEM_A_FILES))
LIBC_MEM_A_SRCS_S = $(filter %.S,$(LIBC_MEM_A_FILES))
LIBC_MEM_A_SRCS_C = $(filter %.c,$(LIBC_MEM_A_FILES))
LIBC_MEM_A_SRCS = $(filter %.c,$(LIBC_MEM_A_FILES))
LIBC_MEM_A_OBJS = $(LIBC_MEM_A_SRCS:%.c=o/$(MODE)/%.o)
LIBC_MEM_A_FILES := \
$(wildcard libc/mem/*) \
$(wildcard libc/mem/cxx/*) \
$(wildcard libc/mem/hook/*)
LIBC_MEM_A_SRCS = \
$(LIBC_MEM_A_SRCS_S) \
$(LIBC_MEM_A_SRCS_C)
LIBC_MEM_A_OBJS = \
$(LIBC_MEM_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_MEM_A_SRCS_C:%.c=o/$(MODE)/%.o)
$(wildcard libc/mem/cxx/*)
LIBC_MEM_A_CHECKS = \
$(LIBC_MEM_A).pkg \

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,21 +16,26 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Allocates aligned memory.
//
// Returns a pointer to a newly allocated chunk of n bytes, aligned in
// accord with the alignment argument. The alignment argument shall be
// rounded up to the nearest two power and higher 2 powers may be used
// if the allocator imposes a minimum alignment requirement.
//
// @param rdi is alignment in bytes, coerced to 1+ w/ 2-power roundup
// @param rsi is number of bytes needed, coerced to 1+
// @return rax is memory address, or NULL w/ errno
// @see valloc(), pvalloc()
// @threadsafe
memalign:
jmp *hook_memalign(%rip)
.endfn memalign,globl
void *(*hook_memalign)(size_t, size_t) = dlmemalign;
/**
* Allocates aligned memory.
*
* Returns a pointer to a newly allocated chunk of n bytes, aligned in
* accord with the alignment argument. The alignment argument shall be
* rounded up to the nearest two power and higher 2 powers may be used
* if the allocator imposes a minimum alignment requirement.
*
* @param align is alignment in bytes, coerced to 1+ w/ 2-power roundup
* @param bytes is number of bytes needed, coerced to 1+
* @return rax is memory address, or NULL w/ errno
* @see valloc(), pvalloc()
* @threadsafe
*/
void *memalign(size_t align, size_t bytes) {
return hook_memalign(align, bytes);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,44 +16,49 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
// Allocates / resizes / frees memory, e.g.
//
// Returns a pointer to a chunk of size n that contains the same data as
// does chunk p up to the minimum of (n, p's size) bytes, or null if no
// space is available.
//
// If p is NULL, realloc is equivalent to malloc.
// If p is not NULL and n is 0, realloc is equivalent to free.
//
// The returned pointer may or may not be the same as p. The algorithm
// prefers extending p in most cases when possible, otherwise it employs
// the equivalent of a malloc-copy-free sequence.
//
// Please note that p is NOT free()'d should realloc() fail, thus:
//
// if ((p2 = realloc(p, n2))) {
// p = p2;
// ...
// } else {
// ...
// }
//
// if n is for fewer bytes than already held by p, the newly unused
// space is lopped off and freed if possible.
//
// The old unix realloc convention of allowing the last-free'd chunk to
// be used as an argument to realloc is not supported.
//
// @param rdi (p) is address of current allocation or NULL
// @param rsi (n) is number of bytes needed
// @return rax is result, or NULL w/ errno w/o free(p)
// @note realloc(p=0, n=0) → malloc(32)
// @note realloc(p≠0, n=0) → free(p)
// @see dlrealloc()
// @threadsafe
realloc:
jmp *hook_realloc(%rip)
.endfn realloc,globl
void *(*hook_realloc)(void *, size_t) = dlrealloc;
/**
* Allocates / resizes / frees memory, e.g.
*
* Returns a pointer to a chunk of size n that contains the same data as
* does chunk p up to the minimum of (n, p's size) bytes, or null if no
* space is available.
*
* If p is NULL, realloc is equivalent to malloc.
* If p is not NULL and n is 0, realloc is equivalent to free.
*
* The returned pointer may or may not be the same as p. The algorithm
* prefers extending p in most cases when possible, otherwise it employs
* the equivalent of a malloc-copy-free sequence.
*
* Please note that p is NOT free()'d should realloc() fail, thus:
*
* if ((p2 = realloc(p, n2))) {
* p = p2;
* ...
* } else {
* ...
* }
*
* if n is for fewer bytes than already held by p, the newly unused
* space is lopped off and freed if possible.
*
* The old unix realloc convention of allowing the last-free'd chunk to
* be used as an argument to realloc is not supported.
*
* @param p is address of current allocation or NULL
* @param n is number of bytes needed
* @return rax is result, or NULL w/ errno w/o free(p)
* @note realloc(p=0, n=0) malloc(32)
* @note realloc(p0, n=0) free(p)
* @see dlrealloc()
* @threadsafe
*/
void *realloc(void *p, size_t n) {
return hook_realloc(p, n);
}