Skip to content

Commit 5a30055

Browse files
committed
Import all this stuff into a single repo called Serenity.
0 parents  commit 5a30055

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+8836
-0
lines changed

AK/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
akit-test

AK/Assertions.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <assert.h>
4+
5+
#define ASSERT(x) assert(x)
6+
#define ASSERT_NOT_REACHED() assert(false)
7+
8+
namespace AK {
9+
10+
inline void notImplemented() { assert(false); }
11+
12+
}
13+
14+
using AK::notImplemented;
15+

AK/Bitmap.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include "StdLib.h"
4+
#include "Types.h"
5+
#include "kmalloc.h"
6+
7+
namespace AK {
8+
9+
class Bitmap {
10+
public:
11+
// NOTE: A wrapping Bitmap won't try to free the wrapped data.
12+
static Bitmap wrap(byte* data, unsigned size)
13+
{
14+
return Bitmap(data, size);
15+
}
16+
17+
static Bitmap create(unsigned size)
18+
{
19+
return Bitmap(size);
20+
}
21+
22+
~Bitmap()
23+
{
24+
if (m_owned)
25+
kfree(m_data);
26+
m_data = nullptr;
27+
}
28+
29+
unsigned size() const { return m_size; }
30+
bool get(unsigned index) const
31+
{
32+
ASSERT(index < m_size);
33+
return 0 != (m_data[index / 8] & (1u << (index % 8)));
34+
}
35+
void set(unsigned index, bool value) const
36+
{
37+
ASSERT(index < m_size);
38+
if (value)
39+
m_data[index / 8] |= static_cast<byte>((1u << (index % 8)));
40+
else
41+
m_data[index / 8] &= static_cast<byte>(~(1u << (index % 8)));
42+
}
43+
44+
byte* data() { return m_data; }
45+
const byte* data() const { return m_data; }
46+
47+
private:
48+
explicit Bitmap(unsigned size)
49+
: m_size(size)
50+
, m_owned(true)
51+
{
52+
ASSERT(m_size != 0);
53+
m_data = reinterpret_cast<byte*>(kmalloc(ceilDiv(size, 8u)));
54+
}
55+
56+
Bitmap(byte* data, unsigned size)
57+
: m_data(data)
58+
, m_size(size)
59+
, m_owned(false)
60+
{
61+
}
62+
63+
byte* m_data { nullptr };
64+
unsigned m_size { 0 };
65+
bool m_owned { false };
66+
};
67+
68+
}
69+
70+
using AK::Bitmap;
71+

