Cache.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <cstdint>
  17. #include <new>
  18. #include "common.hpp"
  19. #include "dataset.hpp"
  20. #include "virtualMemory.hpp"
  21. namespace RandomX {
  22. class Cache {
  23. public:
  24. static void* alloc(bool largePages) {
  25. if (largePages) {
  26. return allocLargePagesMemory(sizeof(Cache));
  27. }
  28. else {
  29. void* ptr = _mm_malloc(sizeof(Cache), sizeof(__m128i));
  30. if (ptr == nullptr)
  31. throw std::bad_alloc();
  32. return ptr;
  33. }
  34. }
  35. static void dealloc(Cache* cache, bool largePages) {
  36. if (largePages) {
  37. //allocLargePagesMemory(sizeof(Cache));
  38. }
  39. else {
  40. _mm_free(cache);
  41. }
  42. }
  43. /*void* operator new(size_t size) {
  44. void* ptr = _mm_malloc(size, sizeof(__m128i));
  45. if (ptr == nullptr)
  46. throw std::bad_alloc();
  47. return ptr;
  48. }
  49. void operator delete(void* ptr) {
  50. _mm_free(ptr);
  51. }*/
  52. template<bool softAes>
  53. void initialize(const void* seed, size_t seedSize);
  54. const KeysContainer& getKeys() const {
  55. return keys;
  56. }
  57. const uint8_t* getCache() const {
  58. return memory;
  59. }
  60. private:
  61. alignas(16) KeysContainer keys;
  62. uint8_t memory[CacheSize];
  63. void argonFill(const void* seed, size_t seedSize);
  64. };
  65. }