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

@ -90,7 +90,8 @@ int ttyident(struct TtyIdent *ti, int ttyinfd, int ttyoutfd) {
: _weaken(malloc)(sizeof(struct TtyIdent))))) {
memcpy(ti->next, &outer, sizeof(outer));
} else {
free_s(&ti->next);
free(ti->next);
ti->next = 0;
}
}
ttyrestore(ttyinfd, &old);

View file

@ -26,7 +26,7 @@ static void ttyidentfree(struct TtyIdent *ti) {
if (ti) {
assert(ti != ti->next);
ttyidentfree(ti->next);
free_s(&ti);
free(ti);
}
}

View file

@ -26,7 +26,6 @@
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/lockcmpxchgp.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/sysv/errfuns.h"

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dns/hoststxt.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
/**
@ -24,8 +25,9 @@
*/
void FreeHostsTxt(struct HostsTxt **ht) {
if (*ht) {
free_s(&(*ht)->entries.p);
free_s(&(*ht)->strings.p);
free_s(ht);
free((*ht)->entries.p);
free((*ht)->strings.p);
free(*ht);
*ht = 0;
}
}

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dns/resolvconf.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
/**
@ -24,7 +25,8 @@
*/
void FreeResolvConf(struct ResolvConf **rvp) {
if (*rvp) {
free_s(&(*rvp)->nameservers.p);
free_s(rvp);
free((*rvp)->nameservers.p);
free(*rvp);
*rvp = 0;
}
}

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef __x86_64__
#include "libc/calls/state.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
@ -33,7 +32,7 @@
#include "libc/log/libfatal.internal.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/mem/mem.h"
#include "libc/nexgen32e/gc.internal.h"
#include "libc/nexgen32e/stackframe.h"
@ -1087,7 +1086,7 @@ size_t __asan_get_heap_size(const void *p) {
return 0;
}
static size_t __asan_malloc_usable_size(const void *p) {
static size_t __asan_malloc_usable_size(void *p) {
size_t n, c;
struct AsanExtra *e;
if ((e = __asan_get_extra(p, &c)) && __asan_read48(e->size, &n)) {
@ -1498,5 +1497,3 @@ __attribute__((__constructor__)) void __asan_init(int argc, char **argv,
STRACE("/_/ \\_\\____/_/ \\_\\_| \\_|");
STRACE("cosmopolitan memory safety module initialized");
}
#endif /* __x86_64__ */

View file

@ -1,46 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_BITS_CMPXCHG16B_INTERNAL_H_
#define COSMOPOLITAN_LIBC_BITS_CMPXCHG16B_INTERNAL_H_
#include "libc/intrin/asmflag.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/**
* Compares and exchanges 128-bit value, i.e.
*
* if (*IfThing == *IsEqualToMe) {
* *IfThing = ReplaceItWithMe;
* return true;
* } else {
* *IsEqualToMe = *IfThing;
* return false;
* }
*
* Please note that Intel Architecture doesn't guarantee 16-byte memory
* accesses to be atomic on their own. Therefore _lockcmpxchg16b should
* be considered instead for both thread and asynchronous signal safety
*
* @param IfThing should point to aligned memory
* @param IsEqualToMe should point to in/out local variable
* @param ReplaceItWithMe might become the new memory value
* @return true if *IfThing was changed
*/
static inline bool _cmpxchg16b(uint128_t *IfThing, uint128_t *IsEqualToMe,
uint128_t ReplaceItWithMe) {
bool DidIt;
uint64_t ax, bx, cx, dx;
ax = *IsEqualToMe;
dx = *IsEqualToMe >> 64;
bx = ReplaceItWithMe;
cx = ReplaceItWithMe >> 64;
asm volatile(ZFLAG_ASM("cmpxchg16b\t%1")
: ZFLAG_CONSTRAINT(DidIt), "+m"(*IfThing), "+a"(ax), "+d"(dx)
: "b"(bx), "c"(cx));
if (!DidIt) {
*IsEqualToMe = ax | (uint128_t)dx << 64;
}
return DidIt;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_CMPXCHG16B_INTERNAL_H_ */

View file

@ -20,7 +20,9 @@
#include "libc/macros.internal.h"
.privileged
_futex: push %rbp
_futex:
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov %rcx,%r10
mov __NR_futex,%eax
@ -29,5 +31,11 @@ _futex: push %rbp
jnc 1f
neg %eax
1: pop %rbp
#elif defined(__aarch64__)
mov x8,#0x062
svc #0
#else
#error "unsupported architecture"
#endif /* __x86_64__ */
ret
.endfn _futex,globl,hidden

View file

@ -189,6 +189,8 @@ o/$(MODE)/libc/intrin/memmove.o: private \
# these assembly files are safe to build on aarch64
o/$(MODE)/libc/intrin/fenv.o: libc/intrin/fenv.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/futex.o: libc/intrin/futex.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/kclocknames.o: libc/intrin/kclocknames.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/kdos2errno.o: libc/intrin/kdos2errno.S

View file

@ -1,24 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_LOCKCMPXCHG_H_
#define COSMOPOLITAN_LIBC_INTRIN_LOCKCMPXCHG_H_
#include "libc/intrin/asmflag.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86__)
#define _lockcmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
autotype(IFTHING) IfThing = (IFTHING); \
typeof(*IfThing) IsEqualToMe = (ISEQUALTOME); \
typeof(*IfThing) ReplaceItWithMe = (REPLACEITWITHME); \
asm volatile(ZFLAG_ASM("lock cmpxchg\t%3,%1") \
: ZFLAG_CONSTRAINT(DidIt), "+m"(*IfThing), "+a"(IsEqualToMe) \
: "r"(ReplaceItWithMe) \
: "cc"); \
DidIt; \
})
#endif /* GNUC && !ANSI && x86 */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_LOCKCMPXCHG_H_ */

View file

@ -1,46 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_BITS_LOCKCMPXCHG16B_H_
#define COSMOPOLITAN_LIBC_BITS_LOCKCMPXCHG16B_H_
#include "libc/intrin/asmflag.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86__)
/**
* Compares and exchanges 128-bit value, i.e.
*
* if (*IfThing == *IsEqualToMe) {
* *IfThing = ReplaceItWithMe;
* return true;
* } else {
* *IsEqualToMe = *IfThing;
* return false;
* }
*
* @param IfThing should point to aligned memory
* @param IsEqualToMe should point to in/out local variable
* @param ReplaceItWithMe might become the new memory value
* @return true if *IfThing was changed
* @asyncsignalsafe
* @threadsafe
*/
static inline bool _lockcmpxchg16b(uint128_t *IfThing, uint128_t *IsEqualToMe,
uint128_t ReplaceItWithMe) {
bool DidIt;
uint64_t ax, bx, cx, dx;
ax = *IsEqualToMe;
dx = *IsEqualToMe >> 64;
bx = ReplaceItWithMe;
cx = ReplaceItWithMe >> 64;
asm volatile(ZFLAG_ASM("lock cmpxchg16b\t%1")
: ZFLAG_CONSTRAINT(DidIt), "+m"(*IfThing), "+a"(ax), "+d"(dx)
: "b"(bx), "c"(cx));
if (!DidIt) {
*IsEqualToMe = ax | (uint128_t)dx << 64;
}
return DidIt;
}
#endif /* __GNUC__ && !__STRICT_ANSI__ */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_LOCKCMPXCHG16B_H_ */

View file

@ -1,24 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_LOCKCMPXCHGP_H_
#define COSMOPOLITAN_LIBC_INTRIN_LOCKCMPXCHGP_H_
#include "libc/intrin/asmflag.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86__)
#define _lockcmpxchgp(IN_OUT_IFTHING, IN_OUT_ISEQUALTOME, IN_REPLACEITWITHME) \
({ \
bool DidIt; \
autotype(IN_OUT_IFTHING) IfThing = (IN_OUT_IFTHING); \
typeof(IfThing) IsEqualToMe = (IN_OUT_ISEQUALTOME); \
typeof(*IfThing) ReplaceItWithMe = (IN_REPLACEITWITHME); \
asm volatile(ZFLAG_ASM("lock cmpxchg\t%3,%1") \
: ZFLAG_CONSTRAINT(DidIt), "+m"(*IfThing), "+a"(*IsEqualToMe) \
: "r"(ReplaceItWithMe) \
: "cc"); \
DidIt; \
})
#endif /* GNUC && !ANSI && x86 */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_LOCKCMPXCHGP_H_ */

View file

@ -1,31 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_LOCKXCHG_H_
#define COSMOPOLITAN_LIBC_INTRIN_LOCKXCHG_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/* TODO(jart): DELETE */
intptr_t lockxchg(void *, void *, size_t);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86_64__)
/**
* Exchanges *MEMORY into *LOCALVAR w/ one operation.
*
* @param MEMORY is uint𝑘_t[hasatleast 1] where 𝑘 {8,16,32,64}
* @param LOCALVAR is uint𝑘_t[hasatleast 1]
* @return LOCALVAR[0]
* @see xchg()
*/
#define lockxchg(MEMORY, LOCALVAR) \
({ \
asm("xchg\t%0,%1" : "+%m"(*(MEMORY)), "+r"(*(LOCALVAR))); \
*(LOCALVAR); \
})
#else
#define lockxchg(MEM, VAR) \
lockxchg(MEM, VAR, sizeof(*(MEM)) / (sizeof(*(MEM)) == sizeof(*(VAR))))
#endif /* __GNUC__ && !__STRICT_ANSI__ */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_LOCKXCHG_H_ */

View file

@ -7,7 +7,6 @@
* Exchanges *MEMORY into *LOCALVAR.
*
* @return *MEMORY
* @see lockcmpxchg()
* todo(jart): what's the point of this?
*/
#define xchg(MEMORY, LOCALVAR) \

View file

@ -22,7 +22,7 @@
#include "libc/log/backtrace.internal.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/mem/mem.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/sysv/consts/o.h"

View file

@ -348,7 +348,7 @@ relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
} else {
// somehow __minicrash() crashed not possible
for (;;) {
asm("ud2");
__builtin_trap();
}
}
} else {

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,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_calloc
hook_calloc:
.quad 0
.endobj hook_calloc,globl,hidden
.previous
.init.start 202,_init_calloc
.hidden dlcalloc
ezlea dlcalloc,ax
stosq
.yoink free
.init.end 202,_init_calloc

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: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,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/hook.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "third_party/dlmalloc/dlmalloc.h"
/* TODO(jart): DELETE */
int (*hook_malloc_trim)(size_t) = dlmalloc_trim;
void free_s(void *v) {
void **pp = (void **)v;
void *p = *pp;
*pp = 0;
if (_isheap(p)) {
_weakfree(p);
}
/**
* 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);
}

View file

@ -10,6 +10,7 @@
* @see libc/testlib/bench.h
*/
#ifdef __x86__
#define __startbench() \
({ \
uint64_t Ticks; \
@ -25,7 +26,6 @@
: "rcx", "rdx", "memory", "cc"); \
Ticks; \
})
#define __endbench() \
({ \
uint64_t Ticks; \
@ -42,6 +42,10 @@
: "rax", "rcx", "rdx", "memory", "cc"); \
Ticks; \
})
#else
#define __startbench() rdtsc()
#define __endbench() rdtsc()
#endif
#define __startbench_m() mfence_lfence_rdtsc_lfence()
#define __endbench_m() __startbench_m()

