1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:37:44 +00:00

Add clang-format file

Also run it across the whole tree to get everything using the One True Style.
We don't yet run this in an automated fashion as it's a little slow, but
there is a snippet to do so in makeall.sh.
This commit is contained in:
Robin Burchell 2019-05-28 11:53:16 +02:00 committed by Andreas Kling
parent c11351ac50
commit 0dc9af5f7e
286 changed files with 3244 additions and 2424 deletions

View file

@ -3,18 +3,18 @@
#include <AK/ByteBuffer.h>
#include <AK/RetainPtr.h>
#include <AK/StringImpl.h>
#include <AK/StringView.h>
#include <AK/Traits.h>
#include <AK/Vector.h>
#include <AK/StringView.h>
#include <AK/kstdio.h>
namespace AK {
class String {
public:
~String() { }
~String() {}
String() { }
String() {}
String(StringView view)
: m_impl(StringImpl::create(view.characters(), view.length()))
@ -96,7 +96,11 @@ public:
bool is_empty() const { return length() == 0; }
ssize_t length() const { return m_impl ? m_impl->length() : 0; }
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
char operator[](ssize_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
char operator[](ssize_t i) const
{
ASSERT(m_impl);
return (*m_impl)[i];
}
bool ends_with(const String&) const;
@ -131,7 +135,7 @@ public:
static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp)
{
if (buffer.is_null())
return { };
return {};
if (buffer.is_empty())
return empty();
return String((const char*)buffer.data(), buffer.size(), should_chomp);

View file

@ -1,13 +1,13 @@
#pragma once
#ifdef KERNEL
#include <Kernel/Assertions.h>
# include <Kernel/Assertions.h>
#else
#include <assert.h>
#ifndef __serenity__
#define ASSERT assert
#define ASSERT_NOT_REACHED assert(false)
#endif
# include <assert.h>
# ifndef __serenity__
# define ASSERT assert
# define ASSERT_NOT_REACHED assert(false)
# endif
#endif
namespace AK {
@ -17,4 +17,3 @@ inline void not_implemented() { ASSERT(false); }
}
using AK::not_implemented;

View file

@ -3,5 +3,5 @@
template<typename T>
class Badge {
friend T;
Badge() { }
Badge() {}
};

View file

@ -1,9 +1,9 @@
#pragma once
#include "Assertions.h"
#include "StdLibExtras.h"
#include "Types.h"
#include "kmalloc.h"
#include "Assertions.h"
namespace AK {
@ -77,4 +77,3 @@ private:
}
using AK::Bitmap;

View file

@ -1,9 +1,9 @@
#pragma once
#include "Types.h"
#include "StdLibExtras.h"
#include <AK/Retainable.h>
#include "Types.h"
#include <AK/RetainPtr.h>
#include <AK/Retainable.h>
#include <AK/kmalloc.h>
namespace AK {
@ -28,8 +28,16 @@ public:
m_data = nullptr;
}
byte& operator[](int i) { ASSERT(i < m_size); return m_data[i]; }
const byte& operator[](int i) const { ASSERT(i < m_size); return m_data[i]; }
byte& operator[](int i)
{
ASSERT(i < m_size);
return m_data[i];
}
const byte& operator[](int i) const
{
ASSERT(i < m_size);
return m_data[i];
}
bool is_empty() const { return !m_size; }
int size() const { return m_size; }
@ -52,11 +60,16 @@ public:
void grow(int size);
private:
enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt };
explicit ByteBufferImpl(int); // For ConstructionMode=Uninitialized
enum ConstructionMode {
Uninitialized,
Copy,
Wrap,
Adopt
};
explicit ByteBufferImpl(int); // For ConstructionMode=Uninitialized
ByteBufferImpl(const void*, int, ConstructionMode); // For ConstructionMode=Copy
ByteBufferImpl(void*, int, ConstructionMode); // For ConstructionMode=Wrap/Adopt
ByteBufferImpl() { }
ByteBufferImpl(void*, int, ConstructionMode); // For ConstructionMode=Wrap/Adopt
ByteBufferImpl() {}
byte* m_data { nullptr };
int m_size { 0 };
@ -65,8 +78,8 @@ private:
class ByteBuffer {
public:
ByteBuffer() { }
ByteBuffer(std::nullptr_t) { }
ByteBuffer() {}
ByteBuffer(std::nullptr_t) {}
ByteBuffer(const ByteBuffer& other)
: m_impl(other.m_impl.copy_ref())
{
@ -101,8 +114,16 @@ public:
bool operator!() const { return is_null(); }
bool is_null() const { return m_impl == nullptr; }
byte& operator[](ssize_t i) { ASSERT(m_impl); return (*m_impl)[i]; }
byte operator[](ssize_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
byte& operator[](ssize_t i)
{
ASSERT(m_impl);
return (*m_impl)[i];
}
byte operator[](ssize_t i) const
{
ASSERT(m_impl);
return (*m_impl)[i];
}
bool is_empty() const { return !m_impl || m_impl->is_empty(); }
ssize_t size() const { return m_impl ? m_impl->size() : 0; }
@ -121,7 +142,7 @@ public:
ByteBuffer isolated_copy() const
{
if (!m_impl)
return { };
return {};
return copy(m_impl->pointer(), m_impl->size());
}
@ -135,9 +156,9 @@ public:
ByteBuffer slice(ssize_t offset, ssize_t size) const
{
if (is_null())
return { };
return {};
if (offset >= this->size())
return { };
return {};
if (offset + size >= this->size())
size = this->size() - offset;
return copy(offset_pointer(offset), size);
@ -241,4 +262,3 @@ inline Retained<ByteBufferImpl> ByteBufferImpl::adopt(void* data, int size)
}
using AK::ByteBuffer;

View file

@ -60,9 +60,14 @@ public:
}
const T& operator*() const { return m_queue.m_elements[m_index]; }
private:
friend class CircularQueue;
ConstIterator(const CircularQueue& queue, const int index) : m_queue(queue), m_index(index) { }
ConstIterator(const CircularQueue& queue, const int index)
: m_queue(queue)
, m_index(index)
{
}
const CircularQueue& m_queue;
int m_index { 0 };
};
@ -82,4 +87,3 @@ private:
}
using AK::CircularQueue;

View file

@ -9,22 +9,28 @@ template<typename T>
class DoublyLinkedList {
private:
struct Node {
explicit Node(const T& v) : value(v) { }
explicit Node(T&& v) : value(move(v)) { }
explicit Node(const T& v)
: value(v)
{
}
explicit Node(T&& v)
: value(move(v))
{
}
T value;
Node* next { nullptr };
Node* prev { nullptr };
};
public:
DoublyLinkedList() { }
DoublyLinkedList() {}
~DoublyLinkedList() { clear(); }
bool is_empty() const { return !head(); }
void clear()
{
for (auto* node = m_head; node; ) {
for (auto* node = m_head; node;) {
auto* next = node->next;
delete node;
node = next;
@ -33,15 +39,30 @@ public:
m_tail = nullptr;
}
T& first() { ASSERT(head()); return head()->value; }
const T& first() const { ASSERT(head()); return head()->value; }
T& last() { ASSERT(head()); return tail()->value; }
const T& last() const { ASSERT(head()); return tail()->value; }
T& first()
{
ASSERT(head());
return head()->value;
}
const T& first() const
{
ASSERT(head());
return head()->value;
}
T& last()
{
ASSERT(head());
return tail()->value;
}
const T& last() const
{
ASSERT(head());
return tail()->value;
}
void append(T&& value)
{
append_node(new Node(move(value)));
}
void append(const T& value)
@ -62,14 +83,22 @@ public:
public:
bool operator!=(const Iterator& other) const { return m_node != other.m_node; }
bool operator==(const Iterator& other) const { return m_node == other.m_node; }
Iterator& operator++() { m_node = m_node->next; return *this; }
Iterator& operator++()
{
m_node = m_node->next;
return *this;
}
T& operator*() { return m_node->value; }
T* operator->() { return &m_node->value; }
bool is_end() const { return !m_node; }
static Iterator universal_end() { return Iterator(nullptr); }
private:
friend class DoublyLinkedList;
explicit Iterator(DoublyLinkedList::Node* node) : m_node(node) { }
explicit Iterator(DoublyLinkedList::Node* node)
: m_node(node)
{
}
DoublyLinkedList::Node* m_node;
};
@ -80,14 +109,22 @@ public:
public:
bool operator!=(const ConstIterator& other) const { return m_node != other.m_node; }
bool operator==(const ConstIterator& other) const { return m_node == other.m_node; }
ConstIterator& operator++() { m_node = m_node->next; return *this; }
ConstIterator& operator++()
{
m_node = m_node->next;
return *this;
}
const T& operator*() const { return m_node->value; }
const T* operator->() const { return &m_node->value; }
bool is_end() const { return !m_node; }
static ConstIterator universal_end() { return ConstIterator(nullptr); }
private:
friend class DoublyLinkedList;
explicit ConstIterator(const DoublyLinkedList::Node* node) : m_node(node) { }
explicit ConstIterator(const DoublyLinkedList::Node* node)
: m_node(node)
{
}
const DoublyLinkedList::Node* m_node;
};
@ -163,4 +200,3 @@ private:
}
using AK::DoublyLinkedList;

View file

@ -1,7 +1,7 @@
#include "FileSystemPath.h"
#include "StringBuilder.h"
#include "Vector.h"
#include "kstdio.h"
#include "StringBuilder.h"
namespace AK {
@ -14,7 +14,7 @@ FileSystemPath::FileSystemPath(const String& s)
bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
{
// FIXME: Implement "resolve_symbolic_links"
(void) resolve_symbolic_links;
(void)resolve_symbolic_links;
auto parts = m_string.split('/');
Vector<String> canonical_parts;

View file

@ -6,7 +6,7 @@ namespace AK {
class FileSystemPath {
public:
FileSystemPath() { }
FileSystemPath() {}
explicit FileSystemPath(const String&);
bool is_valid() const { return m_is_valid; }

View file

@ -31,13 +31,14 @@
namespace AK {
template<typename> class Function;
template<typename>
class Function;
template <typename Out, typename... In>
template<typename Out, typename... In>
class Function<Out(In...)> {
public:
Function() = default;
Function(std::nullptr_t) { }
Function(std::nullptr_t) {}
template<typename CallableType, class = typename EnableIf<!(IsPointer<CallableType>::value && IsFunction<typename RemovePointer<CallableType>::Type>::value) && IsRvalueReference<CallableType&&>::value>::Type>
Function(CallableType&& callable)
@ -82,7 +83,7 @@ public:
private:
class CallableWrapperBase {
public:
virtual ~CallableWrapperBase() { }
virtual ~CallableWrapperBase() {}
virtual Out call(In...) const = 0;
};
@ -109,4 +110,3 @@ private:
}
using AK::Function;

View file

@ -17,4 +17,3 @@ inline unsigned pair_int_hash(dword key1, dword key2)
{
return int_hash((int_hash(key1) * 209) ^ (int_hash(key2 * 413)));
}

View file

@ -32,7 +32,7 @@ private:
};
public:
HashMap() { }
HashMap() {}
HashMap(HashMap&& other)
: m_table(move(other.m_table))
@ -115,13 +115,13 @@ private:
template<typename K, typename V>
void HashMap<K, V>::set(const K& key, V&& value)
{
m_table.set(Entry{key, move(value)});
m_table.set(Entry { key, move(value) });
}
template<typename K, typename V>
void HashMap<K, V>::set(const K& key, const V& value)
{
m_table.set(Entry{key, value});
m_table.set(Entry { key, value });
}
template<typename K, typename V>
@ -148,4 +148,3 @@ auto HashMap<K, V>::find(const K& key) const -> ConstIteratorType
}
using AK::HashMap;

View file

@ -2,15 +2,16 @@
#include "Assertions.h"
#include "DoublyLinkedList.h"
#include "Traits.h"
#include "StdLibExtras.h"
#include "Traits.h"
#include "kstdio.h"
//#define HASHTABLE_DEBUG
namespace AK {
template<typename T, typename = Traits<T>> class HashTable;
template<typename T, typename = Traits<T>>
class HashTable;
template<typename T, typename TraitsForT>
class HashTable {
@ -20,7 +21,7 @@ private:
};
public:
HashTable() { }
HashTable() {}
explicit HashTable(HashTable&& other)
: m_buckets(other.m_buckets)
, m_size(other.m_size)
@ -112,6 +113,7 @@ public:
return;
}
}
private:
friend class HashTable;
explicit Iterator(HashTable& table, bool is_end, typename DoublyLinkedList<T>::Iterator bucket_iterator = DoublyLinkedList<T>::Iterator::universal_end(), int bucket_index = 0)
@ -190,6 +192,7 @@ public:
return;
}
}
private:
friend class HashTable;
ConstIterator(const HashTable& table, bool is_end, typename DoublyLinkedList<T>::ConstIterator bucket_iterator = DoublyLinkedList<T>::ConstIterator::universal_end(), int bucket_index = 0)
@ -285,7 +288,6 @@ void HashTable<T, TraitsForT>::set(const T& value)
m_size++;
}
template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::rehash(int new_capacity)
{
@ -308,14 +310,14 @@ void HashTable<T, TraitsForT>::rehash(int new_capacity)
}
}
delete [] old_buckets;
delete[] old_buckets;
}
template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::clear()
{
if (m_buckets) {
delete [] m_buckets;
delete[] m_buckets;
m_buckets = nullptr;
}
m_capacity = 0;
@ -429,4 +431,3 @@ void HashTable<T, TraitsForT>::dump() const
}
using AK::HashTable;

