paul@93 | 1 | /* |
paul@93 | 2 | * A memory pool providing regions from a preallocated amount of memory. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@72 | 22 | #pragma once |
paul@72 | 23 | |
paul@72 | 24 | #include <systypes/base.h> |
paul@72 | 25 | |
paul@72 | 26 | #include <list> |
paul@72 | 27 | #include <mutex> |
paul@72 | 28 | |
paul@94 | 29 | #include <mem/memory.h> |
paul@72 | 30 | |
paul@72 | 31 | |
paul@72 | 32 | |
paul@72 | 33 | /* A memory pool abstraction. */ |
paul@72 | 34 | |
paul@72 | 35 | class MemoryPreallocated : public Memory |
paul@72 | 36 | { |
paul@72 | 37 | protected: |
paul@72 | 38 | std::mutex _lock; |
paul@72 | 39 | std::list<Region *> _regions; |
paul@72 | 40 | |
paul@72 | 41 | Memory *_memory; |
paul@72 | 42 | offset_t _amount; |
paul@72 | 43 | |
paul@72 | 44 | public: |
paul@72 | 45 | explicit MemoryPreallocated(Memory *memory, offset_t amount); |
paul@72 | 46 | |
paul@72 | 47 | explicit MemoryPreallocated(); |
paul@72 | 48 | |
paul@73 | 49 | virtual ~MemoryPreallocated(); |
paul@73 | 50 | |
paul@72 | 51 | virtual Region *region(); |
paul@72 | 52 | |
paul@72 | 53 | virtual offset_t region_size() |
paul@72 | 54 | { return _memory->region_size(); } |
paul@72 | 55 | |
paul@72 | 56 | virtual void release(Region *region); |
paul@72 | 57 | }; |
paul@72 | 58 | |
paul@72 | 59 | // vim: tabstop=4 expandtab shiftwidth=4 |