Skip to content

Commit

Permalink
Integrate ext2 from VFS into Kernel.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Oct 17, 2018
1 parent aec8ab0 commit 9171521
Show file tree
Hide file tree
Showing 45 changed files with 662 additions and 1,085 deletions.
3 changes: 1 addition & 2 deletions AK/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "Assertions.h"
#include "Retainable.h"
#include "RetainPtr.h"
#include <cstdlib>
#include <cstring>
#include "StdLib.h"
#include "kmalloc.h"

namespace AK {
Expand Down
4 changes: 2 additions & 2 deletions AK/DoublyLinkedList.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <utility>
#include "StdLib.h"

namespace AK {

Expand Down Expand Up @@ -38,7 +38,7 @@ class DoublyLinkedList {

void append(T&& value)
{
auto* node = new Node(std::move(value));
auto* node = new Node(move(value));
if (!m_head) {
ASSERT(!m_tail);
m_head = node;
Expand Down
111 changes: 111 additions & 0 deletions AK/Function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "OwnPtr.h"
#include "StdLib.h"

namespace AK {

template<typename> class Function;

template <typename Out, typename... In>
class Function<Out(In...)> {
public:
Function() = default;
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)
: m_callableWrapper(make<CallableWrapper<CallableType>>(move(callable)))
{
}

template<typename FunctionType, class = typename EnableIf<IsPointer<FunctionType>::value && IsFunction<typename RemovePointer<FunctionType>::Type>::value>::Type>
Function(FunctionType f)
: m_callableWrapper(make<CallableWrapper<FunctionType>>(move(f)))
{
}

Out operator()(In... in)
{
ASSERT(m_callableWrapper);
return m_callableWrapper->call(forward<In>(in)...);
}

explicit operator bool() const { return !!m_callableWrapper; }

template<typename CallableType, class = typename EnableIf<!(IsPointer<CallableType>::value && IsFunction<typename RemovePointer<CallableType>::Type>::value) && IsRvalueReference<CallableType&&>::value>::Type>
Function& operator=(CallableType&& callable)
{
m_callableWrapper = make<CallableWrapper<CallableType>>(move(callable));
return *this;
}

template<typename FunctionType, class = typename EnableIf<IsPointer<FunctionType>::value && IsFunction<typename RemovePointer<FunctionType>::Type>::value>::Type>
Function& operator=(FunctionType f)
{
m_callableWrapper = make<CallableWrapper<FunctionType>>(move(f));
return *this;
}

Function& operator=(std::nullptr_t)
{
m_callableWrapper = nullptr;
return *this;
}

private:
class CallableWrapperBase {
public:
virtual ~CallableWrapperBase() { }
virtual Out call(In...) = 0;
};

template<typename CallableType>
class CallableWrapper : public CallableWrapperBase {
public:
explicit CallableWrapper(CallableType&& callable)
: m_callable(move(callable))
{
}

CallableWrapper(const CallableWrapper&) = delete;
CallableWrapper& operator=(const CallableWrapper&) = delete;

Out call(In... in) final { return m_callable(forward<In>(in)...); }

private:
CallableType m_callable;
};

OwnPtr<CallableWrapperBase> m_callableWrapper;
};

}

using AK::Function;

13 changes: 7 additions & 6 deletions AK/HashMap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "HashTable.h"
#include <utility>
#include "StdLib.h"
#include "kstdio.h"

namespace AK {

Expand All @@ -22,9 +23,9 @@ class HashMap {
static unsigned hash(const Entry& entry) { return Traits<K>::hash(entry.key); }
static void dump(const Entry& entry)
{
printf("key=");
kprintf("key=");
Traits<K>::dump(entry.key);
printf(" value=");
kprintf(" value=");
Traits<V>::dump(entry.value);
}
};
Expand All @@ -33,14 +34,14 @@ class HashMap {
HashMap() { }

HashMap(HashMap&& other)
: m_table(std::move(other.m_table))
: m_table(move(other.m_table))
{
}

HashMap& operator=(HashMap&& other)
{
if (this != &other) {
m_table = std::move(other.m_table);
m_table = move(other.m_table);
}
return *this;
}
Expand Down Expand Up @@ -73,7 +74,7 @@ class HashMap {
template<typename K, typename V>
void HashMap<K, V>::set(const K& key, V&& value)
{
m_table.set(Entry{key, std::move(value)});
m_table.set(Entry{key, move(value)});
}

template<typename K, typename V>
Expand Down
44 changes: 22 additions & 22 deletions AK/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "Assertions.h"
#include "DoublyLinkedList.h"
#include "Traits.h"
#include <cstdlib>
#include <utility>
#include "StdLib.h"
#include "kstdio.h"

//#define HASHTABLE_DEBUG

Expand Down Expand Up @@ -68,7 +68,7 @@ class HashTable {
T& operator*()
{
#ifdef HASHTABLE_DEBUG
printf("retrieve { bucketIndex: %u, isEnd: %u }\n", m_bucketIndex, m_isEnd);
kprintf("retrieve { bucketIndex: %u, isEnd: %u }\n", m_bucketIndex, m_isEnd);
#endif
return *m_bucketIterator;
}
Expand All @@ -86,7 +86,7 @@ class HashTable {
while (!m_isEnd) {
#ifdef HASHTABLE_DEBUG
++pass;
printf("skipToNext pass %u, m_bucketIndex=%u\n", pass, m_bucketIndex);
kprintf("skipToNext pass %u, m_bucketIndex=%u\n", pass, m_bucketIndex);
#endif
if (m_bucketIterator.isEnd()) {
++m_bucketIndex;
Expand All @@ -112,7 +112,7 @@ class HashTable {
{
if (!isEnd && !m_table.isEmpty() && !(m_bucketIterator != DoublyLinkedList<T>::Iterator::universalEnd())) {
#ifdef HASHTABLE_DEBUG
printf("bucket iterator init!\n");
kprintf("bucket iterator init!\n");
#endif
m_bucketIterator = m_table.m_buckets[0].chain.begin();
if (m_bucketIterator.isEnd())
Expand Down Expand Up @@ -143,7 +143,7 @@ class HashTable {
const T& operator*() const
{
#ifdef HASHTABLE_DEBUG
printf("retrieve { bucketIndex: %u, isEnd: %u }\n", m_bucketIndex, m_isEnd);
kprintf("retrieve { bucketIndex: %u, isEnd: %u }\n", m_bucketIndex, m_isEnd);
#endif
return *m_bucketIterator;
}
Expand All @@ -161,7 +161,7 @@ class HashTable {
while (!m_isEnd) {
#ifdef HASHTABLE_DEBUG
++pass;
printf("skipToNext pass %u, m_bucketIndex=%u\n", pass, m_bucketIndex);
kprintf("skipToNext pass %u, m_bucketIndex=%u\n", pass, m_bucketIndex);
#endif
if (m_bucketIterator.isEnd()) {
++m_bucketIndex;
Expand All @@ -188,7 +188,7 @@ class HashTable {
{
if (!isEnd && !m_table.isEmpty() && !(m_bucketIterator != DoublyLinkedList<T>::ConstIterator::universalEnd())) {
#ifdef HASHTABLE_DEBUG
printf("const bucket iterator init!\n");
kprintf("const bucket iterator init!\n");
#endif
const DoublyLinkedList<T>& chain = m_table.m_buckets[0].chain;
m_bucketIterator = chain.begin();
Expand Down Expand Up @@ -242,9 +242,9 @@ void HashTable<T, TraitsForT>::set(T&& value)
}
if (size() >= capacity()) {
rehash(size() + 1);
insert(std::move(value));
insert(move(value));
} else {
bucket.chain.append(std::move(value));
bucket.chain.append(move(value));
}
m_size++;
}
Expand All @@ -254,7 +254,7 @@ void HashTable<T, TraitsForT>::rehash(unsigned newCapacity)
{
newCapacity *= 2;
#ifdef HASHTABLE_DEBUG
printf("rehash to %u buckets\n", newCapacity);
kprintf("rehash to %u buckets\n", newCapacity);
#endif
auto* newBuckets = new Bucket[newCapacity];
auto* oldBuckets = m_buckets;
Expand All @@ -263,11 +263,11 @@ void HashTable<T, TraitsForT>::rehash(unsigned newCapacity)
m_capacity = newCapacity;

#ifdef HASHTABLE_DEBUG
printf("reinsert %u buckets\n", oldCapacity);
kprintf("reinsert %u buckets\n", oldCapacity);
#endif
for (unsigned i = 0; i < oldCapacity; ++i) {
for (auto& value : oldBuckets[i].chain) {
insert(std::move(value));
insert(move(value));
}
}

Expand All @@ -286,7 +286,7 @@ template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::insert(T&& value)
{
auto& bucket = lookup(value);
bucket.chain.append(std::move(value));
bucket.chain.append(move(value));
}

template<typename T, typename TraitsForT>
Expand Down Expand Up @@ -341,9 +341,9 @@ typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::lookup(cons
{
unsigned hash = TraitsForT::hash(value);
#ifdef HASHTABLE_DEBUG
printf("hash for ");
kprintf("hash for ");
TraitsForT::dump(value);
printf(" is %u\n", hash);
kprintf(" is %u\n", hash);
#endif
if (bucketIndex)
*bucketIndex = hash % m_capacity;
Expand All @@ -355,9 +355,9 @@ const typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::looku
{
unsigned hash = TraitsForT::hash(value);
#ifdef HASHTABLE_DEBUG
printf("hash for ");
kprintf("hash for ");
TraitsForT::dump(value);
printf(" is %u\n", hash);
kprintf(" is %u\n", hash);
#endif
if (bucketIndex)
*bucketIndex = hash % m_capacity;
Expand All @@ -367,14 +367,14 @@ const typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::looku
template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::dump() const
{
printf("HashTable{%p} m_size=%u, m_capacity=%u, m_buckets=%p\n", this, m_size, m_capacity, m_buckets);
kprintf("HashTable{%p} m_size=%u, m_capacity=%u, m_buckets=%p\n", this, m_size, m_capacity, m_buckets);
for (unsigned i = 0; i < m_capacity; ++i) {
auto& bucket = m_buckets[i];
printf("Bucket %u\n", i);
kprintf("Bucket %u\n", i);
for (auto& e : bucket.chain) {
printf(" > ");
kprintf(" > ");
TraitsForT::dump(e);
printf("\n");
kprintf("\n");
}
}
}
Expand Down
Loading

0 comments on commit 9171521

Please sign in to comment.