AK/Buffer.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#pragma once
2+
3+
#include "Assertions.h"
4+
#include "Retainable.h"
5+
#include "RetainPtr.h"
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include "kmalloc.h"
9+
10+
namespace AK {
11+
12+
template<typename T>
13+
class Buffer : public Retainable<Buffer<T>> {
14+
public:
15+
static RetainPtr<Buffer> createUninitialized(size_t count);
16+
static RetainPtr<Buffer> copy(const T*, size_t count);
17+
static RetainPtr<Buffer> wrap(T*, size_t count);
18+
static RetainPtr<Buffer> adopt(T*, size_t count);
19+
20+
~Buffer() { clear(); }
21+
22+
void clear()
23+
{
24+
if (!m_elements)
25+
return;
26+
kfree(m_elements);
27+
m_elements = nullptr;
28+
}
29+
30+
T& operator[](size_t i) { ASSERT(i < m_size); return m_elements[i]; }
31+
const T& operator[](size_t i) const { ASSERT(i < m_size); return m_elements[i]; }
32+
bool isEmpty() const { return !m_size; }
33+
size_t size() const { return m_size; }
34+
35+
T* pointer() { return m_elements; }
36+
const T* pointer() const { return m_elements; }
37+
38+
T* offsetPointer(size_t offset) { return m_elements + offset; }
39+
const T* offsetPointer(size_t offset) const { return m_elements + offset; }
40+
41+
const void* endPointer() const { return m_elements + m_size; }
42+
43+
// NOTE: trim() does not reallocate.
44+
void trim(size_t size)
45+
{
46+
ASSERT(size <= m_size);
47+
m_size = size;
48+
}
49+
50+
private:
51+
enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt };
52+
explicit Buffer(size_t); // For ConstructionMode=Uninitialized
53+
Buffer(const T*, size_t, ConstructionMode); // For ConstructionMode=Copy
54+
Buffer(T*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt
55+
Buffer() { }
56+
57+
T* m_elements { nullptr };
58+
size_t m_size { 0 };
59+
bool m_owned { false };
60+
};
61+
62+
template<typename T>
63+
inline Buffer<T>::Buffer(size_t size)
64+
: m_size(size)
65+
{
66+
m_elements = static_cast<T*>(kmalloc(size * sizeof(T)));
67+
m_owned = true;
68+
}
69+
70+
template<typename T>
71+
inline Buffer<T>::Buffer(const T* elements, size_t size, ConstructionMode mode)
72+
: m_size(size)
73+
{
74+
ASSERT(mode == Copy);
75+
m_elements = static_cast<T*>(kmalloc(size * sizeof(T)));
76+
memcpy(m_elements, elements, size * sizeof(T));
77+
m_owned = true;
78+
}
79+
80+
template<typename T>
81+
inline Buffer<T>::Buffer(T* elements, size_t size, ConstructionMode mode)
82+
: m_elements(elements)
83+
, m_size(size)
84+
{
85+
if (mode == Adopt) {
86+
m_owned = true;
87+
} else if (mode == Wrap) {
88+
m_owned = false;
89+
}
90+
91+
}
92+
93+
template<typename T>
94+
inline RetainPtr<Buffer<T>> Buffer<T>::createUninitialized(size_t size)
95+
{
96+
return ::adopt(*new Buffer<T>(size));
97+
}
98+
99+
template<typename T>
100+
inline RetainPtr<Buffer<T>> Buffer<T>::copy(const T* elements, size_t size)
101+
{
102+
return ::adopt(*new Buffer<T>(elements, size, Copy));
103+
}
104+
105+
template<typename T>
106+
inline RetainPtr<Buffer<T>> Buffer<T>::wrap(T* elements, size_t size)
107+
{
108+
return ::adopt(*new Buffer<T>(elements, size, Wrap));
109+
}
110+
111+
template<typename T>
112+
inline RetainPtr<Buffer<T>> Buffer<T>::adopt(T* elements, size_t size)
113+
{
114+
return ::adopt(*new Buffer<T>(elements, size, Adopt));
115+
}
116+
117+
}
118+
119+
using AK::Buffer;
120+

AK/ByteBuffer.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#pragma once
2+
3+
#include "Buffer.h"
4+
#include "Types.h"
5+
#include <cstdlib>
6+
#include <cstring>
7+
#include <cstdio>
8+
9+
namespace AK {
10+
11+
class ByteBuffer {
12+
public:
13+
ByteBuffer() { }
14+
ByteBuffer(std::nullptr_t) { }
15+
ByteBuffer(const ByteBuffer& other)
16+
: m_impl(other.m_impl.copyRef())
17+
{
18+
}
19+
ByteBuffer(ByteBuffer&& other)
20+
: m_impl(std::move(other.m_impl))
21+
{
22+
}
23+
ByteBuffer& operator=(ByteBuffer&& other)
24+
{
25+
if (this != &other)
26+
m_impl = std::move(other.m_impl);
27+
return *this;
28+
}
29+
30+
static ByteBuffer createUninitialized(size_t size) { return ByteBuffer(Buffer<byte>::createUninitialized(size)); }
31+
static ByteBuffer copy(const byte* data, size_t size) { return ByteBuffer(Buffer<byte>::copy(data, size)); }
32+
static ByteBuffer wrap(byte* data, size_t size) { return ByteBuffer(Buffer<byte>::wrap(data, size)); }
33+
static ByteBuffer adopt(byte* data, size_t size) { return ByteBuffer(Buffer<byte>::adopt(data, size)); }
34+
35+
~ByteBuffer() { clear(); }
36+
void clear() { m_impl = nullptr; }
37+
38+
operator bool() const { return !isNull(); }
39+
bool operator!() const { return isNull(); }
40+
bool isNull() const { return m_impl == nullptr; }
41+
42+
byte& operator[](size_t i) { ASSERT(m_impl); return (*m_impl)[i]; }
43+
byte operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
44+
bool isEmpty() const { return !m_impl || m_impl->isEmpty(); }
45+
size_t size() const { return m_impl ? m_impl->size() : 0; }
46+
47+
byte* pointer() { return m_impl ? m_impl->pointer() : nullptr; }
48+
const byte* pointer() const { return m_impl ? m_impl->pointer() : nullptr; }
49+
50+
byte* offsetPointer(size_t offset) { return m_impl ? m_impl->offsetPointer(offset) : nullptr; }
51+
const byte* offsetPointer(size_t offset) const { return m_impl ? m_impl->offsetPointer(offset) : nullptr; }
52+
53+
const void* endPointer() const { return m_impl ? m_impl->endPointer() : nullptr; }
54+
55+
// NOTE: trim() does not reallocate.
56+
void trim(size_t size)
57+
{
58+
if (m_impl)
59+
m_impl->trim(size);
60+
}
61+
62+
ByteBuffer slice(size_t offset, size_t size) const
63+
{
64+
if (isNull())
65+
return { };
66+
if (offset >= this->size())
67+
return { };
68+
if (offset + size >= this->size())
69+
size = this->size() - offset;
70+
return copy(offsetPointer(offset), size);
71+
}
72+
73+
private:
74+
explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl)
75+
: m_impl(std::move(impl))
76+
{
77+
}
78+
79+
RetainPtr<Buffer<byte>> m_impl;
80+
};
81+
82+
}
83+
84+
using AK::ByteBuffer;
85+

