xref: /aosp_15_r20/external/marisa-trie/lib/marisa/grimoire/io/mapper.h (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1*ab8db090SAndroid Build Coastguard Worker #ifndef MARISA_GRIMOIRE_IO_MAPPER_H_
2*ab8db090SAndroid Build Coastguard Worker #define MARISA_GRIMOIRE_IO_MAPPER_H_
3*ab8db090SAndroid Build Coastguard Worker 
4*ab8db090SAndroid Build Coastguard Worker #include <cstdio>
5*ab8db090SAndroid Build Coastguard Worker 
6*ab8db090SAndroid Build Coastguard Worker #include "marisa/base.h"
7*ab8db090SAndroid Build Coastguard Worker 
8*ab8db090SAndroid Build Coastguard Worker namespace marisa {
9*ab8db090SAndroid Build Coastguard Worker namespace grimoire {
10*ab8db090SAndroid Build Coastguard Worker namespace io {
11*ab8db090SAndroid Build Coastguard Worker 
12*ab8db090SAndroid Build Coastguard Worker class Mapper {
13*ab8db090SAndroid Build Coastguard Worker  public:
14*ab8db090SAndroid Build Coastguard Worker   Mapper();
15*ab8db090SAndroid Build Coastguard Worker   ~Mapper();
16*ab8db090SAndroid Build Coastguard Worker 
17*ab8db090SAndroid Build Coastguard Worker   void open(const char *filename);
18*ab8db090SAndroid Build Coastguard Worker   void open(const void *ptr, std::size_t size);
19*ab8db090SAndroid Build Coastguard Worker 
20*ab8db090SAndroid Build Coastguard Worker   template <typename T>
map(T * obj)21*ab8db090SAndroid Build Coastguard Worker   void map(T *obj) {
22*ab8db090SAndroid Build Coastguard Worker     MARISA_THROW_IF(obj == NULL, MARISA_NULL_ERROR);
23*ab8db090SAndroid Build Coastguard Worker     *obj = *static_cast<const T *>(map_data(sizeof(T)));
24*ab8db090SAndroid Build Coastguard Worker   }
25*ab8db090SAndroid Build Coastguard Worker 
26*ab8db090SAndroid Build Coastguard Worker   template <typename T>
map(const T ** objs,std::size_t num_objs)27*ab8db090SAndroid Build Coastguard Worker   void map(const T **objs, std::size_t num_objs) {
28*ab8db090SAndroid Build Coastguard Worker     MARISA_THROW_IF((objs == NULL) && (num_objs != 0), MARISA_NULL_ERROR);
29*ab8db090SAndroid Build Coastguard Worker     MARISA_THROW_IF(num_objs > (MARISA_SIZE_MAX / sizeof(T)),
30*ab8db090SAndroid Build Coastguard Worker         MARISA_SIZE_ERROR);
31*ab8db090SAndroid Build Coastguard Worker     *objs = static_cast<const T *>(map_data(sizeof(T) * num_objs));
32*ab8db090SAndroid Build Coastguard Worker   }
33*ab8db090SAndroid Build Coastguard Worker 
34*ab8db090SAndroid Build Coastguard Worker   void seek(std::size_t size);
35*ab8db090SAndroid Build Coastguard Worker 
36*ab8db090SAndroid Build Coastguard Worker   bool is_open() const;
37*ab8db090SAndroid Build Coastguard Worker 
38*ab8db090SAndroid Build Coastguard Worker   void clear();
39*ab8db090SAndroid Build Coastguard Worker   void swap(Mapper &rhs);
40*ab8db090SAndroid Build Coastguard Worker 
41*ab8db090SAndroid Build Coastguard Worker  private:
42*ab8db090SAndroid Build Coastguard Worker   const void *ptr_;
43*ab8db090SAndroid Build Coastguard Worker   void *origin_;
44*ab8db090SAndroid Build Coastguard Worker   std::size_t avail_;
45*ab8db090SAndroid Build Coastguard Worker   std::size_t size_;
46*ab8db090SAndroid Build Coastguard Worker #if (defined _WIN32) || (defined _WIN64)
47*ab8db090SAndroid Build Coastguard Worker   void *file_;
48*ab8db090SAndroid Build Coastguard Worker   void *map_;
49*ab8db090SAndroid Build Coastguard Worker #else  // (defined _WIN32) || (defined _WIN64)
50*ab8db090SAndroid Build Coastguard Worker   int fd_;
51*ab8db090SAndroid Build Coastguard Worker #endif  // (defined _WIN32) || (defined _WIN64)
52*ab8db090SAndroid Build Coastguard Worker 
53*ab8db090SAndroid Build Coastguard Worker   void open_(const char *filename);
54*ab8db090SAndroid Build Coastguard Worker   void open_(const void *ptr, std::size_t size);
55*ab8db090SAndroid Build Coastguard Worker 
56*ab8db090SAndroid Build Coastguard Worker   const void *map_data(std::size_t size);
57*ab8db090SAndroid Build Coastguard Worker 
58*ab8db090SAndroid Build Coastguard Worker   // Disallows copy and assignment.
59*ab8db090SAndroid Build Coastguard Worker   Mapper(const Mapper &);
60*ab8db090SAndroid Build Coastguard Worker   Mapper &operator=(const Mapper &);
61*ab8db090SAndroid Build Coastguard Worker };
62*ab8db090SAndroid Build Coastguard Worker 
63*ab8db090SAndroid Build Coastguard Worker }  // namespace io
64*ab8db090SAndroid Build Coastguard Worker }  // namespace grimoire
65*ab8db090SAndroid Build Coastguard Worker }  // namespace marisa
66*ab8db090SAndroid Build Coastguard Worker 
67*ab8db090SAndroid Build Coastguard Worker #endif  // MARISA_GRIMOIRE_IO_MAPPER_H_
68