| 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 | 5 |
| 6 #ifndef LIBRARIES_NACL_IO_INODE_POOL_H_ | 6 #ifndef LIBRARIES_NACL_IO_INODE_POOL_H_ |
| 7 #define LIBRARIES_NACL_IO_INODE_POOL_H_ | 7 #define LIBRARIES_NACL_IO_INODE_POOL_H_ |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "nacl_io/osstat.h" | 12 #include "nacl_io/osstat.h" |
| 13 #include "pthread.h" | 13 #include "pthread.h" |
| 14 #include "sdk_util/auto_lock.h" | 14 #include "sdk_util/auto_lock.h" |
| 15 | 15 |
| 16 | 16 |
| 17 class INodePool { | 17 class INodePool { |
| 18 public: | 18 public: |
| 19 INodePool() | 19 INodePool() : max_nodes_(0), num_nodes_(0) {} |
| 20 : max_nodes_(0), | |
| 21 num_nodes_(0) { | |
| 22 pthread_mutex_init(&lock_, NULL); | |
| 23 } | |
| 24 ~INodePool() { | |
| 25 pthread_mutex_destroy(&lock_); | |
| 26 } | |
| 27 | 20 |
| 28 ino_t Acquire() { | 21 ino_t Acquire() { |
| 29 AutoLock lock(&lock_); | 22 AUTO_LOCK(lock_); |
| 30 const int INO_CNT = 8; | 23 const int INO_CNT = 8; |
| 31 | 24 |
| 32 // If we run out of INO numbers, then allocate 8 more | 25 // If we run out of INO numbers, then allocate 8 more |
| 33 if (inos_.size() == 0) { | 26 if (inos_.size() == 0) { |
| 34 max_nodes_ += INO_CNT; | 27 max_nodes_ += INO_CNT; |
| 35 // Add eight more to the stack in reverse order, offset by 1 | 28 // Add eight more to the stack in reverse order, offset by 1 |
| 36 // since '0' refers to no INO. | 29 // since '0' refers to no INO. |
| 37 for (int a = 0; a < INO_CNT; a++) { | 30 for (int a = 0; a < INO_CNT; a++) { |
| 38 inos_.push_back(max_nodes_ - a); | 31 inos_.push_back(max_nodes_ - a); |
| 39 } | 32 } |
| 40 } | 33 } |
| 41 | 34 |
| 42 // Return the INO at the top of the stack. | 35 // Return the INO at the top of the stack. |
| 43 int val = inos_.back(); | 36 int val = inos_.back(); |
| 44 inos_.pop_back(); | 37 inos_.pop_back(); |
| 45 num_nodes_++; | 38 num_nodes_++; |
| 46 return val; | 39 return val; |
| 47 } | 40 } |
| 48 | 41 |
| 49 void Release(ino_t ino) { | 42 void Release(ino_t ino) { |
| 50 AutoLock lock(&lock_); | 43 AUTO_LOCK(lock_); |
| 51 inos_.push_back(ino); | 44 inos_.push_back(ino); |
| 52 num_nodes_--; | 45 num_nodes_--; |
| 53 } | 46 } |
| 54 | 47 |
| 55 size_t size() const { return num_nodes_; } | 48 size_t size() const { return num_nodes_; } |
| 56 size_t capacity() const { return max_nodes_; } | 49 size_t capacity() const { return max_nodes_; } |
| 57 | 50 |
| 58 private: | 51 private: |
| 59 size_t num_nodes_; | 52 size_t num_nodes_; |
| 60 size_t max_nodes_; | 53 size_t max_nodes_; |
| 61 std::vector<ino_t> inos_; | 54 std::vector<ino_t> inos_; |
| 62 pthread_mutex_t lock_; | 55 SimpleLock lock_; |
| 63 }; | 56 }; |
| 64 | 57 |
| 65 #endif // LIBRARIES_NACL_IO_INODE_POOL_H_ | 58 #endif // LIBRARIES_NACL_IO_INODE_POOL_H_ |
| OLD | NEW |