View file

@ -5,47 +5,54 @@
namespace AK {
template<typename T> class InlineLinkedListNode {
template<typename T>
class InlineLinkedListNode {
public:
InlineLinkedListNode();
void set_prev(T*);
void set_next(T*);
T* prev() const;
T* next() const;
};
template<typename T> inline InlineLinkedListNode<T>::InlineLinkedListNode()
template<typename T>
inline InlineLinkedListNode<T>::InlineLinkedListNode()
{
set_prev(0);
set_next(0);
}
template<typename T> inline void InlineLinkedListNode<T>::set_prev(T* prev)
template<typename T>
inline void InlineLinkedListNode<T>::set_prev(T* prev)
{
static_cast<T*>(this)->m_prev = prev;
}
template<typename T> inline void InlineLinkedListNode<T>::set_next(T* next)
template<typename T>
inline void InlineLinkedListNode<T>::set_next(T* next)
{
static_cast<T*>(this)->m_next = next;
}
template<typename T> inline T* InlineLinkedListNode<T>::prev() const
template<typename T>
inline T* InlineLinkedListNode<T>::prev() const
{
return static_cast<const T*>(this)->m_prev;
}
template<typename T> inline T* InlineLinkedListNode<T>::next() const
template<typename T>
inline T* InlineLinkedListNode<T>::next() const
{
return static_cast<const T*>(this)->m_next;
}
template<typename T> class InlineLinkedList {
template<typename T>
class InlineLinkedList {
public:
InlineLinkedList() { }
InlineLinkedList() {}
bool is_empty() const { return !m_head; }
size_t size_slow() const;
void clear();
@ -75,7 +82,8 @@ private:
T* m_tail { nullptr };
};
template<typename T> inline size_t InlineLinkedList<T>::size_slow() const
template<typename T>
inline size_t InlineLinkedList<T>::size_slow() const
{
size_t size = 0;
for (T* node = m_head; node; node = node->next())
@ -83,13 +91,15 @@ template<typename T> inline size_t InlineLinkedList<T>::size_slow() const
return size;
}
template<typename T> inline void InlineLinkedList<T>::clear()
template<typename T>
inline void InlineLinkedList<T>::clear()
{
m_head = 0;
m_tail = 0;
}
template<typename T> inline void InlineLinkedList<T>::prepend(T* node)
template<typename T>
inline void InlineLinkedList<T>::prepend(T* node)
{
if (!m_head) {
ASSERT(!m_tail);
@ -107,7 +117,8 @@ template<typename T> inline void InlineLinkedList<T>::prepend(T* node)
m_head = node;
}
template<typename T> inline void InlineLinkedList<T>::append(T* node)
template<typename T>
inline void InlineLinkedList<T>::append(T* node)
{
if (!m_tail) {
ASSERT(!m_head);
@ -125,7 +136,8 @@ template<typename T> inline void InlineLinkedList<T>::append(T* node)
m_tail = node;
}
template<typename T> inline void InlineLinkedList<T>::remove(T* node)
template<typename T>
inline void InlineLinkedList<T>::remove(T* node)
{
if (node->prev()) {
ASSERT(node != m_head);
@ -144,7 +156,8 @@ template<typename T> inline void InlineLinkedList<T>::remove(T* node)
}
}
template<typename T> inline T* InlineLinkedList<T>::remove_head()
template<typename T>
inline T* InlineLinkedList<T>::remove_head()
{
T* node = head();
if (node)
@ -152,7 +165,8 @@ template<typename T> inline T* InlineLinkedList<T>::remove_head()
return node;
}
template<typename T> inline T* InlineLinkedList<T>::remove_tail()
template<typename T>
inline T* InlineLinkedList<T>::remove_tail()
{
T* node = tail();
if (node)
@ -160,7 +174,8 @@ template<typename T> inline T* InlineLinkedList<T>::remove_tail()
return node;
}
template<typename T> inline void InlineLinkedList<T>::append(InlineLinkedList<T>& other)
template<typename T>
inline void InlineLinkedList<T>::append(InlineLinkedList<T>& other)
{
if (!other.head())
return;

View file

@ -1,9 +1,9 @@
#include <AK/MappedFile.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
//#define DEBUG_MAPPED_FILE
@ -14,7 +14,7 @@ MappedFile::MappedFile(const String& file_name)
{
m_size = PAGE_SIZE;
m_fd = open(m_file_name.characters(), O_RDONLY | O_CLOEXEC);
if (m_fd != -1) {
struct stat st;
fstat(m_fd, &st);
@ -44,7 +44,7 @@ void MappedFile::unmap()
ASSERT(rc == 0);
rc = close(m_fd);
ASSERT(rc == 0);
m_file_name = { };
m_file_name = {};
m_size = 0;
m_fd = -1;
m_map = (void*)-1;
@ -74,4 +74,3 @@ MappedFile& MappedFile::operator=(MappedFile&& other)
}
}

View file

@ -6,7 +6,7 @@ namespace AK {
class MappedFile {
public:
MappedFile() { }
MappedFile() {}
explicit MappedFile(const String& file_name);
MappedFile(MappedFile&&);
~MappedFile();
@ -30,4 +30,3 @@ private:
}
using AK::MappedFile;

View file

@ -18,7 +18,8 @@ template<typename T>
}
template<typename T>
class [[gnu::packed]] NetworkOrdered {
class [[gnu::packed]] NetworkOrdered
{
public:
NetworkOrdered()
: m_network_value(0)

View file

@ -1,6 +1,6 @@
#pragma once
#define AK_MAKE_NONCOPYABLE(c) \
private: \
c(const c&) = delete; \
private: \
c(const c&) = delete; \
c& operator=(const c&) = delete;

View file

@ -1,24 +1,34 @@
#pragma once
#include "StdLibExtras.h"
#include "Types.h"
#include "Traits.h"
#include "Types.h"
namespace AK {
template<typename T>
class OwnPtr {
public:
OwnPtr() { }
explicit OwnPtr(T* ptr) : m_ptr(ptr) { }
OwnPtr(OwnPtr&& other) : m_ptr(other.leak_ptr()) { }
template<typename U> OwnPtr(OwnPtr<U>&& other) : m_ptr(static_cast<T*>(other.leak_ptr())) { }
OwnPtr(std::nullptr_t) { };
OwnPtr() {}
explicit OwnPtr(T* ptr)
: m_ptr(ptr)
{
}
OwnPtr(OwnPtr&& other)
: m_ptr(other.leak_ptr())
{
}
template<typename U>
OwnPtr(OwnPtr<U>&& other)
: m_ptr(static_cast<T*>(other.leak_ptr()))
{
}
OwnPtr(std::nullptr_t) {};
~OwnPtr()
{
clear();
#ifdef SANITIZE_PTRS
if constexpr(sizeof(T*) == 8)
if constexpr (sizeof(T*) == 8)
m_ptr = (T*)(0xe1e1e1e1e1e1e1e1);
else
m_ptr = (T*)(0xe1e1e1e1);
@ -91,7 +101,8 @@ private:
T* m_ptr = nullptr;
};
template<class T, class... Args> inline OwnPtr<T>
template<class T, class... Args>
inline OwnPtr<T>
make(Args&&... args)
{
return OwnPtr<T>(new T(AK::forward<Args>(args)...));
@ -105,6 +116,5 @@ struct Traits<OwnPtr<T>> {
}
using AK::OwnPtr;
using AK::make;
using AK::OwnPtr;

View file

@ -8,7 +8,7 @@ bool is_less_than(const T& a, const T& b)
return a < b;
}
template <typename Iterator, typename LessThan>
template<typename Iterator, typename LessThan>
void quick_sort(Iterator start, Iterator end, LessThan less_than = is_less_than)
{
int size = end - start;
@ -24,7 +24,7 @@ void quick_sort(Iterator start, Iterator end, LessThan less_than = is_less_than)
int i = 1;
for (int j = 1; j < size; ++j) {
if (less_than(*(start + j), pivot)) {
swap(*(start+j), *(start + i));
swap(*(start + j), *(start + i));
++i;
}
}

View file

@ -1,38 +1,80 @@
#pragma once
#include <AK/Types.h>
#include <AK/Retained.h>
#include <AK/Types.h>
namespace AK {
template<typename T>
class RetainPtr {
public:
enum AdoptTag { Adopt };
enum AdoptTag {
Adopt
};
RetainPtr() { }
RetainPtr(const T* ptr) : m_ptr(const_cast<T*>(ptr)) { retain_if_not_null(m_ptr); }
RetainPtr(T* ptr) : m_ptr(ptr) { retain_if_not_null(m_ptr); }
RetainPtr(T& object) : m_ptr(&object) { m_ptr->retain(); }
RetainPtr(const T& object) : m_ptr(const_cast<T*>(&object)) { m_ptr->retain(); }
RetainPtr(AdoptTag, T& object) : m_ptr(&object) { }
RetainPtr(RetainPtr& other) : m_ptr(other.copy_ref().leak_ref()) { }
RetainPtr(RetainPtr&& other) : m_ptr(other.leak_ref()) { }
template<typename U> RetainPtr(Retained<U>&& other) : m_ptr(static_cast<T*>(&other.leak_ref())) { }
template<typename U> RetainPtr(RetainPtr<U>&& other) : m_ptr(static_cast<T*>(other.leak_ref())) { }
RetainPtr(const RetainPtr& other) : m_ptr(const_cast<RetainPtr&>(other).copy_ref().leak_ref()) { }
template<typename U> RetainPtr(const RetainPtr<U>& other) : m_ptr(const_cast<RetainPtr<U>&>(other).copy_ref().leak_ref()) { }
RetainPtr() {}
RetainPtr(const T* ptr)
: m_ptr(const_cast<T*>(ptr))
{
retain_if_not_null(m_ptr);
}
RetainPtr(T* ptr)
: m_ptr(ptr)
{
retain_if_not_null(m_ptr);
}
RetainPtr(T& object)
: m_ptr(&object)
{
m_ptr->retain();
}
RetainPtr(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->retain();
}
RetainPtr(AdoptTag, T& object)
: m_ptr(&object)
{
}
RetainPtr(RetainPtr& other)
: m_ptr(other.copy_ref().leak_ref())
{
}
RetainPtr(RetainPtr&& other)
: m_ptr(other.leak_ref())
{
}
template<typename U>
RetainPtr(Retained<U>&& other)
: m_ptr(static_cast<T*>(&other.leak_ref()))
{
}
template<typename U>
RetainPtr(RetainPtr<U>&& other)
: m_ptr(static_cast<T*>(other.leak_ref()))
{
}
RetainPtr(const RetainPtr& other)
: m_ptr(const_cast<RetainPtr&>(other).copy_ref().leak_ref())
{
}
template<typename U>
RetainPtr(const RetainPtr<U>& other)
: m_ptr(const_cast<RetainPtr<U>&>(other).copy_ref().leak_ref())
{
}
~RetainPtr()
{
clear();
#ifdef SANITIZE_PTRS
if constexpr(sizeof(T*) == 8)
if constexpr (sizeof(T*) == 8)
m_ptr = (T*)(0xe0e0e0e0e0e0e0e0);
else
m_ptr = (T*)(0xe0e0e0e0);
#endif
}
RetainPtr(std::nullptr_t) { }
RetainPtr(std::nullptr_t) {}
RetainPtr& operator=(RetainPtr&& other)
{
@ -143,4 +185,3 @@ private:
}
using AK::RetainPtr;

View file

@ -6,27 +6,27 @@
namespace AK {
template<class T>
constexpr auto call_will_be_destroyed_if_present(T* object) -> decltype(object->will_be_destroyed(), TrueType { })
constexpr auto call_will_be_destroyed_if_present(T* object) -> decltype(object->will_be_destroyed(), TrueType {})
{
object->will_be_destroyed();
return { };
return {};
}
constexpr auto call_will_be_destroyed_if_present(...) -> FalseType
{
return { };
return {};
}
template<class T>
constexpr auto call_one_retain_left_if_present(T* object) -> decltype(object->one_retain_left(), TrueType { })
constexpr auto call_one_retain_left_if_present(T* object) -> decltype(object->one_retain_left(), TrueType {})
{
object->one_retain_left();
return { };
return {};
}
constexpr auto call_one_retain_left_if_present(...) -> FalseType
{
return { };
return {};
}
class RetainableBase {
@ -43,7 +43,7 @@ public:
}
protected:
RetainableBase() { }
RetainableBase() {}
~RetainableBase()
{
ASSERT(!m_retain_count);
@ -76,4 +76,3 @@ public:
}
using AK::Retainable;

View file

@ -4,15 +4,15 @@
#include <AK/Types.h>
#ifdef __clang__
#define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
#define CALLABLE_WHEN(...) __attribute__((callable_when(__VA_ARGS__)))
#define SET_TYPESTATE(state) __attribute__((set_typestate(state)))
#define RETURN_TYPESTATE(state) __attribute__((return_typestate(state)))
# define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
# define CALLABLE_WHEN(...) __attribute__((callable_when(__VA_ARGS__)))
# define SET_TYPESTATE(state) __attribute__((set_typestate(state)))
# define RETURN_TYPESTATE(state) __attribute__((return_typestate(state)))
#else
#define CONSUMABLE(initial_state)
#define CALLABLE_WHEN(state)
#define SET_TYPESTATE(state)
#define RETURN_TYPESTATE(state)
# define CONSUMABLE(initial_state)
# define CALLABLE_WHEN(state)
# define SET_TYPESTATE(state)
# define RETURN_TYPESTATE(state)
#endif
namespace AK {
@ -34,30 +34,75 @@ inline void release_if_not_null(T* ptr)
template<typename T>
class CONSUMABLE(unconsumed) Retained {
public:
enum AdoptTag { Adopt };
enum AdoptTag {
Adopt
};
RETURN_TYPESTATE(unconsumed) Retained(const T& object) : m_ptr(const_cast<T*>(&object)) { m_ptr->retain(); }
RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); }
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(U& object) : m_ptr(&static_cast<T&>(object)) { m_ptr->retain(); }
RETURN_TYPESTATE(unconsumed) Retained(AdoptTag, T& object) : m_ptr(&object) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained& other) : m_ptr(&other.copy_ref().leak_ref()) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { }
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(Retained<U>&& other) : m_ptr(static_cast<T*>(&other.leak_ref())) { }
RETURN_TYPESTATE(unconsumed) Retained(const Retained& other) : m_ptr(&const_cast<Retained&>(other).copy_ref().leak_ref()) { }
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(const Retained<U>& other) : m_ptr(&const_cast<Retained<U>&>(other).copy_ref().leak_ref()) { }
RETURN_TYPESTATE(unconsumed)
Retained(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->retain();
}
RETURN_TYPESTATE(unconsumed)
Retained(T& object)
: m_ptr(&object)
{
m_ptr->retain();
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
Retained(U& object)
: m_ptr(&static_cast<T&>(object))
{
m_ptr->retain();
}
RETURN_TYPESTATE(unconsumed)
Retained(AdoptTag, T& object)
: m_ptr(&object)
{
}
RETURN_TYPESTATE(unconsumed)
Retained(Retained& other)
: m_ptr(&other.copy_ref().leak_ref())
{
}
RETURN_TYPESTATE(unconsumed)
Retained(Retained&& other)
: m_ptr(&other.leak_ref())
{
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
Retained(Retained<U>&& other)
: m_ptr(static_cast<T*>(&other.leak_ref()))
{
}
RETURN_TYPESTATE(unconsumed)
Retained(const Retained& other)
: m_ptr(&const_cast<Retained&>(other).copy_ref().leak_ref())
{
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
Retained(const Retained<U>& other)
: m_ptr(&const_cast<Retained<U>&>(other).copy_ref().leak_ref())
{
}
~Retained()
{
release_if_not_null(m_ptr);
m_ptr = nullptr;
#ifdef SANITIZE_PTRS
if constexpr(sizeof(T*) == 8)
if constexpr (sizeof(T*) == 8)
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
else
m_ptr = (T*)(0xb0b0b0b0);
#endif
}
CALLABLE_WHEN(unconsumed) Retained& operator=(Retained&& other)
CALLABLE_WHEN(unconsumed)
Retained& operator=(Retained&& other)
{
if (this != &other) {
release_if_not_null(m_ptr);
@ -67,7 +112,8 @@ public:
}
template<typename U>
CALLABLE_WHEN(unconsumed) Retained& operator=(Retained<U>&& other)
CALLABLE_WHEN(unconsumed)
Retained& operator=(Retained<U>&& other)
{
if (this != static_cast<void*>(&other)) {
release_if_not_null(m_ptr);
@ -76,7 +122,8 @@ public:
return *this;
}
CALLABLE_WHEN(unconsumed) Retained& operator=(T& object)
CALLABLE_WHEN(unconsumed)
Retained& operator=(T& object)
{
if (m_ptr != &object)
release_if_not_null(m_ptr);
@ -85,12 +132,14 @@ public:
return *this;
}
CALLABLE_WHEN(unconsumed) Retained copy_ref() const
CALLABLE_WHEN(unconsumed)
Retained copy_ref() const
{
return Retained(*m_ptr);
}
CALLABLE_WHEN(unconsumed) SET_TYPESTATE(consumed)
CALLABLE_WHEN(unconsumed)
SET_TYPESTATE(consumed)
T& leak_ref()
{
ASSERT(m_ptr);
@ -99,20 +148,60 @@ public:
return *leakedPtr;
}
CALLABLE_WHEN(unconsumed) T* ptr() { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) const T* ptr() const { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed)
T* ptr()
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
const T* ptr() const
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed) T* operator->() { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) const T* operator->() const { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed)
T* operator->()
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
const T* operator->() const
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed) T& operator*() { ASSERT(m_ptr); return *m_ptr; }
CALLABLE_WHEN(unconsumed) const T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
CALLABLE_WHEN(unconsumed)
T& operator*()
{
ASSERT(m_ptr);
return *m_ptr;
}
CALLABLE_WHEN(unconsumed)
const T& operator*() const
{
ASSERT(m_ptr);
return *m_ptr;
}
CALLABLE_WHEN(unconsumed) operator T*() { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed) operator const T*() const { ASSERT(m_ptr); return m_ptr; }
CALLABLE_WHEN(unconsumed)
operator T*()
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
operator const T*() const
{
ASSERT(m_ptr);
return m_ptr;
}
private:
Retained() { }
Retained() {}
T* m_ptr { nullptr };
};
@ -125,5 +214,5 @@ inline Retained<T> adopt(T& object)
}
using AK::Retained;
using AK::adopt;
using AK::Retained;

View file

@ -8,13 +8,16 @@ template<typename T>
class SinglyLinkedList {
private:
struct Node {
explicit Node(T&& v) : value(v) { }
explicit Node(T&& v)
: value(v)
{
}
T value;
Node* next { nullptr };
};
public:
SinglyLinkedList() { }
SinglyLinkedList() {}
~SinglyLinkedList() { clear(); }
bool is_empty() const { return !head(); }
@ -29,7 +32,7 @@ public:
void clear()
{
for (auto* node = m_head; node; ) {
for (auto* node = m_head; node;) {
auto* next = node->next;
delete node;
node = next;
@ -38,10 +41,26 @@ public:
m_tail = nullptr;
}
T& first() { ASSERT(head()); return head()->value; }
const T& first() const { ASSERT(head()); return head()->value; }
T& last() { ASSERT(head()); return tail()->value; }
const T& last() const { ASSERT(head()); return tail()->value; }
T& first()
{
ASSERT(head());
return head()->value;
}
const T& first() const
{
ASSERT(head());
return head()->value;
}
T& last()
{
ASSERT(head());
return tail()->value;
}
const T& last() const
{
ASSERT(head());
return tail()->value;
}
T take_first()
{
@ -79,13 +98,21 @@ public:
class Iterator {
public:
bool operator!=(const Iterator& other) { return m_node != other.m_node; }
Iterator& operator++() { m_node = m_node->next; return *this; }
Iterator& operator++()
{
m_node = m_node->next;
return *this;
}
T& operator*() { return m_node->value; }
bool is_end() const { return !m_node; }
static Iterator universal_end() { return Iterator(nullptr); }
private:
friend class SinglyLinkedList;
explicit Iterator(SinglyLinkedList::Node* node) : m_node(node) { }
explicit Iterator(SinglyLinkedList::Node* node)
: m_node(node)
{
}
SinglyLinkedList::Node* m_node;
};
@ -95,13 +122,21 @@ public:
class ConstIterator {
public:
bool operator!=(const ConstIterator& other) { return m_node != other.m_node; }
ConstIterator& operator++() { m_node = m_node->next; return *this; }
ConstIterator& operator++()
{
m_node = m_node->next;
return *this;
}
const T& operator*() const { return m_node->value; }
bool is_end() const { return !m_node; }
static ConstIterator universal_end() { return ConstIterator(nullptr); }
private:
friend class SinglyLinkedList;
explicit ConstIterator(const SinglyLinkedList::Node* node) : m_node(node) { }
explicit ConstIterator(const SinglyLinkedList::Node* node)
: m_node(node)
{
}
const SinglyLinkedList::Node* m_node;
};
@ -142,4 +177,3 @@ private:
}
using AK::SinglyLinkedList;

View file

@ -1,5 +1,5 @@
#include <AK/StdLibExtras.h>
#include <AK/Assertions.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
#include <AK/kstdio.h>
@ -20,32 +20,33 @@ void* mmx_memcpy(void* dest, const void* src, size_t len)
"rep movsb\n"
: "=S"(src_ptr), "=D"(dest_ptr), "=c"(prologue)
: "0"(src_ptr), "1"(dest_ptr), "2"(prologue)
: "memory"
);
: "memory");
}
for (dword i = len / 64; i; --i) {
asm volatile(
"movq (%0), %%mm0\n"
"movq 8(%0), %%mm1\n"
"movq 16(%0), %%mm2\n"
"movq 24(%0), %%mm3\n"
"movq 32(%0), %%mm4\n"
"movq 40(%0), %%mm5\n"
"movq 48(%0), %%mm6\n"
"movq 56(%0), %%mm7\n"
"movq %%mm0, (%1)\n"
"movq %%mm1, 8(%1)\n"
"movq %%mm2, 16(%1)\n"
"movq %%mm3, 24(%1)\n"
"movq %%mm4, 32(%1)\n"
"movq %%mm5, 40(%1)\n"
"movq %%mm6, 48(%1)\n"
"movq %%mm7, 56(%1)\n"
:: "r" (src_ptr), "r" (dest_ptr) : "memory");
"movq (%0), %%mm0\n"
"movq 8(%0), %%mm1\n"
"movq 16(%0), %%mm2\n"
"movq 24(%0), %%mm3\n"
"movq 32(%0), %%mm4\n"
"movq 40(%0), %%mm5\n"
"movq 48(%0), %%mm6\n"
"movq 56(%0), %%mm7\n"
"movq %%mm0, (%1)\n"
"movq %%mm1, 8(%1)\n"
"movq %%mm2, 16(%1)\n"
"movq %%mm3, 24(%1)\n"
"movq %%mm4, 32(%1)\n"
"movq %%mm5, 40(%1)\n"
"movq %%mm6, 48(%1)\n"
"movq %%mm7, 56(%1)\n" ::"r"(src_ptr),
"r"(dest_ptr)
: "memory");
src_ptr += 64;
dest_ptr += 64;
}
asm volatile("emms":::"memory");
asm volatile("emms" ::
: "memory");
// Whatever remains we'll have to memcpy.
len %= 64;
if (len)
@ -62,13 +63,15 @@ static inline uint32_t divq(uint64_t n, uint32_t d)
uint32_t n0 = n;
uint32_t q;
uint32_t r;
asm volatile("divl %4" : "=d"(r), "=a"(q) : "0"(n1), "1"(n0), "rm"(d));
asm volatile("divl %4"
: "=d"(r), "=a"(q)
: "0"(n1), "1"(n0), "rm"(d));
return q;
}
static uint64_t unsigned_divide64(uint64_t n, uint64_t d)
{
if ((d >> 32) == 0) {
if ((d >> 32) == 0) {
uint64_t b = 1ULL << 32;
uint32_t n1 = n >> 32;
uint32_t n0 = n;
@ -149,5 +152,4 @@ uint64_t __udivmoddi4(uint64_t n, uint64_t d, uint64_t* r)
return q;
}
#endif
}

View file

@ -1,10 +1,10 @@
#pragma once
#ifdef KERNEL
#include <Kernel/StdLib.h>
# include <Kernel/StdLib.h>
#else
#include <stdlib.h>
#include <string.h>
# include <stdlib.h>
# include <string.h>
#endif
#define UNUSED_PARAM(x) (void)x
@ -27,8 +27,7 @@ extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
"rep movsl\n"
: "=S"(src), "=D"(dest), "=c"(count)
: "S"(src), "D"(dest), "c"(count)
: "memory"
);
: "memory");
}
[[gnu::always_inline]] inline void fast_dword_fill(dword* dest, dword value, size_t count)
@ -37,13 +36,12 @@ extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
"rep stosl\n"
: "=D"(dest), "=c"(count)
: "D"(dest), "c"(count), "a"(value)
: "memory"
);
: "memory");
}
inline constexpr dword round_up_to_power_of_two(dword value, dword power_of_two)
{
return ((value - 1) & ~ (power_of_two - 1)) + power_of_two;
return ((value - 1) & ~(power_of_two - 1)) + power_of_two;
}
namespace AK {
@ -60,7 +58,6 @@ inline T max(const T& a, const T& b)
return a < b ? b : a;
}
template<typename T, typename U>
static inline T ceil_div(T a, U b)
{
@ -72,16 +69,16 @@ static inline T ceil_div(T a, U b)
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconsumed"
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wconsumed"
#endif
template <typename T>
template<typename T>
T&& move(T& arg)
{
return static_cast<T&&>(arg);
}
#ifdef __clang__
#pragma clang diagnostic pop
# pragma clang diagnostic pop
#endif
template<typename T>
@ -107,26 +104,37 @@ template<typename T, typename U>
void swap(T& a, U& b)
{
U tmp = move((U&)a);
a = (T&&)move(b);
a = (T &&) move(b);
b = move(tmp);
}
template<bool B, class T = void>
struct EnableIf
{
struct EnableIf {
};
template<class T>
struct EnableIf<true, T>
{
struct EnableIf<true, T> {
typedef T Type;
};
template<class T> struct RemoveConst { typedef T Type; };
template<class T> struct RemoveConst<const T> { typedef T Type; };
template<class T> struct RemoveVolatile { typedef T Type; };
template<class T> struct RemoveVolatile<const T> { typedef T Type; };
template<class T> struct RemoveCV {
template<class T>
struct RemoveConst {
typedef T Type;
};
template<class T>
struct RemoveConst<const T> {
typedef T Type;
};
template<class T>
struct RemoveVolatile {
typedef T Type;
};
template<class T>
struct RemoveVolatile<const T> {
typedef T Type;
};
template<class T>
struct RemoveCV {
typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
};
@ -143,69 +151,145 @@ typedef IntegralConstant<bool, false> FalseType;
typedef IntegralConstant<bool, true> TrueType;
template<class T>
struct __IsPointerHelper : FalseType { };
struct __IsPointerHelper : FalseType {
};
template<class T>
struct __IsPointerHelper<T*> : TrueType { };
struct __IsPointerHelper<T*> : TrueType {
};
template<class T>
struct IsPointer : __IsPointerHelper<typename RemoveCV<T>::Type> { };
struct IsPointer : __IsPointerHelper<typename RemoveCV<T>::Type> {
};
template<class> struct IsFunction : FalseType { };
template<class>
struct IsFunction : FalseType {
};
template<class Ret, class... Args> struct IsFunction<Ret(Args...)> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...)> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile> : TrueType { };
template<class Ret, class... Args>
struct IsFunction<Ret(Args...)> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...)> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) const> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) const> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) volatile> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) volatile> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) const volatile> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) const volatile> : TrueType {
};
template<class Ret, class... Args> struct IsFunction<Ret(Args...) &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile &> : TrueType { };
template<class Ret, class... Args>
struct IsFunction<Ret(Args...)&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...)&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) const&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) const&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) volatile&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) volatile&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) const volatile&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) const volatile&> : TrueType {
};
template<class Ret, class... Args> struct IsFunction<Ret(Args...) &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &&> : TrueType { };
template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile &&> : TrueType { };
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) &&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) &&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) const&&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) const&&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) volatile&&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) volatile&&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args...) const volatile&&> : TrueType {
};
template<class Ret, class... Args>
struct IsFunction<Ret(Args..., ...) const volatile&&> : TrueType {
};
template<class T> struct IsRvalueReference : FalseType { };
template<class T> struct IsRvalueReference<T&&> : TrueType { };
template<class T>
struct IsRvalueReference : FalseType {
};
template<class T>
struct IsRvalueReference<T&&> : TrueType {
};
template<class T> struct RemovePointer { typedef T Type; };
template<class T> struct RemovePointer<T*> { typedef T Type; };
template<class T> struct RemovePointer<T* const> { typedef T Type; };
template<class T> struct RemovePointer<T* volatile> { typedef T Type; };
template<class T> struct RemovePointer<T* const volatile> { typedef T Type; };
template<class T>
struct RemovePointer {
typedef T Type;
};
template<class T>
struct RemovePointer<T*> {
typedef T Type;
};
template<class T>
struct RemovePointer<T* const> {
typedef T Type;
};
template<class T>
struct RemovePointer<T* volatile> {
typedef T Type;
};
template<class T>
struct RemovePointer<T* const volatile> {
typedef T Type;
};
template<typename T, typename U>
struct IsSame {
enum { value = 0 };
enum {
value = 0
};
};
template<typename T>
struct IsSame<T, T> {
enum { value = 1 };
enum {
value = 1
};
};
}
using AK::min;
using AK::max;
using AK::move;
using AK::forward;
using AK::exchange;
using AK::swap;
using AK::ceil_div;
using AK::exchange;
using AK::forward;
using AK::IsSame;
using AK::max;
using AK::min;
using AK::move;
using AK::swap;