AK/HashMap.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
3+
#include "HashTable.h"
4+
#include <utility>
5+
6+
namespace AK {
7+
8+
template<typename K, typename V>
9+
class HashMap {
10+
private:
11+
struct Entry {
12+
K key;
13+
V value;
14+
15+
bool operator==(const Entry& other)
16+
{
17+
return key == other.key;
18+
}
19+
};
20+
21+
struct EntryTraits {
22+
static unsigned hash(const Entry& entry) { return Traits<K>::hash(entry.key); }
23+
static void dump(const Entry& entry)
24+
{
25+
printf("key=");
26+
Traits<K>::dump(entry.key);
27+
printf(" value=");
28+
Traits<V>::dump(entry.value);
29+
}
30+
};
31+
32+
public:
33+
HashMap() { }
34+
35+
HashMap(HashMap&& other)
36+
: m_table(std::move(other.m_table))
37+
{
38+
}
39+
40+
HashMap& operator=(HashMap&& other)
41+
{
42+
if (this != &other) {
43+
m_table = std::move(other.m_table);
44+
}
45+
return *this;
46+
}
47+
48+
bool isEmpty() const { return m_table.isEmpty(); }
49+
unsigned size() const { return m_table.size(); }
50+
unsigned capacity() const { return m_table.capacity(); }
51+
52+
void set(const K&, V&&);
53+
54+
typedef HashTable<Entry, EntryTraits> HashTableType;
55+
typedef typename HashTableType::Iterator IteratorType;
56+
typedef typename HashTableType::ConstIterator ConstIteratorType;
57+
58+
IteratorType begin() { return m_table.begin(); }
59+
IteratorType end() { return m_table.end(); }
60+
IteratorType find(const K&);
61+
62+
ConstIteratorType begin() const { return m_table.begin(); }
63+
ConstIteratorType end() const { return m_table.end(); }
64+
ConstIteratorType find(const K&) const;
65+
66+
void dump() const { m_table.dump(); }
67+
68+
private:
69+
HashTable<Entry, EntryTraits> m_table;
70+
};
71+
72+
template<typename K, typename V>
73+
void HashMap<K, V>::set(const K& key, V&& value)
74+
{
75+
m_table.set(Entry{key, std::move(value)});
76+
}
77+
78+
template<typename K, typename V>
79+
auto HashMap<K, V>::find(const K& key) -> IteratorType
80+
{
81+
Entry dummy { key, V() };
82+
return m_table.find(dummy);
83+
}
84+
85+
template<typename K, typename V>
86+
auto HashMap<K, V>::find(const K& key) const -> ConstIteratorType
87+
{
88+
Entry dummy { key, V() };
89+
return m_table.find(dummy);
90+
}
91+
92+
}
93+
94+
using AK::HashMap;
95+

0 commit comments

Comments
 (0)