• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef MEMFD_LINUX
16 #define MEMFD_LINUX
17 
18 #include <cstddef>
19 
20 // Implementation of shared-memory regions backed by memfd_create(), which
21 // unfortunately is not exported by older GLibc versions, though it has been
22 // supported by the Linux kernel since 3.17 (good enough for Android and desktop
23 // Linux).
24 
25 class LinuxMemFd
26 {
27 public:
28 	LinuxMemFd() = default;
29 
LinuxMemFd(const char * name,size_t size)30 	LinuxMemFd(const char *name, size_t size)
31 	    : LinuxMemFd()
32 	{
33 		allocate(name, size);
34 	}
35 
36 	~LinuxMemFd();
37 
38 	// Return true iff the region is valid/allocated.
isValid() const39 	bool isValid() const { return fd_ >= 0; }
40 
41 	// Return region's internal file descriptor value. Useful for mapping.
fd() const42 	int fd() const { return fd_; }
43 
44 	// Set the internal handle to |fd|.
45 	void importFd(int fd);
46 
47 	// Return a copy of this instance's file descriptor (with CLO_EXEC set).
48 	int exportFd() const;
49 
50 	// Implement memfd_create() through direct syscalls if possible.
51 	// On success, return true and sets |fd| accordingly. On failure, return
52 	// false and sets errno.
53 	bool allocate(const char *name, size_t size);
54 
55 	// Map a segment of |size| bytes from |offset| from the region.
56 	// Both |offset| and |size| should be page-aligned. Returns nullptr/errno
57 	// on failure.
58 	void *mapReadWrite(size_t offset, size_t size);
59 
60 	// Unmap a region segment starting at |addr| of |size| bytes.
61 	// Both |addr| and |size| should be page-aligned. Returns true on success
62 	// or false/errno on failure.
63 	bool unmap(void *addr, size_t size);
64 
65 	void close();
66 
67 private:
68 	int fd_ = -1;
69 };
70 
71 #endif  // MEMFD_LINUX
72