Start fixing up AK to work inside the kernel.

This commit is contained in:
Andreas Kling 2018-10-16 13:59:28 +02:00
parent 1203c327c7
commit 5d465582a3
Notes: sideshowbarker 2024-07-19 18:47:27 +09:00
10 changed files with 68 additions and 47 deletions

View file

@ -1,13 +1,16 @@
#pragma once
#ifdef SERENITY_KERNEL
#include "kassert.h"
#else
#include <assert.h>
#define ASSERT(x) assert(x)
#define ASSERT_NOT_REACHED() assert(false)
#endif
namespace AK {
inline void notImplemented() { assert(false); }
inline void notImplemented() { ASSERT(false); }
}

View file

@ -22,7 +22,7 @@ MappedFile::MappedFile(String&& fileName)
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()

View file

@ -15,7 +15,7 @@ public:
void release()
{
assert(m_retainCount);
ASSERT(m_retainCount);
if (!--m_retainCount)
delete static_cast<const T*>(this);
}

View file

@ -222,6 +222,8 @@ byte* allocateZeroed(dword size)
byte* reallocate(byte* ptr, dword size)
{
// FIXME;
(void) ptr;
(void) size;
ASSERT_NOT_REACHED();
return nullptr;
}

View file

@ -1,5 +1,12 @@
#pragma once
#ifdef SERENITY_KERNEL
#include <Kernel/StdLib.h>
#else
#include <cstring>
#include <utility>
#endif
namespace AK {
template<typename T>
@ -34,10 +41,10 @@ template<typename T>
struct identity {
typedef T type;
};
template<typename T>
T&& forward(typename identity<T>::type&& param)
template<class T>
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>

View file

@ -1,10 +1,18 @@
#pragma once
#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
#include <stdint.h>
#include <sys/types.h>
#endif
typedef uint8_t byte;
typedef uint16_t word;
@ -15,6 +23,7 @@ typedef int8_t signed_byte;
typedef int16_t signed_word;
typedef int32_t signed_dword;
typedef int64_t signed_qword;
#endif
constexpr unsigned KB = 1024;
constexpr unsigned MB = KB * KB;

View file

@ -13,24 +13,24 @@ template<typename T>
class VectorImpl {
public:
~VectorImpl() { }
static OwnPtr<VectorImpl> create(unsigned capacity)
static OwnPtr<VectorImpl> create(size_t capacity)
{
size_t size = sizeof(VectorImpl) + sizeof(T) * capacity;
void* slot = kmalloc(size);
return OwnPtr<VectorImpl>(new (slot) VectorImpl(capacity));
}
unsigned size() const { return m_size; }
unsigned capacity() const { return m_capacity; }
size_t size() const { return m_size; }
size_t capacity() const { return m_capacity; }
T& at(unsigned i) { return *slot(i); }
const T& at(unsigned i) const { return *slot(i); }
T& at(size_t i) { 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);
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)));
at(i).~T();
}
@ -41,16 +41,16 @@ public:
private:
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* 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* slot(unsigned i) const { return &tail()[i]; }
const T* slot(size_t i) const { return &tail()[i]; }
unsigned m_size { 0 };
unsigned m_capacity;
size_t m_size { 0 };
size_t m_capacity;
};
template<typename T>
@ -73,21 +73,21 @@ public:
void clear()
{
for (unsigned i = 0; i < size(); ++i) {
for (size_t i = 0; i < size(); ++i) {
at(i).~T();
}
m_impl = nullptr;
}
bool isEmpty() const { return size() == 0; }
unsigned size() const { return m_impl ? m_impl->size() : 0; }
unsigned capacity() const { return m_impl ? m_impl->capacity() : 0; }
size_t size() const { return m_impl ? m_impl->size() : 0; }
size_t capacity() const { return m_impl ? m_impl->capacity() : 0; }
const T& at(unsigned i) const { return m_impl->at(i); }
T& at(unsigned i) { return m_impl->at(i); }
const T& at(size_t i) const { return m_impl->at(i); }
T& at(size_t i) { return m_impl->at(i); }
const T& operator[](unsigned i) const { return at(i); }
T& operator[](unsigned i) { return at(i); }
const T& operator[](size_t i) const { return at(i); }
T& operator[](size_t i) { return at(i); }
const T& first() const { return at(0); }
T& first() { return at(0); }
@ -104,7 +104,7 @@ public:
return value;
}
void remove(unsigned index)
void remove(size_t index)
{
m_impl->remove(index);
}
@ -123,7 +123,7 @@ public:
++m_impl->m_size;
}
void ensureCapacity(unsigned neededCapacity)
void ensureCapacity(size_t neededCapacity)
{
if (capacity() >= neededCapacity)
return;
@ -131,7 +131,7 @@ public:
auto newImpl = VectorImpl<T>::create(newCapacity);
if (m_impl) {
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)));
m_impl->at(i).~T();
}
@ -146,9 +146,9 @@ public:
T& operator*() { return m_vector[m_index]; }
private:
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;
unsigned m_index { 0 };
size_t m_index { 0 };
};
Iterator begin() { return Iterator(*this, 0); }
@ -161,18 +161,18 @@ public:
const T& operator*() const { return m_vector[m_index]; }
private:
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;
unsigned m_index { 0 };
size_t m_index { 0 };
};
ConstIterator begin() const { return ConstIterator(*this, 0); }
ConstIterator end() const { return ConstIterator(*this, size()); }
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;

View file

@ -9,12 +9,12 @@
extern "C" {
void* kcalloc(dword nmemb, dword size)
void* kcalloc(size_t nmemb, size_t size)
{
return calloc(nmemb, size);
}
void* kmalloc(dword size)
void* kmalloc(size_t size)
{
return malloc(size);
}
@ -24,7 +24,7 @@ void kfree(void* ptr)
free(ptr);
}
void* krealloc(void* ptr, dword size)
void* krealloc(void* ptr, size_t size)
{
return realloc(ptr, size);
}
@ -35,14 +35,14 @@ void* krealloc(void* ptr, dword size)
extern "C" {
void* kcalloc(dword nmemb, dword size)
void* kcalloc(size_t nmemb, size_t size)
{
if (!nmemb || !size)
return nullptr;
return SimpleMalloc::allocateZeroed(nmemb * size);
}
void* kmalloc(dword size)
void* kmalloc(size_t size)
{
if (!size)
return nullptr;
@ -56,7 +56,7 @@ void kfree(void* ptr)
SimpleMalloc::free((byte*)ptr);
}
void* krealloc(void* ptr, dword size)
void* krealloc(void* ptr, size_t size)
{
if (!ptr)
return ptr;

View file

@ -4,10 +4,10 @@
extern "C" {
void* kcalloc(dword nmemb, dword size);
void* kmalloc(dword size);
void* kcalloc(size_t nmemb, size_t size);
void* kmalloc(size_t size);
void kfree(void* ptr);
void* krealloc(void* ptr, dword size);
void* krealloc(void* ptr, size_t size);
}

View file

@ -136,7 +136,7 @@ int main(int, char**)
}
auto charbuf = Buffer<char>::createUninitialized(1024);
printf("charbuf.size() = %u\n", charbuf->size());
printf("charbuf.size() = %zu\n", charbuf->size());
{
Vector<String> problem;
@ -146,7 +146,7 @@ int main(int, char**)
{
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)
printf("%d ", i);
printf("\n}\n");