View file

@ -1,40 +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"
// Castagnoli CRC32 ISCSI Polynomial
// x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
// 0b00011110110111000110111101000001
// _bitreverse32(0x1edc6f41)
#define ISCSI_POLYNOMIAL 0x82f63b78
.initbss 300,_init_kCrc32cTab
kCrc32cTab:
.rept 256
.long 0
.endr
.endobj kCrc32cTab,globl,hidden
.previous
.init.start 300,_init_kCrc32cTab
push %rsi
mov $ISCSI_POLYNOMIAL,%esi
call crc32init
pop %rsi
.init.end 300,_init_kCrc32cTab

View file

@ -106,7 +106,6 @@ void _loadxmm(void *);
void _peekall(void);
void _savexmm(void *);
void _weakfree(void *);
void free_s(void *) paramsnonnull() libcesque;
int _OpenExecutable(void);
int ftrace_install(void);
int ftrace_enabled(int);

View file

@ -73,6 +73,8 @@
#include "libc/sysv/consts/epoll.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
/**
* @fileoverview epoll
*
@ -1530,3 +1532,5 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents,
timeoutms, rc);
return rc;
}
#endif /* __x86_64__ */

View file

@ -45,6 +45,8 @@
#include "libc/sysv/errfuns.h"
#include "net/http/ip.h"
#ifdef __x86_64__
#define ORIG_RAX 120
#define RAX 80
#define RDI 112
@ -337,3 +339,5 @@ int nointernet(void) {
HandleSeccompTrace(act);
}
}
#endif /* __x86_64__ */

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
@ -26,7 +27,8 @@
int __fflush_impl(FILE *f) {
size_t i;
ssize_t rc;
free_s(&f->getln);
free(f->getln);
f->getln = 0;
if (f->beg && !f->end && (f->iomode & O_ACCMODE) != O_RDONLY) {
for (i = 0; i < f->beg; i += rc) {
if ((rc = write(f->fd, f->buf + i, f->beg - i)) == -1) {

View file

@ -1,30 +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"
.bss
.balign 8
g_rando:
.quad 0
.endobj g_rando,globl
.previous
.init.start 100,_init_g_rando
movb $1,g_rando(%rip)
.init.end 100,_init_g_rando

View file

@ -1,7 +1,7 @@
/*-*- 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,12 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/lockxchg.h"
#include "libc/stdio/stdio.h"
// TODO(jart): delete
int fclose_s(FILE **fp) {
FILE *f = NULL;
return fclose(lockxchg(fp, &f));
}
uint64_t g_rando = 1;

View file

@ -1,39 +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/sysv/consts/o.h"
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/thread/thread.h"
#include "libc/macros.internal.h"
.init.start 400,_init_stderr
ezlea __stderr,ax
push $_IOLBF
pop (%rax) # f.fd
push STDERR_FILENO
pop 12(%rax)
mov O_WRONLY,%edx
mov %edx,4(%rax) # f.iomode
lea 0x50(%rax),%rcx # f.mem
mov %rcx,0x18(%rax) # f.buf
movl $BUFSIZ,0x20(%rax) # f.size
movb $PTHREAD_MUTEX_RECURSIVE,0x38+4(%rax) # f.lock._type
mov %rax,stderr(%rip)
.init.end 400,_init_stderr,globl,hidden

View file

@ -16,20 +16,27 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
STATIC_YOINK("_init_stderr");
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h"
/**
* Pointer to standard error stream.
*/
FILE *stderr;
_Hide FILE __stderr;
static FILE __stderr;
static textstartup void __stderr_init() {
__attribute__((__constructor__)) static void __stderr_init(void) {
stderr = &__stderr;
stderr->fd = STDERR_FILENO;
stderr->bufmode = _IOLBF;
stderr->iomode = O_WRONLY;
stderr->buf = stderr->mem;
stderr->size = sizeof(stderr->mem);
((pthread_mutex_t *)stderr->lock)->_type = PTHREAD_MUTEX_RECURSIVE;
__fflush_register(stderr);
}
const void *const __stderr_ctor[] initarray = {__stderr_init};

View file

@ -1,35 +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/sysv/consts/o.h"
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/thread/thread.h"
#include "libc/macros.internal.h"
.init.start 400,_init_stdin
ezlea __stdin,ax
mov O_RDONLY,%edx
mov %edx,4(%rax) # f.iomode
lea 0x50(%rax),%rcx # f.mem
mov %rcx,0x18(%rax) # f.buf
movl $BUFSIZ,0x20(%rax) # f.size
movb $PTHREAD_MUTEX_RECURSIVE,0x38+4(%rax) # f.lock._type
mov %rax,stdin(%rip)
.init.end 400,_init_stdin,globl,hidden

View file

@ -16,20 +16,26 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
STATIC_YOINK("_init_stdin");
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h"
/**
* Pointer to standard input stream.
*/
FILE *stdin;
_Hide FILE __stdin;
static FILE __stdin;
static textstartup void __stdin_init() {
__attribute__((__constructor__)) static void __stdin_init(void) {
stdin = &__stdin;
stdin->fd = STDIN_FILENO;
stdin->iomode = O_RDONLY;
stdin->buf = stdin->mem;
stdin->size = sizeof(stdin->mem);
((pthread_mutex_t *)stdin->lock)->_type = PTHREAD_MUTEX_RECURSIVE;
__fflush_register(stdin);
}
const void *const __stdin_ctor[] initarray = {__stdin_init};

View file

@ -74,7 +74,6 @@ FILE *freopen(const char *, const char *, FILE *) paramsnonnull((2, 3));
size_t fread(void *, size_t, size_t, FILE *) paramsnonnull((4));
size_t fwrite(const void *, size_t, size_t, FILE *) paramsnonnull((4));
int fclose(FILE *);
int fclose_s(FILE **) paramsnonnull();
int fseek(FILE *, long, int) paramsnonnull();
long ftell(FILE *) paramsnonnull();
int fseeko(FILE *, int64_t, int) paramsnonnull();

View file

@ -1,37 +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/sysv/consts/o.h"
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/thread/thread.h"
#include "libc/macros.internal.h"
.init.start 400,_init_stdout
ezlea __stdout,ax
push STDOUT_FILENO
pop 0x0c(%rax) # f.fd
mov O_WRONLY,%edx
mov %edx,4(%rax) # f.iomode
lea 0x50(%rax),%rcx # f.mem
mov %rcx,0x18(%rax) # f.buf
movl $BUFSIZ,0x20(%rax) # f.size
movb $PTHREAD_MUTEX_RECURSIVE,0x38+4(%rax) # f.lock._type
mov %rax,stdout(%rip)
.init.end 400,_init_stdout,globl,hidden

View file

@ -18,23 +18,28 @@
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/intrin/pushpop.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
STATIC_YOINK("_init_stdout");
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h"
/**
* Pointer to standard output stream.
*/
FILE *stdout;
_Hide FILE __stdout;
static FILE __stdout;
__attribute__((__constructor__)) static void __stdout_init(void) {
stdout = &__stdout;
stdout->fd = STDOUT_FILENO;
stdout->iomode = O_WRONLY;
stdout->buf = stdout->mem;
stdout->size = sizeof(stdout->mem);
((pthread_mutex_t *)stdout->lock)->_type = PTHREAD_MUTEX_RECURSIVE;
static textstartup void __stdout_init() {
struct FILE *sf;
sf = stdout;
asm("" : "+r"(sf));
/*
* Unlike other C libraries we don't bother calling fstat() to check
* if stdio is a character device and we instead choose to always line
@ -43,8 +48,7 @@ static textstartup void __stdout_init() {
* value latency more than throughput, and stdio isn't the best api
* when the goal is throughput.
*/
sf->bufmode = _IOLBF;
__fflush_register(sf);
}
stdout->bufmode = _IOLBF;
const void *const __stdout_ctor[] initarray = {__stdout_init};
__fflush_register(stdout);
}

View file

@ -19,57 +19,13 @@
#include "libc/nexgen32e/crc32.h"
#include "libc/nexgen32e/x86feature.h"
#ifndef __x86_64__
const uint32_t kCrc32cTab[256] = {
0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c,
0x26a1e7e8, 0xd4ca64eb, 0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b,
0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24, 0x105ec76f, 0xe235446c,
0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384,
0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54, 0x5d1d08bf, 0xaf768bbc,
0xbc267848, 0x4e4dfb4b, 0x20bd8ede, 0xd2d60ddd, 0xc186fe29, 0x33ed7d2a,
0xe72719c1, 0x154c9ac2, 0x061c6936, 0xf477ea35, 0xaa64d611, 0x580f5512,
0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa,
0x30e349b1, 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x05125dad,
0x1642ae59, 0xe4292d5a, 0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a,
0x7da08661, 0x8fcb0562, 0x9c9bf696, 0x6ef07595, 0x417b1dbc, 0xb3109ebf,
0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0, 0x67dafa54, 0x95b17957,
0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0x0c38d26c, 0xfe53516f,
0xed03a29b, 0x1f682198, 0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927,
0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38, 0xdbfc821c, 0x2997011f,
0x3ac7f2eb, 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0x0f36e6f7,
0x61c69362, 0x93ad1061, 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e,
0x4767748a, 0xb50cf789, 0xeb1fcbad, 0x197448ae, 0x0a24bb5a, 0xf84f3859,
0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46, 0x7198540d, 0x83f3d70e,
0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5, 0xa55230e6,
0xfb410cc2, 0x092a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de,
0xdde0eb2a, 0x2f8b6829, 0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c,
0x456cac67, 0xb7072f64, 0xa457dc90, 0x563c5f93, 0x082f63b7, 0xfa44e0b4,
0xe9141340, 0x1b7f9043, 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c,
0x92a8fc17, 0x60c37f14, 0x73938ce0, 0x81f80fe3, 0x55326b08, 0xa759e80b,
0xb4091bff, 0x466298fc, 0x1871a4d8, 0xea1a27db, 0xf94ad42f, 0x0b21572c,
0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033, 0xa24bb5a6, 0x502036a5,
0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d,
0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975,
0x0e330a81, 0xfc588982, 0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d,
0x758fe5d6, 0x87e466d5, 0x94b49521, 0x66df1622, 0x38cc2a06, 0xcaa7a905,
0xd9f75af1, 0x2b9cd9f2, 0xff56bd19, 0x0d3d3e1a, 0x1e6dcdee, 0xec064eed,
0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530, 0x0417b1db, 0xf67c32d8,
0xe52cc12c, 0x1747422f, 0x49547e0b, 0xbb3ffd08, 0xa86f0efc, 0x5a048dff,
0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0, 0xd3d3e1ab, 0x21b862a8,
0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x07198540,
0x590ab964, 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78,
0x7fab5e8c, 0x8dc0dd8f, 0xe330a81a, 0x115b2b19, 0x020bd8ed, 0xf0605bee,
0x24aa3f05, 0xd6c1bc06, 0xc5914ff2, 0x37faccf1, 0x69e9f0d5, 0x9b8273d6,
0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9, 0x4f48173d, 0xbd23943e,
0xf36e6f75, 0x0105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a, 0xc69f7b69,
0xd5cf889d, 0x27a40b9e, 0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e,
0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351,
};
#endif
/**
* Computes 32-bit Castagnoli Cyclic Redundancy Check.
*
* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
* 0b00011110110111000110111101000001
* _bitreverse32(0x1edc6f41)
*
* @param init is the initial hash value
* @param data points to the data
* @param size is the byte size of data
@ -78,7 +34,13 @@ const uint32_t kCrc32cTab[256] = {
*/
uint32_t crc32c(uint32_t init, const void *data, size_t size) {
uint64_t h;
static bool once;
const unsigned char *p, *pe;
static uint32_t kCrc32cTab[256];
if (!once) {
crc32init(kCrc32cTab, 0x82f63b78);
once = 0;
}
p = data;
pe = p + size;
h = init ^ 0xffffffff;

View file

@ -16,10 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/macros.internal.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/nexgen32e/x86feature.h"

View file

@ -19,6 +19,7 @@
#include "libc/testlib/testlib.h"
void testlib_clearxmmregisters(void) {
#ifdef __x86_64__
asm("pxor\t%xmm0,%xmm0\n\t"
"pxor\t%xmm1,%xmm1\n\t"
"pxor\t%xmm2,%xmm2\n\t"
@ -27,4 +28,5 @@ void testlib_clearxmmregisters(void) {
"pxor\t%xmm5,%xmm5\n\t"
"pxor\t%xmm6,%xmm6\n\t"
"pxor\t%xmm7,%xmm7");
#endif
}

View file

@ -20,7 +20,11 @@
#include "libc/testlib/ezbench.h"
int __testlib_getcore(void) {
#ifdef __x86__
long tscaux;
tscaux = rdpid();
return TSC_AUX_CORE(tscaux);
#else
return 0;
#endif
}

View file

@ -25,6 +25,7 @@
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
@ -51,8 +52,8 @@ void testlib_showerror(const char *file, int line, const char *func,
RED2, UNBOLD, BLUE1, file, (long)line, RESET, method, func,
g_fixturename, hostname, getpid(), gettid(), code, v1, symbol, v2,
SUBTLE, strerror(errno), GetProgramExecutableName(), RESET);
free_s(&v1);
free_s(&v2);
free(v1);
free(v2);
}
/* TODO(jart): Pay off tech debt re duplication */
@ -89,8 +90,8 @@ void testlib_showerror_(int line, const char *wantcode, const char *gotcode,
"\t%s%s @ %s%s\n",
SUBTLE, strerror(e), RESET, SUBTLE,
firstnonnull(program_invocation_name, "unknown"), hostname, RESET);
free_s(&FREED_want);
free_s(&FREED_got);
free(FREED_want);
free(FREED_got);
++g_testlib_failed;
if (testlib_showerror_isfatal) {
testlib_abort();

View file

@ -36,6 +36,8 @@
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#ifdef __x86_64__
int systemfive_cancel(void);
extern const char systemfive_cancellable[];
@ -293,6 +295,8 @@ errno_t pthread_cancel(pthread_t thread) {
return rc;
}
#endif /* __x86_64__ */
/**
* Creates cancellation point in calling thread.
*

View file

@ -16,11 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "net/https/sslcache.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/lockcmpxchg.h"
#include "libc/intrin/cmpxchg.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
@ -32,7 +33,6 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/time/time.h"
#include "net/https/sslcache.h"
#include "third_party/mbedtls/ssl.h"
#include "third_party/mbedtls/x509_crt.h"
@ -131,7 +131,7 @@ int UncacheSslSession(void *data, mbedtls_ssl_session *session) {
ts = time(0);
if (!(e->time <= ts && ts <= e->time + cache->lifetime)) {
DEBUGF("%u sslcache expired", i);
_lockcmpxchg(&e->tick, tick, 0);
_cmpxchg(&e->tick, tick, 0);
return 1;
}
cert = 0;
@ -201,7 +201,7 @@ int CacheSslSession(void *data, const mbedtls_ssl_session *session) {
e->time = time(0);
tick = rdtsc();
asm volatile("" ::: "memory");
if (tick && _lockcmpxchg(&e->pid, pid, 0)) {
if (tick && _cmpxchg(&e->pid, pid, 0)) {
DEBUGF("%u saved %s%s %`#.*s", i,
mbedtls_ssl_get_ciphersuite_name(session->ciphersuite),
session->compression ? " DEFLATE" : "", session->id_len,

View file

@ -16,9 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/log/libfatal.internal.h"
#include "libc/mem/alg.h"
#include "libc/mem/arraylist.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
@ -48,7 +48,8 @@ TEST(append, worksGreatForScalars) {
}
ASSERT_EQ(1024, s.i);
for (size_t i = 0; i < s.i; ++i) ASSERT_EQ('a', s.p[i]);
free_s(&s.p);
free(s.p);
s.p = 0;
}
TEST(append, isGenericallyTyped) {
@ -63,7 +64,8 @@ TEST(append, isGenericallyTyped) {
for (size_t i = 0; i < s.i; ++i) {
ASSERT_EQ(0x31337, s.p[i]);
}
free_s(&s.p);
free(s.p);
s.p = 0;
}
TEST(concat, worksGreatForStrings) {
@ -79,7 +81,8 @@ TEST(concat, worksGreatForStrings) {
"Wir werden wieder auferstehen\n",
s.p);
ASSERT_EQ(strlen(ks) * 2 + 1, s.i);
free_s(&s.p);
free(s.p);
s.p = 0;
}
TEST(concat, isGenericallyTyped) {
@ -95,5 +98,6 @@ TEST(concat, isGenericallyTyped) {
u"Unsere schwarzen Seelen bekommt ihr nicht.\n",
s.p);
ASSERT_EQ(strlen16(ks) * 2 + 1, s.i);
free_s(&s.p);
free(s.p);
s.p = 0;
}

View file

@ -32,7 +32,7 @@ TEST(grow, testNull_hasAllocatingBehavior) {
EXPECT_TRUE(__grow(&p, &capacity, 1, 0));
EXPECT_NE(NULL, p);
EXPECT_EQ(32, capacity);
free_s(&p);
free(p);
}
TEST(grow, testCapacity_isInUnits_withTerminatorGuarantee) {
@ -41,7 +41,7 @@ TEST(grow, testCapacity_isInUnits_withTerminatorGuarantee) {
EXPECT_TRUE(__grow(&p, &capacity, 8, 0));
EXPECT_NE(NULL, p);
EXPECT_EQ(32 / 8 + 1, capacity);
free_s(&p);
free(p);
}
TEST(grow, testStackMemory_convertsToDynamic) {
@ -69,7 +69,7 @@ TEST(grow, testGrowth_clearsNewMemory) {
EXPECT_GT(capacity, 123);
for (i = 0; i < 123; ++i) ASSERT_EQ('a', p[i]);
for (i = 123; i < capacity; ++i) ASSERT_EQ(0, p[i]);
free_s(&p);
free(p);
}
TEST(grow, testBonusParam_willGoAboveAndBeyond) {
@ -77,11 +77,11 @@ TEST(grow, testBonusParam_willGoAboveAndBeyond) {
char *p = malloc(capacity);
EXPECT_TRUE(__grow(&p, &capacity, 1, 0));
EXPECT_LT(capacity, 1024);
free_s(&p);
free(p);
p = malloc((capacity = 32));
EXPECT_TRUE(__grow(&p, &capacity, 1, 1024));
EXPECT_GT(capacity, 1024);
free_s(&p);
free(p);
}
TEST(grow, testOverflow_returnsFalseAndDoesNotFree) {
@ -95,6 +95,6 @@ TEST(grow, testOverflow_returnsFalseAndDoesNotFree) {
EXPECT_EQ(1, p[0]);
EXPECT_EQ(2, p[1]);
EXPECT_EQ(3, p[2]);
free_s(&p);
free(p);
}
}

View file

@ -1,3 +1,4 @@
#include "third_party/dlmalloc/dlmalloc.h"
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
@ -16,7 +17,6 @@
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/thread/thread.h"
#include "third_party/dlmalloc/dlmalloc.h"
#include "third_party/dlmalloc/vespene.internal.h"
// clang-format off
@ -1418,7 +1418,7 @@ int dlmallopt(int param_number, int value) {
return change_mparam(param_number, value);
}
size_t dlmalloc_usable_size(const void* mem) {
size_t dlmalloc_usable_size(void* mem) {
mchunkptr p;
size_t bytes;
if (mem) {

View file

@ -415,7 +415,7 @@ void dlmalloc_stats(void);
p = malloc(n);
assert(malloc_usable_size(p) >= 256);
*/
size_t dlmalloc_usable_size(const void*);
size_t dlmalloc_usable_size(void*);
/*
mspace is an opaque type representing an independent

View file

@ -9,16 +9,8 @@ THIRD_PARTY_DLMALLOC_A = o/$(MODE)/third_party/dlmalloc/dlmalloc.a
THIRD_PARTY_DLMALLOC_A_FILES := $(wildcard third_party/dlmalloc/*)
THIRD_PARTY_DLMALLOC_A_HDRS = $(filter %.h,$(THIRD_PARTY_DLMALLOC_A_FILES))
THIRD_PARTY_DLMALLOC_A_INCS = $(filter %.inc,$(THIRD_PARTY_DLMALLOC_A_FILES))
THIRD_PARTY_DLMALLOC_A_SRCS_S = $(filter %.S,$(THIRD_PARTY_DLMALLOC_A_FILES))
THIRD_PARTY_DLMALLOC_A_SRCS_C = $(filter %.c,$(THIRD_PARTY_DLMALLOC_A_FILES))
THIRD_PARTY_DLMALLOC_A_SRCS = \
$(THIRD_PARTY_DLMALLOC_A_SRCS_S) \
$(THIRD_PARTY_DLMALLOC_A_SRCS_C)
THIRD_PARTY_DLMALLOC_A_OBJS = \
$(THIRD_PARTY_DLMALLOC_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(THIRD_PARTY_DLMALLOC_A_SRCS_C:%.c=o/$(MODE)/%.o)
THIRD_PARTY_DLMALLOC_A_SRCS = $(filter %.c,$(THIRD_PARTY_DLMALLOC_A_FILES))
THIRD_PARTY_DLMALLOC_A_OBJS = $(THIRD_PARTY_DLMALLOC_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_DLMALLOC_A_CHECKS = \
$(THIRD_PARTY_DLMALLOC_A).pkg \

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 2022 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"
.init.start 600,_init_dlmalloc
push %rdi
push %rsi
call init_mparams
pop %rsi
pop %rdi
.init.end 600,_init_dlmalloc

View file

@ -1,7 +1,5 @@
// clang-format off
STATIC_YOINK("_init_dlmalloc");
/* ---------------------------- setting mparams -------------------------- */
#if LOCK_AT_FORK
@ -11,7 +9,7 @@ static void dlmalloc_post_fork_child(void) { INITIAL_LOCK(&(gm)->mutex); }
#endif /* LOCK_AT_FORK */
/* Initialize mparams */
int init_mparams(void) {
__attribute__((__constructor__)) int init_mparams(void) {
#ifdef NEED_GLOBAL_LOCK_INIT
if (malloc_global_mutex_status <= 0)
init_malloc_global_mutex();

View file

@ -45,12 +45,17 @@ $(THIRD_PARTY_GGML_A_OBJS): private \
OVERRIDE_CFLAGS += \
-O3 \
-ffunction-sections \
-fdata-sections \
-fdata-sections
ifeq ($(ARCH), x86_64)
$(THIRD_PARTY_GGML_A_OBJS): private \
OVERRIDE_CFLAGS += \
-msse3 \
-mavx \
-mavx2 \
-mf16c \
-mfma
endif
################################################################################
# command for running inference on large language models

View file

@ -58,6 +58,8 @@ asm(".include \"libc/disclaimer.inc\"");
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
#define NMAX 5552
#ifdef ADLER32_SIMD_SSSE3
uint32_t adler32_simd_(uint32_t adler, const unsigned char *buf, size_t len) {
/*
* Split Adler-32 into component sums.
@ -192,3 +194,5 @@ uint32_t adler32_simd_(uint32_t adler, const unsigned char *buf, size_t len) {
*/
return s1 | (s2 << 16);
}
#endif /* ADLER32_SIMD_SSSE3 */

View file

@ -41,17 +41,17 @@ $(THIRD_PARTY_ZLIB_A).pkg: \
$(THIRD_PARTY_ZLIB_A_OBJS) \
$(foreach x,$(THIRD_PARTY_ZLIB_A_DIRECTDEPS),$($(x)_A).pkg)
ifeq ($(ARCH), x86_64)
o/$(MODE)/third_party/zlib/adler32simd.o: private \
OVERRIDE_CFLAGS += \
-mssse3
o/$(MODE)/third_party/zlib/adler32.o: private \
OVERRIDE_CPPFLAGS += \
-DADLER32_SIMD_SSSE3
o/$(MODE)/third_party/zlib/deflate.o: private \
OVERRIDE_CPPFLAGS += \
-DCRC32_SIMD_SSE42_PCLMUL
endif
$(THIRD_PARTY_ZLIB_A_OBJS): private \
OVERRIDE_CFLAGS += \

View file

@ -16,13 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/build/lib/interner.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/x/x.h"
#include "tool/build/lib/interner.h"
#define kInitialItems 16
@ -50,7 +50,7 @@ static void rehash(struct InternerObject *it) {
} while (it->p[j].hash);
memcpy(&it->p[j], &p[i], sizeof(p[i]));
}
free_s(&p);
free(p);
}
/**

View file

@ -114,7 +114,7 @@ void GetOpts(int argc, char *argv[]) {
test_ = true;
break;
case 'o':
fclose_s(&fout_);
fclose(fout_);
if (!(fout_ = fopen((hint_ = optarg), "w"))) {
PrintIoErrorMessage();
exit(1);
@ -191,9 +191,11 @@ int Run(char **paths, size_t count) {
hint_ = "/dev/stdin";
if (!fout_) fout_ = stdout;
rc = RunLengthCode();
rc |= fclose_s(&fin_);
rc |= fclose(fin_);
fin_ = 0;
} else {
rc = fclose_s(&fin_);
rc = fclose(fin_);
fin_ = 0;
for (i = 0; i < count && rc != -1; ++i) {
rc = -1;
if ((fin_ = fopen((hint_ = paths[i]), "r"))) {
@ -219,18 +221,21 @@ int Run(char **paths, size_t count) {
if (rc != -1 && !decompress_) {
rc = RunLengthEncode2();
}
if ((rc |= fclose_s(&fout_)) != -1) {
if ((rc |= fclose(fout_)) != -1) {
unlink(paths[i]);
}
fout_ = 0;
}
}
rc |= fclose_s(&fin_);
rc |= fclose(fin_);
fin_ = 0;
}
}
}
if (rc != -1 && fout_) {
rc = RunLengthEncode2();
rc |= fclose_s(&fout_);
rc |= fclose(fout_);
fout_ = 0;
}
return rc;
}

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/decode/lib/bitabuilder.h"
#include "libc/assert.h"
#include "libc/intrin/bits.h"
#include "libc/log/check.h"
@ -24,7 +25,6 @@
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "tool/decode/lib/bitabuilder.h"
/**
* @fileoverview Sparse bit array builder.
@ -41,8 +41,9 @@ struct BitaBuilder *bitabuilder_new(void) {
void bitabuilder_free(struct BitaBuilder **bbpp) {
if (*bbpp) {
free_s(&(*bbpp)->p);
free_s(bbpp);
free((*bbpp)->p);
free(*bbpp);
*bbpp = 0;
}
}

View file

@ -95,9 +95,11 @@ int main(int argc, char *argv[]) {
for (i = optind; i < argc; ++i) {
CHECK_NOTNULL((in_ = fopen((inpath_ = argv[i]), "r")));
ProcessFile();
CHECK_NE(-1, fclose_s(&in_));
CHECK_NE(-1, fclose(in_));
in_ = 0;
}
CHECK_NE(-1, fclose_s(&out_));
CHECK_NE(-1, fclose(out_));
out_ = 0;
free(line_);
return 0;
}

View file

@ -107,8 +107,10 @@ int main(int argc, char *argv[]) {
for (i = optind; i < argc; ++i) {
CHECK_NOTNULL((fi_ = fopen((inpath_ = argv[i]), "r")));
processfile();
CHECK_NE(-1, fclose_s(&fi_));
CHECK_NE(-1, fclose(fi_));
fi_ = 0;
}
CHECK_NE(-1, fclose_s(&fo_));
CHECK_NE(-1, fclose(fo_));
fo_ = 0;
return 0;
}

View file

@ -16,15 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/viz/lib/formatstringtable.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/str/strwidth.h"
#include "tool/viz/lib/formatstringtable.h"
void *FreeStringTableCells(long yn, long xn, char *T[yn][xn]) {
long y, x;
for (y = 0; y < yn; ++y) {
for (x = 0; x < xn; ++x) {
free_s(&T[y][x]);
free(T[y][x]);
T[y][x] = 0;
}
}
return T;

View file

@ -102,8 +102,10 @@ int main(int argc, char *argv[]) {
for (i = optind; i < argc; ++i) {
CHECK_NOTNULL((in_ = fopen((inpath_ = argv[i]), "r")));
ProcessFile();
CHECK_NE(-1, fclose_s(&in_));
CHECK_NE(-1, fclose(in_));
in_ = 0;
}
CHECK_NE(-1, fclose_s(&out_));
CHECK_NE(-1, fclose(out_));
out_ = 0;
return 0;
}

View file

@ -122,11 +122,13 @@ int main(int argc, char *argv[]) {
while ((getline(&line_, &linecap_, stdin)) != -1) {
processarg(_chomp(line_));
}
free_s(&line_);
free(line_);
line_ = 0;
}
if (cleanup_) {
fprintf(fout_, "%s[0m\n", rawmode_ ? "\e" : "\\e");
}
CHECK_NE(-1, fclose_s(&fout_));
CHECK_NE(-1, fclose(fout_));
fout_ = 0;
return 0;
}

View file

@ -164,10 +164,12 @@ int main(int argc, char *argv[]) {
for (i = optind; i < argc; ++i) {
CHECK_NOTNULL((fi_ = fopen((inpath_ = argv[i]), "r")));
processfile();
CHECK_NE(-1, fclose_s(&fi_));
CHECK_NE(-1, fclose(fi_));
fi_ = 0;
}
flush();
CHECK_NE(-1, fclose_s(&fo_));
CHECK_NE(-1, fclose(fo_));
fo_ = 0;
free(lines_.p);
free(pool_.p);
free(line_);

View file

@ -176,9 +176,11 @@ int main(int argc, char *argv[]) {
for (i = optind; i < argc; ++i) {
CHECK_NOTNULL((in_ = fopen((inpath_ = argv[i]), "r")));
ProcessFile();
CHECK_NE(-1, fclose_s(&in_));
CHECK_NE(-1, fclose(in_));
in_ = 0;
}
CHECK_NE(-1, fclose_s(&out_));
CHECK_NE(-1, fclose(out_));
out_ = 0;
free(line_);
return 0;
}