Compare commits

...
10 Commits
Author SHA1 Message Date
Justine Tunney 239f8ce76e Release Cosmopolitan v3.5.3 2024-07-01 02:07:56 -07:00
Justine Tunney ca4cf67eb8 Include more programs in cosmocc
The Cosmopolitan Compiler Collection now includes the following programs

- `ar.ape` is a faster alternative to `ar rcsD` for creating determistic
  static archives. It's ~10x faster than GNU because it isn't quadratic.
  It'll even outperform LLVM ar by 2x, thanks to writev/copy_file_range.

- `sha256sum.ape` is a faster alternative to the `sha256sum` command. It
  goes 2x faster since it leverages vectorized assembly implementations.

- `resymbol` is a brand new program we invented, like objcopy, that lets
  you rename all the global symbols in a .o file to have a new suffix or
  prefix. In the future, this will be used by cosmocc automatically when
  building -O3 math kernels, that need to be vectorized for all hardware

- `gzip.ape` is a faster version of the `gzip` command, that is included
  by most Linux distros. It gains better performance using Chromium Zlib
  which, once again, includes highly optimized assembly, that Mark Adler
  won't merge into the official MS-DOS compatible zlib codebase.

- `cocmd` is the cosmopolitan shell. It can function as a faster `sh -c`
  alternative than bash and dash as the `SHELL = /opt/cosmocc/bin/cocmd`
  at the top of your Makefile. Please note you should be using the cosmo
  fork of GNU make (already included), since normal make won't recognize
  this as a bourne-compatible shell and remove the execve() optimization
  which makes things slower. In some ways that's true. This doesn't have
  a complete POSIX shell implementation. However it's enough for cosmo's
  mono repo. It also implements faster behaviors in some respects.

The following programs are also introduced, which aren't as interesting.
The main reason why they're here is so Cosmopolitan's mono repo shall be
able to remove build/bootstrap/ in future editions. That way we can keep
build utilities better up to date, without bloating the git history much

- `chmod.ape` for hermeticity
- `cp.ape` for hermeticity
- `echo.ape` for hermeticity
- `objbincopy` is an objcopy-like tool that's used to build ape loader
- `package.ape` is used for strict dependency checking of object graph
- `rm.ape` for hermeticity
- `touch.ape` for hermeticity
2024-07-01 02:05:25 -07:00
Justine Tunney 78d3b86ec7 Fix Android support
Thanks to @aj47 (techfren.net) the new Cosmo memory manager is confirmed
to be working on Android!! The only issue turned out to be forgetting to
update the program address in the linker script. We now know w/ absolute
certainty that APE binaries as complex as llamafile, now work correctly.
2024-07-01 01:06:47 -07:00
Justine Tunney 44191b3f50 Add more type traits to CTL 2024-06-30 20:59:38 -07:00
Justine Tunney e437bed006 Fix crash caused when Windows needs a lot of TLS 2024-06-30 20:53:43 -07:00
Justine Tunney 76957983cf Make POSIX threads improvements
- Ensure SIGTHR isn't blocked in newly created threads
- Use TIB rather than thread_local for thread atexits
- Make POSIX thread keys atomic within thread
- Don't bother logging prctl() to --strace
- Log thread destructor names to --strace
2024-06-30 15:38:59 -07:00
Justine Tunney 387310c659 Fix issue with ctl::vector constructor 2024-06-30 02:26:38 -07:00
Justine Tunney 4cb5e21ba8 Introduce pthread_decimate_np() api
This is useful with CheckForMemoryLeaks().
2024-06-30 02:26:06 -07:00
Justine Tunney 1bf2d8e308 Further improve mmap() locking story
The way to use double linked lists, is to remove all the things you want
to work on, insert them into a new list on the stack. Then once you have
all the work items, you release the lock, do your work, and then lock it
again, to add the shelled out items back to a global freelist.
2024-06-29 17:12:43 -07:00
Justine Tunney 98e684622b Add iostream to CTL 2024-06-29 15:45:09 -07:00
63 changed files with 2088 additions and 364 deletions

No files matched your search

