blob: 6dca6a83a3f849dce62fb639feecc8a9694338c0 [file] [log] [blame]
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef CUTTLEFISH_COMMON_COMMON_LIBS_AUTO_RESOURCES_AUTO_RESOURCES_H_
17#define CUTTLEFISH_COMMON_COMMON_LIBS_AUTO_RESOURCES_AUTO_RESOURCES_H_
18
19#include <stdarg.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <pthread.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26
27template <typename T, size_t N>
28char (&ArraySizeHelper(T (&array)[N]))[N];
29
30template <typename T, size_t N>
31char (&ArraySizeHelper(const T (&array)[N]))[N];
32
33#define arraysize(array) (sizeof(ArraySizeHelper(array)))
34
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070035// In C++11 this is just std::vector<char>, but Android isn't
36// there yet.
37class AutoFreeBuffer {
38 public:
39 enum {
40 // Minimum reserve size of AutoFreeBuffer to consider shrinking reservation.
41 // Any buffer shorter than this will not be shrunk.
42 kAutoBufferShrinkReserveThreshold = 8192
43 };
44
45 AutoFreeBuffer()
46 : data_(NULL), size_(0), reserve_size_(0) {}
47
48 AutoFreeBuffer(size_t reserve_size)
49 : data_(NULL), size_(0), reserve_size_(0) {
50 Reserve(reserve_size);
51 }
52
53 ~AutoFreeBuffer();
54 void Clear();
55 bool Resize(size_t newsize);
56 bool Reserve(size_t newsize);
57 bool SetToString(const char* in);
58 bool Append(const void* new_data, size_t new_data_size);
59 size_t PrintF(const char* format, ... );
60
61 char* data() {
62 return data_;
63 }
64
65 const char* data() const {
66 return data_;
67 }
68
69 char* begin() {
70 return data_;
71 }
72
73 const char* begin() const {
74 return data_;
75 }
76
77 char* end() {
78 return data_ + size_;
79 }
80
81 const char* end() const {
82 return data_ + size_;
83 }
84
85 size_t size() const {
86 return size_;
87 }
88
89 size_t reserve_size() const {
90 return reserve_size_;
91 }
92
93 void Swap(AutoFreeBuffer& other) {
94 char* temp_ptr = data_;
95 data_ = other.data_;
96 other.data_ = temp_ptr;
97
98 size_t temp_size = size_;
99 size_ = other.size_;
100 other.size_ = temp_size;
101
102 temp_size = reserve_size_;
103 reserve_size_ = other.reserve_size_;
104 other.reserve_size_ = temp_size;
105 }
106
107 bool operator==(const AutoFreeBuffer& other) const {
108 return (size_ == other.size_) && !memcmp(data_, other.data_, size_);
109 }
110
111 bool operator!=(const AutoFreeBuffer& other) const {
112 return !(*this == other);
113 }
114
115 protected:
116 char *data_;
117 size_t size_;
118 size_t reserve_size_;
119
120 private:
121 AutoFreeBuffer& operator=(const AutoFreeBuffer&);
122 explicit AutoFreeBuffer(const AutoFreeBuffer&);
123};
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700124#endif // CUTTLEFISH_COMMON_COMMON_LIBS_AUTO_RESOURCES_AUTO_RESOURCES_H_