1 // Copyright 2024 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstddef> 17 #include <cstdint> 18 19 #include "pw_allocator/capability.h" 20 #include "pw_allocator/deallocator.h" 21 #include "pw_allocator/layout.h" 22 #include "pw_bytes/span.h" 23 #include "pw_result/result.h" 24 25 namespace pw::allocator { 26 27 /// Abstract interface for fixed-layout memory allocation. 28 /// 29 /// The interface makes no guarantees about its implementation. Consumers of the 30 /// generic interface must not make any assumptions around allocator behavior, 31 /// thread safety, or performance. 32 class Pool : public Deallocator { 33 public: Pool(const Capabilities & capabilities,const Layout & layout)34 constexpr Pool(const Capabilities& capabilities, const Layout& layout) 35 : Deallocator(capabilities), layout_(layout) {} 36 layout()37 const Layout& layout() const { return layout_; } 38 39 /// Returns a chunk of memory with this object's fixed layout. 40 /// 41 /// Like `pw::allocator::Allocate`, returns null if memory is exhausted. 42 /// 43 /// @retval The allocated memory. Allocate()44 void* Allocate() { return DoAllocate(); } 45 46 private: 47 /// Virtual `Allocate` function that can be overridden by derived classes. 48 virtual void* DoAllocate() = 0; 49 50 const Layout layout_; 51 }; 52 53 } // namespace pw::allocator 54