Skip to content

Commit

Permalink
Merge pull request #2169 from ghaerr/watcom
Browse files Browse the repository at this point in the history
[libc, Watcom C] Fix gettimeofday, ctime and __amalloc debug speed
  • Loading branch information
ghaerr authored Jan 2, 2025
2 parents 69768f6 + decc6c0 commit ce97f8b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
104 changes: 104 additions & 0 deletions libc/include/watcom/stddef.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

// 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
16 changes: 8 additions & 8 deletions libc/malloc/amalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <unistd.h>
#include <errno.h>
#include <sys/sysctl.h>
#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
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))) {
Expand Down
2 changes: 1 addition & 1 deletion libc/malloc/dprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libc/time/ctime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion libc/watcom/syscall/gettimeofday.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit ce97f8b

Please sign in to comment.