xref: /aosp_15_r20/system/chre/util/include/chre/util/system/message_router_callback_allocator_impl.h (revision 84e339476a462649f82315436d70fd732297a399)
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_MESSAGE_ROUTER_CALLBACK_ALLOCATOR_IMPL_H_
18 #define CHRE_UTIL_SYSTEM_MESSAGE_ROUTER_CALLBACK_ALLOCATOR_IMPL_H_
19 
20 #include <pw_allocator/allocator.h>
21 #include <pw_allocator/capability.h>
22 #include <pw_allocator/unique_ptr.h>
23 #include <pw_containers/vector.h>
24 #include <pw_function/function.h>
25 #include <cstddef>
26 #include <optional>
27 
28 #include "chre/util/lock_guard.h"
29 #include "chre/util/system/message_common.h"
30 #include "chre/util/system/message_router_callback_allocator.h"
31 
32 namespace chre::message {
33 
34 template <typename Metadata>
MessageRouterCallbackAllocator(MessageFreeCallback && callback,pw::Vector<FreeCallbackRecord> & freeCallbackRecords)35 MessageRouterCallbackAllocator<Metadata>::MessageRouterCallbackAllocator(
36     MessageFreeCallback &&callback,
37     pw::Vector<FreeCallbackRecord> &freeCallbackRecords)
38         : pw::Allocator(kCapabilities),
39           mCallback(std::move(callback)),
40           mFreeCallbackRecords(freeCallbackRecords) {}
41 
42 template <typename Metadata>
DoAllocate(Layout)43 void *MessageRouterCallbackAllocator<Metadata>::DoAllocate(
44     Layout /* layout */) {
45   return nullptr;
46 }
47 
48 template <typename Metadata>
DoDeallocate(void * ptr)49 void MessageRouterCallbackAllocator<Metadata>::DoDeallocate(void *ptr) {
50   std::optional<FreeCallbackRecord> freeCallbackRecord;
51   {
52     LockGuard<Mutex> lock(mMutex);
53     for (FreeCallbackRecord &record : mFreeCallbackRecords) {
54       if (record.message == ptr) {
55         freeCallbackRecord = std::move(record);
56         mFreeCallbackRecords.erase(&record);
57         break;
58       }
59     }
60   }
61 
62   if (freeCallbackRecord.has_value()) {
63     mCallback(freeCallbackRecord->message, freeCallbackRecord->messageSize,
64               freeCallbackRecord->metadata);
65   }
66 }
67 
68 template <typename Metadata>
69 pw::UniquePtr<std::byte[]>
MakeUniqueArrayWithCallback(std::byte * ptr,size_t size,Metadata && metadata)70 MessageRouterCallbackAllocator<Metadata>::MakeUniqueArrayWithCallback(
71     std::byte *ptr, size_t size, Metadata &&metadata) {
72   {
73     LockGuard<Mutex> lock(mMutex);
74     if (mFreeCallbackRecords.full()) {
75       return pw::UniquePtr<std::byte[]>();
76     }
77 
78     mFreeCallbackRecords.push_back(
79         {.message = ptr, .metadata = std::move(metadata), .messageSize = size});
80   }
81 
82   return WrapUniqueArray(ptr, size);
83 }
84 
85 }  // namespace chre::message
86 
87 #endif  // CHRE_UTIL_SYSTEM_MESSAGE_ROUTER_CALLBACK_ALLOCATOR_IMPL_H_
88