Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/Kyoto/Alloc/CCircularBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _CCIRCULARBUFFER

#include <types.h>
#include <rstl/auto_ptr.hpp>

class CCircularBuffer {
public:
Expand All @@ -12,10 +13,12 @@ class CCircularBuffer {
void* Alloc(int len);
void Free(void* ptr, int len);
int GetAllocatedAmount() const;
void* GetOffsettedMemory(int offset) {
return x0_ptr.get() + offset;
}

private:
uchar x0_owned;
void* x4_ptr;
rstl::auto_ptr<char> x0_ptr;
int x8_bufferLen;
int xc_;
int x10_nextFreeAddr;
Expand Down
5 changes: 5 additions & 0 deletions include/Kyoto/Alloc/CGameAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CGameAllocator : public IAllocator {
uint GetNextMaskedFlags();
void SetTopOfHeapAllocated(bool topOfHeap);
size_t GetLength() const { return x4_len; }
void SetLength(size_t len) { x4_len = len; }
SGameMemInfo* GetNextFree() const { return (SGameMemInfo*)((size_t)x18_nextFree & ~31); }
void SetNextFree(SGameMemInfo* info) {
void* ptr = x18_nextFree;
Expand All @@ -49,6 +50,10 @@ class CGameAllocator : public IAllocator {
}

bool IsAllocated() const { return ((size_t)x10_prev) & 1; }
void SetNotAllocated() {
void* ptr = x10_prev;
x10_prev = (SGameMemInfo*)((size_t)ptr & ~1);
}

bool IsPostGuardIntact() const { return x1c_postGuard == 0xeaeaeaea; }
bool IsPriorGuardIntact() const { return x0_priorGuard == 0xefefefef; }
Expand Down
46 changes: 32 additions & 14 deletions include/Kyoto/Alloc/CMediumAllocPool.hpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
#ifndef _CMEDIUMALLOCPOOL
#define _CMEDIUMALLOCPOOL

#include <rstl/auto_ptr.hpp>
#include <rstl/list.hpp>

struct SMediumAllocPuddle {
SMediumAllocPuddle(const uint numBlocks, void* data, const bool canErase);
~SMediumAllocPuddle();
void* FindFree(uint blockCount);
void* FindFreeEntry(uint blockCount);
void Free(const void* ptr);

const uint GetNumBlocks() const { return x14_numBlocks; }
const uint GetNumAllocs() const { return x18_numAllocs; }
const uint GetNumEntries() const { return x1c_numEntries; }
const bool CanErase() const { return x20_canErase; }
const uint GetPtrOffset(const void* ptr) const { return (uchar*)ptr - x0_mainData.get(); }
static ushort GetBlockOffset(const void* ptrA, const void* ptrB);
static void InitBookKeeping(uchar* bookKeepingPtr, const ushort blockCount);

private:
rstl::auto_ptr< uchar > x0_mainData;
uchar* x8_bookKeeping;
uchar* xc_cachedBookKeepingAddr;
uint x10_unused;
uint x14_numBlocks;
uint x18_numAllocs;
uint x1c_numEntries;
bool x20_canErase : 1;
};

class CMediumAllocPool {
public:
struct SMediumAllocPuddle {
uchar unk;
void* x4_mainData;
void* x8_bookKeeping;
void* xc_cachedBookKeepingOffset;
int x10_;
int x14_numBlocks;
int x18_numAllocs;
int x1c_numEntries;
};

rstl::list< SMediumAllocPuddle > x0_list;
/*rstl::list_node<SMediumAllocPuddle>* x18_lastNodePrev; */
void* x18_lastNodePrev;
rstl::list< SMediumAllocPuddle >::iterator x18_lastNodePrev;
CMediumAllocPool();
void* Alloc(uint size);
bool HasPuddles() const;
void AddPuddle(uint, void*, int);
void AddPuddle(const uint, void*, const bool);
void ClearPuddles();

int Free(const void* ptr);

uint GetTotalEntries();
uint GetNumBlocksAvailable();
uint GetNumAllocs();

static CMediumAllocPool* gMediumAllocPtr;
};

#endif // _CMEDIUMALLOCPOOL
10 changes: 5 additions & 5 deletions include/Kyoto/Alloc/CMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CMemory {
static void* Alloc(size_t len, IAllocator::EHint hint = IAllocator::kHI_None,
IAllocator::EScope scope = IAllocator::kSC_Unk1,
IAllocator::EType type = IAllocator::kTP_Heap,
const CCallStack& callstack = CCallStack(-1, "??(??)"));
const CCallStack& callstack = CCallStack(-1, "\?\?(\?\?)"));
static void Free(const void* ptr);
static void SetOutOfMemoryCallback(IAllocator::FOutOfMemoryCb callback, const void* context);
static void OffsetFakeStatics(int);
Expand All @@ -44,11 +44,11 @@ inline void* operator new(size_t n, void* ptr) { return ptr; };
#ifdef __MWERKS__
inline void operator delete(void* ptr) { CMemory::Free(ptr); }
inline void operator delete[](void* ptr) { CMemory::Free(ptr); }
#define NEW new ("??(??)", nullptr)
#define rs_new new ("\?\?(\?\?)", nullptr)
#else
__attribute__((weak)) void operator delete(void* ptr) { CMemory::Free(ptr); }
__attribute__((weak)) void operator delete[](void* ptr) { CMemory::Free(ptr); }
#define NEW new
// void operator delete(void* ptr);
// void operator delete[](void* ptr);
#define rs_new new
#endif

#endif // _CMEMORY
4 changes: 2 additions & 2 deletions include/Kyoto/Streams/CInputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ inline rstl::vector< T, Alloc >::vector(CInputStream& in, const Alloc& allocator
iterator out = begin() + x4_count;
for (int i = 0; i < count; i++) {
// Maybe this got improved?
// push_back(in.Get(TType< T >()));
out++ = in.Get(TType< T >());
push_back(in.Get(TType< T >()));
// out++ = in.Get(TType< T >());
++x4_count;
}
}
Expand Down
91 changes: 55 additions & 36 deletions include/rstl/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ template < typename I1, typename I2 >
inline void iter_swap(I1 a, I2 b) {
swap(*a, *b);
}
template < class T, class Cmp >
void __sort3(T& a, T& b, T& c, Cmp comp); // TODO

template < typename It, class Cmp >
void __insertion_sort(It first, It last, Cmp cmp);

template < class T, class Cmp >
void __sort3(T& a, T& b, T& c, Cmp comp); // TODO


template < typename It, class Cmp >
void sort(It first, It last, Cmp cmp); // TODO
Expand All @@ -38,20 +39,22 @@ void sort(It first, It last, Cmp cmp); // TODO

template < typename It, class Cmp >
void __insertion_sort(It first, It last, Cmp cmp) {
for (It next = first + 1; next < last; ++next) {
It next = first;
for (++next; next < last; ++next) {
typename iterator_traits< It >::value_type value = *next;

It t1 = next - 1;
It t2 = next;
while (first < t2 && cmp(value, *t1)) {
*t2-- = *t1--;
*t2-- = *t1;
--t1;
}
*t2 = value;
}
}

template < typename T, class Cmp >
void __sort3(T& a, T& b, T& c, Cmp comp) {
void __sort3(T& a, T& b, T& c, const Cmp comp) {
if (comp(b, a)) {
swap(a, b);
}
Expand All @@ -70,28 +73,30 @@ void __sort3(T& a, T& b, T& c, Cmp comp) {
template < typename It, class Cmp >
void sort(It first, It last, Cmp cmp) {
int count = last - first; // use distance?
if (count > 1) {
if (count <= 20) {
__insertion_sort(first, last, cmp);
} else {
It pivot = first + count / 2;
It end = last;
__sort3(*first, *pivot, *--end, cmp);
typename iterator_traits< It >::value_type value = *pivot;
It it = first + 1;
--end;
while (true) {
while (cmp(*it, value))
++it;
while (cmp(value, *end))
--end;
if (it >= end)
break;
iter_swap(it++, end--);
}
sort(first, it, cmp);
sort(it, last, cmp);
if (count <= 1) {
return;
}
if (20 >= count) {
__insertion_sort(first, last, cmp);
} else {
It pivot = first + count / 2;
It end = last;
__sort3(*first, *pivot, *--end, cmp);
typename iterator_traits< It >::value_type value = *pivot;
It it = first + 1;
--end;
while (true) {
while (cmp(*it, value))
++it;
while (cmp(value, *end))
--end;
if (it >= end)
break;
iter_swap(it, end--);
++it;
}
sort(first, it, cmp);
sort(it, last, cmp);
}
}

Expand Down Expand Up @@ -147,18 +152,21 @@ class pair_sorter_finder< pair< K, V >, Cmp > {
bool operator()(const pair< K, V >& a, const pair< K, V >& b) const;
};

template <typename T>
inline pair_sorter_finder< typename T::value_type, less< typename select1st< typename T::value_type >::value_type > > default_pair_sorter_finder()
{
less< typename select1st< typename T::value_type >::value_type > l;
pair_sorter_finder< typename T::value_type, less< typename select1st< typename T::value_type >::value_type > > a(l);
return a;
template < typename T >
inline pair_sorter_finder< typename T::value_type,
less< typename select1st< typename T::value_type >::value_type > >
default_pair_sorter_finder() {
less< typename select1st< typename T::value_type >::value_type > l;
pair_sorter_finder< typename T::value_type,
less< typename select1st< typename T::value_type >::value_type > >
a(l);
return a;
}

template < typename K, typename V, typename Cmp >
inline bool pair_sorter_finder< pair< K, V >, Cmp >::operator()(const K& a,
const pair< K, V >& b) const {
return cmp(a, b.first);
return !!cmp(a, b.first);
}

template < typename K, typename V, typename Cmp >
Expand All @@ -181,8 +189,15 @@ find_by_key(const T& container,
template < typename T >
typename T::const_iterator inline find_by_key(
const T& container, const typename select1st< typename T::value_type >::value_type& key) {
return binary_find(container.begin(), container.end(), key, default_pair_sorter_finder< T >());
}

template < typename T, class Cmp >
typename T::const_iterator inline find_by_key(
const T& container, const typename select1st< typename T::value_type >::value_type& key,
Cmp cmp) {
return binary_find(container.begin(), container.end(), key,
default_pair_sorter_finder<T>());
pair_sorter_finder< typename T::value_type, Cmp >(cmp));
}

template < typename T >
Expand All @@ -192,8 +207,12 @@ find_by_key_nc(T& container, const typename select1st< typename T::value_type >:
template < typename T >
typename T::iterator inline find_by_key_nc(
T& container, const typename select1st< typename T::value_type >::value_type& key) {
return binary_find(container.begin(), container.end(), key,
default_pair_sorter_finder<T>());
return binary_find(container.begin(), container.end(), key, default_pair_sorter_finder< T >());
}

template < typename T, class Cmp >
inline void sort_by_key(T& container, const Cmp& cmp) {
sort(container.begin(), container.end(), pair_sorter_finder< typename T::value_type, Cmp >(cmp));
}

} // namespace rstl
Expand Down
19 changes: 12 additions & 7 deletions include/rstl/auto_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class auto_ptr {
other.x0_has = false;
}
auto_ptr& operator=(const auto_ptr& other) {
if (&other != this) {
if (x0_has) {
delete x4_item;
}
x0_has = other.x0_has;
x4_item = other.x4_item;
other.x0_has = false;
if (&other == this) {
return *this;
}
if (x0_has) {
delete x4_item;
}
x0_has = other.x0_has;
x4_item = other.x4_item;
other.x0_has = false;
return *this;
}
T* get() const { return x4_item; }
Expand All @@ -40,6 +41,10 @@ class auto_ptr {
return x4_item;
}
bool null() const { return x4_item == nullptr; }
void reset() {
x0_has = false;
x4_item = nullptr;
}
};
} // namespace rstl

Expand Down
9 changes: 6 additions & 3 deletions include/rstl/construct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ inline void destroy(It begin, It end) {
}

template < typename It, typename T >
inline void uninitialized_copy(It begin, It end, T* out) {
inline T* uninitialized_copy(It begin, It end, T* out) {
T* tmp = out;
It cur = begin;
for (; cur != end; ++out, ++cur) {
construct(out, *cur);
for (; cur != end; ++tmp, ++cur) {
construct(tmp, *cur);
}

return tmp;
}

template < typename S, typename D >
Expand Down
4 changes: 4 additions & 0 deletions include/rstl/hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace rstl {
template < typename K, typename P, int unk, typename Select, typename Hash, typename Equal,
typename Alloc = rmemory_allocator >
class hash_table {
public:
int size() const { return x.size(); }
private:
rstl::vector< rstl::list< P, Alloc > /*::iterator*/, Alloc > x;
};
Expand All @@ -21,6 +23,8 @@ template < typename K, typename V, typename Hash, typename Equal,
typename Alloc = rmemory_allocator >
class hash_map {
typedef rstl::pair< K, V > Pair;
public:
int size() const { return table.size(); }

private:
hash_table< K, Pair, 0, select1st< Pair >, Hash, Equal, Alloc > table;
Expand Down
6 changes: 3 additions & 3 deletions include/rstl/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typename It::difference_type __distance(It first, It last, forward_iterator_tag)
}

template < typename It >
inline typename It::difference_type distance(It first, It last) {
typename It::difference_type distance(It first, It last) {
return __distance(first, last, typename It::iterator_category());
}

Expand All @@ -33,8 +33,8 @@ void __advance(It& it, S count, forward_iterator_tag) {
}

template < typename It, typename S >
inline void advance(It& it, S count) {
return __advance(it, count, typename It::iterator_category());
void advance(It& it, S count) {
__advance(it, count, typename It::iterator_category());
}

} // namespace rstl
Expand Down
Loading
Loading