1 /* 2 * A memory pool allocating a region at a time from the system. 3 * 4 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "memory_incremental.h" 23 24 #include <ipc/mem_ipc.h> 25 26 #include <stdlib.h> 27 28 29 30 /* Initialise the memory pool with an optional 'limit' in pages. */ 31 32 MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size) 33 : _limit(limit), _region_size(region_size) 34 { 35 _limited = true; 36 } 37 38 MemoryIncremental::MemoryIncremental() 39 : _region_size(PAGE_SIZE) 40 { 41 _limited = false; 42 } 43 44 /* Allocate a block of the given 'size'. */ 45 46 Region *MemoryIncremental::allocate(offset_t size) 47 { 48 /* Attempt to allocate aligned memory. */ 49 50 void *current = NULL; 51 l4re_ds_t ds; 52 53 /* Make the size appropriate for the invocation. */ 54 55 size = round_multiple(size, PAGE_SIZE); 56 57 /* Use allocation permitting executable mapping of the memory. 58 NOTE: Here, it might be beneficial to employ an allocator that obtains 59 dataspaces and provides multiple blocks from each dataspace. */ 60 61 if (ipc_allocate_align(size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RWX, page_order(size), ¤t, &ds)) 62 return NULL; 63 64 return new Region((offset_t) current, (offset_t) current + size); 65 } 66 67 /* Deallocate the given region. */ 68 69 void MemoryIncremental::deallocate(Region *region) 70 { 71 ipc_detach_dataspace((void *) region->start); 72 delete region; 73 } 74 75 /* Allocate a new region of the given 'size' rounded to the nearest page. */ 76 77 Region *MemoryIncremental::region(offset_t size) 78 { 79 std::lock_guard<std::mutex> guard(_lock); 80 81 offset_t rounded = round(size, PAGE_SIZE); 82 offset_t pages = rounded / PAGE_SIZE; 83 84 /* Check for sufficient pages. */ 85 86 if (!_limited || (_limit >= pages)) 87 { 88 /* Attempt to allocate aligned memory. */ 89 90 Region *region = allocate(rounded); 91 92 if (region == NULL) 93 return NULL; 94 95 if (_limited) 96 _limit -= pages; 97 98 return region; 99 } 100 101 /* Return no region without sufficient pages. */ 102 103 else 104 return NULL; 105 } 106 107 /* Allocate a new region having the default region size rounded to the nearest 108 page. */ 109 110 Region *MemoryIncremental::region() 111 { 112 return region(_region_size); 113 } 114 115 /* Release the allocated 'region'. */ 116 117 void MemoryIncremental::release(Region *region) 118 { 119 std::lock_guard<std::mutex> guard(_lock); 120 121 offset_t rounded = round(region->size(), PAGE_SIZE); 122 offset_t pages = rounded / PAGE_SIZE; 123 124 if (_limited) 125 _limit += pages; 126 127 deallocate(region); 128 } 129 130 // vim: tabstop=4 expandtab shiftwidth=4