paul@93 | 1 | /* |
paul@93 | 2 | * A memory pool allocating a region at a time from the system. |
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_incremental.h" |
paul@1 | 23 | |
paul@14 | 24 | #include <stdlib.h> |
paul@1 | 25 | |
paul@1 | 26 | /* Initialise the memory pool with an optional 'limit' in pages. */ |
paul@1 | 27 | |
paul@72 | 28 | MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size) |
paul@1 | 29 | : _limit(limit), _region_size(region_size) |
paul@1 | 30 | { |
paul@1 | 31 | _limited = true; |
paul@1 | 32 | } |
paul@1 | 33 | |
paul@72 | 34 | MemoryIncremental::MemoryIncremental() |
paul@1 | 35 | : _region_size(PAGE_SIZE) |
paul@1 | 36 | { |
paul@1 | 37 | _limited = false; |
paul@1 | 38 | } |
paul@1 | 39 | |
paul@14 | 40 | /* Allocate a block of the given 'size'. */ |
paul@14 | 41 | |
paul@72 | 42 | Region *MemoryIncremental::allocate(offset_t size) |
paul@14 | 43 | { |
paul@14 | 44 | /* Attempt to allocate aligned memory. */ |
paul@14 | 45 | |
paul@14 | 46 | void *current; |
paul@14 | 47 | |
paul@14 | 48 | /* Make the size appropriate for the invocation. */ |
paul@14 | 49 | |
paul@14 | 50 | size = round_multiple(size, PAGE_SIZE); |
paul@14 | 51 | |
paul@14 | 52 | if (posix_memalign(¤t, size, size)) |
paul@14 | 53 | return NULL; |
paul@14 | 54 | |
paul@14 | 55 | return new Region((offset_t) current, (offset_t) current + size); |
paul@14 | 56 | } |
paul@14 | 57 | |
paul@1 | 58 | /* Allocate a new region of the given 'size' rounded to the nearest page. */ |
paul@1 | 59 | |
paul@72 | 60 | Region *MemoryIncremental::region(offset_t size) |
paul@1 | 61 | { |
paul@1 | 62 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 63 | |
paul@1 | 64 | offset_t rounded = round(size, PAGE_SIZE); |
paul@1 | 65 | offset_t pages = rounded / PAGE_SIZE; |
paul@1 | 66 | |
paul@1 | 67 | /* Check for sufficient pages. */ |
paul@1 | 68 | |
paul@1 | 69 | if (!_limited || (_limit >= pages)) |
paul@1 | 70 | { |
paul@1 | 71 | /* Attempt to allocate aligned memory. */ |
paul@1 | 72 | |
paul@14 | 73 | Region *region = allocate(rounded); |
paul@1 | 74 | |
paul@1 | 75 | if (region == NULL) |
paul@1 | 76 | return NULL; |
paul@1 | 77 | |
paul@1 | 78 | if (_limited) |
paul@1 | 79 | _limit -= pages; |
paul@1 | 80 | |
paul@1 | 81 | return region; |
paul@1 | 82 | } |
paul@1 | 83 | |
paul@1 | 84 | /* Return no region without sufficient pages. */ |
paul@1 | 85 | |
paul@1 | 86 | else |
paul@1 | 87 | return NULL; |
paul@1 | 88 | } |
paul@1 | 89 | |
paul@72 | 90 | Region *MemoryIncremental::region() |
paul@1 | 91 | { |
paul@1 | 92 | return region(_region_size); |
paul@1 | 93 | } |
paul@1 | 94 | |
paul@1 | 95 | /* Release the allocated 'region'. */ |
paul@1 | 96 | |
paul@72 | 97 | void MemoryIncremental::release(Region *region) |
paul@1 | 98 | { |
paul@1 | 99 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 100 | |
paul@1 | 101 | offset_t rounded = round(region->size(), PAGE_SIZE); |
paul@1 | 102 | offset_t pages = rounded / PAGE_SIZE; |
paul@1 | 103 | |
paul@1 | 104 | if (_limited) |
paul@1 | 105 | _limit += pages; |
paul@1 | 106 | |
paul@1 | 107 | delete region; |
paul@1 | 108 | } |
paul@1 | 109 | |
paul@1 | 110 | // vim: tabstop=4 expandtab shiftwidth=4 |