1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libmem/lib/src/memory_incremental.cc Thu Apr 15 23:15:17 2021 +0200
1.3 @@ -0,0 +1,110 @@
1.4 +/*
1.5 + * A memory pool allocating a region at a time from the system.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include "memory_incremental.h"
1.26 +
1.27 +#include <stdlib.h>
1.28 +
1.29 +/* Initialise the memory pool with an optional 'limit' in pages. */
1.30 +
1.31 +MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size)
1.32 +: _limit(limit), _region_size(region_size)
1.33 +{
1.34 + _limited = true;
1.35 +}
1.36 +
1.37 +MemoryIncremental::MemoryIncremental()
1.38 +: _region_size(PAGE_SIZE)
1.39 +{
1.40 + _limited = false;
1.41 +}
1.42 +
1.43 +/* Allocate a block of the given 'size'. */
1.44 +
1.45 +Region *MemoryIncremental::allocate(offset_t size)
1.46 +{
1.47 + /* Attempt to allocate aligned memory. */
1.48 +
1.49 + void *current;
1.50 +
1.51 + /* Make the size appropriate for the invocation. */
1.52 +
1.53 + size = round_multiple(size, PAGE_SIZE);
1.54 +
1.55 + if (posix_memalign(¤t, size, size))
1.56 + return NULL;
1.57 +
1.58 + return new Region((offset_t) current, (offset_t) current + size);
1.59 +}
1.60 +
1.61 +/* Allocate a new region of the given 'size' rounded to the nearest page. */
1.62 +
1.63 +Region *MemoryIncremental::region(offset_t size)
1.64 +{
1.65 + std::lock_guard<std::mutex> guard(_lock);
1.66 +
1.67 + offset_t rounded = round(size, PAGE_SIZE);
1.68 + offset_t pages = rounded / PAGE_SIZE;
1.69 +
1.70 + /* Check for sufficient pages. */
1.71 +
1.72 + if (!_limited || (_limit >= pages))
1.73 + {
1.74 + /* Attempt to allocate aligned memory. */
1.75 +
1.76 + Region *region = allocate(rounded);
1.77 +
1.78 + if (region == NULL)
1.79 + return NULL;
1.80 +
1.81 + if (_limited)
1.82 + _limit -= pages;
1.83 +
1.84 + return region;
1.85 + }
1.86 +
1.87 + /* Return no region without sufficient pages. */
1.88 +
1.89 + else
1.90 + return NULL;
1.91 +}
1.92 +
1.93 +Region *MemoryIncremental::region()
1.94 +{
1.95 + return region(_region_size);
1.96 +}
1.97 +
1.98 +/* Release the allocated 'region'. */
1.99 +
1.100 +void MemoryIncremental::release(Region *region)
1.101 +{
1.102 + std::lock_guard<std::mutex> guard(_lock);
1.103 +
1.104 + offset_t rounded = round(region->size(), PAGE_SIZE);
1.105 + offset_t pages = rounded / PAGE_SIZE;
1.106 +
1.107 + if (_limited)
1.108 + _limit += pages;
1.109 +
1.110 + delete region;
1.111 +}
1.112 +
1.113 +// vim: tabstop=4 expandtab shiftwidth=4