paul@72 | 1 | #include "memory_incremental.h" |
paul@1 | 2 | |
paul@14 | 3 | #include <stdlib.h> |
paul@1 | 4 | |
paul@1 | 5 | |
paul@1 | 6 | /* Initialise the memory pool with an optional 'limit' in pages. */ |
paul@1 | 7 | |
paul@91 | 8 | MemoryIncremental::MemoryIncremental(unsigned int limit, length_t region_size) |
paul@1 | 9 | : _limit(limit), _region_size(region_size) |
paul@1 | 10 | { |
paul@1 | 11 | _limited = true; |
paul@1 | 12 | } |
paul@1 | 13 | |
paul@72 | 14 | MemoryIncremental::MemoryIncremental() |
paul@1 | 15 | : _region_size(PAGE_SIZE) |
paul@1 | 16 | { |
paul@1 | 17 | _limited = false; |
paul@1 | 18 | } |
paul@1 | 19 | |
paul@14 | 20 | /* Allocate a block of the given 'size'. */ |
paul@14 | 21 | |
paul@91 | 22 | Region *MemoryIncremental::allocate(length_t size) |
paul@14 | 23 | { |
paul@14 | 24 | /* Attempt to allocate aligned memory. */ |
paul@14 | 25 | |
paul@14 | 26 | void *current; |
paul@14 | 27 | |
paul@14 | 28 | /* Make the size appropriate for the invocation. */ |
paul@14 | 29 | |
paul@14 | 30 | size = round_multiple(size, PAGE_SIZE); |
paul@14 | 31 | |
paul@14 | 32 | if (posix_memalign(¤t, size, size)) |
paul@14 | 33 | return NULL; |
paul@14 | 34 | |
paul@91 | 35 | return new Region((address_t) current, (address_t) current + size); |
paul@14 | 36 | } |
paul@14 | 37 | |
paul@1 | 38 | /* Allocate a new region of the given 'size' rounded to the nearest page. */ |
paul@1 | 39 | |
paul@91 | 40 | Region *MemoryIncremental::region(length_t size) |
paul@1 | 41 | { |
paul@1 | 42 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 43 | |
paul@91 | 44 | length_t rounded = round(size, PAGE_SIZE); |
paul@91 | 45 | length_t pages = rounded / PAGE_SIZE; |
paul@1 | 46 | |
paul@1 | 47 | /* Check for sufficient pages. */ |
paul@1 | 48 | |
paul@1 | 49 | if (!_limited || (_limit >= pages)) |
paul@1 | 50 | { |
paul@1 | 51 | /* Attempt to allocate aligned memory. */ |
paul@1 | 52 | |
paul@14 | 53 | Region *region = allocate(rounded); |
paul@1 | 54 | |
paul@1 | 55 | if (region == NULL) |
paul@1 | 56 | return NULL; |
paul@1 | 57 | |
paul@1 | 58 | if (_limited) |
paul@1 | 59 | _limit -= pages; |
paul@1 | 60 | |
paul@1 | 61 | return region; |
paul@1 | 62 | } |
paul@1 | 63 | |
paul@1 | 64 | /* Return no region without sufficient pages. */ |
paul@1 | 65 | |
paul@1 | 66 | else |
paul@1 | 67 | return NULL; |
paul@1 | 68 | } |
paul@1 | 69 | |
paul@72 | 70 | Region *MemoryIncremental::region() |
paul@1 | 71 | { |
paul@1 | 72 | return region(_region_size); |
paul@1 | 73 | } |
paul@1 | 74 | |
paul@1 | 75 | /* Release the allocated 'region'. */ |
paul@1 | 76 | |
paul@72 | 77 | void MemoryIncremental::release(Region *region) |
paul@1 | 78 | { |
paul@1 | 79 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 80 | |
paul@91 | 81 | length_t rounded = round(region->size(), PAGE_SIZE); |
paul@91 | 82 | length_t pages = rounded / PAGE_SIZE; |
paul@1 | 83 | |
paul@1 | 84 | if (_limited) |
paul@1 | 85 | _limit += pages; |
paul@1 | 86 | |
paul@1 | 87 | delete region; |
paul@1 | 88 | } |
paul@1 | 89 | |
paul@1 | 90 | // vim: tabstop=4 expandtab shiftwidth=4 |