5 changed files with 142 additions and 134 deletions
@ -1,97 +0,0 @@
@@ -1,97 +0,0 @@
|
||||
// This is a custom C++ allocator that wraps the 2LSF C heap allocator
|
||||
// -despair
|
||||
// in case of emergency, set DEBUG_TLSF_CPP somewhere before including this header
|
||||
|
||||
#include <limits> |
||||
#include <iostream> |
||||
#include "tlsf.h" |
||||
|
||||
namespace ntr { |
||||
template <class T> |
||||
class TLSFAlloc { |
||||
public: |
||||
typedef T value_type; |
||||
typedef T* pointer; |
||||
typedef const T* const_pointer; |
||||
typedef T& reference; |
||||
typedef const T& const_reference; |
||||
typedef std::size_t size_type; |
||||
typedef std::ptrdiff_t difference_type; |
||||
|
||||
template <class U> |
||||
struct rebind { |
||||
typedef TLSFAlloc<U> other; |
||||
}; |
||||
|
||||
pointer address (reference value) const { |
||||
return &value; |
||||
} |
||||
const_pointer address (const_reference value) const { |
||||
return &value; |
||||
} |
||||
|
||||
// these are the constructors for same: had I not ported sbrk(2) to winnt,
|
||||
// the constructor would have generated a memory pool from above.
|
||||
TLSFAlloc() throw() {} |
||||
TLSFAlloc(const TLSFAlloc&) throw() {} |
||||
template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {} |
||||
~TLSFAlloc() throw() {} |
||||
|
||||
// placement new
|
||||
static void* operator new (std::size_t size, void*) { |
||||
// because this is a static, it can't access this->allocate
|
||||
// so I've jsut copied allocate here
|
||||
#ifdef DEBUG_TLSF_CPP |
||||
// print message and allocate memory with 2LSF
|
||||
std::cerr << "allocate " << size << " element(s)" << " of size " << sizeof(T) << std::endl; |
||||
ptr ret = (ptr)(tlsf_malloc(size * sizeof(T))); |
||||
std::cerr << " allocated at: " << (void*)ret << std::endl; |
||||
#else |
||||
pointer ret = static_cast<pointer>(tlsf_malloc(size * sizeof(T))); |
||||
#endif |
||||
return ret; |
||||
} |
||||
|
||||
// destroy elements of initialized storage p
|
||||
void destroy (pointer p) { |
||||
p->~T(); |
||||
} |
||||
|
||||
// return maximum number of elements that can be allocated
|
||||
size_type max_size() const throw() { |
||||
return std::numeric_limits<std::size_t>::max() / sizeof(T); |
||||
} |
||||
|
||||
// allocate but don't initialize num elements of type T
|
||||
pointer allocate(size_type num, const void* = 0) { |
||||
#ifdef DEBUG_TLSF_CPP |
||||
// print message and allocate memory with 2LSF
|
||||
std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl; |
||||
pointer ret = (pointer)(tlsf_malloc(num*sizeof(T))); |
||||
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret; |
||||
#else |
||||
pointer ret = static_cast<pointer>(tlsf_malloc(num*sizeof(T))); |
||||
return ret; |
||||
#endif |
||||
} |
||||
// initialize elements of allocated storage p with value value
|
||||
// using placement new (defined here)
|
||||
void construct (pointer p, const T& value) { |
||||
new (reinterpret_cast<void*>(p))T(value); |
||||
} |
||||
|
||||
|
||||
// deallocate storage p of deleted elements
|
||||
void deallocate(pointer p, size_type num) { |
||||
#ifdef DEBUG_TLSF_CPP |
||||
// print message and deallocate memory with global delete
|
||||
std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl; |
||||
#endif |
||||
tlsf_free(reinterpret_cast<void*>(p)); |
||||
} |
||||
}; |
||||
|
||||
// return that all specializations of this allocator are interchangeable
|
||||
template <class T1, class T2> bool operator== (const TLSFAlloc<T1>&,const TLSFAlloc<T2>&) throw() { return true; } |
||||
template <class T1, class T2> bool operator!= (const TLSFAlloc<T1>&, const TLSFAlloc<T2>&) throw() { return false; } |
||||
} |
@ -0,0 +1,124 @@
@@ -0,0 +1,124 @@
|
||||
#include "Murmur3.h" |
||||
#include <string> |
||||
#include <unordered_map> |
||||
#include <utility> |
||||
#include <algorithm> |
||||
#include <limits> |
||||
#include <iostream> |
||||
#include <cstring> |
||||
#include "tlsf.h" |
||||
|
||||
// let's try fast strings
|
||||
namespace ntr{ |
||||
template <class T> |
||||
class TLSFAlloc { |
||||
public: |
||||
typedef T value_type; |
||||
typedef T* pointer; |
||||
typedef const T* const_pointer; |
||||
typedef T& reference; |
||||
typedef const T& const_reference; |
||||
typedef std::size_t size_type; |
||||
typedef std::ptrdiff_t difference_type; |
||||
|
||||
template <class U> |
||||
struct rebind { |
||||
typedef TLSFAlloc<U> other; |
||||
}; |
||||
|
||||
pointer address (reference value) const { |
||||
return &value; |
||||
} |
||||
const_pointer address (const_reference value) const { |
||||
return &value; |
||||
} |
||||
|
||||
// these are the constructors for same: had I not ported sbrk(2) to winnt,
|
||||
// the constructor would have generated a memory pool from above.
|
||||
TLSFAlloc() throw() {} |
||||
TLSFAlloc(const TLSFAlloc&) throw() {} |
||||
template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {} |
||||
~TLSFAlloc() throw() {} |
||||
|
||||
// placement new
|
||||
static void* operator new (std::size_t size, void*) { |
||||
// because this is a static, it can't access this->allocate
|
||||
// so I've jsut copied allocate here
|
||||
#ifdef DEBUG_TLSF_CPP |
||||
// print message and allocate memory with 2LSF
|
||||
std::cerr << "allocate " << size << " element(s)" << " of size " << sizeof(T) << std::endl; |
||||
ptr ret = (ptr)(tlsf_malloc(size * sizeof(T))); |
||||
std::cerr << " allocated at: " << (void*)ret << std::endl; |
||||
#else |
||||
pointer ret = static_cast<pointer>(tlsf_malloc(size * sizeof(T))); |
||||
#endif |
||||
return ret; |
||||
} |
||||
|
||||
// destroy elements of initialized storage p
|
||||
void destroy (pointer p) { |
||||
p->~T(); |
||||
} |
||||
|
||||
// return maximum number of elements that can be allocated
|
||||
size_type max_size() const throw() { |
||||
return std::numeric_limits<std::size_t>::max() / sizeof(T); |
||||
} |
||||
|
||||
// allocate but don't initialize num elements of type T
|
||||
pointer allocate(size_type num, const void* = 0) { |
||||
#ifdef DEBUG_TLSF_CPP |
||||
// print message and allocate memory with 2LSF
|
||||
std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl; |
||||
pointer ret = (pointer)(tlsf_malloc(num*sizeof(T))); |
||||
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret; |
||||
#else |
||||
pointer ret = static_cast<pointer>(tlsf_malloc(num*sizeof(T))); |
||||
return ret; |
||||
#endif |
||||
} |
||||
// initialize elements of allocated storage p with value value
|
||||
// using placement new (defined here)
|
||||
void construct (pointer p, const T& value) { |
||||
new (reinterpret_cast<void*>(p))T(value); |
||||
} |
||||
|
||||
|
||||
// deallocate storage p of deleted elements
|
||||
void deallocate(pointer p, size_type num) { |
||||
#ifdef DEBUG_TLSF_CPP |
||||
// print message and deallocate memory with global delete
|
||||
std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl; |
||||
#endif |
||||
tlsf_free(reinterpret_cast<void*>(p)); |
||||
} |
||||
}; |
||||
|
||||
// return that all specializations of this allocator are interchangeable
|
||||
template <class T1, class T2> bool operator== (const TLSFAlloc<T1>&,const TLSFAlloc<T2>&) throw() { return true; } |
||||
template <class T1, class T2> bool operator!= (const TLSFAlloc<T1>&, const TLSFAlloc<T2>&) throw() { return false; } |
||||
|
||||
typedef std::basic_string<char, std::char_traits<char>, TLSFAlloc<char>> fast_string; |
||||
class StringHash{ |
||||
public: |
||||
std::size_t operator() (const fast_string& s) const { |
||||
size_t out = 0; |
||||
// 32-bit hash only, all others overflow. RIP AMD64 users...
|
||||
MurmurHash3_x86_32 ( &s, sizeof(s), 7904542L, &out ); |
||||
return out; |
||||
} |
||||
}; |
||||
class StringComparison{ |
||||
public: |
||||
bool operator ()(const fast_string &a, const fast_string &b) const { |
||||
int i = strcmp(a.c_str(), b.c_str()); |
||||
if (!i){ |
||||
return true; |
||||
} |
||||
else{ |
||||
return false; |
||||
} |
||||
} |
||||
}; |
||||
typedef std::unordered_map<fast_string,fast_string,StringHash,StringComparison,TLSFAlloc<std::pair<const fast_string, fast_string>>> stringmap; |
||||
} |
Loading…
Reference in new issue