Skip to content

Commit f4b8c2d

Browse files
committed
A bit of xalloc refactor
Also formatting changes
1 parent 2ac00fb commit f4b8c2d

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

src/xrCommon/xr_unordered_map.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
#include <unordered_map>
33
#include "xrCore/Memory/XRayAllocator.hpp"
44

5-
template <class Key, class T, class Hasher = std::hash<Key>, class KeyEqual = std::equal_to<Key>,
6-
class allocator = XRay::xray_allocator<std::pair<const Key, T>>>
7-
using xr_unordered_map = std::unordered_map<Key, T, Hasher, KeyEqual, allocator>;
5+
template <typename K, class V, class Hasher = std::hash<K>, class Traits = std::equal_to<K>,
6+
typename allocator = XRay::xray_allocator<std::pair<const K, V>>>
7+
using xr_unordered_map = std::unordered_map<K, V, Hasher, Traits, allocator>;

src/xrCore/Memory/xalloc.h

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
#pragma once
22
#include "xrCore/xrMemory.h"
33

4-
template <class T>
4+
template <typename T>
55
class xalloc
66
{
77
public:
8-
typedef size_t size_type;
9-
typedef std::ptrdiff_t difference_type;
10-
typedef T* pointer;
11-
typedef const T* const_pointer;
12-
typedef T& reference;
13-
typedef const T& const_reference;
14-
typedef T value_type;
8+
using size_type = size_t;
9+
using difference_type = std::ptrdiff_t;
10+
using pointer = T*;
11+
using const_pointer = const T*;
12+
using reference = T&;
13+
using const_reference = const T&;
14+
using value_type = T;
1515

16-
public:
17-
template<class _Other>
16+
template<class Other>
1817
struct rebind
1918
{
20-
typedef xalloc<_Other> other;
19+
using other = xalloc<Other>;
2120
};
2221

23-
public:
24-
pointer address(reference _Val) const { return &_Val; }
25-
const_pointer address(const_reference _Val) const { return &_Val; }
26-
xalloc() {}
27-
xalloc(const xalloc<T>&) {}
28-
template<class _Other>
29-
xalloc(const xalloc<_Other>&) {}
30-
template<class _Other>
31-
xalloc<T>& operator=(const xalloc<_Other>&) { return *this; }
32-
pointer allocate(size_type n, const void* p = nullptr) const { return xr_alloc<T>(n); }
33-
char* _charalloc(size_type n) { return (char*)allocate(n); }
34-
void deallocate(pointer p, size_type n) const { xr_free(p); }
35-
void deallocate(void* p, size_type n) const { xr_free(p); }
36-
void construct(pointer p, const T& _Val) { new(p) T(_Val); }
22+
pointer address(reference ref) const { return &ref; }
23+
const_pointer address(const_reference ref) const { return &ref; }
24+
25+
xalloc() = default;
26+
xalloc(const xalloc<T>&) = default;
27+
28+
template<class Other>
29+
xalloc(const xalloc<Other>&) {}
30+
31+
template<class Other>
32+
xalloc& operator=(const xalloc<Other>&) { return *this; }
33+
34+
pointer allocate(const size_type n, const void* p = nullptr) const { return xr_alloc<T>(n); }
35+
void deallocate(pointer p, const size_type /*n*/) const { xr_free(p); }
36+
void deallocate(void* p, const size_type /*n*/) const { xr_free(p); }
37+
void construct(pointer p, const T& ref) { new(p) T(ref); }
3738
void destroy(pointer p) { p->~T(); }
3839
size_type max_size() const
3940
{
40-
size_type _Count = (size_type)(-1)/sizeof(T);
41-
return 0<_Count ? _Count : 1;
41+
constexpr auto count = std::numeric_limits<size_type>::max() / sizeof(T);
42+
return 0 < count ? count : 1;
4243
}
4344
};
4445

@@ -47,16 +48,16 @@ struct xr_allocator
4748
template <typename T>
4849
struct helper
4950
{
50-
typedef xalloc<T> result;
51+
using result = xalloc<T>;
5152
};
5253

53-
static void* alloc(const u32& n) { return xr_malloc((u32)n); }
54+
static void* alloc(const size_t n) { return xr_malloc(n); }
5455
template <typename T>
5556
static void dealloc(T*& p) { xr_free(p); }
5657
};
5758

58-
template <class _Ty, class _Other>
59-
inline bool operator==(const xalloc<_Ty>&, const xalloc<_Other>&) { return true; }
59+
template <class T, class Other>
60+
bool operator==(const xalloc<T>&, const xalloc<Other>&) { return true; }
6061

61-
template <class _Ty, class _Other>
62-
inline bool operator!=(const xalloc<_Ty>&, const xalloc<_Other>&) { return false; }
62+
template <class T, class Other>
63+
bool operator!=(const xalloc<T>&, const xalloc<Other>&) { return false; }

src/xrCore/xrMemory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ IC void xr_free(T*& P) throw()
117117
if (P)
118118
{
119119
Memory.mem_free((void*)P);
120-
P = NULL;
120+
P = nullptr;
121121
};
122122
}
123123

0 commit comments

Comments
 (0)