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