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 | #include "memory_preallocated.h" |
paul@72 | 23 | |
paul@72 | 24 | |
paul@72 | 25 | |
paul@72 | 26 | /* Initialise the memory pool with the given amount in bytes. */ |
paul@72 | 27 | |
paul@72 | 28 | MemoryPreallocated::MemoryPreallocated(Memory *memory, offset_t amount) |
paul@72 | 29 | : _memory(memory), _amount(amount) |
paul@72 | 30 | { |
paul@72 | 31 | // NOTE: Handle allocation failure. |
paul@72 | 32 | |
paul@72 | 33 | offset_t remaining = amount; |
paul@72 | 34 | |
paul@72 | 35 | while (remaining) |
paul@72 | 36 | { |
paul@72 | 37 | _regions.push_back(memory->region()); |
paul@72 | 38 | |
paul@72 | 39 | if (memory->region_size() >= remaining) |
paul@72 | 40 | break; |
paul@72 | 41 | |
paul@72 | 42 | remaining -= memory->region_size(); |
paul@72 | 43 | } |
paul@72 | 44 | } |
paul@72 | 45 | |
paul@73 | 46 | /* Discard all unused regions. */ |
paul@73 | 47 | |
paul@73 | 48 | MemoryPreallocated::~MemoryPreallocated() |
paul@73 | 49 | { |
paul@73 | 50 | while (!_regions.empty()) |
paul@73 | 51 | { |
paul@73 | 52 | Region *region = _regions.front(); |
paul@73 | 53 | |
paul@73 | 54 | _regions.pop_front(); |
paul@73 | 55 | _memory->release(region); |
paul@73 | 56 | } |
paul@73 | 57 | } |
paul@73 | 58 | |
paul@72 | 59 | /* Obtain an allocated region. */ |
paul@72 | 60 | |
paul@72 | 61 | Region *MemoryPreallocated::region() |
paul@72 | 62 | { |
paul@72 | 63 | std::lock_guard<std::mutex> guard(_lock); |
paul@72 | 64 | |
paul@72 | 65 | Region *region = _regions.front(); |
paul@72 | 66 | |
paul@72 | 67 | _regions.pop_front(); |
paul@72 | 68 | return region; |
paul@72 | 69 | } |
paul@72 | 70 | |
paul@72 | 71 | /* Release the allocated 'region'. */ |
paul@72 | 72 | |
paul@72 | 73 | void MemoryPreallocated::release(Region *region) |
paul@72 | 74 | { |
paul@72 | 75 | std::lock_guard<std::mutex> guard(_lock); |
paul@72 | 76 | |
paul@72 | 77 | _regions.push_back(region); |
paul@72 | 78 | } |
paul@72 | 79 | |
paul@72 | 80 | // vim: tabstop=4 expandtab shiftwidth=4 |