View file

@ -38,7 +38,7 @@ String String::empty()
String String::isolated_copy() const
{
if (!m_impl)
return { };
return {};
if (!m_impl->length())
return empty();
char* buffer;
@ -50,7 +50,7 @@ String String::isolated_copy() const
String String::substring(int start, int length) const
{
if (!length)
return { };
return {};
ASSERT(m_impl);
ASSERT(start + length <= m_impl->length());
// FIXME: This needs some input bounds checking.
@ -60,7 +60,7 @@ String String::substring(int start, int length) const
StringView String::substring_view(int start, int length) const
{
if (!length)
return { };
return {};
ASSERT(m_impl);
ASSERT(start + length <= m_impl->length());
// FIXME: This needs some input bounds checking.
@ -70,7 +70,7 @@ StringView String::substring_view(int start, int length) const
Vector<String> String::split(const char separator) const
{
if (is_empty())
return { };
return {};
Vector<String> v;
ssize_t substart = 0;
@ -94,7 +94,7 @@ Vector<String> String::split(const char separator) const
Vector<StringView> String::split_view(const char separator) const
{
if (is_empty())
return { };
return {};
Vector<StringView> v;
ssize_t substart = 0;
@ -232,7 +232,7 @@ bool String::match_helper(const String& mask) const
if (!*++mask_ptr)
return true;
mp = mask_ptr;
cp = string_ptr+1;
cp = string_ptr + 1;
} else if ((*mask_ptr == *string_ptr) || (*mask_ptr == '?')) {
mask_ptr++;
string_ptr++;

View file

@ -1,7 +1,7 @@
#include "StringBuilder.h"
#include <LibC/stdarg.h>
#include "printf.cpp"
#include <AK/StdLibExtras.h>
#include <LibC/stdarg.h>
namespace AK {
@ -43,9 +43,10 @@ void StringBuilder::append(char ch)
void StringBuilder::appendvf(const char* fmt, va_list ap)
{
printf_internal([this] (char*&, char ch) {
printf_internal([this](char*&, char ch) {
append(ch);
}, nullptr, fmt, ap);
},
nullptr, fmt, ap);
}
void StringBuilder::appendf(const char* fmt, ...)
@ -71,4 +72,3 @@ String StringBuilder::to_string()
}
}

View file

@ -9,7 +9,7 @@ namespace AK {
class StringBuilder {
public:
explicit StringBuilder(ssize_t initial_capacity = 16);
~StringBuilder() { }
~StringBuilder() {}
void append(const String&);
void append(char);
@ -30,4 +30,3 @@ private:
}
using AK::StringBuilder;

View file

@ -1,7 +1,7 @@
#include "StringImpl.h"
#include "HashTable.h"
#include "StdLibExtras.h"
#include "kmalloc.h"
#include "HashTable.h"
//#define DEBUG_STRINGIMPL
@ -26,7 +26,8 @@ static StringImpl* s_the_empty_stringimpl = nullptr;
StringImpl& StringImpl::the_empty_stringimpl()
{
if (!s_the_empty_stringimpl)
s_the_empty_stringimpl = new StringImpl(ConstructTheEmptyStringImpl);;
s_the_empty_stringimpl = new StringImpl(ConstructTheEmptyStringImpl);
;
return *s_the_empty_stringimpl;
}
@ -168,4 +169,3 @@ void StringImpl::compute_hash() const
}
}

View file

@ -1,12 +1,15 @@
#pragma once
#include "Retainable.h"
#include "RetainPtr.h"
#include "Retainable.h"
#include "Types.h"
namespace AK {
enum ShouldChomp { NoChomp, Chomp };
enum ShouldChomp {
NoChomp,
Chomp
};
class StringImpl : public Retainable<StringImpl> {
public:
@ -22,7 +25,11 @@ public:
ssize_t length() const { return m_length; }
const char* characters() const { return m_characters; }
char operator[](ssize_t i) const { ASSERT(i >= 0 && i < m_length); return m_characters[i]; }
char operator[](ssize_t i) const
{
ASSERT(i >= 0 && i < m_length);
return m_characters[i];
}
unsigned hash() const
{
@ -32,10 +39,17 @@ public:
}
private:
enum ConstructTheEmptyStringImplTag { ConstructTheEmptyStringImpl };
explicit StringImpl(ConstructTheEmptyStringImplTag) : m_characters("") { }
enum ConstructTheEmptyStringImplTag {
ConstructTheEmptyStringImpl
};
explicit StringImpl(ConstructTheEmptyStringImplTag)
: m_characters("")
{
}
enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer };
enum ConstructWithInlineBufferTag {
ConstructWithInlineBuffer
};
StringImpl(ConstructWithInlineBufferTag, ssize_t length);
void compute_hash() const;
@ -63,6 +77,6 @@ inline dword string_hash(const char* characters, int length)
}
using AK::StringImpl;
using AK::Chomp;
using AK::string_hash;
using AK::StringImpl;

View file

@ -1,12 +1,12 @@
#include <AK/StringView.h>
#include <AK/AKString.h>
#include <AK/StringView.h>
namespace AK {
Vector<StringView> StringView::split_view(const char separator) const
{
if (is_empty())
return { };
return {};
Vector<StringView> v;
ssize_t substart = 0;
@ -30,7 +30,7 @@ Vector<StringView> StringView::split_view(const char separator) const
StringView StringView::substring_view(int start, int length) const
{
if (!length)
return { };
return {};
ASSERT(start + length <= m_length);
return { m_characters + start, length };
}
@ -50,5 +50,4 @@ unsigned StringView::to_uint(bool& ok) const
return value;
}
}

View file

@ -8,9 +8,17 @@ class String;
class StringView {
public:
StringView() { }
StringView(const char* characters, int length) : m_characters(characters), m_length(length) { }
StringView(const unsigned char* characters, int length) : m_characters((const char*)characters), m_length(length) { }
StringView() {}
StringView(const char* characters, int length)
: m_characters(characters)
, m_length(length)
{
}
StringView(const unsigned char* characters, int length)
: m_characters((const char*)characters)
, m_length(length)
{
}
StringView(const char* cstring)
: m_characters(cstring)
{

View file

@ -5,7 +5,12 @@ namespace AK {
template<typename T>
class TemporaryChange {
public:
TemporaryChange(T& variable, T value) : m_variable(variable), m_old_value(variable) { m_variable = value; }
TemporaryChange(T& variable, T value)
: m_variable(variable)
, m_old_value(variable)
{
m_variable = value;
}
~TemporaryChange() { m_variable = m_old_value; }
private:

View file

@ -1,13 +1,12 @@
#pragma once
#include "kstdio.h"
#include "HashFunctions.h"
#include "kstdio.h"
namespace AK {
template<typename T>
struct Traits
{
struct Traits {
};
template<>
@ -38,4 +37,3 @@ struct Traits<T*> {
};
}

View file

@ -30,8 +30,8 @@ typedef signed_dword int32_t;
typedef signed_qword int64_t;
#else
#include <stdint.h>
#include <sys/types.h>
# include <stdint.h>
# include <sys/types.h>
typedef uint8_t byte;
typedef uint16_t word;
@ -48,9 +48,11 @@ constexpr unsigned KB = 1024;
constexpr unsigned MB = KB * KB;
constexpr unsigned GB = KB * KB * KB;
enum class IterationDecision { Continue, Abort };
enum class IterationDecision {
Continue,
Abort
};
namespace std {
typedef decltype(nullptr) nullptr_t;
}

View file

@ -112,8 +112,16 @@ public:
return m_outline_buffer;
}
const T& at(int i) const { ASSERT(i >= 0 && i < m_size); return data()[i]; }
T& at(int i) { ASSERT(i >= 0 && i < m_size); return data()[i]; }
const T& at(int i) const
{
ASSERT(i >= 0 && i < m_size);
return data()[i];
}
T& at(int i)
{
ASSERT(i >= 0 && i < m_size);
return data()[i];
}
const T& operator[](int i) const { return at(i); }
T& operator[](int i) { return at(i); }
@ -314,8 +322,16 @@ public:
bool operator<(const Iterator& other) { return m_index < other.m_index; }
bool operator>(const Iterator& other) { return m_index > other.m_index; }
bool operator>=(const Iterator& other) { return m_index >= other.m_index; }
Iterator& operator++() { ++m_index; return *this; }
Iterator& operator--() { --m_index; return *this; }
Iterator& operator++()
{
++m_index;
return *this;
}
Iterator& operator--()
{
--m_index;
return *this;
}
Iterator operator-(int value) { return { m_vector, m_index - value }; }
Iterator operator+(int value) { return { m_vector, m_index + value }; }
Iterator& operator=(const Iterator& other)
@ -325,9 +341,14 @@ public:
}
T& operator*() { return m_vector[m_index]; }
int operator-(const Iterator& other) { return m_index - other.m_index; }
private:
friend class Vector;
Iterator(Vector& vector, int index) : m_vector(vector), m_index(index) { }
Iterator(Vector& vector, int index)
: m_vector(vector)
, m_index(index)
{
}
Vector& m_vector;
int m_index { 0 };
};
@ -342,8 +363,16 @@ public:
bool operator<(const ConstIterator& other) { return m_index < other.m_index; }
bool operator>(const ConstIterator& other) { return m_index > other.m_index; }
bool operator>=(const ConstIterator& other) { return m_index >= other.m_index; }
ConstIterator& operator++() { ++m_index; return *this; }
ConstIterator& operator--() { --m_index; return *this; }
ConstIterator& operator++()
{
++m_index;
return *this;
}
ConstIterator& operator--()
{
--m_index;
return *this;
}
ConstIterator operator-(int value) { return { m_vector, m_index - value }; }
ConstIterator operator+(int value) { return { m_vector, m_index + value }; }
ConstIterator& operator=(const ConstIterator& other)
@ -353,9 +382,14 @@ public:
}
const T& operator*() const { return m_vector[m_index]; }
int operator-(const ConstIterator& other) { return m_index - other.m_index; }
private:
friend class Vector;
ConstIterator(const Vector& vector, const int index) : m_vector(vector), m_index(index) { }
ConstIterator(const Vector& vector, const int index)
: m_vector(vector)
, m_index(index)
{
}
const Vector& m_vector;
int m_index { 0 };
};
@ -377,8 +411,16 @@ private:
T* slot(int i) { return &data()[i]; }
const T* slot(int i) const { return &data()[i]; }
T* inline_buffer() { static_assert(inline_capacity > 0); return reinterpret_cast<T*>(m_inline_buffer_storage); }
const T* inline_buffer() const { static_assert(inline_capacity > 0); return reinterpret_cast<const T*>(m_inline_buffer_storage); }
T* inline_buffer()
{
static_assert(inline_capacity > 0);
return reinterpret_cast<T*>(m_inline_buffer_storage);
}
const T* inline_buffer() const
{
static_assert(inline_capacity > 0);
return reinterpret_cast<const T*>(m_inline_buffer_storage);
}
int m_size { 0 };
int m_capacity { 0 };

View file

@ -4,14 +4,16 @@
namespace AK {
template<typename T> class OwnPtr;
template<typename T>
class OwnPtr;
template<typename T>
class WeakPtr {
friend class Weakable<T>;
public:
WeakPtr() { }
WeakPtr(std::nullptr_t) { }
WeakPtr() {}
WeakPtr(std::nullptr_t) {}
template<typename U>
WeakPtr(WeakPtr<U>&& other)
@ -48,7 +50,10 @@ public:
bool operator==(const OwnPtr<T>& other) const { return ptr() == other.ptr(); }
private:
WeakPtr(RetainPtr<WeakLink<T>>&& link) : m_link(move(link)) { }
WeakPtr(RetainPtr<WeakLink<T>>&& link)
: m_link(move(link))
{
}
RetainPtr<WeakLink<T>> m_link;
};
@ -64,4 +69,3 @@ inline WeakPtr<T> Weakable<T>::make_weak_ptr()
}
using AK::WeakPtr;

View file

@ -1,23 +1,29 @@
#pragma once
#include "Assertions.h"
#include "Retainable.h"
#include "RetainPtr.h"
#include "Retainable.h"
namespace AK {
template<typename T> class Weakable;
template<typename T> class WeakPtr;
template<typename T>
class Weakable;
template<typename T>
class WeakPtr;
template<typename T>
class WeakLink : public Retainable<WeakLink<T>> {
friend class Weakable<T>;
public:
T* ptr() { return static_cast<T*>(m_ptr); }
const T* ptr() const { return static_cast<const T*>(m_ptr); }
private:
explicit WeakLink(T& weakable) : m_ptr(&weakable) { }
explicit WeakLink(T& weakable)
: m_ptr(&weakable)
{
}
T* m_ptr;
};
@ -25,11 +31,12 @@ template<typename T>
class Weakable {
private:
class Link;
public:
WeakPtr<T> make_weak_ptr();
protected:
Weakable() { }
Weakable() {}
~Weakable()
{

View file

@ -1,25 +1,26 @@
#pragma once
#ifdef KERNEL
#define AK_MAKE_ETERNAL \
public: \
void* operator new(size_t size) { return kmalloc_eternal(size); } \
private:
# define AK_MAKE_ETERNAL \
public: \
void* operator new(size_t size) { return kmalloc_eternal(size); } \
\
private:
#else
#define AK_MAKE_ETERNAL
# define AK_MAKE_ETERNAL
#endif
#ifdef KERNEL
#include <Kernel/kmalloc.h>
# include <Kernel/kmalloc.h>
#else
#include <stdlib.h>
# include <stdlib.h>
#define kcalloc calloc
#define kmalloc malloc
#define kfree free
#define krealloc realloc
# define kcalloc calloc
# define kmalloc malloc
# define kfree free
# define krealloc realloc
#ifdef __serenity__
# ifdef __serenity__
inline void* operator new(size_t size)
{
return kmalloc(size);
@ -44,6 +45,6 @@ inline void* operator new(size_t, void* ptr)
{
return ptr;
}
#endif
# endif
#endif

View file

@ -169,7 +169,6 @@ template<typename PutChFunc>
return fieldWidth;
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, dword fieldWidth)
{
@ -183,7 +182,7 @@ template<typename PutChFunc>
template<typename PutChFunc>
[[gnu::always_inline]] inline int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, char*& ap)
{
const char *p;
const char* p;
int ret = 0;
char* bufptr = buffer;
@ -195,7 +194,7 @@ template<typename PutChFunc>
unsigned long_qualifiers = 0;
bool alternate_form = 0;
if (*p == '%' && *(p + 1)) {
one_more:
one_more:
++p;
if (*p == ' ') {
leftPad = true;
@ -223,87 +222,81 @@ one_more:
if (*(p + 1))
goto one_more;
}
switch( *p )
{
case 's':
{
const char* sp = va_arg(ap, const char*);
ret += print_string(putch, bufptr, sp ? sp : "(null)", leftPad, fieldWidth);
}
break;
switch (*p) {
case 's': {
const char* sp = va_arg(ap, const char*);
ret += print_string(putch, bufptr, sp ? sp : "(null)", leftPad, fieldWidth);
} break;
case 'd':
ret += print_signed_number(putch, bufptr, va_arg(ap, int), leftPad, zeroPad, fieldWidth);
break;
case 'd':
ret += print_signed_number(putch, bufptr, va_arg(ap, int), leftPad, zeroPad, fieldWidth);
break;
case 'u':
ret += print_number(putch, bufptr, va_arg(ap, dword), leftPad, zeroPad, fieldWidth);
break;
case 'u':
ret += print_number(putch, bufptr, va_arg(ap, dword), leftPad, zeroPad, fieldWidth);
break;
case 'Q':
ret += print_qword(putch, bufptr, va_arg(ap, qword), leftPad, zeroPad, fieldWidth);
break;
case 'Q':
ret += print_qword(putch, bufptr, va_arg(ap, qword), leftPad, zeroPad, fieldWidth);
break;
case 'q':
ret += print_hex(putch, bufptr, va_arg(ap, qword), 16);
break;
case 'q':
ret += print_hex(putch, bufptr, va_arg(ap, qword), 16);
break;
#ifndef KERNEL
case 'f':
// FIXME: Print as float!
ret += print_number(putch, bufptr, (int)va_arg(ap, double), leftPad, zeroPad, fieldWidth);
break;
case 'f':
// FIXME: Print as float!
ret += print_number(putch, bufptr, (int)va_arg(ap, double), leftPad, zeroPad, fieldWidth);
break;
#endif
case 'o':
if (alternate_form) {
putch(bufptr, '0');
++ret;
}
ret += print_octal_number(putch, bufptr, va_arg(ap, dword), leftPad, zeroPad, fieldWidth);
break;
case 'x':
if (alternate_form) {
putch(bufptr, '0');
putch(bufptr, 'x');
ret += 2;
}
ret += print_hex(putch, bufptr, va_arg(ap, dword), 8);
break;
case 'w':
ret += print_hex(putch, bufptr, va_arg(ap, int), 4);
break;
case 'b':
ret += print_hex(putch, bufptr, va_arg(ap, int), 2);
break;
case 'c':
putch(bufptr, (char)va_arg(ap, int));
case 'o':
if (alternate_form) {
putch(bufptr, '0');
++ret;
break;
}
ret += print_octal_number(putch, bufptr, va_arg(ap, dword), leftPad, zeroPad, fieldWidth);
break;
case '%':
putch(bufptr, '%');
++ret;
break;
case 'p':
case 'x':
if (alternate_form) {
putch(bufptr, '0');
putch(bufptr, 'x');
ret += 2;
ret += print_hex(putch, bufptr, va_arg(ap, dword), 8);
break;
}
ret += print_hex(putch, bufptr, va_arg(ap, dword), 8);
break;
case 'w':
ret += print_hex(putch, bufptr, va_arg(ap, int), 4);
break;
case 'b':
ret += print_hex(putch, bufptr, va_arg(ap, int), 2);
break;
case 'c':
putch(bufptr, (char)va_arg(ap, int));
++ret;
break;
case '%':
putch(bufptr, '%');
++ret;
break;
case 'p':
putch(bufptr, '0');
putch(bufptr, 'x');
ret += 2;
ret += print_hex(putch, bufptr, va_arg(ap, dword), 8);
break;
}
}
else {
} else {
putch(bufptr, *p);
++ret;
}
}
return ret;
}