dataset.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <vector>
  18. #include <type_traits>
  19. #include "common.hpp"
  20. #include "superscalar_program.hpp"
  21. #include "allocator.hpp"
  22. /* Global scope for C binding */
  23. struct randomx_dataset {
  24. uint8_t* memory = nullptr;
  25. randomx::DatasetDeallocFunc dealloc;
  26. };
  27. namespace randomx {
  28. class JitCompilerX86;
  29. }
  30. /* Global scope for C binding */
  31. struct randomx_cache {
  32. uint8_t* memory = nullptr;
  33. randomx::CacheDeallocFunc dealloc;
  34. randomx::JitCompilerX86* jit;
  35. randomx::CacheInitializeFunc initialize;
  36. randomx::DatasetInitFunc datasetInit;
  37. randomx::SuperscalarProgram programs[RANDOMX_CACHE_ACCESSES];
  38. std::vector<uint64_t> reciprocalCache;
  39. };
  40. //A pointer to a standard-layout struct object points to its initial member
  41. static_assert(std::is_standard_layout<randomx_dataset>(), "randomx_dataset must be a standard-layout struct");
  42. static_assert(std::is_standard_layout<randomx_cache>(), "randomx_cache must be a standard-layout struct");
  43. namespace randomx {
  44. using DefaultAllocator = AlignedAllocator<CacheLineSize>;
  45. template<class Allocator>
  46. void deallocDataset(randomx_dataset* dataset) {
  47. if (dataset->memory != nullptr)
  48. Allocator::freeMemory(dataset->memory, DatasetSize);
  49. }
  50. template<class Allocator>
  51. void deallocCache(randomx_cache* cache) {
  52. if(cache->memory != nullptr)
  53. Allocator::freeMemory(cache->memory, CacheSize);
  54. if (cache->jit != nullptr)
  55. delete cache->jit;
  56. }
  57. void initCache(randomx_cache*, const void*, size_t);
  58. void initCacheCompile(randomx_cache*, const void*, size_t);
  59. void initDatasetItem(randomx_cache* cache, uint8_t* out, uint64_t blockNumber);
  60. void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startBlock, uint32_t endBlock);
  61. }