• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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_SYSTEM_CALLBACK_ALLOCATOR_IMPL_H_
18 #define CHRE_UTIL_SYSTEM_CALLBACK_ALLOCATOR_IMPL_H_
19 
20 #include <cstddef>
21 #include <optional>
22 
23 #include "chre/util/lock_guard.h"
24 #include "chre/util/system/callback_allocator.h"
25 
26 #include "pw_allocator/allocator.h"
27 #include "pw_allocator/capability.h"
28 #include "pw_allocator/unique_ptr.h"
29 #include "pw_containers/vector.h"
30 #include "pw_function/function.h"
31 
32 namespace chre {
33 
34 template <typename Metadata>
CallbackAllocator(Callback && callback,pw::Vector<CallbackRecord> & callbackRecords,bool doEraseRecord)35 CallbackAllocator<Metadata>::CallbackAllocator(
36     Callback &&callback, pw::Vector<CallbackRecord> &callbackRecords,
37     bool doEraseRecord)
38     : pw::Allocator(kCapabilities),
39       mCallback(std::move(callback)),
40       mCallbackRecords(callbackRecords),
41       mDoEraseRecord(doEraseRecord) {}
42 
43 template <typename Metadata>
DoAllocate(Layout)44 void *CallbackAllocator<Metadata>::DoAllocate(Layout /* layout */) {
45   // Do not allow usage of this allocator without providing a callback
46   // function. This allocator does not manage the memory, only guarantees
47   // that the callback will be called. Use MakeUniqueArrayWithCallback.
48   return nullptr;
49 }
50 
51 template <typename Metadata>
DoDeallocate(void * ptr)52 void CallbackAllocator<Metadata>::DoDeallocate(void *ptr) {
53   std::optional<CallbackRecord> callbackRecord;
54   {
55     LockGuard<Mutex> lock(mMutex);
56     for (CallbackRecord &record : mCallbackRecords) {
57       if (record.message == ptr) {
58         if (mDoEraseRecord) {
59           callbackRecord = std::move(record);
60           mCallbackRecords.erase(&record);
61         } else {
62           callbackRecord = record;
63         }
64         break;
65       }
66     }
67   }
68 
69   if (callbackRecord.has_value()) {
70     mCallback(callbackRecord->message, callbackRecord->messageSize,
71               std::move(callbackRecord->metadata));
72   }
73 }
74 
75 template <typename Metadata>
76 pw::UniquePtr<std::byte[]>
MakeUniqueArrayWithCallback(std::byte * ptr,size_t size,Metadata && metadata)77 CallbackAllocator<Metadata>::MakeUniqueArrayWithCallback(std::byte *ptr,
78                                                          size_t size,
79                                                          Metadata &&metadata) {
80   {
81     LockGuard<Mutex> lock(mMutex);
82     if (mCallbackRecords.full()) {
83       return pw::UniquePtr<std::byte[]>();
84     }
85 
86     mCallbackRecords.push_back(
87         {.message = ptr, .metadata = std::move(metadata), .messageSize = size});
88   }
89 
90   return WrapUnique<std::byte[]>(ptr, size);
91 }
92 
93 template <typename Metadata>
94 std::optional<typename CallbackAllocator<Metadata>::CallbackRecord>
GetAndRemoveCallbackRecord(void * ptr)95 CallbackAllocator<Metadata>::GetAndRemoveCallbackRecord(void *ptr) {
96   LockGuard<Mutex> lock(mMutex);
97   std::optional<CallbackRecord> foundRecord;
98   for (CallbackRecord &record : mCallbackRecords) {
99     if (record.message == ptr) {
100       foundRecord = std::move(record);
101       mCallbackRecords.erase(&record);
102       break;
103     }
104   }
105   return foundRecord;
106 }
107 
108 }  // namespace chre
109 
110 #endif  // CHRE_UTIL_SYSTEM_CALLBACK_ALLOCATOR_IMPL_H_
111