1.1 --- a/memory.cc Sat Mar 27 01:42:25 2021 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,90 +0,0 @@
1.4 -#include "memory.h"
1.5 -
1.6 -#include <stdlib.h>
1.7 -
1.8 -
1.9 -/* Initialise the memory pool with an optional 'limit' in pages. */
1.10 -
1.11 -Memory::Memory(unsigned int limit, offset_t region_size)
1.12 -: _limit(limit), _region_size(region_size)
1.13 -{
1.14 - _limited = true;
1.15 -}
1.16 -
1.17 -Memory::Memory()
1.18 -: _region_size(PAGE_SIZE)
1.19 -{
1.20 - _limited = false;
1.21 -}
1.22 -
1.23 -/* Allocate a block of the given 'size'. */
1.24 -
1.25 -Region *Memory::allocate(offset_t size)
1.26 -{
1.27 - /* Attempt to allocate aligned memory. */
1.28 -
1.29 - void *current;
1.30 -
1.31 - /* Make the size appropriate for the invocation. */
1.32 -
1.33 - size = round_multiple(size, PAGE_SIZE);
1.34 -
1.35 - if (posix_memalign(¤t, size, size))
1.36 - return NULL;
1.37 -
1.38 - return new Region((offset_t) current, (offset_t) current + size);
1.39 -}
1.40 -
1.41 -/* Allocate a new region of the given 'size' rounded to the nearest page. */
1.42 -
1.43 -Region *Memory::region(offset_t size)
1.44 -{
1.45 - std::lock_guard<std::mutex> guard(_lock);
1.46 -
1.47 - offset_t rounded = round(size, PAGE_SIZE);
1.48 - offset_t pages = rounded / PAGE_SIZE;
1.49 -
1.50 - /* Check for sufficient pages. */
1.51 -
1.52 - if (!_limited || (_limit >= pages))
1.53 - {
1.54 - /* Attempt to allocate aligned memory. */
1.55 -
1.56 - Region *region = allocate(rounded);
1.57 -
1.58 - if (region == NULL)
1.59 - return NULL;
1.60 -
1.61 - if (_limited)
1.62 - _limit -= pages;
1.63 -
1.64 - return region;
1.65 - }
1.66 -
1.67 - /* Return no region without sufficient pages. */
1.68 -
1.69 - else
1.70 - return NULL;
1.71 -}
1.72 -
1.73 -Region *Memory::region()
1.74 -{
1.75 - return region(_region_size);
1.76 -}
1.77 -
1.78 -/* Release the allocated 'region'. */
1.79 -
1.80 -void Memory::release(Region *region)
1.81 -{
1.82 - std::lock_guard<std::mutex> guard(_lock);
1.83 -
1.84 - offset_t rounded = round(region->size(), PAGE_SIZE);
1.85 - offset_t pages = rounded / PAGE_SIZE;
1.86 -
1.87 - if (_limited)
1.88 - _limit += pages;
1.89 -
1.90 - delete region;
1.91 -}
1.92 -
1.93 -// vim: tabstop=4 expandtab shiftwidth=4