mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:47:45 +00:00
Start fixing up AK to work inside the kernel.
This commit is contained in:
parent
1203c327c7
commit
5d465582a3
10 changed files with 68 additions and 47 deletions
|
@ -1,13 +1,16 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef SERENITY_KERNEL
|
||||||
|
#include "kassert.h"
|
||||||
|
#else
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#define ASSERT(x) assert(x)
|
#define ASSERT(x) assert(x)
|
||||||
#define ASSERT_NOT_REACHED() assert(false)
|
#define ASSERT_NOT_REACHED() assert(false)
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
inline void notImplemented() { assert(false); }
|
inline void notImplemented() { ASSERT(false); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ MappedFile::MappedFile(String&& fileName)
|
||||||
perror("");
|
perror("");
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("MappedFile{%s} := { m_fd=%d, m_fileLength=%u, m_map=%p }\n", m_fileName.characters(), m_fd, m_fileLength, m_map);
|
printf("MappedFile{%s} := { m_fd=%d, m_fileLength=%zu, m_map=%p }\n", m_fileName.characters(), m_fd, m_fileLength, m_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedFile::~MappedFile()
|
MappedFile::~MappedFile()
|
||||||
|
|
|
@ -15,7 +15,7 @@ public:
|
||||||
|
|
||||||
void release()
|
void release()
|
||||||
{
|
{
|
||||||
assert(m_retainCount);
|
ASSERT(m_retainCount);
|
||||||
if (!--m_retainCount)
|
if (!--m_retainCount)
|
||||||
delete static_cast<const T*>(this);
|
delete static_cast<const T*>(this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,6 +222,8 @@ byte* allocateZeroed(dword size)
|
||||||
byte* reallocate(byte* ptr, dword size)
|
byte* reallocate(byte* ptr, dword size)
|
||||||
{
|
{
|
||||||
// FIXME;
|
// FIXME;
|
||||||
|
(void) ptr;
|
||||||
|
(void) size;
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
13
AK/StdLib.h
13
AK/StdLib.h
|
@ -1,5 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef SERENITY_KERNEL
|
||||||
|
#include <Kernel/StdLib.h>
|
||||||
|
#else
|
||||||
|
#include <cstring>
|
||||||
|
#include <utility>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -34,10 +41,10 @@ template<typename T>
|
||||||
struct identity {
|
struct identity {
|
||||||
typedef T type;
|
typedef T type;
|
||||||
};
|
};
|
||||||
template<typename T>
|
template<class T>
|
||||||
T&& forward(typename identity<T>::type&& param)
|
constexpr T&& forward(typename identity<T>::type& param)
|
||||||
{
|
{
|
||||||
return static_cast<typename identity<T>::type&&>(param);
|
return static_cast<T&&>(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename U>
|
template<typename T, typename U>
|
||||||
|
|
11
AK/Types.h
11
AK/Types.h
|
@ -1,10 +1,18 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef SERENITY_KERNEL
|
#ifdef SERENITY_KERNEL
|
||||||
|
typedef unsigned char byte;
|
||||||
|
typedef unsigned short word;
|
||||||
|
typedef unsigned int dword;
|
||||||
|
typedef unsigned long long int qword;
|
||||||
|
|
||||||
|
typedef signed char signed_byte;
|
||||||
|
typedef signed short signed_word;
|
||||||
|
typedef signed int signed_dword;
|
||||||
|
typedef signed long long int signed_qword;
|
||||||
#else
|
#else
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef uint8_t byte;
|
typedef uint8_t byte;
|
||||||
typedef uint16_t word;
|
typedef uint16_t word;
|
||||||
|
@ -15,6 +23,7 @@ typedef int8_t signed_byte;
|
||||||
typedef int16_t signed_word;
|
typedef int16_t signed_word;
|
||||||
typedef int32_t signed_dword;
|
typedef int32_t signed_dword;
|
||||||
typedef int64_t signed_qword;
|
typedef int64_t signed_qword;
|
||||||
|
#endif
|
||||||
|
|
||||||
constexpr unsigned KB = 1024;
|
constexpr unsigned KB = 1024;
|
||||||
constexpr unsigned MB = KB * KB;
|
constexpr unsigned MB = KB * KB;
|
||||||
|
|
56
AK/Vector.h
56
AK/Vector.h
|
@ -13,24 +13,24 @@ template<typename T>
|
||||||
class VectorImpl {
|
class VectorImpl {
|
||||||
public:
|
public:
|
||||||
~VectorImpl() { }
|
~VectorImpl() { }
|
||||||
static OwnPtr<VectorImpl> create(unsigned capacity)
|
static OwnPtr<VectorImpl> create(size_t capacity)
|
||||||
{
|
{
|
||||||
size_t size = sizeof(VectorImpl) + sizeof(T) * capacity;
|
size_t size = sizeof(VectorImpl) + sizeof(T) * capacity;
|
||||||
void* slot = kmalloc(size);
|
void* slot = kmalloc(size);
|
||||||
return OwnPtr<VectorImpl>(new (slot) VectorImpl(capacity));
|
return OwnPtr<VectorImpl>(new (slot) VectorImpl(capacity));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned size() const { return m_size; }
|
size_t size() const { return m_size; }
|
||||||
unsigned capacity() const { return m_capacity; }
|
size_t capacity() const { return m_capacity; }
|
||||||
|
|
||||||
T& at(unsigned i) { return *slot(i); }
|
T& at(size_t i) { return *slot(i); }
|
||||||
const T& at(unsigned i) const { return *slot(i); }
|
const T& at(size_t i) const { return *slot(i); }
|
||||||
|
|
||||||
void remove(unsigned index)
|
void remove(size_t index)
|
||||||
{
|
{
|
||||||
ASSERT(index < m_size);
|
ASSERT(index < m_size);
|
||||||
at(index).~T();
|
at(index).~T();
|
||||||
for (unsigned i = index + 1; i < m_size; ++i) {
|
for (size_t i = index + 1; i < m_size; ++i) {
|
||||||
new (slot(i - 1)) T(move(at(i)));
|
new (slot(i - 1)) T(move(at(i)));
|
||||||
at(i).~T();
|
at(i).~T();
|
||||||
}
|
}
|
||||||
|
@ -41,16 +41,16 @@ public:
|
||||||
private:
|
private:
|
||||||
friend class Vector<T>;
|
friend class Vector<T>;
|
||||||
|
|
||||||
VectorImpl(unsigned capacity) : m_capacity(capacity) { }
|
VectorImpl(size_t capacity) : m_capacity(capacity) { }
|
||||||
|
|
||||||
T* tail() { return reinterpret_cast<T*>(this + 1); }
|
T* tail() { return reinterpret_cast<T*>(this + 1); }
|
||||||
T* slot(unsigned i) { return &tail()[i]; }
|
T* slot(size_t i) { return &tail()[i]; }
|
||||||
|
|
||||||
const T* tail() const { return reinterpret_cast<const T*>(this + 1); }
|
const T* tail() const { return reinterpret_cast<const T*>(this + 1); }
|
||||||
const T* slot(unsigned i) const { return &tail()[i]; }
|
const T* slot(size_t i) const { return &tail()[i]; }
|
||||||
|
|
||||||
unsigned m_size { 0 };
|
size_t m_size { 0 };
|
||||||
unsigned m_capacity;
|
size_t m_capacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -73,21 +73,21 @@ public:
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < size(); ++i) {
|
for (size_t i = 0; i < size(); ++i) {
|
||||||
at(i).~T();
|
at(i).~T();
|
||||||
}
|
}
|
||||||
m_impl = nullptr;
|
m_impl = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isEmpty() const { return size() == 0; }
|
bool isEmpty() const { return size() == 0; }
|
||||||
unsigned size() const { return m_impl ? m_impl->size() : 0; }
|
size_t size() const { return m_impl ? m_impl->size() : 0; }
|
||||||
unsigned capacity() const { return m_impl ? m_impl->capacity() : 0; }
|
size_t capacity() const { return m_impl ? m_impl->capacity() : 0; }
|
||||||
|
|
||||||
const T& at(unsigned i) const { return m_impl->at(i); }
|
const T& at(size_t i) const { return m_impl->at(i); }
|
||||||
T& at(unsigned i) { return m_impl->at(i); }
|
T& at(size_t i) { return m_impl->at(i); }
|
||||||
|
|
||||||
const T& operator[](unsigned i) const { return at(i); }
|
const T& operator[](size_t i) const { return at(i); }
|
||||||
T& operator[](unsigned i) { return at(i); }
|
T& operator[](size_t i) { return at(i); }
|
||||||
|
|
||||||
const T& first() const { return at(0); }
|
const T& first() const { return at(0); }
|
||||||
T& first() { return at(0); }
|
T& first() { return at(0); }
|
||||||
|
@ -104,7 +104,7 @@ public:
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove(unsigned index)
|
void remove(size_t index)
|
||||||
{
|
{
|
||||||
m_impl->remove(index);
|
m_impl->remove(index);
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ public:
|
||||||
++m_impl->m_size;
|
++m_impl->m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ensureCapacity(unsigned neededCapacity)
|
void ensureCapacity(size_t neededCapacity)
|
||||||
{
|
{
|
||||||
if (capacity() >= neededCapacity)
|
if (capacity() >= neededCapacity)
|
||||||
return;
|
return;
|
||||||
|
@ -131,7 +131,7 @@ public:
|
||||||
auto newImpl = VectorImpl<T>::create(newCapacity);
|
auto newImpl = VectorImpl<T>::create(newCapacity);
|
||||||
if (m_impl) {
|
if (m_impl) {
|
||||||
newImpl->m_size = m_impl->m_size;
|
newImpl->m_size = m_impl->m_size;
|
||||||
for (unsigned i = 0; i < size(); ++i) {
|
for (size_t i = 0; i < size(); ++i) {
|
||||||
new (newImpl->slot(i)) T(move(m_impl->at(i)));
|
new (newImpl->slot(i)) T(move(m_impl->at(i)));
|
||||||
m_impl->at(i).~T();
|
m_impl->at(i).~T();
|
||||||
}
|
}
|
||||||
|
@ -146,9 +146,9 @@ public:
|
||||||
T& operator*() { return m_vector[m_index]; }
|
T& operator*() { return m_vector[m_index]; }
|
||||||
private:
|
private:
|
||||||
friend class Vector;
|
friend class Vector;
|
||||||
Iterator(Vector& vector, unsigned index) : m_vector(vector), m_index(index) { }
|
Iterator(Vector& vector, size_t index) : m_vector(vector), m_index(index) { }
|
||||||
Vector& m_vector;
|
Vector& m_vector;
|
||||||
unsigned m_index { 0 };
|
size_t m_index { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
Iterator begin() { return Iterator(*this, 0); }
|
Iterator begin() { return Iterator(*this, 0); }
|
||||||
|
@ -161,18 +161,18 @@ public:
|
||||||
const T& operator*() const { return m_vector[m_index]; }
|
const T& operator*() const { return m_vector[m_index]; }
|
||||||
private:
|
private:
|
||||||
friend class Vector;
|
friend class Vector;
|
||||||
ConstIterator(const Vector& vector, const unsigned index) : m_vector(vector), m_index(index) { }
|
ConstIterator(const Vector& vector, const size_t index) : m_vector(vector), m_index(index) { }
|
||||||
const Vector& m_vector;
|
const Vector& m_vector;
|
||||||
unsigned m_index { 0 };
|
size_t m_index { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
||||||
ConstIterator end() const { return ConstIterator(*this, size()); }
|
ConstIterator end() const { return ConstIterator(*this, size()); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static unsigned paddedCapacity(unsigned capacity)
|
static size_t paddedCapacity(size_t capacity)
|
||||||
{
|
{
|
||||||
return max(4u, capacity + (capacity / 4) + 4);
|
return max(size_t(4), capacity + (capacity / 4) + 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<VectorImpl<T>> m_impl;
|
OwnPtr<VectorImpl<T>> m_impl;
|
||||||
|
|
|
@ -9,12 +9,12 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
void* kcalloc(dword nmemb, dword size)
|
void* kcalloc(size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
return calloc(nmemb, size);
|
return calloc(nmemb, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* kmalloc(dword size)
|
void* kmalloc(size_t size)
|
||||||
{
|
{
|
||||||
return malloc(size);
|
return malloc(size);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ void kfree(void* ptr)
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* krealloc(void* ptr, dword size)
|
void* krealloc(void* ptr, size_t size)
|
||||||
{
|
{
|
||||||
return realloc(ptr, size);
|
return realloc(ptr, size);
|
||||||
}
|
}
|
||||||
|
@ -35,14 +35,14 @@ void* krealloc(void* ptr, dword size)
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
void* kcalloc(dword nmemb, dword size)
|
void* kcalloc(size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
if (!nmemb || !size)
|
if (!nmemb || !size)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return SimpleMalloc::allocateZeroed(nmemb * size);
|
return SimpleMalloc::allocateZeroed(nmemb * size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* kmalloc(dword size)
|
void* kmalloc(size_t size)
|
||||||
{
|
{
|
||||||
if (!size)
|
if (!size)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -56,7 +56,7 @@ void kfree(void* ptr)
|
||||||
SimpleMalloc::free((byte*)ptr);
|
SimpleMalloc::free((byte*)ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* krealloc(void* ptr, dword size)
|
void* krealloc(void* ptr, size_t size)
|
||||||
{
|
{
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|
|
@ -4,10 +4,10 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
void* kcalloc(dword nmemb, dword size);
|
void* kcalloc(size_t nmemb, size_t size);
|
||||||
void* kmalloc(dword size);
|
void* kmalloc(size_t size);
|
||||||
void kfree(void* ptr);
|
void kfree(void* ptr);
|
||||||
void* krealloc(void* ptr, dword size);
|
void* krealloc(void* ptr, size_t size);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -136,7 +136,7 @@ int main(int, char**)
|
||||||
}
|
}
|
||||||
|
|
||||||
auto charbuf = Buffer<char>::createUninitialized(1024);
|
auto charbuf = Buffer<char>::createUninitialized(1024);
|
||||||
printf("charbuf.size() = %u\n", charbuf->size());
|
printf("charbuf.size() = %zu\n", charbuf->size());
|
||||||
|
|
||||||
{
|
{
|
||||||
Vector<String> problem;
|
Vector<String> problem;
|
||||||
|
@ -146,7 +146,7 @@ int main(int, char**)
|
||||||
|
|
||||||
{
|
{
|
||||||
auto printInts = [] (const Vector<int>& v) {
|
auto printInts = [] (const Vector<int>& v) {
|
||||||
printf("Vector {\n size: %u\n capacity: %u\n elements: ", v.size(), v.capacity());
|
printf("Vector {\n size: %zu\n capacity: %zu\n elements: ", v.size(), v.capacity());
|
||||||
for (auto i : v)
|
for (auto i : v)
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
printf("\n}\n");
|
printf("\n}\n");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue