From decc6c09574406ed0d714c75f8e83cb9d6fe2bdb Mon Sep 17 00:00:00 2001 From: Greg Haerr Date: Wed, 1 Jan 2025 23:41:18 -0700 Subject: [PATCH] [libc, Watcom C] Fix gettimeofday, ctime and __amalloc debug speed --- libc/include/watcom/stddef.h | 104 +++++++++++++++++++++++++++++ libc/malloc/amalloc.c | 16 ++--- libc/malloc/dprintf.c | 2 +- libc/time/ctime.c | 2 +- libc/watcom/syscall/gettimeofday.c | 3 +- 5 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 libc/include/watcom/stddef.h diff --git a/libc/include/watcom/stddef.h b/libc/include/watcom/stddef.h new file mode 100644 index 000000000..fc7fb5802 --- /dev/null +++ b/libc/include/watcom/stddef.h @@ -0,0 +1,104 @@ +/*************************************************************************** + * FILE: stddef.h/cstddef (Standard definitions) + * + * ========================================================================= + * + * Open Watcom Project + * + * Copyright (c) 2004-2024 The Open Watcom Contributors. All Rights Reserved. + * Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. + * + * This file is automatically generated. Do not edit directly. + * + * ========================================================================= + * + * Description: This header is part of the C/C++ standard library. It + * introduces certain commonly needed type names and + * supplies the offsetof macro. + ***************************************************************************/ +#ifndef _STDDEF_H_INCLUDED +#define _STDDEF_H_INCLUDED + +#ifndef _ENABLE_AUTODEPEND + #pragma read_only_file +#endif + +#ifdef __cplusplus + +#include + +// C99 types in stddef.h +#ifndef _SIZE_T_DEFINED +#define _SIZE_T_DEFINED + #define _SIZE_T_DEFINED_ + using std::size_t; +#endif +#ifndef _PTRDIFF_T_DEFINED +#define _PTRDIFF_T_DEFINED + #define _PTRDIFF_T_DEFIEND_ + using std::ptrdiff_t; +#endif + +#else /* __cplusplus not defined */ + +#ifndef __COMDEF_H_INCLUDED + #include <_comdef.h> +#endif + +#ifndef NULL + #ifdef __RDOSDEV__ + #define NULL 0 + #else + #define NULL ((void *)0) + #endif +#endif + +#define offsetof(__typ,__id) ((size_t)((char *)&(((__typ*)0)->__id) - (char *)0)) + + #ifndef _WCHAR_T_DEFINED + #define _WCHAR_T_DEFINED + #define _WCHAR_T_DEFINED_ + typedef unsigned short wchar_t; + #endif + + #ifndef _SIZE_T_DEFINED + #define _SIZE_T_DEFINED + #define _SIZE_T_DEFINED_ + typedef unsigned size_t; + typedef size_t __w_size_t; + #endif + + #ifndef _PTRDIFF_T_DEFINED + #define _PTRDIFF_T_DEFINED + #define _PTRDIFF_T_DEFINED_ + #ifdef __HUGE__ + typedef long ptrdiff_t; + #else + typedef int ptrdiff_t; + #endif + #endif + +#if !defined( _NO_EXT_KEYS ) /* extensions enabled */ +#ifdef _M_I86 + _WCRTDATA extern int _WCFAR *_threadid; /* pointer to thread id */ +#else + #define _threadid (__threadid()) + _WCRTLINK extern int *__threadid( void ); /* pointer to thread id */ + #ifdef __NT__ + _WCRTLINK extern unsigned long __threadhandle( void ); + #endif +#endif +#endif /* extensions enabled */ + +#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1 + +#ifndef _RSIZE_T_DEFINED +#define _RSIZE_T_DEFINED + typedef size_t rsize_t; +#endif + +#endif /* __STDC_WANT_LIB_EXT1__ */ + +#endif /* __cplusplus not defined */ + +#endif diff --git a/libc/malloc/amalloc.c b/libc/malloc/amalloc.c index 47bab2875..24ac19f41 100644 --- a/libc/malloc/amalloc.c +++ b/libc/malloc/amalloc.c @@ -17,7 +17,7 @@ #include #include #include -#define DEBUG 1 /* =1 use sysctl, =2 debug output, =3 show heap */ +#define DEBUG 0 /* =1 use sysctl, =2 debug output, =3 show heap */ /* C storage allocator * circular first-fit strategy @@ -94,10 +94,10 @@ int __amalloc_add_heap(char __far *start, size_t size) allocsize = size / sizeof(union store); debug("Adding SEG %04x size %d DS %04x\n", FP_SEG(start), size, FP_SEG(&size)); - allocs[0].ptr = setbusy(&allocs[1]); + allocs[0].ptr = setbusy((NPTR)&allocs[1]); allocs[1].ptr = (NPTR)&allocs[allocsize-2]; - allocs[allocsize-2].ptr = setbusy(&allocs[allocsize-1]); - allocs[allocsize-1].ptr = setbusy(&allocs[0]); + allocs[allocsize-2].ptr = setbusy((NPTR)&allocs[allocsize-1]); + allocs[allocsize-1].ptr = setbusy((NPTR)&allocs[0]); alloct = (NPTR)&allocs[allocsize-1]; allocp = (NPTR)&allocs[0]; return 1; @@ -114,7 +114,7 @@ __amalloc(size_t nbytes) #endif debug("(%d)malloc(%5u) ", getpid(), nbytes); - if (!allocs) + if (!allocseg) return NULL; errno = 0; if (nbytes == 0) { @@ -132,7 +132,7 @@ __amalloc(size_t nbytes) } nw = (nbytes+WORD+WORD-1)/WORD; /* extra word for link ptr/size*/ - ASSERT(allocp>=allocs && allocp<=alloct); + ASSERT(allocp>=(NPTR)allocs && allocp<=alloct); ASSERT(malloc_check_heap()); /* combine free areas at heap start before allocating from free area past allocp */ allocp = (NPTR)allocs; @@ -207,7 +207,7 @@ __amalloc(size_t nbytes) debug2("(TOTAL %u) ", sizeof(union store) + (clearbusy(alloct) - clearbusy(allocs[allocsize-1].ptr)) * sizeof(union store)); - next(alloct) = setbusy(allocs); + next(alloct) = setbusy((NPTR)allocs); #endif } found: @@ -335,7 +335,7 @@ malloc_show_heap(void) unsigned int size, alloc = 0, free = 0; static unsigned int maxalloc; - if (!debug_level) return; + if (debug_level < 2) return; debug2("--- heap size ---\n"); malloc_check_heap(); for(p = (NPTR)&allocs[0]; clearbusy(next(p)) > p; p=clearbusy(next(p))) { diff --git a/libc/malloc/dprintf.c b/libc/malloc/dprintf.c index 1a2482fe4..7d16e68bf 100644 --- a/libc/malloc/dprintf.c +++ b/libc/malloc/dprintf.c @@ -53,7 +53,7 @@ int __dprintf(const char *fmt, ...) static int fd = -1; if (fd < 0) { - if (!isatty(STDERR_FILENO)) + if (!isatty(STDERR_FILENO)) /* continue piping __dprintf to redirected stderr */ fd = STDERR_FILENO; else fd = open(_PATH_CONSOLE, O_WRONLY); diff --git a/libc/time/ctime.c b/libc/time/ctime.c index f7d022384..1c00046eb 100644 --- a/libc/time/ctime.c +++ b/libc/time/ctime.c @@ -7,7 +7,7 @@ ctime(const time_t *timep) time_t offt; struct tm tmb; struct timezone tz; - static char cbuf[26]; + static char cbuf[30]; gettimeofday((void*)0, &tz); diff --git a/libc/watcom/syscall/gettimeofday.c b/libc/watcom/syscall/gettimeofday.c index 70402f8f5..dcec80b38 100644 --- a/libc/watcom/syscall/gettimeofday.c +++ b/libc/watcom/syscall/gettimeofday.c @@ -8,7 +8,8 @@ int gettimeofday(struct timeval * restrict __tv, void * restrict __tzp) { - sys_setseg(__tv); + if (__tv) sys_setseg(__tv); + else if (__tzp) sys_setseg(__tzp); syscall_res res = sys_call2(SYS_gettimeofday, (unsigned)__tv, (unsigned)__tzp); __syscall_return(int, res); }