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