| OLD | NEW |
| 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 #ifndef LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ | 5 #ifndef LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ |
| 6 #define LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ | 6 #define LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ |
| 7 | 7 |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "nacl_io/path.h" | 13 #include "nacl_io/path.h" |
| 14 | 14 |
| 15 class KernelHandle; | 15 class KernelHandle; |
| 16 class Mount; | 16 class Mount; |
| 17 | 17 |
| 18 // KernelObject provides basic functionality for threadsafe | 18 // KernelObject provides basic functionality for threadsafe |
| 19 // acces to kernel objects such as file descriptors and | 19 // access to kernel objects such as file descriptors and |
| 20 // file handles. It also provides access to the CWD for | 20 // file handles. It also provides access to the CWD for |
| 21 // path resolution. | 21 // path resolution. |
| 22 class KernelObject { | 22 class KernelObject { |
| 23 public: | 23 public: |
| 24 struct MMapInfo { | 24 struct MMapInfo { |
| 25 MMapInfo(); | 25 MMapInfo(); |
| 26 MMapInfo(void* addr, size_t length, KernelHandle* handle); | 26 MMapInfo(void* addr, size_t length, KernelHandle* handle); |
| 27 | 27 |
| 28 void* addr; | 28 void* addr; |
| 29 size_t length; | 29 size_t length; |
| 30 KernelHandle* handle; | 30 KernelHandle* handle; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 typedef std::vector<KernelHandle*> HandleMap_t; | 33 typedef std::vector<KernelHandle*> HandleMap_t; |
| 34 typedef std::map<std::string, Mount*> MountMap_t; | 34 typedef std::map<std::string, Mount*> MountMap_t; |
| 35 typedef std::vector<MMapInfo> MMapInfoList_t; | 35 typedef std::vector<MMapInfo> MMapInfoList_t; |
| 36 | 36 |
| 37 KernelObject(); | 37 KernelObject(); |
| 38 virtual ~KernelObject(); | 38 virtual ~KernelObject(); |
| 39 | 39 |
| 40 // Find the mount for the given path, and acquires it | 40 // Find the mount for the given path, and acquires it |
| 41 Mount* AcquireMountAndPath(const std::string& relpath, Path *pobj); | 41 Mount* AcquireMountAndPath(const std::string& relpath, Path *pobj); |
| 42 void ReleaseMount(Mount* mnt); | 42 void ReleaseMount(Mount* mnt); |
| 43 | 43 |
| 44 // Convert from FD to KernelHandle, and aquire the handle. | 44 // Convert from FD to KernelHandle, and acquire the handle. |
| 45 KernelHandle* AcquireHandle(int fd); | 45 KernelHandle* AcquireHandle(int fd); |
| 46 void ReleaseHandle(KernelHandle* handle); | 46 void ReleaseHandle(KernelHandle* handle); |
| 47 | 47 |
| 48 // Allocate a new fd and assign the handle to it, while | 48 // Allocate a new fd and assign the handle to it, while |
| 49 // ref counting the handle and associated mount. | 49 // ref counting the handle and associated mount. |
| 50 int AllocateFD(KernelHandle* handle); | 50 int AllocateFD(KernelHandle* handle); |
| 51 void FreeAndReassignFD(int fd, KernelHandle* handle); | 51 void FreeAndReassignFD(int fd, KernelHandle* handle); |
| 52 void FreeFD(int fd); | 52 void FreeFD(int fd); |
| 53 | 53 |
| 54 protected: | 54 protected: |
| 55 Path GetAbsPathLocked(const std::string& path); | 55 Path GetAbsPathLocked(const std::string& path); |
| 56 | 56 |
| 57 std::vector<int> free_fds_; | 57 std::vector<int> free_fds_; |
| 58 std::string cwd_; | 58 std::string cwd_; |
| 59 | 59 |
| 60 HandleMap_t handle_map_; | 60 HandleMap_t handle_map_; |
| 61 MountMap_t mounts_; | 61 MountMap_t mounts_; |
| 62 MMapInfoList_t mmap_info_list_; | 62 MMapInfoList_t mmap_info_list_; |
| 63 | 63 |
| 64 // Kernel lock protects kernel wide resources such as the mount table... | 64 // Kernel lock protects kernel wide resources such as the mount table... |
| 65 pthread_mutex_t kernel_lock_; | 65 pthread_mutex_t kernel_lock_; |
| 66 | 66 |
| 67 // Process lock proctects process wide resources such as CWD, file handles... | 67 // Process lock protects process wide resources such as CWD, file handles... |
| 68 pthread_mutex_t process_lock_; | 68 pthread_mutex_t process_lock_; |
| 69 | 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(KernelObject); | 70 DISALLOW_COPY_AND_ASSIGN(KernelObject); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ | 73 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ |
| OLD | NEW |