Skip to content

Commit

Permalink
A bit of xalloc refactor
Browse files Browse the repository at this point in the history
Also formatting changes
  • Loading branch information
Xottab-DUTY committed Jan 7, 2018
1 parent 2ac00fb commit f4b8c2d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/xrCommon/xr_unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#include <unordered_map>
#include "xrCore/Memory/XRayAllocator.hpp"

template <class Key, class T, class Hasher = std::hash<Key>, class KeyEqual = std::equal_to<Key>,
class allocator = XRay::xray_allocator<std::pair<const Key, T>>>
using xr_unordered_map = std::unordered_map<Key, T, Hasher, KeyEqual, allocator>;
template <typename K, class V, class Hasher = std::hash<K>, class Traits = std::equal_to<K>,
typename allocator = XRay::xray_allocator<std::pair<const K, V>>>
using xr_unordered_map = std::unordered_map<K, V, Hasher, Traits, allocator>;
67 changes: 34 additions & 33 deletions src/xrCore/Memory/xalloc.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
#pragma once
#include "xrCore/xrMemory.h"

template <class T>
template <typename T>
class xalloc
{
public:
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using value_type = T;

public:
template<class _Other>
template<class Other>
struct rebind
{
typedef xalloc<_Other> other;
using other = xalloc<Other>;
};

public:
pointer address(reference _Val) const { return &_Val; }
const_pointer address(const_reference _Val) const { return &_Val; }
xalloc() {}
xalloc(const xalloc<T>&) {}
template<class _Other>
xalloc(const xalloc<_Other>&) {}
template<class _Other>
xalloc<T>& operator=(const xalloc<_Other>&) { return *this; }
pointer allocate(size_type n, const void* p = nullptr) const { return xr_alloc<T>(n); }
char* _charalloc(size_type n) { return (char*)allocate(n); }
void deallocate(pointer p, size_type n) const { xr_free(p); }
void deallocate(void* p, size_type n) const { xr_free(p); }
void construct(pointer p, const T& _Val) { new(p) T(_Val); }
pointer address(reference ref) const { return &ref; }
const_pointer address(const_reference ref) const { return &ref; }

xalloc() = default;
xalloc(const xalloc<T>&) = default;

template<class Other>
xalloc(const xalloc<Other>&) {}

template<class Other>
xalloc& operator=(const xalloc<Other>&) { return *this; }

pointer allocate(const size_type n, const void* p = nullptr) const { return xr_alloc<T>(n); }
void deallocate(pointer p, const size_type /*n*/) const { xr_free(p); }
void deallocate(void* p, const size_type /*n*/) const { xr_free(p); }
void construct(pointer p, const T& ref) { new(p) T(ref); }
void destroy(pointer p) { p->~T(); }
size_type max_size() const
{
size_type _Count = (size_type)(-1)/sizeof(T);
return 0<_Count ? _Count : 1;
constexpr auto count = std::numeric_limits<size_type>::max() / sizeof(T);
return 0 < count ? count : 1;
}
};

Expand All @@ -47,16 +48,16 @@ struct xr_allocator
template <typename T>
struct helper
{
typedef xalloc<T> result;
using result = xalloc<T>;
};

static void* alloc(const u32& n) { return xr_malloc((u32)n); }
static void* alloc(const size_t n) { return xr_malloc(n); }
template <typename T>
static void dealloc(T*& p) { xr_free(p); }
};

template <class _Ty, class _Other>
inline bool operator==(const xalloc<_Ty>&, const xalloc<_Other>&) { return true; }
template <class T, class Other>
bool operator==(const xalloc<T>&, const xalloc<Other>&) { return true; }

template <class _Ty, class _Other>
inline bool operator!=(const xalloc<_Ty>&, const xalloc<_Other>&) { return false; }
template <class T, class Other>
bool operator!=(const xalloc<T>&, const xalloc<Other>&) { return false; }
2 changes: 1 addition & 1 deletion src/xrCore/xrMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ IC void xr_free(T*& P) throw()
if (P)
{
Memory.mem_free((void*)P);
P = NULL;
P = nullptr;
};
}

Expand Down

0 comments on commit f4b8c2d

Please sign in to comment.