llama-mmap.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <cstdint>
  3. #include <memory>
  4. #include <vector>
  5. #include <cstdio>
  6. struct llama_file;
  7. struct llama_mmap;
  8. struct llama_mlock;
  9. using llama_files = std::vector<std::unique_ptr<llama_file>>;
  10. using llama_mmaps = std::vector<std::unique_ptr<llama_mmap>>;
  11. using llama_mlocks = std::vector<std::unique_ptr<llama_mlock>>;
  12. struct llama_file {
  13. llama_file(const char * fname, const char * mode, bool use_direct_io = false);
  14. ~llama_file();
  15. size_t tell() const;
  16. size_t size() const;
  17. int file_id() const; // fileno overload
  18. void seek(size_t offset, int whence) const;
  19. void read_raw(void * ptr, size_t len) const;
  20. void read_raw_at(void * ptr, size_t len, size_t offset) const;
  21. void read_aligned_chunk(size_t offset, void * dest, size_t size) const;
  22. uint32_t read_u32() const;
  23. void write_raw(const void * ptr, size_t len) const;
  24. void write_u32(uint32_t val) const;
  25. size_t read_alignment() const;
  26. private:
  27. struct impl;
  28. std::unique_ptr<impl> pimpl;
  29. };
  30. struct llama_mmap {
  31. llama_mmap(const llama_mmap &) = delete;
  32. llama_mmap(struct llama_file * file, size_t prefetch = (size_t) -1, bool numa = false);
  33. ~llama_mmap();
  34. size_t size() const;
  35. void * addr() const;
  36. void unmap_fragment(size_t first, size_t last);
  37. static const bool SUPPORTED;
  38. private:
  39. struct impl;
  40. std::unique_ptr<impl> pimpl;
  41. };
  42. struct llama_mlock {
  43. llama_mlock();
  44. ~llama_mlock();
  45. void init(void * ptr);
  46. void grow_to(size_t target_size);
  47. static const bool SUPPORTED;
  48. private:
  49. struct impl;
  50. std::unique_ptr<impl> pimpl;
  51. };
  52. size_t llama_path_max();