mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-12 05:59:10 +00:00
Make improvements
- Make rand64() thread safe - Introduce lemur64 lcg prng - Improve strace on New Technology - Improve msync() on New Technology
This commit is contained in:
parent
43ba3009b2
commit
29bf8b1a30
73 changed files with 888 additions and 269 deletions
36
libc/intrin/closehandle.greg.c
Normal file
36
libc/intrin/closehandle.greg.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*-*- 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 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/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
|
||||
extern typeof(CloseHandle) *const __imp_CloseHandle __msabi;
|
||||
|
||||
/**
|
||||
* Closes an open object handle.
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
textwindows bool32 CloseHandle(int64_t hObject) {
|
||||
bool32 ok;
|
||||
ok = __imp_CloseHandle(hObject);
|
||||
if (!ok) __winerr();
|
||||
STRACE("CloseHandle(%ld) → %hhhd% m", hObject, ok);
|
||||
return ok;
|
||||
}
|
55
libc/intrin/createnamedpipe.greg.c
Normal file
55
libc/intrin/createnamedpipe.greg.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*-*- 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 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/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/nt/ipc.h"
|
||||
#include "libc/nt/struct/securityattributes.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
|
||||
extern typeof(CreateNamedPipe) *const __imp_CreateNamedPipeW __msabi;
|
||||
|
||||
/**
|
||||
* Creates pipe.
|
||||
*
|
||||
* @return handle to server end
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
textwindows int64_t CreateNamedPipe(
|
||||
const char16_t *lpName, uint32_t dwOpenMode, uint32_t dwPipeMode,
|
||||
uint32_t nMaxInstances, uint32_t nOutBufferSize, uint32_t nInBufferSize,
|
||||
uint32_t nDefaultTimeOutMs,
|
||||
const struct NtSecurityAttributes *opt_lpSecurityAttributes) {
|
||||
int64_t hServer;
|
||||
hServer = __imp_CreateNamedPipeW(lpName, dwOpenMode, dwPipeMode,
|
||||
nMaxInstances, nOutBufferSize, nInBufferSize,
|
||||
nDefaultTimeOutMs, opt_lpSecurityAttributes);
|
||||
if (hServer == -1) __winerr();
|
||||
STRACE("CreateNamedPipe(%#hs,"
|
||||
" dwOpenMode=%u,"
|
||||
" dwPipeMode=%u,"
|
||||
" nMaxInstances=%u,"
|
||||
" nOutBufferSize=%'u,"
|
||||
" nInBufferSize=%'u,"
|
||||
" nDefaultTimeOutMs=%'u,"
|
||||
" lpSecurity=%p) → "
|
||||
"%ld% m",
|
||||
lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize,
|
||||
nInBufferSize, nDefaultTimeOutMs, opt_lpSecurityAttributes, hServer);
|
||||
return hServer;
|
||||
}
|
41
libc/intrin/createpipe.greg.c
Normal file
41
libc/intrin/createpipe.greg.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*-*- 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 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/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/nt/ipc.h"
|
||||
#include "libc/nt/struct/securityattributes.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
|
||||
extern typeof(CreatePipe) *const __imp_CreatePipe __msabi;
|
||||
|
||||
/**
|
||||
* Creates anonymous pipe.
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
textwindows bool32 CreatePipe(
|
||||
int64_t *out_hReadPipe, int64_t *out_hWritePipe,
|
||||
const struct NtSecurityAttributes *opt_lpPipeAttributes, uint32_t nSize) {
|
||||
bool32 ok;
|
||||
ok = __imp_CreatePipe(out_hReadPipe, out_hWritePipe, opt_lpPipeAttributes,
|
||||
nSize);
|
||||
if (!ok) __winerr();
|
||||
STRACE("CreatePipe([%ld], [%ld], %p, %'zu) → %hhhd% m", *out_hReadPipe,
|
||||
*out_hWritePipe, opt_lpPipeAttributes, nSize, ok);
|
||||
return ok;
|
||||
}
|
|
@ -26,6 +26,7 @@ extern typeof(CreateThread) *const __imp_CreateThread __msabi;
|
|||
/**
|
||||
* Opens file on the New Technology.
|
||||
*
|
||||
* @param dwStackSize may be 0 for default per executable
|
||||
* @return thread handle, or 0 on failure
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
|
|
43
libc/intrin/flushfilebuffers.greg.c
Normal file
43
libc/intrin/flushfilebuffers.greg.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*-*- 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 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/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/nt/files.h"
|
||||
|
||||
extern typeof(FlushFileBuffers) *const __imp_FlushFileBuffers __msabi;
|
||||
|
||||
/**
|
||||
* Flushes buffers of specified file to disk.
|
||||
*
|
||||
* This provides a stronger degree of assurance and blocking for things
|
||||
* to be sent to a physical medium, but it's not guaranteed unless your
|
||||
* file is opened in a direct non-caching mode. One main advantage here
|
||||
* seems to be coherency.
|
||||
*
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
* @note consider buying a ups
|
||||
* @see FlushViewOfFile()
|
||||
*/
|
||||
textwindows bool32 FlushFileBuffers(int64_t hFile) {
|
||||
bool32 ok;
|
||||
ok = __imp_FlushFileBuffers(hFile);
|
||||
if (!ok) __winerr();
|
||||
STRACE("FlushFileBuffers(%ld) → %hhhd% m", hFile, ok);
|
||||
return ok;
|
||||
}
|
43
libc/intrin/flushviewoffile.greg.c
Normal file
43
libc/intrin/flushviewoffile.greg.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*-*- 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 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/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/nt/memory.h"
|
||||
|
||||
extern typeof(FlushViewOfFile) *const __imp_FlushViewOfFile __msabi;
|
||||
|
||||
/**
|
||||
* Syncs memory created by MapViewOfFileEx().
|
||||
*
|
||||
* This doesn't wait until the pages are written out to the physical
|
||||
* medium. This doesn't update timestamps or file/dir metadata.
|
||||
*
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
* @note consider buying a ups
|
||||
* @see FlushFileBuffers()
|
||||
*/
|
||||
textwindows bool32 FlushViewOfFile(const void *lpBaseAddress,
|
||||
size_t dwNumberOfBytesToFlush) {
|
||||
bool32 ok;
|
||||
ok = __imp_FlushViewOfFile(lpBaseAddress, dwNumberOfBytesToFlush);
|
||||
if (!ok) __winerr();
|
||||
STRACE("FlushViewOfFile(%p, %'zu) → %hhhd% m", lpBaseAddress,
|
||||
dwNumberOfBytesToFlush, ok);
|
||||
return ok;
|
||||
}
|
|
@ -64,7 +64,14 @@ o/$(MODE)/libc/intrin/kprintf.greg.o: \
|
|||
$(NO_MAGIC)
|
||||
|
||||
o/$(MODE)/libc/intrin/createfile.greg.o \
|
||||
o/$(MODE)/libc/intrin/createpipe.greg.o \
|
||||
o/$(MODE)/libc/intrin/closehandle.greg.o \
|
||||
o/$(MODE)/libc/intrin/createthread.greg.o \
|
||||
o/$(MODE)/libc/intrin/describeflags.greg.o \
|
||||
o/$(MODE)/libc/intrin/createnamedpipe.greg.o \
|
||||
o/$(MODE)/libc/intrin/unmapviewoffile.greg.o \
|
||||
o/$(MODE)/libc/intrin/flushviewoffile.greg.o \
|
||||
o/$(MODE)/libc/intrin/flushfilebuffers.greg.o \
|
||||
o/$(MODE)/libc/intrin/mapviewoffileexnuma.greg.o \
|
||||
o/$(MODE)/libc/intrin/createfilemappingnuma.greg.o \
|
||||
o/$(MODE)/libc/intrin/kstarttsc.o \
|
||||
|
|
42
libc/intrin/nt2prot.greg.c
Normal file
42
libc/intrin/nt2prot.greg.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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/nt/enum/filemapflags.h"
|
||||
#include "libc/nt/enum/pageflags.h"
|
||||
#include "libc/runtime/directmap.internal.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
||||
privileged struct ProtectNt __nt2prot(int prot) {
|
||||
if (prot & PROT_WRITE) {
|
||||
if (prot & PROT_EXEC) {
|
||||
return (struct ProtectNt){kNtPageExecuteReadwrite,
|
||||
kNtFileMapWrite | kNtFileMapExecute};
|
||||
} else {
|
||||
return (struct ProtectNt){kNtPageReadwrite, kNtFileMapWrite};
|
||||
}
|
||||
} else if (prot & PROT_READ) {
|
||||
if (prot & PROT_EXEC) {
|
||||
return (struct ProtectNt){kNtPageExecuteRead,
|
||||
kNtFileMapRead | kNtFileMapExecute};
|
||||
} else {
|
||||
return (struct ProtectNt){kNtPageReadonly, kNtFileMapRead};
|
||||
}
|
||||
} else {
|
||||
return (struct ProtectNt){kNtPageNoaccess};
|
||||
}
|
||||
}
|
35
libc/intrin/unmapviewoffile.greg.c
Normal file
35
libc/intrin/unmapviewoffile.greg.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*-*- 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 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/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/nt/memory.h"
|
||||
|
||||
extern typeof(UnmapViewOfFile) *const __imp_UnmapViewOfFile __msabi;
|
||||
|
||||
/**
|
||||
* Unmaps memory created by MapViewOfFileEx().
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
textwindows bool32 UnmapViewOfFile(const void *lpBaseAddress) {
|
||||
bool32 ok;
|
||||
ok = __imp_UnmapViewOfFile(lpBaseAddress);
|
||||
if (!ok) __winerr();
|
||||
STRACE("UnmapViewOfFile(%p) → %hhhd% m", lpBaseAddress, ok);
|
||||
return ok;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue