summaryrefslogtreecommitdiff
path: root/common/libs/auto_resources/auto_resources.h
blob: 1c226bc7e96b2b68a0f2a3e7ab36331f0a27c750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef CUTTLEFISH_COMMON_COMMON_LIBS_AUTO_RESOURCES_AUTO_RESOURCES_H_
#define CUTTLEFISH_COMMON_COMMON_LIBS_AUTO_RESOURCES_AUTO_RESOURCES_H_

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>

template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];

template <typename T, size_t N>
char (&ArraySizeHelper(const T (&array)[N]))[N];

#define arraysize(array) (sizeof(ArraySizeHelper(array)))

// Automatically close a file descriptor
class AutoCloseFileDescriptor {
 public:
  explicit AutoCloseFileDescriptor(int fd) : fd_(fd) { }
  virtual ~AutoCloseFileDescriptor() {
    if (fd_ != -1) {
      (void)::close(fd_);
      fd_ = -1;
    }
  }

  operator int() const {
    return fd_;
  }

  bool IsError() const {
    return fd_ == -1;
  }

  // Close the underlying file descriptor, returning a status to give the caller
  // the chance to act on failure to close.
  // Returns true on success.
  bool close() {
    bool rval = true;
    if (fd_ != -1) {
      rval = !::close(fd_);
      fd_ = -1;
    }
    return rval;
  }

 private:
  AutoCloseFileDescriptor& operator=(const AutoCloseFileDescriptor & o);
  explicit AutoCloseFileDescriptor(const AutoCloseFileDescriptor &);

  int fd_;
};

// In C++11 this is just std::vector<char>, but Android isn't
// there yet.
class AutoFreeBuffer {
 public:
  enum {
    // Minimum reserve size of AutoFreeBuffer to consider shrinking reservation.
    // Any buffer shorter than this will not be shrunk.
    kAutoBufferShrinkReserveThreshold = 8192
  };

  AutoFreeBuffer()
      : data_(NULL), size_(0), reserve_size_(0) {}

  AutoFreeBuffer(size_t reserve_size)
      : data_(NULL), size_(0), reserve_size_(0) {
    Reserve(reserve_size);
  }

  ~AutoFreeBuffer();
  void Clear();
  bool Resize(size_t newsize);
  bool Reserve(size_t newsize);
  bool SetToString(const char* in);
  bool Append(const void* new_data, size_t new_data_size);
  size_t PrintF(const char* format, ... );

  char* data() {
    return data_;
  }

  const char* data() const {
    return data_;
  }

  char* begin() {
    return data_;
  }

  const char* begin() const {
    return data_;
  }

  char* end() {
    return data_ + size_;
  }

  const char* end() const {
    return data_ + size_;
  }

  size_t size() const {
    return size_;
  }

  size_t reserve_size() const {
    return reserve_size_;
  }

  void Swap(AutoFreeBuffer& other) {
    char* temp_ptr = data_;
    data_ = other.data_;
    other.data_ = temp_ptr;

    size_t temp_size = size_;
    size_ = other.size_;
    other.size_ = temp_size;

    temp_size = reserve_size_;
    reserve_size_ = other.reserve_size_;
    other.reserve_size_ = temp_size;
  }

  bool operator==(const AutoFreeBuffer& other) const {
    return (size_ == other.size_) && !memcmp(data_, other.data_, size_);
  }

  bool operator!=(const AutoFreeBuffer& other) const {
    return !(*this == other);
  }

 protected:
  char *data_;
  size_t size_;
  size_t reserve_size_;

 private:
  AutoFreeBuffer& operator=(const AutoFreeBuffer&);
  explicit AutoFreeBuffer(const AutoFreeBuffer&);
};
#endif  // CUTTLEFISH_COMMON_COMMON_LIBS_AUTO_RESOURCES_AUTO_RESOURCES_H_