xref: /aosp_15_r20/system/chre/util/include/chre/util/buffer_base.h (revision 84e339476a462649f82315436d70fd732297a399)
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:
BufferBase()28   BufferBase() : mBuffer(nullptr), mSize(0), mBufferRequiresFree(false) {}
29 
30   /**
31    * Cleans up for the buffer. If the buffer is currently owned by this object,
32    * it is released.
33    */
34   ~BufferBase();
35 
36   //! The buffer to manage.
37   void *mBuffer;
38 
39   //! The number of elements in the buffer.
40   size_t mSize : 31;
41 
42   //! Set to true when mBuffer needs to be released by the destructor.
43   bool mBufferRequiresFree : 1;
44 
45   /**
46    * @see Buffer::wrap.
47    */
48   void wrap(void *buffer, size_t size);
49 
50   /**
51    * @see Buffer::copy_array.
52    *
53    * @param elementSize The size of each element. This is multiplied by size
54    *        to determine the effective size of the buffer to copy.
55    */
56   bool copy_array(const void *buffer, size_t size, size_t elementSize);
57 
58  private:
59   /**
60    * Cleans up the buffer so a new one can be wrapped or copied. If the current
61    * buffer is owned by this object, it is released.
62    */
63   void reset();
64 };
65 
66 }  // namespace chre
67 
68 #endif  // CHRE_UTIL_BUFFER_BASE_H_
69