You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

143 lines
5.0 KiB

// ©2008-2017 despair <despair@netrunner.cc>
#ifndef NETRUNNER_H
#define NETRUNNER_H
#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;
pointer ret = static_cast<pointer>(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; }
template <class CharT>
using fast_basic_string = std::basic_string<CharT, std::char_traits<CharT>, TLSFAlloc<CharT>>;
using fast_string = fast_basic_string<char>;
// NT uses UTF-16
using fast_hexstring = fast_basic_string<wchar_t>;
// POSIX demands 32-bit Unicode
using fast_u32string = fast_basic_string<char32_t>;
// Depending on yer environment, these may or may not be meaningful
using fast_octstring = fast_basic_string<unsigned char>;
using fast_sepstring = fast_basic_string<signed char>;
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;
}
#endif