1 // Copyright 2023 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 15 #pragma once 16 #include "pw_bluetooth_sapphire/internal/host/common/assert.h" 17 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h" 18 19 namespace bt { 20 21 template <size_t BackingBufferSize> 22 class SlabBuffer : public MutableByteBuffer { 23 public: SlabBuffer(size_t size)24 explicit SlabBuffer(size_t size) : size_(size) { 25 PW_CHECK(size); 26 PW_CHECK(size_ <= buffer_.size()); 27 } 28 29 // ByteBuffer overrides: data()30 const uint8_t* data() const override { return buffer_.data(); } size()31 size_t size() const override { return size_; } cbegin()32 const_iterator cbegin() const override { return buffer_.cbegin(); } cend()33 const_iterator cend() const override { return cbegin() + size_; } 34 35 // MutableByteBuffer overrides: mutable_data()36 uint8_t* mutable_data() override { return buffer_.mutable_data(); } Fill(uint8_t value)37 void Fill(uint8_t value) override { 38 buffer_.mutable_view(0, size_).Fill(value); 39 } 40 41 private: 42 size_t size_; 43 44 // The backing backing buffer can have a different size from what was 45 // requested. 46 StaticByteBuffer<BackingBufferSize> buffer_; 47 48 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(SlabBuffer); 49 }; 50 51 } // namespace bt 52