• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GRALLOC_QSD8K_PMEMALLOC_H
18 #define GRALLOC_QSD8K_PMEMALLOC_H
19 
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <unistd.h>
24 
25 
26 /**
27  * An interface to the PMEM allocators.
28  */
29 class PmemAllocator {
30 
31  public:
32 
33     virtual ~PmemAllocator();
34 
35     // Only valid after init_pmem_area() has completed successfully.
36     virtual void* get_base_address() = 0;
37 
38     virtual int alloc_pmem_buffer(size_t size, int usage, void** pBase,
39             int* pOffset, int* pFd) = 0;
40     virtual int free_pmem_buffer(size_t size, void* base, int offset, int fd) = 0;
41 };
42 
43 
44 /**
45  * A PMEM allocator that allocates the entire pmem memory from the kernel and
46  * then uses a user-space allocator to suballocate from that.  This requires
47  * that the PMEM device driver have kernel allocation disabled.
48  */
49 class PmemUserspaceAllocator: public PmemAllocator {
50 
51  public:
52 
53     class Deps {
54      public:
55 
56         class Allocator {
57          public:
58             virtual ~Allocator();
59             virtual ssize_t setSize(size_t size) = 0;
60             virtual size_t  size() const = 0;
61             virtual ssize_t allocate(size_t size, uint32_t flags = 0) = 0;
62             virtual ssize_t deallocate(size_t offset) = 0;
63         };
64 
65         virtual ~Deps();
66 
67         // pmem
68         virtual size_t getPmemTotalSize(int fd, size_t* size) = 0;
69         virtual int connectPmem(int fd, int master_fd) = 0;
70         virtual int mapPmem(int fd, int offset, size_t size) = 0;
71         virtual int unmapPmem(int fd, int offset, size_t size) = 0;
72 
73         // C99
74         virtual int getErrno() = 0;
75 
76         // POSIX
77         virtual void* mmap(void* start, size_t length, int prot, int flags, int fd,
78                 off_t offset) = 0;
79         virtual int open(const char* pathname, int flags, int mode) = 0;
80         virtual int close(int fd) = 0;
81     };
82 
83     PmemUserspaceAllocator(Deps& deps, Deps::Allocator& allocator, const char* pmemdev);
84     virtual ~PmemUserspaceAllocator();
85 
86     // Only valid after init_pmem_area() has completed successfully.
87     virtual void* get_base_address();
88 
89     virtual int init_pmem_area_locked();
90     virtual int init_pmem_area();
91     virtual int alloc_pmem_buffer(size_t size, int usage, void** pBase,
92             int* pOffset, int* pFd);
93     virtual int free_pmem_buffer(size_t size, void* base, int offset, int fd);
94 
95 #ifndef ANDROID_OS
96     // DO NOT USE: For testing purposes only.
set_master_values(int fd,void * base)97     void set_master_values(int fd, void* base) {
98         master_fd = fd;
99         master_base = base;
100     }
101 #endif // ANDROID_OS
102 
103  private:
104 
105     enum {
106         MASTER_FD_INIT = -1,
107     };
108 
109     Deps& deps;
110     Deps::Allocator& allocator;
111 
112     pthread_mutex_t lock;
113     const char* pmemdev;
114     int master_fd;
115     void* master_base;
116 };
117 
118 
119 /**
120  * A PMEM allocator that allocates each individual allocation from the kernel
121  * (using the kernel's allocator).  This requires the kernel driver for the
122  * particular PMEM device being allocated from to support kernel allocation.
123  */
124 class PmemKernelAllocator: public PmemAllocator {
125 
126  public:
127 
128     class Deps {
129      public:
130 
131         virtual ~Deps();
132 
133         // C99
134         virtual int getErrno() = 0;
135 
136         // POSIX
137         virtual void* mmap(void* start, size_t length, int prot, int flags, int fd,
138                 off_t offset) = 0;
139         virtual int munmap(void* start, size_t length) = 0;
140         virtual int open(const char* pathname, int flags, int mode) = 0;
141         virtual int close(int fd) = 0;
142     };
143 
144     PmemKernelAllocator(Deps& deps, const char* pmemdev);
145     virtual ~PmemKernelAllocator();
146 
147     // Only valid after init_pmem_area() has completed successfully.
148     virtual void* get_base_address();
149 
150     virtual int alloc_pmem_buffer(size_t size, int usage, void** pBase,
151             int* pOffset, int* pFd);
152     virtual int free_pmem_buffer(size_t size, void* base, int offset, int fd);
153 
154  private:
155 
156     Deps& deps;
157 
158     const char* pmemdev;
159 };
160 
161 #endif  // GRALLOC_QSD8K_PMEMALLOC_H
162