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.

netrunner.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // ©2008-2017 despair <despair@netrunner.cc>
  2. #ifndef NETRUNNER_H
  3. #define NETRUNNER_H
  4. #include "Murmur3.h"
  5. #include <string>
  6. #include <unordered_map>
  7. #include <utility>
  8. #include <algorithm>
  9. #include <limits>
  10. #include <iostream>
  11. #include <cstring>
  12. #include "tlsf.h"
  13. // let's try fast strings
  14. namespace ntr{
  15. template <class T>
  16. class TLSFAlloc {
  17. public:
  18. typedef T value_type;
  19. typedef T* pointer;
  20. typedef const T* const_pointer;
  21. typedef T& reference;
  22. typedef const T& const_reference;
  23. typedef std::size_t size_type;
  24. typedef std::ptrdiff_t difference_type;
  25. template <class U>
  26. struct rebind {
  27. typedef TLSFAlloc<U> other;
  28. };
  29. pointer address (reference value) const {
  30. return &value;
  31. }
  32. const_pointer address (const_reference value) const {
  33. return &value;
  34. }
  35. // these are the constructors for same: had I not ported sbrk(2) to winnt,
  36. // the constructor would have generated a memory pool from above.
  37. TLSFAlloc() throw() {}
  38. TLSFAlloc(const TLSFAlloc&) throw() {}
  39. template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {}
  40. ~TLSFAlloc() throw() {}
  41. // placement new
  42. static void* operator new (std::size_t size, void*) {
  43. // because this is a static, it can't access this->allocate
  44. // so I've jsut copied allocate here
  45. #ifdef DEBUG_TLSF_CPP
  46. // print message and allocate memory with 2LSF
  47. std::cerr << "allocate " << size << " element(s)" << " of size " << sizeof(T) << std::endl;
  48. ptr ret = (ptr)(tlsf_malloc(size * sizeof(T)));
  49. std::cerr << " allocated at: " << (void*)ret << std::endl;
  50. #else
  51. pointer ret = static_cast<pointer>(tlsf_malloc(size * sizeof(T)));
  52. #endif
  53. return ret;
  54. }
  55. // destroy elements of initialized storage p
  56. void destroy (pointer p) {
  57. p->~T();
  58. }
  59. // return maximum number of elements that can be allocated
  60. size_type max_size() const throw() {
  61. return std::numeric_limits<std::size_t>::max() / sizeof(T);
  62. }
  63. // allocate but don't initialize num elements of type T
  64. pointer allocate(size_type num, const void* = 0) {
  65. #ifdef DEBUG_TLSF_CPP
  66. // print message and allocate memory with 2LSF
  67. std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
  68. pointer ret = (pointer)(tlsf_malloc(num*sizeof(T)));
  69. std::cerr << " allocated at: " << (void*)ret << std::endl; return ret;
  70. #else
  71. pointer ret = static_cast<pointer>(tlsf_malloc(num*sizeof(T)));
  72. return ret;
  73. #endif
  74. }
  75. // initialize elements of allocated storage p with value value
  76. // using placement new (defined here)
  77. void construct (pointer p, const T& value) {
  78. new (reinterpret_cast<void*>(p))T(value);
  79. }
  80. // deallocate storage p of deleted elements
  81. void deallocate(pointer p, size_type num) {
  82. #ifdef DEBUG_TLSF_CPP
  83. // print message and deallocate memory with global delete
  84. std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl;
  85. #endif
  86. tlsf_free(reinterpret_cast<void*>(p));
  87. }
  88. };
  89. // return that all specializations of this allocator are interchangeable
  90. template <class T1, class T2> bool operator== (const TLSFAlloc<T1>&,const TLSFAlloc<T2>&) throw() { return true; }
  91. template <class T1, class T2> bool operator!= (const TLSFAlloc<T1>&, const TLSFAlloc<T2>&) throw() { return false; }
  92. template <class CharT>
  93. using fast_basic_string = std::basic_string<CharT, std::char_traits<CharT>, TLSFAlloc<CharT>>;
  94. using fast_string = fast_basic_string<char>;
  95. // NT uses UTF-16
  96. using fast_hexstring = fast_basic_string<wchar_t>;
  97. // POSIX demands 32-bit Unicode
  98. using fast_u32string = fast_basic_string<char32_t>;
  99. // Depending on yer environment, these may or may not be meaningful
  100. using fast_octstring = fast_basic_string<unsigned char>;
  101. using fast_sepstring = fast_basic_string<signed char>;
  102. class StringHash{
  103. public:
  104. std::size_t operator() (const fast_string& s) const {
  105. size_t out = 0;
  106. // 32-bit hash only, all others overflow. RIP AMD64 users...
  107. MurmurHash3_x86_32 ( &s, sizeof(s), 7904542L, &out );
  108. return out;
  109. }
  110. };
  111. class StringComparison{
  112. public:
  113. bool operator ()(const fast_string &a, const fast_string &b) const {
  114. int i = strcmp(a.c_str(), b.c_str());
  115. if (!i){
  116. return true;
  117. }
  118. else{
  119. return false;
  120. }
  121. }
  122. };
  123. typedef std::unordered_map<fast_string,fast_string,StringHash,StringComparison,TLSFAlloc<std::pair<const fast_string, fast_string>>> stringmap;
  124. }
  125. #endif