Skip to content

Commit abd4f48

Browse files
Import rstl and alloc
1 parent 4f8e620 commit abd4f48

33 files changed

Lines changed: 1983 additions & 254 deletions

include/Kyoto/Alloc/CCircularBuffer.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _CCIRCULARBUFFER
33

44
#include <types.h>
5+
#include <rstl/auto_ptr.hpp>
56

67
class CCircularBuffer {
78
public:
@@ -12,10 +13,12 @@ class CCircularBuffer {
1213
void* Alloc(int len);
1314
void Free(void* ptr, int len);
1415
int GetAllocatedAmount() const;
16+
void* GetOffsettedMemory(int offset) {
17+
return x0_ptr.get() + offset;
18+
}
1519

1620
private:
17-
uchar x0_owned;
18-
void* x4_ptr;
21+
rstl::auto_ptr<char> x0_ptr;
1922
int x8_bufferLen;
2023
int xc_;
2124
int x10_nextFreeAddr;

include/Kyoto/Alloc/CGameAllocator.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CGameAllocator : public IAllocator {
4141
uint GetNextMaskedFlags();
4242
void SetTopOfHeapAllocated(bool topOfHeap);
4343
size_t GetLength() const { return x4_len; }
44+
void SetLength(size_t len) { x4_len = len; }
4445
SGameMemInfo* GetNextFree() const { return (SGameMemInfo*)((size_t)x18_nextFree & ~31); }
4546
void SetNextFree(SGameMemInfo* info) {
4647
void* ptr = x18_nextFree;
@@ -49,6 +50,10 @@ class CGameAllocator : public IAllocator {
4950
}
5051

5152
bool IsAllocated() const { return ((size_t)x10_prev) & 1; }
53+
void SetNotAllocated() {
54+
void* ptr = x10_prev;
55+
x10_prev = (SGameMemInfo*)((size_t)ptr & ~1);
56+
}
5257

5358
bool IsPostGuardIntact() const { return x1c_postGuard == 0xeaeaeaea; }
5459
bool IsPriorGuardIntact() const { return x0_priorGuard == 0xefefefef; }
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
11
#ifndef _CMEDIUMALLOCPOOL
22
#define _CMEDIUMALLOCPOOL
33

4+
#include <rstl/auto_ptr.hpp>
45
#include <rstl/list.hpp>
56

7+
struct SMediumAllocPuddle {
8+
SMediumAllocPuddle(const uint numBlocks, void* data, const bool canErase);
9+
~SMediumAllocPuddle();
10+
void* FindFree(uint blockCount);
11+
void* FindFreeEntry(uint blockCount);
12+
void Free(const void* ptr);
13+
14+
const uint GetNumBlocks() const { return x14_numBlocks; }
15+
const uint GetNumAllocs() const { return x18_numAllocs; }
16+
const uint GetNumEntries() const { return x1c_numEntries; }
17+
const bool CanErase() const { return x20_canErase; }
18+
const uint GetPtrOffset(const void* ptr) const { return (uchar*)ptr - x0_mainData.get(); }
19+
static ushort GetBlockOffset(const void* ptrA, const void* ptrB);
20+
static void InitBookKeeping(uchar* bookKeepingPtr, const ushort blockCount);
21+
22+
private:
23+
rstl::auto_ptr< uchar > x0_mainData;
24+
uchar* x8_bookKeeping;
25+
uchar* xc_cachedBookKeepingAddr;
26+
uint x10_unused;
27+
uint x14_numBlocks;
28+
uint x18_numAllocs;
29+
uint x1c_numEntries;
30+
bool x20_canErase : 1;
31+
};
32+
633
class CMediumAllocPool {
734
public:
8-
struct SMediumAllocPuddle {
9-
uchar unk;
10-
void* x4_mainData;
11-
void* x8_bookKeeping;
12-
void* xc_cachedBookKeepingOffset;
13-
int x10_;
14-
int x14_numBlocks;
15-
int x18_numAllocs;
16-
int x1c_numEntries;
17-
};
18-
1935
rstl::list< SMediumAllocPuddle > x0_list;
20-
/*rstl::list_node<SMediumAllocPuddle>* x18_lastNodePrev; */
21-
void* x18_lastNodePrev;
36+
rstl::list< SMediumAllocPuddle >::iterator x18_lastNodePrev;
37+
CMediumAllocPool();
2238
void* Alloc(uint size);
2339
bool HasPuddles() const;
24-
void AddPuddle(uint, void*, int);
40+
void AddPuddle(const uint, void*, const bool);
2541
void ClearPuddles();
2642

2743
int Free(const void* ptr);
2844

2945
uint GetTotalEntries();
3046
uint GetNumBlocksAvailable();
3147
uint GetNumAllocs();
48+
49+
static CMediumAllocPool* gMediumAllocPtr;
3250
};
3351

3452
#endif // _CMEDIUMALLOCPOOL

include/Kyoto/Alloc/CMemory.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CMemory {
1818
static void* Alloc(size_t len, IAllocator::EHint hint = IAllocator::kHI_None,
1919
IAllocator::EScope scope = IAllocator::kSC_Unk1,
2020
IAllocator::EType type = IAllocator::kTP_Heap,
21-
const CCallStack& callstack = CCallStack(-1, "??(??)"));
21+
const CCallStack& callstack = CCallStack(-1, "\?\?(\?\?)"));
2222
static void Free(const void* ptr);
2323
static void SetOutOfMemoryCallback(IAllocator::FOutOfMemoryCb callback, const void* context);
2424
static void OffsetFakeStatics(int);
@@ -44,11 +44,11 @@ inline void* operator new(size_t n, void* ptr) { return ptr; };
4444
#ifdef __MWERKS__
4545
inline void operator delete(void* ptr) { CMemory::Free(ptr); }
4646
inline void operator delete[](void* ptr) { CMemory::Free(ptr); }
47-
#define NEW new ("??(??)", nullptr)
47+
#define rs_new new ("\?\?(\?\?)", nullptr)
4848
#else
49-
__attribute__((weak)) void operator delete(void* ptr) { CMemory::Free(ptr); }
50-
__attribute__((weak)) void operator delete[](void* ptr) { CMemory::Free(ptr); }
51-
#define NEW new
49+
// void operator delete(void* ptr);
50+
// void operator delete[](void* ptr);
51+
#define rs_new new
5252
#endif
5353

5454
#endif // _CMEMORY

include/Kyoto/Streams/CInputStream.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ inline rstl::vector< T, Alloc >::vector(CInputStream& in, const Alloc& allocator
149149
iterator out = begin() + x4_count;
150150
for (int i = 0; i < count; i++) {
151151
// Maybe this got improved?
152-
// push_back(in.Get(TType< T >()));
153-
out++ = in.Get(TType< T >());
152+
push_back(in.Get(TType< T >()));
153+
// out++ = in.Get(TType< T >());
154154
++x4_count;
155155
}
156156
}

include/rstl/algorithm.hpp

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ template < typename I1, typename I2 >
2424
inline void iter_swap(I1 a, I2 b) {
2525
swap(*a, *b);
2626
}
27+
template < class T, class Cmp >
28+
void __sort3(T& a, T& b, T& c, Cmp comp); // TODO
2729

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

31-
template < class T, class Cmp >
32-
void __sort3(T& a, T& b, T& c, Cmp comp); // TODO
33+
3334

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

3940
template < typename It, class Cmp >
4041
void __insertion_sort(It first, It last, Cmp cmp) {
41-
for (It next = first + 1; next < last; ++next) {
42+
It next = first;
43+
for (++next; next < last; ++next) {
4244
typename iterator_traits< It >::value_type value = *next;
4345

4446
It t1 = next - 1;
4547
It t2 = next;
4648
while (first < t2 && cmp(value, *t1)) {
47-
*t2-- = *t1--;
49+
*t2-- = *t1;
50+
--t1;
4851
}
4952
*t2 = value;
5053
}
5154
}
5255

5356
template < typename T, class Cmp >
54-
void __sort3(T& a, T& b, T& c, Cmp comp) {
57+
void __sort3(T& a, T& b, T& c, const Cmp comp) {
5558
if (comp(b, a)) {
5659
swap(a, b);
5760
}
@@ -70,28 +73,30 @@ void __sort3(T& a, T& b, T& c, Cmp comp) {
7073
template < typename It, class Cmp >
7174
void sort(It first, It last, Cmp cmp) {
7275
int count = last - first; // use distance?
73-
if (count > 1) {
74-
if (count <= 20) {
75-
__insertion_sort(first, last, cmp);
76-
} else {
77-
It pivot = first + count / 2;
78-
It end = last;
79-
__sort3(*first, *pivot, *--end, cmp);
80-
typename iterator_traits< It >::value_type value = *pivot;
81-
It it = first + 1;
82-
--end;
83-
while (true) {
84-
while (cmp(*it, value))
85-
++it;
86-
while (cmp(value, *end))
87-
--end;
88-
if (it >= end)
89-
break;
90-
iter_swap(it++, end--);
91-
}
92-
sort(first, it, cmp);
93-
sort(it, last, cmp);
76+
if (count <= 1) {
77+
return;
78+
}
79+
if (20 >= count) {
80+
__insertion_sort(first, last, cmp);
81+
} else {
82+
It pivot = first + count / 2;
83+
It end = last;
84+
__sort3(*first, *pivot, *--end, cmp);
85+
typename iterator_traits< It >::value_type value = *pivot;
86+
It it = first + 1;
87+
--end;
88+
while (true) {
89+
while (cmp(*it, value))
90+
++it;
91+
while (cmp(value, *end))
92+
--end;
93+
if (it >= end)
94+
break;
95+
iter_swap(it, end--);
96+
++it;
9497
}
98+
sort(first, it, cmp);
99+
sort(it, last, cmp);
95100
}
96101
}
97102

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

150-
template <typename T>
151-
inline pair_sorter_finder< typename T::value_type, less< typename select1st< typename T::value_type >::value_type > > default_pair_sorter_finder()
152-
{
153-
less< typename select1st< typename T::value_type >::value_type > l;
154-
pair_sorter_finder< typename T::value_type, less< typename select1st< typename T::value_type >::value_type > > a(l);
155-
return a;
155+
template < typename T >
156+
inline pair_sorter_finder< typename T::value_type,
157+
less< typename select1st< typename T::value_type >::value_type > >
158+
default_pair_sorter_finder() {
159+
less< typename select1st< typename T::value_type >::value_type > l;
160+
pair_sorter_finder< typename T::value_type,
161+
less< typename select1st< typename T::value_type >::value_type > >
162+
a(l);
163+
return a;
156164
}
157165

158166
template < typename K, typename V, typename Cmp >
159167
inline bool pair_sorter_finder< pair< K, V >, Cmp >::operator()(const K& a,
160168
const pair< K, V >& b) const {
161-
return cmp(a, b.first);
169+
return !!cmp(a, b.first);
162170
}
163171

164172
template < typename K, typename V, typename Cmp >
@@ -181,8 +189,15 @@ find_by_key(const T& container,
181189
template < typename T >
182190
typename T::const_iterator inline find_by_key(
183191
const T& container, const typename select1st< typename T::value_type >::value_type& key) {
192+
return binary_find(container.begin(), container.end(), key, default_pair_sorter_finder< T >());
193+
}
194+
195+
template < typename T, class Cmp >
196+
typename T::const_iterator inline find_by_key(
197+
const T& container, const typename select1st< typename T::value_type >::value_type& key,
198+
Cmp cmp) {
184199
return binary_find(container.begin(), container.end(), key,
185-
default_pair_sorter_finder<T>());
200+
pair_sorter_finder< typename T::value_type, Cmp >(cmp));
186201
}
187202

188203
template < typename T >
@@ -192,8 +207,12 @@ find_by_key_nc(T& container, const typename select1st< typename T::value_type >:
192207
template < typename T >
193208
typename T::iterator inline find_by_key_nc(
194209
T& container, const typename select1st< typename T::value_type >::value_type& key) {
195-
return binary_find(container.begin(), container.end(), key,
196-
default_pair_sorter_finder<T>());
210+
return binary_find(container.begin(), container.end(), key, default_pair_sorter_finder< T >());
211+
}
212+
213+
template < typename T, class Cmp >
214+
inline void sort_by_key(T& container, const Cmp& cmp) {
215+
sort(container.begin(), container.end(), pair_sorter_finder< typename T::value_type, Cmp >(cmp));
197216
}
198217

199218
} // namespace rstl

include/rstl/auto_ptr.hpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ class auto_ptr {
2222
other.x0_has = false;
2323
}
2424
auto_ptr& operator=(const auto_ptr& other) {
25-
if (&other != this) {
26-
if (x0_has) {
27-
delete x4_item;
28-
}
29-
x0_has = other.x0_has;
30-
x4_item = other.x4_item;
31-
other.x0_has = false;
25+
if (&other == this) {
26+
return *this;
3227
}
28+
if (x0_has) {
29+
delete x4_item;
30+
}
31+
x0_has = other.x0_has;
32+
x4_item = other.x4_item;
33+
other.x0_has = false;
3334
return *this;
3435
}
3536
T* get() const { return x4_item; }
@@ -40,6 +41,10 @@ class auto_ptr {
4041
return x4_item;
4142
}
4243
bool null() const { return x4_item == nullptr; }
44+
void reset() {
45+
x0_has = false;
46+
x4_item = nullptr;
47+
}
4348
};
4449
} // namespace rstl
4550

include/rstl/construct.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ inline void destroy(It begin, It end) {
2525
}
2626

2727
template < typename It, typename T >
28-
inline void uninitialized_copy(It begin, It end, T* out) {
28+
inline T* uninitialized_copy(It begin, It end, T* out) {
29+
T* tmp = out;
2930
It cur = begin;
30-
for (; cur != end; ++out, ++cur) {
31-
construct(out, *cur);
31+
for (; cur != end; ++tmp, ++cur) {
32+
construct(tmp, *cur);
3233
}
34+
35+
return tmp;
3336
}
3437

3538
template < typename S, typename D >

include/rstl/hash_map.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace rstl {
1313
template < typename K, typename P, int unk, typename Select, typename Hash, typename Equal,
1414
typename Alloc = rmemory_allocator >
1515
class hash_table {
16+
public:
17+
int size() const { return x.size(); }
1618
private:
1719
rstl::vector< rstl::list< P, Alloc > /*::iterator*/, Alloc > x;
1820
};
@@ -21,6 +23,8 @@ template < typename K, typename V, typename Hash, typename Equal,
2123
typename Alloc = rmemory_allocator >
2224
class hash_map {
2325
typedef rstl::pair< K, V > Pair;
26+
public:
27+
int size() const { return table.size(); }
2428

2529
private:
2630
hash_table< K, Pair, 0, select1st< Pair >, Hash, Equal, Alloc > table;

include/rstl/iterator.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typename It::difference_type __distance(It first, It last, forward_iterator_tag)
2121
}
2222

2323
template < typename It >
24-
inline typename It::difference_type distance(It first, It last) {
24+
typename It::difference_type distance(It first, It last) {
2525
return __distance(first, last, typename It::iterator_category());
2626
}
2727

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

3535
template < typename It, typename S >
36-
inline void advance(It& it, S count) {
37-
return __advance(it, count, typename It::iterator_category());
36+
void advance(It& it, S count) {
37+
__advance(it, count, typename It::iterator_category());
3838
}
3939

4040
} // namespace rstl

0 commit comments

Comments
 (0)