Guest User

Untitled

a guest
Nov 4th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. namespace nall {
  2. namespace memory {
  3.  
  4. template<unsigned Count, unsigned Size>
  5. struct pool {
  6. signed* list = nullptr;
  7. uint8_t* data = nullptr;
  8. unsigned cursor = 0;
  9.  
  10. pool() {
  11. list = (signed*)memory::allocate(Count * sizeof(signed));
  12. data = (uint8_t*)memory::allocate(Count * Size);
  13. for(unsigned n = 0; n < Count; n++) list[n] = n;
  14. }
  15.  
  16. ~pool() {
  17. memory::free(list);
  18. memory::free(data);
  19. }
  20.  
  21. auto allocate(unsigned size) -> void* {
  22. if(size == 0) return nullptr;
  23. if(size > Size) return memory::allocate(size);
  24. signed offset = list[cursor];
  25. if(offset < 0) return memory::allocate(size);
  26. list[cursor] = -1;
  27. cursor = (cursor + 1) % Count;
  28. return (void*)(data + offset * Size);
  29. }
  30.  
  31. auto allocate(unsigned size, uint8_t data) -> void* {
  32. auto result = allocate(size);
  33. memset(result, data, size);
  34. return result;
  35. }
  36.  
  37. auto resize(void* target, unsigned size) -> void* {
  38. if(target == nullptr) return allocate(size);
  39. signed offset = ((uint8_t*)target - data) / Size;
  40. if(offset < 0 || offset >= Count) return memory::resize(target, size);
  41. if(size <= Size) return target;
  42. cursor = (cursor - 1) % Count;
  43. list[cursor] = offset;
  44. return memory::allocate(size);
  45. }
  46.  
  47. auto free(void* target) -> void {
  48. if(target == nullptr) return;
  49. signed offset = ((uint8_t*)target - data) / Size;
  50. if(offset < 0 || offset >= Count) return memory::free(target);
  51. cursor = (cursor - 1) % Count;
  52. list[cursor] = offset;
  53. }
  54.  
  55. pool(const pool&) = delete;
  56. pool& operator=(const pool&) = delete;
  57. };
  58.  
  59. }
  60. }
  61.  
  62. auto nall::main(lstring args) -> void {
  63. memory::pool<65536, 256> pool;
  64.  
  65. double r0;
  66. {
  67. clock_t a = clock();
  68. for(unsigned n = 0; n < 10000; n++) {
  69. for(unsigned l = 0; l < 4000; l++) {
  70. void* data = memory::allocate(128);
  71. *(uint8_t*)data = 0;
  72. data = memory::resize(data, 256);
  73. *(uint8_t*)data = 0;
  74. memory::free(data);
  75. }
  76. }
  77. clock_t b = clock();
  78. r0 = (double)(b - a);
  79. printf("%fs (malloc)\n", r0 / CLOCKS_PER_SEC);
  80. }
  81.  
  82. double r1;
  83. {
  84. clock_t a = clock();
  85. for(unsigned n = 0; n < 10000; n++) {
  86. for(unsigned l = 0; l < 4000; l++) {
  87. void* data = pool.allocate(128);
  88. *(uint8_t*)data = 0;
  89. data = pool.resize(data, 256);
  90. *(uint8_t*)data = 0;
  91. pool.free(data);
  92. }
  93. }
  94. clock_t b = clock();
  95. r1 = (double)(b - a);
  96. printf("%fs (memory::pool)\n", r1 / CLOCKS_PER_SEC);
  97. }
  98.  
  99. printf("delta: %fx\n", r0 / r1);
  100. printf("delta: %f%%\n", r1 / r0 * 100.0);
  101. }
  102.  
  103. 16.562500s (malloc)
  104. 0.195312s (memory::pool)
  105. delta: 84.800000x
  106. delta: 1.179245%
Advertisement
Add Comment
Please, Sign In to add comment