paul@93 | 1 | /* |
paul@93 | 2 | * A memory pool allocating a region at a time from the system. |
paul@93 | 3 | * |
paul@623 | 4 | * Copyright (C) 2021, 2022, 2024 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@1 | 22 | #pragma once |
paul@1 | 23 | |
paul@30 | 24 | #include <systypes/base.h> |
paul@30 | 25 | |
paul@1 | 26 | #include <mutex> |
paul@1 | 27 | |
paul@94 | 28 | #include <mem/memory.h> |
paul@94 | 29 | #include <mem/memory_utils.h> |
paul@1 | 30 | |
paul@1 | 31 | |
paul@1 | 32 | |
paul@1 | 33 | /* A memory pool abstraction. */ |
paul@1 | 34 | |
paul@72 | 35 | class MemoryIncremental : public Memory |
paul@1 | 36 | { |
paul@1 | 37 | protected: |
paul@1 | 38 | std::mutex _lock; |
paul@1 | 39 | |
paul@1 | 40 | unsigned int _limit; |
paul@1 | 41 | offset_t _region_size; |
paul@623 | 42 | l4_cap_idx_t _dma; |
paul@1 | 43 | bool _limited; |
paul@1 | 44 | |
paul@63 | 45 | Region *allocate(offset_t size); |
paul@63 | 46 | |
paul@331 | 47 | void deallocate(Region *region); |
paul@331 | 48 | |
paul@1 | 49 | public: |
paul@623 | 50 | explicit MemoryIncremental(unsigned int limit, |
paul@623 | 51 | offset_t region_size=PAGE_SIZE, |
paul@623 | 52 | l4_cap_idx_t dma=L4_INVALID_CAP); |
paul@72 | 53 | |
paul@623 | 54 | explicit MemoryIncremental(l4_cap_idx_t dma=L4_INVALID_CAP); |
paul@1 | 55 | |
paul@72 | 56 | virtual Region *region(offset_t size); |
paul@1 | 57 | |
paul@72 | 58 | virtual Region *region(); |
paul@72 | 59 | |
paul@72 | 60 | virtual offset_t region_size() |
paul@63 | 61 | { return _region_size; } |
paul@14 | 62 | |
paul@72 | 63 | virtual void release(Region *region); |
paul@1 | 64 | }; |
paul@1 | 65 | |
paul@1 | 66 | // vim: tabstop=4 expandtab shiftwidth=4 |