xref: /aosp_15_r20/system/chre/util/include/chre/util/system/message_router_callback_allocator.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_H_
18 #define CHRE_UTIL_SYSTEM_MESSAGE_ROUTER_CALLBACK_ALLOCATOR_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/platform/mutex.h"
29 
30 namespace chre::message {
31 
32 //! An allocator for message free callbacks
33 //! This allocator is used to store message free callbacks in a vector
34 //! The allocator will call the free callback when the message is deallocated
35 //! This is used to create pw::UniquePtrs for messages that have a free
36 //! callback.
37 //! @param Metadata The metadata type for the callback function
38 template <typename Metadata>
39 class MessageRouterCallbackAllocator : public pw::Allocator {
40  public:
41   static constexpr Capabilities kCapabilities = 0;
42 
43   //! The callback used to free a message
44   using MessageFreeCallback = pw::Function<void(
45       std::byte *message, size_t length, const Metadata &metadata)>;
46 
47   //! A record of a message and its free callback
48   struct FreeCallbackRecord {
49     std::byte *message;
50     Metadata metadata;
51     size_t messageSize;
52   };
53 
54   MessageRouterCallbackAllocator(
55       MessageFreeCallback &&callback,
56       pw::Vector<FreeCallbackRecord> &freeCallbackRecords);
57 
58   //! @see pw::Allocator::DoAllocate
59   virtual void *DoAllocate(Layout /* layout */) override;
60 
61   //! @see pw::Allocator::DoDeallocate
62   virtual void DoDeallocate(void *ptr) override;
63 
64   //! Creates a pw::UniquePtr for a message with a free callback.
65   //! The free callback will be called when the message is deallocated.
66   //! @return A pw::UniquePtr containing the message
67   [[nodiscard]] pw::UniquePtr<std::byte[]> MakeUniqueArrayWithCallback(
68       std::byte *ptr, size_t size, Metadata &&metadata);
69 
70  private:
71   //! The callback used to free a message
72   MessageFreeCallback mCallback;
73 
74   //! The mutex to protect mFreeCallbackRecords
75   Mutex mMutex;
76 
77   //! The map of message pointers to free callbacks
78   pw::Vector<FreeCallbackRecord> &mFreeCallbackRecords;
79 };
80 
81 }  // namespace chre::message
82 
83 #include "chre/util/system/message_router_callback_allocator_impl.h"
84 
85 #endif  // CHRE_UTIL_SYSTEM_MESSAGE_ROUTER_CALLBACK_ALLOCATOR_H_
86