1 /*
2  * Copyright (C) 2018 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 
17 #ifndef CHRE_UTIL_BUFFER_BASE_H_
18 #define CHRE_UTIL_BUFFER_BASE_H_
19 
20 #include <cstddef>
21 
22 #include "chre/util/non_copyable.h"
23 
24 namespace chre {
25 
26 class BufferBase : public NonCopyable {
27  protected:
28   /**
29    * Cleans up for the buffer. If the buffer is currently owned by this object,
30    * it is released.
31    */
32   ~BufferBase();
33 
34   //! The buffer to manage.
35   void *mBuffer = nullptr;
36 
37   //! The number of elements in the buffer.
38   size_t mSize = 0;
39 
40   //! Set to true when mBuffer needs to be released by the destructor.
41   bool mBufferRequiresFree = false;
42 
43   /**
44    * @see Buffer::wrap.
45    */
46   void wrap(void *buffer, size_t size);
47 
48   /**
49    * @see Buffer::copy_array.
50    *
51    * @param elementSize The size of each element. This is multiplied by size
52    *        to determine the effective size of the buffer to copy.
53    */
54   bool copy_array(const void *buffer, size_t size, size_t elementSize);
55 
56  private:
57   /**
58    * Cleans up the buffer so a new one can be wrapped or copied. If the current
59    * buffer is owned by this object, it is released.
60    */
61   void reset();
62 };
63 
64 }  // namespace chre
65 
66 #endif  // CHRE_UTIL_BUFFER_BASE_H_
67