+1 -1
View File
@@ -256,7 +256,6 @@ include third_party/nsync/mem/BUILD.mk # │ You can now use stdio
include libc/proc/BUILD.mk # │ You can now use threads
include libc/dlopen/BUILD.mk # │ You can now use processes
include libc/thread/BUILD.mk # │ You can finally call malloc()
include ctl/BUILD.mk # │
include third_party/zlib/BUILD.mk # │
include libc/stdio/BUILD.mk # │
include tool/hello/BUILD.mk # │
@@ -285,6 +284,7 @@ include third_party/ncurses/BUILD.mk # │
include third_party/readline/BUILD.mk # │
include third_party/libunwind/BUILD.mk # |
include third_party/libcxxabi/BUILD.mk # |
include ctl/BUILD.mk # │
include third_party/libcxx/BUILD.mk # │
include third_party/openmp/BUILD.mk # │
include third_party/double-conversion/BUILD.mk # │
+1 -1
View File
@@ -12,7 +12,7 @@ OUTPUT_FORMAT("elf64-littleaarch64",
SECTIONS {
. = SEGMENT_START("text-segment", 0x010000000000);
. = SEGMENT_START("text-segment", 0x000800000000);
__executable_start = .;
. += SIZEOF_HEADERS;
Binary file not shown.
+5
View File
@@ -18,7 +18,12 @@ CTL_A_CHECKS = \
CTL_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_STDIO \
LIBC_STR \
THIRD_PARTY_GDTOA \
THIRD_PARTY_LIBCXXABI \
THIRD_PARTY_LIBUNWIND \
CTL_A_DEPS := $(call uniq,$(foreach x,$(CTL_A_DIRECTDEPS),$($(x))))
+6 -2
View File
@@ -37,24 +37,28 @@ struct array
constexpr reference at(size_type pos)
{
if (pos >= N)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return elems[pos];
}
constexpr const_reference at(size_type pos) const
{
if (pos >= N)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return elems[pos];
}
constexpr reference operator[](size_type pos)
{
if (pos >= N)
__builtin_trap();
return elems[pos];
}
constexpr const_reference operator[](size_type pos) const
{
if (pos >= N)
__builtin_trap();
return elems[pos];
}
+2
View File
@@ -11,10 +11,12 @@ struct integral_constant
static constexpr T value = v;
typedef T value_type;
typedef integral_constant type;
constexpr operator value_type() const noexcept
{
return value;
}
constexpr value_type operator()() const noexcept
{
return value;
+39
View File
@@ -0,0 +1,39 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ios.h"
namespace ctl {
ios::ios(FILE* file) : file_(file)
{
}
ios::~ios() = default;
ios&
ios::operator=(ios&& other) noexcept
{
if (this != &other) {
file_ = other.file_;
other.file_ = nullptr;
}
return *this;
}
} // namespace ctl
+27
View File
@@ -0,0 +1,27 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IOS_H_
#define CTL_IOS_H_
#include "ios_base.h"
struct FILE;
namespace ctl {
class ios : public ios_base
{
public:
explicit ios(FILE*);
virtual ~ios();
protected:
ios& operator=(ios&&) noexcept;
FILE* file_;
private:
ios(const ios&) = delete;
};
} // namespace ctl
#endif // CTL_IOS_H_
+118
View File
@@ -0,0 +1,118 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ios_base.h"
namespace ctl {
ios_base::ios_base() : state_(goodbit), flags_(skipws | dec)
{
}
ios_base::~ios_base() = default;
ios_base::fmtflags
ios_base::flags() const
{
return static_cast<ios_base::fmtflags>(flags_);
}
ios_base::fmtflags
ios_base::flags(fmtflags fmtfl)
{
int old = flags_;
flags_ = fmtfl;
return static_cast<ios_base::fmtflags>(old);
}
ios_base::fmtflags
ios_base::setf(fmtflags fmtfl)
{
int old = flags_;
flags_ |= fmtfl;
return static_cast<ios_base::fmtflags>(old);
}
ios_base::fmtflags
ios_base::setf(fmtflags fmtfl, fmtflags mask)
{
int old = flags_;
flags_ = (flags_ & ~mask) | (fmtfl & mask);
return static_cast<ios_base::fmtflags>(old);
}
void
ios_base::unsetf(fmtflags mask)
{
flags_ &= ~mask;
}
ios_base::iostate
ios_base::rdstate() const
{
return static_cast<ios_base::iostate>(state_);
}
void
ios_base::clear(int state)
{
state_ = state;
}
void
ios_base::setstate(int state)
{
state_ |= state;
}
bool
ios_base::good() const
{
return state_ == goodbit;
}
bool
ios_base::eof() const
{
return (state_ & eofbit) != 0;
}
bool
ios_base::fail() const
{
return (state_ & (failbit | badbit)) != 0;
}
bool
ios_base::bad() const
{
return (state_ & badbit) != 0;
}
bool
ios_base::operator!() const
{
return fail();
}
ios_base::operator bool() const
{
return !fail();
}
} // namespace ctl
+86
View File
@@ -0,0 +1,86 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IOS_BASE_H_
#define CTL_IOS_BASE_H_
namespace ctl {
class ios_base
{
public:
typedef size_t streamsize;
enum iostate
{
goodbit = 0,
badbit = 1,
failbit = 2,
eofbit = 4
};
enum fmtflags
{
boolalpha = 1 << 0,
dec = 1 << 1,
fixed = 1 << 2,
hex = 1 << 3,
internal = 1 << 4,
left = 1 << 5,
oct = 1 << 6,
right = 1 << 7,
scientific = 1 << 8,
showbase = 1 << 9,
showpoint = 1 << 10,
showpos = 1 << 11,
skipws = 1 << 12,
unitbuf = 1 << 13,
uppercase = 1 << 14,
adjustfield = left | right | internal,
basefield = dec | oct | hex,
floatfield = scientific | fixed
};
enum openmode
{
app = 1 << 0,
binary = 1 << 1,
in = 1 << 2,
out = 1 << 3,
trunc = 1 << 4,
ate = 1 << 5
};
protected:
ios_base();
virtual ~ios_base();
int state_;
int flags_;
public:
fmtflags flags() const;
fmtflags flags(fmtflags);
fmtflags setf(fmtflags);
fmtflags setf(fmtflags, fmtflags);
void unsetf(fmtflags);
iostate rdstate() const;
void clear(int = goodbit);
void setstate(int);
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;
bool operator!() const;
explicit operator bool() const;
private:
ios_base(const ios_base&) = delete;
ios_base& operator=(const ios_base&) = delete;
};
} // namespace ctl
#endif // CTL_IOS_BASE_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_ABSTRACT_H_
#define CTL_IS_ABSTRACT_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_abstract : public integral_constant<bool, __is_abstract(T)>
{};
template<typename T>
inline constexpr bool is_abstract_v = __is_abstract(T);
} // namespace ctl
#endif // CTL_IS_ABSTRACT_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_BASE_OF_H_
#define CTL_IS_BASE_OF_H_
#include "integral_constant.h"
namespace ctl {
template<typename Base, typename Derived>
struct is_base_of : public integral_constant<bool, __is_base_of(Base, Derived)>
{};
template<typename Base, typename Derived>
inline constexpr bool is_base_of_v = __is_base_of(Base, Derived);
} // namespace ctl
#endif // CTL_IS_BASE_OF_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_CLASS_H_
#define CTL_IS_CLASS_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_class : public integral_constant<bool, __is_class(T)>
{};
template<typename T>
inline constexpr bool is_class_v = __is_class(T);
} // namespace ctl
#endif // CTL_IS_CLASS_H_
+19
View File
@@ -0,0 +1,19 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_CONSTRUCTIBLE_H_
#define CTL_IS_CONSTRUCTIBLE_H_
#include "integral_constant.h"
namespace ctl {
template<class _Tp, class... _Args>
struct is_constructible
: public integral_constant<bool, __is_constructible(_Tp, _Args...)>
{};
template<class _Tp, class... _Args>
inline constexpr bool is_constructible_v = __is_constructible(_Tp, _Args...);
} // namespace ctl
#endif // CTL_IS_CONSTRUCTIBLE_H_
-1
View File
@@ -2,7 +2,6 @@
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_CONVERTIBLE_H_
#define CTL_IS_CONVERTIBLE_H_
#include "ctl/integral_constant.h"
#include "ctl/void_t.h"
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_EMPTY_H_
#define CTL_IS_EMPTY_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_empty : public integral_constant<bool, __is_empty(T)>
{};
template<typename T>
inline constexpr bool is_empty_v = __is_empty(T);
} // namespace ctl
#endif // CTL_IS_EMPTY_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_ENUM_H_
#define CTL_IS_ENUM_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_enum : public integral_constant<bool, __is_enum(T)>
{};
template<typename T>
inline constexpr bool is_enum_v = __is_enum(T);
} // namespace ctl
#endif // CTL_IS_ENUM_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_POLYMORPHIC_H_
#define CTL_IS_POLYMORPHIC_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_polymorphic : public integral_constant<bool, __is_polymorphic(T)>
{};
template<typename T>
inline constexpr bool is_polymorphic_v = __is_polymorphic(T);
} // namespace ctl
#endif // CTL_IS_POLYMORPHIC_H_
+19
View File
@@ -0,0 +1,19 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_STANDARD_LAYOUT_H_
#define CTL_IS_STANDARD_LAYOUT_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_standard_layout
: public integral_constant<bool, __is_standard_layout(T)>
{};
template<typename T>
inline constexpr bool is_standard_layout_v = __is_standard_layout(T);
} // namespace ctl
#endif // CTL_IS_STANDARD_LAYOUT_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_TRIVIAL_H_
#define CTL_IS_TRIVIAL_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_trivial : public integral_constant<bool, __is_trivial(T)>
{};
template<typename T>
inline constexpr bool is_trivial_v = __is_trivial(T);
} // namespace ctl
#endif // CTL_IS_TRIVIAL_H_
+18
View File
@@ -0,0 +1,18 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_IS_UNION_H_
#define CTL_IS_UNION_H_
#include "integral_constant.h"
namespace ctl {
template<typename T>
struct is_union : public integral_constant<bool, __is_union(T)>
{};
template<typename T>
inline constexpr bool is_union_v = __is_union(T);
} // namespace ctl
#endif // CTL_IS_UNION_H_
+237
View File
@@ -0,0 +1,237 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "istream.h"
#include "libc/fmt/conv.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "numeric_limits.h"
#include "string.h"
#include "utility.h"
namespace ctl {
istream cin(stdin);
istream::~istream() = default;
istream::istream(FILE* file) : ios(file), gcount_(0)
{
}
istream::istream(istream&& other) noexcept
: ios(other.file_), gcount_(other.gcount_)
{
other.file_ = nullptr;
other.gcount_ = 0;
}
istream&
istream::operator=(istream&& other) noexcept
{
if (this != &other) {
ios::operator=(ctl::move(other));
gcount_ = other.gcount_;
other.gcount_ = 0;
}
return *this;
}
istream&
istream::operator>>(char& c)
{
gcount_ = 0;
int ch = fgetc(file_);
if (ch == EOF) {
setstate(eofbit | failbit);
} else {
c = static_cast<char>(ch);
gcount_ = 1;
}
return *this;
}
istream&
istream::operator>>(int& n)
{
if (fscanf(file_, "%d", &n) != 1)
setstate(failbit);
return *this;
}
istream&
istream::operator>>(long& n)
{
if (fscanf(file_, "%ld", &n) != 1)
setstate(failbit);
return *this;
}
istream&
istream::operator>>(double& d)
{
if (fscanf(file_, "%f", &d) != 1)
setstate(failbit);
return *this;
}
istream&
istream::operator>>(ctl::string& s)
{
char buffer[1024];
if (fscanf(file_, "%1023s", buffer) == 1) {
s = buffer;
} else {
setstate(failbit);
}
return *this;
}
istream&
istream::operator>>(bool& b)
{
char buffer[6];
if (fscanf(file_, "%5s", buffer) == 1) {
if (strcmp(buffer, "true") == 0 || strcmp(buffer, "1") == 0) {
b = true;
} else if (strcmp(buffer, "false") == 0 || strcmp(buffer, "0") == 0) {
b = false;
} else {
setstate(failbit);
}
} else {
setstate(failbit);
}
return *this;
}
istream&
istream::operator>>(istream& (*manip)(istream&))
{
return manip(*this);
}
int
istream::get()
{
gcount_ = 0;
int ch = fgetc(file_);
if (ch == EOF) {
setstate(eofbit);
} else {
gcount_ = 1;
}
return ch;
}
istream&
istream::get(char& c)
{
int ch = get();
if (ch != EOF)
c = static_cast<char>(ch);
return *this;
}
istream&
istream::getline(char* s, streamsize n)
{
return getline(s, n, '\n');
}
istream&
istream::getline(char* s, streamsize n, char delim)
{
gcount_ = 0;
if (n <= 0) {
setstate(failbit);
return *this;
}
while (gcount_ < n - 1) {
int ch = fgetc(file_);
if (ch == EOF) {
setstate(eofbit);
break;
}
if (ch == delim)
break;
s[gcount_++] = static_cast<char>(ch);
}
s[gcount_] = '\0';
if (gcount_ == 0)
setstate(failbit);
return *this;
}
istream&
istream::read(char* s, streamsize n)
{
gcount_ = fread(s, 1, n, file_);
if (gcount_ < n) {
setstate(eofbit);
if (gcount_ == 0)
setstate(failbit);
}
return *this;
}
istream::streamsize
istream::gcount() const
{
return gcount_;
}
int
istream::peek()
{
int ch = fgetc(file_);
if (ch != EOF) {
ungetc(ch, file_);
} else {
setstate(eofbit);
}
return ch;
}
istream&
istream::ignore(streamsize n, int delim)
{
gcount_ = 0;
while (gcount_ < n) {
int ch = fgetc(file_);
if (ch == EOF) {
setstate(eofbit);
break;
}
++gcount_;
if (ch == delim)
break;
}
return *this;
}
istream&
ws(istream& is)
{
int ch;
while ((ch = is.peek()) != EOF && isspace(ch))
is.get();
return is;
}
} // namespace ctl
+53
View File
@@ -0,0 +1,53 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_ISTREAM_H_
#define CTL_ISTREAM_H_
#include "ios.h"
namespace ctl {
struct string;
class istream : public ios
{
public:
istream() = delete;
explicit istream(FILE*);
virtual ~istream();
istream& operator>>(char&);
istream& operator>>(int&);
istream& operator>>(long&);
istream& operator>>(double&);
istream& operator>>(bool&);
istream& operator>>(string&);
istream& operator>>(istream& (*)(istream&));
int get();
istream& get(char&);
istream& getline(char*, streamsize);
istream& getline(char*, streamsize, char);
istream& read(char*, streamsize);
streamsize gcount() const;
int peek();
istream& ignore(streamsize = 1, int = -1);
istream(istream&&) noexcept;
istream& operator=(istream&&) noexcept;
private:
streamsize gcount_;
istream(const istream&) = delete;
istream& operator=(const istream&) = delete;
};
extern istream cin;
istream&
ws(istream& is);
} // namespace ctl
#endif // CTL_ISTREAM_H_
+221
View File
@@ -0,0 +1,221 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "istringstream.h"
#include "libc/fmt/conv.h"
#include "libc/str/str.h"
namespace ctl {
istringstream::istringstream() : buffer_(), read_pos_(0)
{
}
istringstream::istringstream(const string_view& str)
: buffer_(str), read_pos_(0)
{
}
string
istringstream::str() const
{
return buffer_;
}
void
istringstream::str(const string& s)
{
buffer_ = s;
read_pos_ = 0;
clear();
}
istringstream&
istringstream::operator>>(char& c)
{
if (good() && read_pos_ < buffer_.size()) {
c = buffer_[read_pos_++];
} else {
setstate(ios_base::failbit);
}
return *this;
}
istringstream&
istringstream::operator>>(char* s)
{
if (!good())
return *this;
while (read_pos_ < buffer_.size() && isspace(buffer_[read_pos_]))
++read_pos_;
size_t start = read_pos_;
while (read_pos_ < buffer_.size() && !isspace(buffer_[read_pos_])) {
s[read_pos_ - start] = buffer_[read_pos_];
++read_pos_;
}
s[read_pos_ - start] = '\0';
if (start == read_pos_) {
setstate(ios_base::failbit);
} else if (read_pos_ == buffer_.size()) {
setstate(ios_base::eofbit);
}
return *this;
}
istringstream&
istringstream::operator>>(string& s)
{
if (!good())
return *this;
s.clear();
while (read_pos_ < buffer_.size() && isspace(buffer_[read_pos_]))
++read_pos_;
while (read_pos_ < buffer_.size() && !isspace(buffer_[read_pos_])) {
s.push_back(buffer_[read_pos_]);
++read_pos_;
}
if (s.empty()) {
setstate(ios_base::failbit);
} else if (read_pos_ == buffer_.size()) {
setstate(ios_base::eofbit);
}
return *this;
}
template<typename T>
istringstream&
istringstream::read_numeric(T& value)
{
if (!good())
return *this;
while (read_pos_ < buffer_.size() && isspace(buffer_[read_pos_]))
++read_pos_;
// size_t start = read_pos_;
bool is_negative = false;
if (read_pos_ < buffer_.size() &&
(buffer_[read_pos_] == '-' || buffer_[read_pos_] == '+')) {
is_negative = (buffer_[read_pos_] == '-');
++read_pos_;
}
T result = 0;
bool valid = false;
while (read_pos_ < buffer_.size() && isdigit(buffer_[read_pos_])) {
result = result * 10 + (buffer_[read_pos_] - '0');
++read_pos_;
valid = true;
}
if (valid) {
value = is_negative ? -result : result;
if (read_pos_ == buffer_.size())
setstate(ios_base::eofbit);
} else {
setstate(ios_base::failbit);
}
return *this;
}
istringstream&
istringstream::operator>>(short& n)
{
return read_numeric(n);
}
istringstream&
istringstream::operator>>(unsigned short& n)
{
return read_numeric(n);
}
istringstream&
istringstream::operator>>(int& n)
{
return read_numeric(n);
}
istringstream&
istringstream::operator>>(unsigned int& n)
{
return read_numeric(n);
}
istringstream&
istringstream::operator>>(long& n)
{
return read_numeric(n);
}
istringstream&
istringstream::operator>>(unsigned long& n)
{
return read_numeric(n);
}
istringstream&
istringstream::operator>>(float& f)
{
if (!good())
return *this;
char* end;
f = strtof(buffer_.c_str() + read_pos_, &end);
if (end == buffer_.c_str() + read_pos_) {
setstate(ios_base::failbit);
} else {
read_pos_ = end - buffer_.c_str();
if (read_pos_ == buffer_.size())
setstate(ios_base::eofbit);
}
return *this;
}
istringstream&
istringstream::operator>>(double& d)
{
if (!good())
return *this;
char* end;
d = strtod(buffer_.c_str() + read_pos_, &end);
if (end == buffer_.c_str() + read_pos_) {
setstate(ios_base::failbit);
} else {
read_pos_ = end - buffer_.c_str();
if (read_pos_ == buffer_.size())
setstate(ios_base::eofbit);
}
return *this;
}
} // namespace ctl
+41
View File
@@ -0,0 +1,41 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_ISTRINGSTREAM_H_
#define CTL_ISTRINGSTREAM_H_
#include "ios_base.h"
#include "string.h"
namespace ctl {
class istringstream : public ios_base
{
public:
istringstream();
explicit istringstream(const string_view&);
string str() const;
void str(const string&);
istringstream& operator>>(char&);
istringstream& operator>>(char*);
istringstream& operator>>(string&);
istringstream& operator>>(short&);
istringstream& operator>>(unsigned short&);
istringstream& operator>>(int&);
istringstream& operator>>(unsigned int&);
istringstream& operator>>(long&);
istringstream& operator>>(unsigned long&);
istringstream& operator>>(float&);
istringstream& operator>>(double&);
private:
string buffer_;
size_t read_pos_;
template<typename T>
istringstream& read_numeric(T&);
};
} // namespace ctl
#endif // CTL_ISTRINGSTREAM_H_
+22 -11
View File
@@ -2,12 +2,33 @@
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_ITERATOR_TRAITS_H_
#define CTL_ITERATOR_TRAITS_H_
#include "iterator.h"
#include "utility.h"
#include "void_t.h"
namespace ctl {
template<class Iterator>
template<typename Iterator, typename = void>
struct iterator_traits
{};
template<typename T>
struct iterator_traits<T*>
{
using difference_type = ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = ctl::random_access_iterator_tag;
};
template<typename Iterator>
struct iterator_traits<Iterator,
ctl::void_t<typename Iterator::iterator_category,
typename Iterator::value_type,
typename Iterator::difference_type,
typename Iterator::pointer,
typename Iterator::reference>>
{
using iterator_category = typename Iterator::iterator_category;
using value_type = typename Iterator::value_type;
@@ -16,16 +37,6 @@ struct iterator_traits
using reference = typename Iterator::reference;
};
template<class T>
struct iterator_traits<T*>
{
using iterator_category = void*; // We don't actually use this
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
};
} // namespace ctl
#endif // CTL_ITERATOR_TRAITS_H_
+2 -2
View File
@@ -165,7 +165,7 @@ class map
{
auto it = find(key);
if (it == end())
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return it->second;
}
@@ -173,7 +173,7 @@ class map
{
auto it = find(key);
if (it == end())
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return it->second;
}
+166
View File
@@ -0,0 +1,166 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ostream.h"
#include "libc/stdio/stdio.h"
#include "string_view.h"
namespace ctl {
ostream cout(stdout);
ostream cerr(stderr);
ostream::~ostream() = default;
ostream::ostream(FILE* file) : ios(file)
{
}
ostream::ostream(ostream&& other) noexcept : ios(other.file_)
{
other.file_ = nullptr;
}
ostream&
ostream::operator=(ostream&& other) noexcept
{
if (this != &other) {
file_ = other.file_;
other.file_ = nullptr;
}
return *this;
}
ostream&
ostream::operator<<(const char* str)
{
if (good() && str)
if (fprintf(file_, "%s", str) < 0)
setstate(badbit);
return *this;
}
ostream&
ostream::operator<<(char c)
{
if (good())
if (fputc(c, file_) == EOF)
setstate(badbit);
return *this;
}
ostream&
ostream::operator<<(int n)
{
if (good())
if (fprintf(file_, "%d", n) < 0)
setstate(badbit);
return *this;
}
ostream&
ostream::operator<<(long n)
{
if (good())
if (fprintf(file_, "%ld", n) < 0)
setstate(badbit);
return *this;
}
ostream&
ostream::operator<<(double d)
{
if (good())
if (fprintf(file_, "%f", d) < 0)
setstate(badbit);
return *this;
}
ostream&
ostream::operator<<(const string_view& s)
{
if (good())
if (fprintf(file_, "%.*s", (int)s.size(), s.data()) < 0)
setstate(badbit);
return *this;
}
ostream&
ostream::operator<<(bool b)
{
if (good()) {
const char* value =
(flags() & boolalpha) ? (b ? "true" : "false") : (b ? "1" : "0");
if (fprintf(file_, "%s", value) < 0)
setstate(badbit);
}
return *this;
}
ostream&
ostream::operator<<(ostream& (*manip)(ostream&))
{
return manip(*this);
}
ostream&
ostream::put(char c)
{
if (good())
if (fputc(c, file_) == EOF)
setstate(badbit);
return *this;
}
ostream&
ostream::write(const char* s, streamsize n)
{
if (good())
if (fwrite(s, 1, n, file_) != static_cast<size_t>(n))
setstate(badbit);
return *this;
}
ostream&
ostream::flush()
{
if (good())
if (fflush(file_) != 0)
setstate(badbit);
return *this;
}
ostream&
endl(ostream& os)
{
return os.put('\n').flush();
}
ostream&
ends(ostream& os)
{
return os.put('\0');
}
ostream&
flush(ostream& os)
{
return os.flush();
}
} // namespace ctl
+53
View File
@@ -0,0 +1,53 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_OSTREAM_H_
#define CTL_OSTREAM_H_
#include "ios.h"
namespace ctl {
struct string_view;
class ostream : public ios
{
public:
ostream() = delete;
explicit ostream(FILE*);
virtual ~ostream();
ostream& operator<<(const char*);
ostream& operator<<(char);
ostream& operator<<(int);
ostream& operator<<(long);
ostream& operator<<(double);
ostream& operator<<(const string_view&);
ostream& operator<<(bool);
ostream& operator<<(ostream& (*)(ostream&));
ostream& put(char);
ostream& write(const char*, streamsize);
ostream& flush();
ostream(ostream&&) noexcept;
ostream& operator=(ostream&&) noexcept;
private:
ostream(const ostream&) = delete;
ostream& operator=(const ostream&) = delete;
};
extern ostream cout;
extern ostream cerr;
ostream&
endl(ostream&);
ostream&
ends(ostream&);
ostream&
flush(ostream&);
} // namespace ctl
#endif // CTL_OSTREAM_H_
+140
View File
@@ -0,0 +1,140 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ostringstream.h"
#include "libc/fmt/itoa.h"
#include "libc/stdio/stdio.h"
namespace ctl {
ostringstream::ostringstream() : buffer_(), write_pos_(0)
{
}
ostringstream::ostringstream(const string_view& str)
: buffer_(str), write_pos_(0)
{
}
string
ostringstream::str() const
{
return buffer_;
}
void
ostringstream::str(const string& s)
{
buffer_ = s;
write_pos_ = 0;
}
void
ostringstream::clear()
{
ios_base::clear();
}
ostringstream&
ostringstream::operator<<(char c)
{
if (good()) {
if (write_pos_ < buffer_.size()) {
buffer_[write_pos_++] = c;
} else {
buffer_.push_back(c);
++write_pos_;
}
}
return *this;
}
ostringstream&
ostringstream::operator<<(const string_view& s)
{
if (good()) {
if (write_pos_ + s.size() <= buffer_.size()) {
buffer_.replace(write_pos_, s.size(), s);
} else {
buffer_.replace(write_pos_, buffer_.size() - write_pos_, s);
buffer_.append(s.substr(buffer_.size() - write_pos_));
}
write_pos_ += s.size();
}
return *this;
}
ostringstream&
ostringstream::operator<<(int n)
{
char temp[12];
if (good())
*this << string_view(temp, FormatInt32(temp, n) - temp);
return *this;
}
ostringstream&
ostringstream::operator<<(unsigned int n)
{
char temp[12];
if (good())
*this << string_view(temp, FormatUint32(temp, n) - temp);
return *this;
}
ostringstream&
ostringstream::operator<<(long n)
{
char temp[21];
if (good())
*this << string_view(temp, FormatInt64(temp, n) - temp);
return *this;
}
ostringstream&
ostringstream::operator<<(unsigned long n)
{
char temp[21];
if (good())
*this << string_view(temp, FormatUint64(temp, n) - temp);
return *this;
}
ostringstream&
ostringstream::operator<<(float f)
{
if (good()) {
char temp[32];
int len = snprintf(temp, sizeof(temp), "%g", f);
*this << string_view(temp, len);
}
return *this;
}
ostringstream&
ostringstream::operator<<(double d)
{
if (good()) {
char temp[32];
int len = snprintf(temp, sizeof(temp), "%g", d);
*this << string_view(temp, len);
}
return *this;
}
} // namespace ctl
+36
View File
@@ -0,0 +1,36 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_OSTRINGSTREAM_H_
#define CTL_OSTRINGSTREAM_H_
#include "ios_base.h"
#include "string.h"
namespace ctl {
class ostringstream : public ios_base
{
public:
ostringstream();
explicit ostringstream(const string_view&);
string str() const;
void str(const string& s);
void clear();
ostringstream& operator<<(char);
ostringstream& operator<<(const string_view&);
ostringstream& operator<<(int);
ostringstream& operator<<(unsigned int);
ostringstream& operator<<(long);
ostringstream& operator<<(unsigned long);
ostringstream& operator<<(float);
ostringstream& operator<<(double);
private:
string buffer_;
size_t write_pos_;
};
} // namespace ctl
#endif // CTL_OSTRINGSTREAM_H_
+19
View File
@@ -0,0 +1,19 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_REQUIRE_INPUT_ITERATOR_H_
#define CTL_REQUIRE_INPUT_ITERATOR_H_
#include "enable_if.h"
#include "is_convertible.h"
#include "iterator.h"
#include "iterator_traits.h"
namespace ctl {
template<typename InputIt>
using require_input_iterator = typename ctl::enable_if<
ctl::is_convertible<typename ctl::iterator_traits<InputIt>::iterator_category,
ctl::input_iterator_tag>::value>::type;
} // namespace ctl
#endif /* CTL_REQUIRE_INPUT_ITERATOR_H_ */
+3 -3
View File
@@ -1,7 +1,7 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef COSMOPOLITAN_CTL_UTILITY_H_
#define COSMOPOLITAN_CTL_UTILITY_H_
#ifndef CTL_UTILITY_H_
#define CTL_UTILITY_H_
#include "remove_reference.h"
namespace ctl {
@@ -66,4 +66,4 @@ declval() noexcept;
} // namespace ctl
#endif // COSMOPOLITAN_CTL_UTILITY_H_
#endif // CTL_UTILITY_H_
+10 -5
View File
@@ -15,6 +15,7 @@
#include "move_backward.h"
#include "move_iterator.h"
#include "out_of_range.h"
#include "require_input_iterator.h"
#include "reverse_iterator.h"
#include "uninitialized_fill.h"
#include "uninitialized_fill_n.h"
@@ -65,7 +66,7 @@ class vector
resize(count);
}
template<class InputIt>
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator())
: alloc_(alloc), data_(nullptr), size_(0), capacity_(0)
{
@@ -173,7 +174,7 @@ class vector
size_ = count;
}
template<class InputIt>
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
void assign(InputIt first, InputIt last)
{
clear();
@@ -189,24 +190,28 @@ class vector
reference at(size_type pos)
{
if (pos >= size_)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return data_[pos];
}
const_reference at(size_type pos) const
{
if (pos >= size_)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return data_[pos];
}
reference operator[](size_type pos)
{
if (pos >= size_)
__builtin_trap();
return data_[pos];
}
const_reference operator[](size_type pos) const
{
if (pos >= size_)
__builtin_trap();
return data_[pos];
}
@@ -368,7 +373,7 @@ class vector
return it;
}
template<class InputIt>
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
iterator insert(const_iterator pos, InputIt first, InputIt last)
{
difference_type count = ctl::distance(first, last);
-10
View File
@@ -53,15 +53,5 @@ int prctl(int operation, ...) {
rc = enosys();
}
#if SYSDEBUG
if (operation == PR_CAPBSET_READ || operation == PR_CAPBSET_DROP) {
STRACE("prctl(%s, %s) → %d% m", DescribePrctlOperation(operation),
DescribeCapability(a), rc);
} else {
STRACE("prctl(%s, %p, %p, %p, %p) → %d% m",
DescribePrctlOperation(operation), a, b, c, d, rc);
}
#endif
return rc;
}
+1 -1
View File
@@ -4,7 +4,7 @@
#define __COSMOPOLITAN_MAJOR__ 3
#define __COSMOPOLITAN_MINOR__ 5
#define __COSMOPOLITAN_PATCH__ 2
#define __COSMOPOLITAN_PATCH__ 3
#define __COSMOPOLITAN__ \
(100000000 * __COSMOPOLITAN_MAJOR__ + 1000000 * __COSMOPOLITAN_MINOR__ + \
__COSMOPOLITAN_PATCH__)
+3 -3
View File
@@ -1,5 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_
#include "libc/mem/alloca.h"
#include "libc/nexgen32e/stackframe.h"
COSMOPOLITAN_C_START_
@@ -8,4 +8,4 @@ const char *DescribeBacktrace(char[160], const struct StackFrame *) libcesque;
#define DescribeBacktrace(x) DescribeBacktrace(alloca(160), x)
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_ */
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_ */
-4
View File
@@ -13,7 +13,6 @@ const char *DescribeFlags(char *, size_t, const struct DescribeFlags *, size_t,
const char *DescribeArchPrctlCode(char[12], int) libcesque;
const char *DescribeCancelState(char[12], int, int *) libcesque;
const char *DescribeCapability(char[32], int) libcesque;
const char *DescribeClockName(char[32], int) libcesque;
const char *DescribeControlKeyState(char[64], uint32_t) libcesque;
const char *DescribeDirfd(char[12], int) libcesque;
@@ -47,7 +46,6 @@ const char *DescribeOpenFlags(char[128], int) libcesque;
const char *DescribeOpenMode(char[15], int, int) libcesque;
const char *DescribePersonalityFlags(char[128], int) libcesque;
const char *DescribePollFlags(char[64], int) libcesque;
const char *DescribePrctlOperation(int) libcesque;
const char *DescribeProtFlags(char[48], int) libcesque;
const char *DescribePtrace(char[12], int) libcesque;
const char *DescribePtraceEvent(char[32], int) libcesque;
@@ -69,9 +67,7 @@ const char *DescribeVirtualKeyCode(char[32], uint32_t) libcesque;
const char *DescribeWhence(char[12], int) libcesque;
const char *DescribeWhichPrio(char[12], int) libcesque;
#define DescribeArchPrctlCode(x) DescribeArchPrctlCode(alloca(12), x)
#define DescribeCancelState(x, y) DescribeCancelState(alloca(12), x, y)
#define DescribeCapability(x) DescribeCapability(alloca(32), x)
#define DescribeClockName(x) DescribeClockName(alloca(32), x)
#define DescribeControlKeyState(x) DescribeControlKeyState(alloca(64), x)
#define DescribeDirfd(x) DescribeDirfd(alloca(12), x)
-1
View File
@@ -39,7 +39,6 @@ struct AddrSize {
extern struct Maps __maps;
void *randaddr(void);
void __maps_init(void);
void __maps_lock(void);
void __maps_check(void);
+77 -68
View File
@@ -21,6 +21,7 @@
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
@@ -187,10 +188,10 @@ int __munmap(char *addr, size_t size, bool untrack_only) {
!size || (uintptr_t)addr + size < size)
return einval();
// untrack and delete mapping
// untrack mappings
int rc = 0;
struct Dll *delete = 0;
__maps_lock();
StartOver:;
struct Map *map = __maps.maps;
struct Map **prev = &__maps.maps;
while (map) {
@@ -202,30 +203,10 @@ StartOver:;
if (addr <= map_addr && addr + PGUP(size) >= map_addr + PGUP(map_size)) {
// remove mapping completely
dll_remove(&__maps.used, &map->elem);
dll_make_first(&delete, &map->elem);
*prev = next;
__maps.pages -= (map_size + pagesz - 1) / pagesz;
__maps.count -= 1;
if (untrack_only) {
__maps_free(map);
__maps_check();
} else {
__maps_unlock();
if (!IsWindows()) {
ASSERT(addr <= map_addr);
ASSERT(map_addr + PGUP(map_size) <= addr + PGUP(size));
if (sys_munmap(map_addr, map_size))
rc = -1;
} else {
if (!UnmapViewOfFile(map_addr))
rc = -1;
if (!CloseHandle(map->h))
rc = -1;
}
__maps_lock();
__maps_free(map);
__maps_check();
goto StartOver;
}
map = next;
continue;
} else if (IsWindows()) {
@@ -240,35 +221,34 @@ StartOver:;
size_t right = map_size - left;
ASSERT(right > 0);
ASSERT(left > 0);
map->addr += left;
map->size = right;
if (!(map->flags & MAP_ANONYMOUS))
map->off += left;
__maps.pages -= (left + pagesz - 1) / pagesz;
__maps_check();
if (!untrack_only) {
__maps_unlock();
ASSERT(addr <= map_addr);
ASSERT(map_addr + PGUP(left) <= addr + PGUP(size));
if (sys_munmap(map_addr, left) == -1)
rc = -1;
__maps_lock();
goto StartOver;
struct Map *leftmap;
if ((leftmap = __maps_alloc())) {
map->addr += left;
map->size = right;
if (!(map->flags & MAP_ANONYMOUS))
map->off += left;
__maps.pages -= (left + pagesz - 1) / pagesz;
__maps_check();
leftmap->addr = map_addr;
leftmap->size = left;
dll_make_first(&delete, &leftmap->elem);
} else {
rc = -1;
}
} else if (addr + PGUP(size) >= map_addr + PGUP(map_size)) {
// shave off righthand side of mapping
size_t left = addr - map_addr;
size_t right = map_addr + map_size - addr;
map->size = left;
__maps.pages -= (right + pagesz - 1) / pagesz;
__maps_check();
if (!untrack_only) {
__maps_unlock();
ASSERT(PGUP(right) <= PGUP(size));
if (sys_munmap(addr, right) == -1)
rc = -1;
__maps_lock();
goto StartOver;
struct Map *rightmap;
if ((rightmap = __maps_alloc())) {
map->size = left;
__maps.pages -= (right + pagesz - 1) / pagesz;
__maps_check();
rightmap->addr = addr;
rightmap->size = right;
dll_make_first(&delete, &rightmap->elem);
} else {
rc = -1;
}
} else {
// punch hole in mapping
@@ -277,27 +257,28 @@ StartOver:;
size_t right = map_size - middle - left;
struct Map *leftmap;
if ((leftmap = __maps_alloc())) {
leftmap->next = map;
leftmap->addr = map_addr;
leftmap->size = left;
leftmap->off = map->off;
leftmap->prot = map->prot;
leftmap->flags = map->flags;
map->addr += left + middle;
map->size = right;
if (!(map->flags & MAP_ANONYMOUS))
map->off += left + middle;
dll_make_first(&__maps.used, &leftmap->elem);
*prev = leftmap;
__maps.pages -= (middle + pagesz - 1) / pagesz;
__maps.count += 1;
__maps_check();
if (!untrack_only) {
__maps_unlock();
if (sys_munmap(addr, size) == -1)
rc = -1;
__maps_lock();
goto StartOver;
struct Map *middlemap;
if ((middlemap = __maps_alloc())) {
leftmap->next = map;
leftmap->addr = map_addr;
leftmap->size = left;
leftmap->off = map->off;
leftmap->prot = map->prot;
leftmap->flags = map->flags;
map->addr += left + middle;
map->size = right;
if (!(map->flags & MAP_ANONYMOUS))
map->off += left + middle;
dll_make_first(&__maps.used, &leftmap->elem);
*prev = leftmap;
__maps.pages -= (middle + pagesz - 1) / pagesz;
__maps.count += 1;
__maps_check();
middlemap->addr = addr;
middlemap->size = size;
dll_make_first(&delete, &middlemap->elem);
} else {
rc = -1;
}
} else {
rc = -1;
@@ -309,6 +290,34 @@ StartOver:;
}
__maps_unlock();
// delete mappings
for (struct Dll *e = dll_first(delete); e; e = dll_next(delete, e)) {
map = MAP_CONTAINER(e);
if (!untrack_only) {
if (!IsWindows()) {
if (sys_munmap(map->addr, map->size))
rc = -1;
} else {
if (!UnmapViewOfFile(map->addr))
rc = -1;
if (!CloseHandle(map->h))
rc = -1;
}
}
}
// free mappings
if (!dll_is_empty(delete)) {
__maps_lock();
struct Dll *e;
while ((e = dll_first(delete))) {
dll_remove(&delete, e);
__maps_free(MAP_CONTAINER(e));
}
__maps_check();
__maps_unlock();
}
return rc;
}
+2 -4
View File
@@ -64,11 +64,9 @@ int __reservefd_unlocked(int start) {
int fd, f1, f2;
for (;;) {
f1 = atomic_load_explicit(&g_fds.f, memory_order_acquire);
for (fd = MAX(start, f1); fd < g_fds.n; ++fd) {
if (!g_fds.p[fd].kind) {
for (fd = MAX(start, f1); fd < g_fds.n; ++fd)
if (!g_fds.p[fd].kind)
break;
}
}
fd = __ensurefds_unlocked(fd);
bzero(g_fds.p + fd, sizeof(*g_fds.p));
if (_cmpxchg(&g_fds.p[fd].kind, kFdEmpty, kFdReserved)) {
+1 -1
View File
@@ -5,7 +5,7 @@
#define SYSDEBUG 0
#endif
#define _NTTRACE 0 /* not configurable w/ flag yet */
#define _NTTRACE 1 /* not configurable w/ flag yet */
#define _POLLTRACE 0 /* not configurable w/ flag yet */
#define _DATATRACE 1 /* not configurable w/ flag yet */
#define _LOCKTRACE 0 /* not configurable w/ flag yet */
-1
View File
@@ -42,4 +42,3 @@
void *malloc(size_t n) {
return dlmalloc(n);
}
+24 -23
View File
@@ -17,6 +17,7 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/intrin/cxaatexit.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/gc.internal.h"
@@ -30,8 +31,6 @@ struct Dtor {
struct Dtor *next;
};
static _Thread_local struct Dtor *__cxa_thread_atexit_list;
static void _pthread_unwind(struct CosmoTib *tib) {
struct PosixThread *pt;
struct _pthread_cleanup_buffer *cb;
@@ -42,24 +41,24 @@ static void _pthread_unwind(struct CosmoTib *tib) {
}
}
static void __cxa_thread_unkey(struct CosmoTib *tib) {
void *val;
int i, j, gotsome;
pthread_key_dtor dtor;
for (j = 0; j < PTHREAD_DESTRUCTOR_ITERATIONS; ++j) {
for (gotsome = i = 0; i < PTHREAD_KEYS_MAX; ++i) {
if ((val = tib->tib_keys[i]) &&
(dtor = atomic_load_explicit(_pthread_key_dtor + i,
memory_order_relaxed)) &&
dtor != (pthread_key_dtor)-1) {
gotsome = 1;
tib->tib_keys[i] = 0;
static void _pthread_unkey(struct CosmoTib *tib) {
for (int j = 0; j < PTHREAD_DESTRUCTOR_ITERATIONS; ++j) {
bool gotsome = false;
for (int i = 0; i < PTHREAD_KEYS_MAX; ++i) {
void *val;
pthread_key_dtor dtor;
if ((dtor = atomic_load_explicit(&_pthread_key_dtor[i],
memory_order_acquire)) &&
dtor != (pthread_key_dtor)-1 &&
(val = atomic_exchange_explicit(&tib->tib_keys[i], 0,
memory_order_relaxed))) {
STRACE("_pthread_unkey(%t, %p)", dtor, val);
gotsome = true;
dtor(val);
}
}
if (!gotsome) {
if (!gotsome)
break;
}
}
}
@@ -67,9 +66,8 @@ static void _pthread_ungarbage(struct CosmoTib *tib) {
struct Garbages *g;
while ((g = tib->tib_garbages)) {
tib->tib_garbages = 0;
while (g->i--) {
while (g->i--)
((void (*)(intptr_t))g->p[g->i].fn)(g->p[g->i].arg);
}
_weaken(free)(g->p);
_weaken(free)(g);
}
@@ -82,10 +80,11 @@ void __cxa_thread_finalize(void) {
_pthread_unwind(tib);
if (tib->tib_nsync)
_weaken(nsync_waiter_destroy)(tib->tib_nsync);
__cxa_thread_unkey(tib);
_pthread_unkey(tib);
_pthread_ungarbage(tib);
while ((dtor = __cxa_thread_atexit_list)) {
__cxa_thread_atexit_list = dtor->next;
while ((dtor = tib->tib_atexit)) {
STRACE("__cxa_finalize(%t, %p)", dtor->fun, dtor->arg);
tib->tib_atexit = dtor->next;
((void (*)(void *))dtor->fun)(dtor->arg);
_weaken(free)(dtor);
}
@@ -97,9 +96,11 @@ int __cxa_thread_atexit_impl(void *fun, void *arg, void *dso_symbol) {
return -1;
if (!(dtor = _weaken(malloc)(sizeof(struct Dtor))))
return -1;
struct CosmoTib *tib;
tib = __get_tls();
dtor->fun = fun;
dtor->arg = arg;
dtor->next = __cxa_thread_atexit_list;
__cxa_thread_atexit_list = dtor;
dtor->next = tib->tib_atexit;
tib->tib_atexit = dtor;
return 0;
}
+5 -1
View File
@@ -24,6 +24,7 @@
#include "libc/intrin/dll.h"
#include "libc/intrin/getenv.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/maps.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/nt/files.h"
@@ -37,6 +38,8 @@
#include "libc/stdalign.internal.h"
#include "libc/str/locale.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
@@ -168,7 +171,8 @@ textstartup void __enable_tls(void) {
if (I(_tls_align) <= TLS_ALIGNMENT && size <= sizeof(__static_tls)) {
mem = __static_tls;
} else {
mem = _weaken(_mapanon)(size);
mem = __mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
}
struct CosmoTib *tib =
+1 -1
View File
@@ -58,7 +58,7 @@ static struct ZiposHandle *__zipos_alloc(struct Zipos *zipos, size_t size) {
granularity = __granularity();
mapsize = sizeof(struct ZiposHandle) + size;
mapsize = (mapsize + granularity - 1) & -granularity;
if ((h = __mmap(randaddr(), mapsize, PROT_READ | PROT_WRITE,
if ((h = __mmap(0, mapsize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) != MAP_FAILED) {
h->size = size;
h->zipos = zipos;
+3 -2
View File
@@ -54,9 +54,8 @@ long _pthread_cancel_ack(void) {
pthread_exit(PTHREAD_CANCELED);
}
pt->pt_flags |= PT_NOCANCEL;
if (IsOpenbsd()) {
if (IsOpenbsd())
pt->pt_flags |= PT_OPENBSD_KLUDGE;
}
return ecanceled();
}
@@ -351,6 +350,7 @@ static errno_t _pthread_cancel_everyone(void) {
* @param thread may be 0 to cancel all threads except self
* @return 0 on success, or errno on error
* @raise ESRCH if system thread wasn't alive or we lost a race
* @cancelationpoint
*/
errno_t pthread_cancel(pthread_t thread) {
struct PosixThread *arg;
@@ -401,6 +401,7 @@ void pthread_testcancel(void) {
*
* @return 0 if not cancelled or cancelation is blocked or `ECANCELED`
* in masked mode when the calling thread has been cancelled
* @cancelationpoint
*/
errno_t pthread_testcancel_np(void) {
struct PosixThread *pt;
+1
View File
@@ -117,6 +117,7 @@ static int PosixThread(void *arg, int tid) {
}
// set long jump handler so pthread_exit can bring control back here
if (!setjmp(pt->pt_exiter)) {
sigdelset(&pt->pt_attr.__sigmask, SIGTHR);
if (IsWindows()) {
atomic_store_explicit(&__get_tls()->tib_sigmask, pt->pt_attr.__sigmask,
memory_order_release);
@@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2022 Justine Alexandra Roberts Tunney │
│ Copyright 2024 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,24 +16,22 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/sysv/consts/pr.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
const char *DescribePrctlOperation(int x) {
switch (x) {
case PR_GET_NAME:
return "PR_GET_NAME";
case PR_SET_NO_NEW_PRIVS:
return "PR_SET_NO_NEW_PRIVS";
case PR_SET_SECCOMP:
return "PR_SET_SECCOMP";
case PR_GET_SECCOMP:
return "PR_GET_SECCOMP";
case PR_CAPBSET_READ:
return "PR_CAPBSET_READ";
case PR_CAPBSET_DROP:
return "PR_CAPBSET_DROP";
default:
return "PRCTL_???";
}
/**
* Garbage collects POSIX threads runtime.
*
* Let's say you want to run a memory leak detector. You can say:
*
* while (!pthread_orphan_np())
* pthread_decimate_np();
*
* To wait until all threads have exited.
*
* @return 0 on success, or errno on error
*/
int pthread_decimate_np(void) {
_pthread_decimate();
return 0;
}
+3 -3
View File
@@ -48,9 +48,9 @@ int pthread_key_create(pthread_key_t *key, pthread_key_dtor dtor) {
if (!dtor)
dtor = (pthread_key_dtor)-1;
for (i = 0; i < PTHREAD_KEYS_MAX; ++i) {
if (!(expect = atomic_load_explicit(_pthread_key_dtor + i,
memory_order_acquire)) &&
atomic_compare_exchange_strong_explicit(_pthread_key_dtor + i, &expect,
if (!(expect = atomic_load_explicit(&_pthread_key_dtor[i],
memory_order_relaxed)) &&
atomic_compare_exchange_strong_explicit(&_pthread_key_dtor[i], &expect,
dtor, memory_order_release,
memory_order_relaxed)) {
*key = i;
+5 -4
View File
@@ -16,7 +16,6 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/thread/posixthread.internal.h"
@@ -32,10 +31,12 @@
*
* @param key was created by pthread_key_create()
* @return 0 on success, or errno on error
* @raise EINVAL if `key` is invalid
*/
int pthread_key_delete(pthread_key_t k) {
unassert(0 <= k && k < PTHREAD_KEYS_MAX);
unassert(atomic_load_explicit(_pthread_key_dtor + k, memory_order_acquire));
atomic_store_explicit(_pthread_key_dtor + k, 0, memory_order_release);
if (!(0 <= k && k < PTHREAD_KEYS_MAX))
return EINVAL; // corrupt key identifier
if (!atomic_exchange_explicit(&_pthread_key_dtor[k], 0, memory_order_acq_rel))
return EINVAL; // delete called twice
return 0;
}
+2 -1
View File
@@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_THREAD_THREAD_H_
#define COSMOPOLITAN_LIBC_THREAD_THREAD_H_
#define PTHREAD_KEYS_MAX 48
#define PTHREAD_KEYS_MAX 46
#define PTHREAD_STACK_MIN 65536
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
@@ -166,6 +166,7 @@ int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int) libcesque paramsnon
int pthread_mutexattr_settype(pthread_mutexattr_t *, int) libcesque paramsnonnull();
int pthread_once(pthread_once_t *, void (*)(void)) paramsnonnull();
int pthread_orphan_np(void) libcesque;
int pthread_decimate_np(void) libcesque;
int pthread_rwlock_destroy(pthread_rwlock_t *) libcesque paramsnonnull();
int pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *) libcesque paramsnonnull((1));
int pthread_rwlock_rdlock(pthread_rwlock_t *) libcesque paramsnonnull();
+2 -1
View File
@@ -39,7 +39,8 @@ struct CosmoTib {
uint32_t tib_sigstack_flags;
_Atomic(int) tib_relock_maps;
void *tib_nsync;
void *tib_keys[47];
void *tib_atexit;
_Atomic(void *) tib_keys[46];
} __attribute__((__aligned__(64)));
extern int __threaded;
+3 -1
View File
@@ -14,9 +14,11 @@ TEST_CTL_TESTS = $(TEST_CTL_COMS:%=%.ok)
TEST_CTL_DIRECTDEPS = \
CTL \
LIBC_CALLS \
LIBC_STDIO \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_STDIO \
LIBC_STDIO \
LIBC_THREAD \
THIRD_PARTY_LIBCXX \
+3 -3
View File
@@ -20,9 +20,9 @@
#include "ctl/array.h"
#include "libc/mem/leaks.h"
#include <algorithm>
#include <array>
#define ctl std
// #include <algorithm>
// #include <array>
// #define ctl std
int
main()
+102
View File
@@ -0,0 +1,102 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ctl/is_base_of.h"
// #include <type_traits>
// #define ctl std
// Test classes
class A
{};
class B : public A
{};
class C : public B
{};
class D
{};
struct Empty
{};
template<typename T>
class TemplateClass
{};
class VirtualBase
{};
class VirtualDerived : virtual public VirtualBase
{};
int
main()
{
// Test basic inheritance
if (!ctl::is_base_of_v<A, B>)
return 1;
if (!ctl::is_base_of_v<A, C>)
return 2;
if (ctl::is_base_of_v<B, A>)
return 3;
if (ctl::is_base_of_v<C, A>)
return 4;
if (ctl::is_base_of_v<A, D>)
return 5;
// Test with same type
if (!ctl::is_base_of_v<A, A>)
return 6;
if (!ctl::is_base_of_v<B, B>)
return 7;
// Test with void
if (ctl::is_base_of_v<void, void>)
return 8;
if (ctl::is_base_of_v<A, void>)
return 9;
if (ctl::is_base_of_v<void, A>)
return 10;
// Test with fundamental types
if (ctl::is_base_of_v<int, int>)
return 11;
if (ctl::is_base_of_v<int, double>)
return 12;
if (ctl::is_base_of_v<double, int>)
return 13;
// Test with empty class
if (ctl::is_base_of_v<Empty, A>)
return 14;
if (ctl::is_base_of_v<A, Empty>)
return 15;
// Test with template class
if (ctl::is_base_of_v<TemplateClass<int>, A>)
return 16;
if (ctl::is_base_of_v<A, TemplateClass<int>>)
return 17;
// Test with virtual inheritance
if (!ctl::is_base_of_v<VirtualBase, VirtualDerived>)
return 18;
if (ctl::is_base_of_v<VirtualDerived, VirtualBase>)
return 19;
return 0; // All tests passed
}
+163
View File
@@ -0,0 +1,163 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ctl/istringstream.h"
#include "libc/mem/leaks.h"
// #include <sstream>
// #define ctl std
int
main()
{
// Test default constructor
{
ctl::istringstream iss;
if (iss.eof())
return 1;
if (!iss.good())
return 1;
}
// Test constructor with initial string
{
ctl::istringstream iss("Hello, world!");
if (iss.str() != "Hello, world!")
return 2;
}
// Test reading a char
{
ctl::istringstream iss("A");
char c;
iss >> c;
if (c != 'A' || !iss.good())
return 3;
}
// Test reading a string
{
ctl::istringstream iss("Hello World");
ctl::string s;
iss >> s;
if (s != "Hello" || !iss.good())
return 4;
}
// Test reading multiple strings
{
ctl::istringstream iss("One Two Three");
ctl::string s1, s2, s3;
iss >> s1 >> s2 >> s3;
if (s1 != "One" || s2 != "Two" || s3 != "Three" || iss.good() ||
!iss.eof())
return 5;
}
// Test reading integers
{
ctl::istringstream iss("123 -456 789");
int a, b, c;
iss >> a >> b >> c;
if (a != 123 || b != -456 || c != 789 || iss.good() || !iss.eof())
return 6;
}
// Test reading floating-point numbers
{
ctl::istringstream iss("3.14 -2.718");
float f;
double d;
iss >> f >> d;
if (f != 3.14f || d != -2.718 || iss.good() || !iss.eof())
return 7;
}
// Test reading past the end of the stream
{
ctl::istringstream iss("42");
int n;
iss >> n;
if (n != 42 || iss.good() || !iss.eof())
return 8;
iss >> n;
if (iss.good())
return 9;
if (!iss.eof())
return 10;
}
// Test reading invalid data
{
ctl::istringstream iss("not_a_number");
int n;
iss >> n;
if (iss.good() || !iss.fail())
return 11;
}
// Test str() setter
{
ctl::istringstream iss;
iss.str("New content");
if (iss.str() != "New content" || !iss.good())
return 12;
}
// Test reading after setting a new string
{
ctl::istringstream iss("Old content");
iss.str("New content");
ctl::string s;
iss >> s;
if (s != "New" || !iss.good())
return 13;
}
// Test reading a mixture of types
{
ctl::istringstream iss("42 3.14 Hello");
int i;
double d;
ctl::string s;
iss >> i >> d >> s;
if (i != 42 || d != 3.14 || s != "Hello" || iss.good() || !iss.eof())
return 14;
}
// Test reading with leading whitespace
{
ctl::istringstream iss(" 42");
int n;
iss >> n;
if (n != 42 || iss.good() || !iss.eof())
return 15;
}
// Test clear() only affects error state, not content
{
ctl::istringstream iss("Test");
iss.setstate(ctl::ios_base::failbit);
iss.clear();
if (!iss.good() || iss.str() != "Test")
return 16;
}
CheckForMemoryLeaks();
}
+140
View File
@@ -0,0 +1,140 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 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 "ctl/ostringstream.h"
#include "libc/mem/leaks.h"
// #include <sstream>
// #define ctl std
int
main()
{
// Test default constructor and basic string insertion
{
ctl::ostringstream oss;
oss << "Hello, world!";
if (oss.str() != "Hello, world!")
return 1;
}
// Test constructor with initial string (overwrite mode)
{
ctl::ostringstream oss("Initial content");
oss << "New";
if (oss.str() != "Newtial content")
return 2;
}
// Test overwrite and append behavior
{
ctl::ostringstream oss("Hello, world!");
oss << "Hi";
if (oss.str() != "Hillo, world!")
return 3;
oss << " Earth";
if (oss.str() != "Hi Earthorld!")
return 4;
}
// Test multiple insertions of different types
{
ctl::ostringstream oss;
oss << "Int: " << 42 << ", Float: " << 3.14f << ", Double: " << 2.718;
if (oss.str() != "Int: 42, Float: 3.14, Double: 2.718")
return 5;
}
// Test char insertion
{
ctl::ostringstream oss("ABCDEF");
oss << 'X' << 'Y' << 'Z';
if (oss.str() != "XYZDEF")
return 6;
}
// Test unsigned types
{
ctl::ostringstream oss("Numbers: ");
unsigned int ui = 12345;
unsigned long ul = 67890UL;
oss << ui << " " << ul;
if (oss.str() != "12345 67890")
return 7;
}
// Test long type
{
ctl::ostringstream oss("Long: ");
long l = -9876543210L;
oss << l;
if (oss.str() != "-9876543210")
return 8;
}
// Test clear() method
{
ctl::ostringstream oss("not that kind of clear");
oss.clear();
if (!oss.good() || oss.str() != "not that kind of clear")
return 9;
}
// Test str() setter method
{
ctl::ostringstream oss;
oss.str("New content");
oss << "Old";
if (oss.str() != "Old content")
return 10;
}
// Test chaining of insertion operators
{
ctl::ostringstream oss("Start: ");
oss << "Chain " << 1 << " " << 2.0 << " " << 'Z';
if (oss.str() != "Chain 1 2 Z")
return 11;
}
// Test insertion when stream is not in good state
{
ctl::ostringstream oss("Original");
oss.setstate(ctl::ios_base::failbit);
oss << "This should not be inserted";
if (oss.str() != "Original")
return 12;
}
// Test with larger amounts of data
{
ctl::ostringstream oss("Prefix: ");
for (int i = 0; i < 1000; ++i) {
oss << "Line " << i << "\n";
}
ctl::string result = oss.str();
if (result.substr(0, 14) != "Line 0\nLine 1\n")
return 13;
if (result.substr(result.length() - 8) != "ine 999\n")
return 14;
}
CheckForMemoryLeaks();
return 0;
}
+8
View File
@@ -352,5 +352,13 @@ main()
return 80;
}
{
ctl::vector<int> dog(8, 0);
if (dog.size() != 8)
return 81;
if (dog[0] != 0)
return 82;
}
CheckForMemoryLeaks();
}
@@ -16,10 +16,19 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
void *randaddr(void) {
static unsigned long lcg = 1;
lcg *= 6364136223846793005;
lcg += 1442695040888963407;
return (void *)(lcg >> 48 << 28);
void *wut(void *arg) {
pthread_id_np_t tid;
pthread_getunique_np(pthread_self(), &tid);
ASSERT_EQ(gettid(), tid);
return 0;
}
TEST(pthread_getunique_np, test) {
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, wut, (void *)1));
ASSERT_EQ(0, pthread_join(id, 0));
}
+1
View File
@@ -427,6 +427,7 @@ is_bourne_compatible_shell (const char *path)
"sh",
"bash",
"dash",
"cocmd",
"ksh",
"rksh",
"zsh",
-177
View File
@@ -1,177 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et 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/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "tool/build/lib/getargs.h"
#include "tool/build/lib/interner.h"
#define LOOKINGAT(p, pe, s) LookingAt(p, pe, s, strlen(s))
struct Visited {
size_t i, n;
const char **p;
};
char *output;
const char *prog;
struct Interner *visited;
void Visit(const char *);
static wontreturn void Die(const char *reason) {
tinyprint(2, prog, ": ", reason, "\n", NULL);
exit(1);
}
static wontreturn void DieSys(const char *thing) {
perror(thing);
exit(1);
}
static wontreturn void DieOom(void) {
Die("out of memory");
}
static void Appends(char **b, const char *s) {
if (appends(b, s) == -1)
DieOom();
}
static void Appendd(char **b, const void *p, size_t n) {
if (appendd(b, p, n) == -1)
DieOom();
}
size_t GetFdSize(int fd) {
struct stat st;
if (fstat(fd, &st)) {
DieSys("fstat");
}
return st.st_size;
}
bool LookingAt(const char *p, const char *pe, const char *s, size_t n) {
return pe - p >= n && memcmp(p, s, n) == 0;
}
void Process(const char *p, const char *pe, const char *path, bool isheader) {
int level;
bool noformat;
const char *p2, *dq, *name;
for (noformat = false, level = 0; p < pe; p = p2) {
p2 = memchr(p, '\n', pe - p);
p2 = p2 ? p2 + 1 : pe;
if (LOOKINGAT(p, pe, "#if")) {
if (isheader && !level++)
continue;
}
if (LOOKINGAT(p, pe, "#endif")) {
if (isheader && !--level)
continue;
}
if (LOOKINGAT(p, pe, "/* clang-format off */")) {
noformat = true;
} else if (LOOKINGAT(p, pe, "/* clang-format on */")) {
noformat = false;
}
if (LOOKINGAT(p, pe, "#include \"")) {
name = p + strlen("#include \"");
dq = memchr(name, '"', pe - name);
if (dq) {
Visit(strndup(name, dq - name));
continue;
}
}
Appendd(&output, p, p2 - p);
}
if (noformat) {
Appends(&output, "/* clang-format on */\n");
}
}
void Visit(const char *path) {
int fd;
char *map;
size_t size;
bool isheader;
if (!endswith(path, ".h") && !endswith(path, ".inc"))
return;
if (endswith(path, ".internal.h"))
return;
if (endswith(path, "/internal.h"))
return;
if (endswith(path, ".internal.inc"))
return;
if (endswith(path, "/internal.inc"))
return;
if (startswith(path, "libc/isystem/"))
return;
isheader = endswith(path, ".h");
if (isheader && isinterned(visited, path))
return;
Appends(&output, "\n\f\n/*!BEGIN ");
Appends(&output, path);
Appends(&output, " */\n\n");
intern(visited, path);
if ((fd = open(path, O_RDONLY)) == -1)
DieSys(path);
if ((size = GetFdSize(fd))) {
map = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED)
DieSys(path);
Process(map, map + size, path, isheader);
if (munmap(map, size))
DieSys(path);
}
if (close(fd))
DieSys(path);
}
int main(int argc, char *argv[]) {
size_t bytes;
const char *src;
struct GetArgs ga;
prog = argv[0];
if (!prog)
prog = "rollup";
visited = newinterner();
Appends(&output, "#ifndef COSMOPOLITAN_H_\n");
Appends(&output, "#define COSMOPOLITAN_H_\n");
getargs_init(&ga, argv + 1);
while ((src = getargs_next(&ga))) {
Visit(src);
}
getargs_destroy(&ga);
Appends(&output, "\n");
Appends(&output, "#endif /* COSMOPOLITAN_H_ */\n");
bytes = appendz(output).i;
if (write(1, output, bytes) != bytes) {
DieSys(prog);
}
freeinterner(visited);
return 0;
}
+36 -3
View File
@@ -44,6 +44,19 @@ make -j64 m=$AMD64 \
o/$AMD64/tool/build/zipobj.dbg \
o/$AMD64/tool/build/apelink.dbg \
o/$AMD64/tool/build/pecheck.dbg \
o/$AMD64/tool/build/ar.dbg \
o/$AMD64/tool/build/chmod.dbg \
o/$AMD64/tool/build/cocmd.dbg \
o/$AMD64/tool/build/compile.dbg \
o/$AMD64/tool/build/cp.dbg \
o/$AMD64/tool/build/echo.dbg \
o/$AMD64/tool/build/gzip.dbg \
o/$AMD64/tool/build/objbincopy.dbg \
o/$AMD64/tool/build/package.dbg \
o/$AMD64/tool/build/rm.dbg \
o/$AMD64/tool/build/touch.dbg \
o/$AMD64/tool/build/sha256sum.dbg \
o/$AMD64/tool/build/resymbol.dbg \
o/$AMD64/third_party/make/make.dbg \
o/$AMD64/third_party/ctags/ctags.dbg
@@ -64,6 +77,19 @@ make -j64 m=$ARM64 \
o/$ARM64/tool/build/zipobj.dbg \
o/$ARM64/tool/build/apelink.dbg \
o/$ARM64/tool/build/pecheck.dbg \
o/$ARM64/tool/build/ar.dbg \
o/$ARM64/tool/build/chmod.dbg \
o/$ARM64/tool/build/cocmd.dbg \
o/$ARM64/tool/build/compile.dbg \
o/$ARM64/tool/build/cp.dbg \
o/$ARM64/tool/build/echo.dbg \
o/$ARM64/tool/build/gzip.dbg \
o/$ARM64/tool/build/objbincopy.dbg \
o/$ARM64/tool/build/package.dbg \
o/$ARM64/tool/build/rm.dbg \
o/$ARM64/tool/build/touch.dbg \
o/$ARM64/tool/build/sha256sum.dbg \
o/$ARM64/tool/build/resymbol.dbg \
o/$ARM64/third_party/make/make.dbg \
o/$ARM64/third_party/ctags/ctags.dbg
@@ -90,10 +116,10 @@ fetch() {
OLD=$PWD
cd "$OUTDIR/"
if [ ! -x bin/x86_64-linux-cosmo-gcc ]; then
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.44/aarch64-gcc.zip
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.45/aarch64-gcc.zip
unzip aarch64-gcc.zip
rm -f aarch64-gcc.zip
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.44/x86_64-gcc.zip
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.45/x86_64-gcc.zip
unzip x86_64-gcc.zip
rm -f x86_64-gcc.zip
fi
@@ -146,7 +172,9 @@ cp -af tool/cosmocc/bin/* "$OUTDIR/bin/"
cp -f o/$AMD64/ape/ape.elf "$OUTDIR/bin/ape-x86_64.elf"
cp -f o/$AMD64/ape/ape.macho "$OUTDIR/bin/ape-x86_64.macho"
cp -f o/$ARM64/ape/ape.elf "$OUTDIR/bin/ape-aarch64.elf"
for x in assimilate march-native mktemper fixupobj zipcopy apelink pecheck mkdeps zipobj; do
for x in assimilate march-native mktemper fixupobj zipcopy apelink pecheck mkdeps zipobj \
ar chmod cocmd cp echo gzip objbincopy package rm touch sha256sum resymbol; do
ape $APELINK \
-l o/$AMD64/ape/ape.elf \
-l o/$ARM64/ape/ape.elf \
@@ -155,6 +183,11 @@ for x in assimilate march-native mktemper fixupobj zipcopy apelink pecheck mkdep
o/$AMD64/tool/build/$x.dbg \
o/$ARM64/tool/build/$x.dbg
done
for x in ar chmod cp echo gzip package rm touch sha256sum; do
mv "$OUTDIR/bin/$x" "$OUTDIR/bin/$x.ape"
done
for x in make ctags; do
ape $APELINK \
-l o/$AMD64/ape/ape.elf \