1.1 --- a/Makefile Sat Mar 20 01:40:39 2021 +0100
1.2 +++ b/Makefile Sat Mar 20 01:41:43 2021 +0100
1.3 @@ -48,7 +48,8 @@
1.4
1.5 PLAIN_SRC_CC_common_server = \
1.6 access_map.cc accessing.cc accessor.cc \
1.7 - flexpage.cc file_pager.cc ipc.cc memory.cc \
1.8 + flexpage.cc file_pager.cc ipc.cc \
1.9 + memory.cc memory_incremental.cc memory_preallocated.cc \
1.10 opener_resource.cc opener_context_resource.cc \
1.11 page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.12 region.cc resource_server.cc simple_pager.cc
2.1 --- a/dstest_block_server.cc Sat Mar 20 01:40:39 2021 +0100
2.2 +++ b/dstest_block_server.cc Sat Mar 20 01:41:43 2021 +0100
2.3 @@ -27,7 +27,7 @@
2.4 #include <stdlib.h>
2.5
2.6 #include "accessing.h"
2.7 -#include "memory.h"
2.8 +#include "memory_incremental.h"
2.9 #include "memory_utils.h"
2.10 #include "pages.h"
2.11 #include "paging.h"
2.12 @@ -42,7 +42,7 @@
2.13 {
2.14 /* Some memory plus infrastructure. */
2.15
2.16 - Memory mem(MEMORY_PAGES);
2.17 + MemoryIncremental mem(MEMORY_PAGES);
2.18 Accessing accessing;
2.19 Paging paging;
2.20 Pages pages(&mem);
3.1 --- a/dstest_host_server.cc Sat Mar 20 01:40:39 2021 +0100
3.2 +++ b/dstest_host_server.cc Sat Mar 20 01:41:43 2021 +0100
3.3 @@ -27,7 +27,7 @@
3.4 #include <stdlib.h>
3.5
3.6 #include "accessing.h"
3.7 -#include "memory.h"
3.8 +#include "memory_incremental.h"
3.9 #include "memory_utils.h"
3.10 #include "pages.h"
3.11 #include "paging.h"
3.12 @@ -42,7 +42,7 @@
3.13 {
3.14 /* Some memory plus infrastructure. */
3.15
3.16 - Memory mem(MEMORY_PAGES);
3.17 + MemoryIncremental mem(MEMORY_PAGES);
3.18 Accessing accessing;
3.19 Paging paging;
3.20 Pages pages(&mem);
4.1 --- a/dstest_test_server.cc Sat Mar 20 01:40:39 2021 +0100
4.2 +++ b/dstest_test_server.cc Sat Mar 20 01:41:43 2021 +0100
4.3 @@ -29,7 +29,7 @@
4.4 #include <stdlib.h>
4.5
4.6 #include "accessing.h"
4.7 -#include "memory.h"
4.8 +#include "memory_incremental.h"
4.9 #include "memory_utils.h"
4.10 #include "pages.h"
4.11 #include "paging.h"
4.12 @@ -65,7 +65,7 @@
4.13
4.14 /* Some memory plus infrastructure. */
4.15
4.16 - Memory mem(memory_pages, page(REGION_PAGES));
4.17 + MemoryIncremental mem(memory_pages, page(REGION_PAGES));
4.18 Accessing accessing;
4.19 Paging paging;
4.20 Pages pages(&mem);
5.1 --- a/memory.cc Sat Mar 20 01:40:39 2021 +0100
5.2 +++ b/memory.cc Sat Mar 20 01:41:43 2021 +0100
5.3 @@ -5,16 +5,9 @@
5.4
5.5 /* Initialise the memory pool with an optional 'limit' in pages. */
5.6
5.7 -Memory::Memory(unsigned int limit, offset_t region_size)
5.8 -: _limit(limit), _region_size(region_size)
5.9 +Memory::Memory(offset_t region_size)
5.10 +: _region_size(region_size)
5.11 {
5.12 - _limited = true;
5.13 -}
5.14 -
5.15 -Memory::Memory()
5.16 -: _region_size(PAGE_SIZE)
5.17 -{
5.18 - _limited = false;
5.19 }
5.20
5.21 /* Allocate a block of the given 'size'. */
5.22 @@ -35,56 +28,4 @@
5.23 return new Region((offset_t) current, (offset_t) current + size);
5.24 }
5.25
5.26 -/* Allocate a new region of the given 'size' rounded to the nearest page. */
5.27 -
5.28 -Region *Memory::region(offset_t size)
5.29 -{
5.30 - std::lock_guard<std::mutex> guard(_lock);
5.31 -
5.32 - offset_t rounded = round(size, PAGE_SIZE);
5.33 - offset_t pages = rounded / PAGE_SIZE;
5.34 -
5.35 - /* Check for sufficient pages. */
5.36 -
5.37 - if (!_limited || (_limit >= pages))
5.38 - {
5.39 - /* Attempt to allocate aligned memory. */
5.40 -
5.41 - Region *region = allocate(rounded);
5.42 -
5.43 - if (region == NULL)
5.44 - return NULL;
5.45 -
5.46 - if (_limited)
5.47 - _limit -= pages;
5.48 -
5.49 - return region;
5.50 - }
5.51 -
5.52 - /* Return no region without sufficient pages. */
5.53 -
5.54 - else
5.55 - return NULL;
5.56 -}
5.57 -
5.58 -Region *Memory::region()
5.59 -{
5.60 - return region(_region_size);
5.61 -}
5.62 -
5.63 -/* Release the allocated 'region'. */
5.64 -
5.65 -void Memory::release(Region *region)
5.66 -{
5.67 - std::lock_guard<std::mutex> guard(_lock);
5.68 -
5.69 - offset_t rounded = round(region->size(), PAGE_SIZE);
5.70 - offset_t pages = rounded / PAGE_SIZE;
5.71 -
5.72 - if (_limited)
5.73 - _limit += pages;
5.74 -
5.75 - delete region;
5.76 -}
5.77 -
5.78 // vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/memory.h Sat Mar 20 01:40:39 2021 +0100
6.2 +++ b/memory.h Sat Mar 20 01:41:43 2021 +0100
6.3 @@ -2,7 +2,6 @@
6.4
6.5 #include <systypes/base.h>
6.6
6.7 -#include <list>
6.8 #include <mutex>
6.9
6.10 #include "memory_utils.h"
6.11 @@ -16,23 +15,16 @@
6.12 {
6.13 protected:
6.14 std::mutex _lock;
6.15 -
6.16 - unsigned int _limit;
6.17 offset_t _region_size;
6.18 - bool _limited;
6.19
6.20 public:
6.21 - explicit Memory(unsigned int limit, offset_t region_size=PAGE_SIZE);
6.22 + explicit Memory(offset_t region_size=PAGE_SIZE);
6.23
6.24 - explicit Memory();
6.25 -
6.26 - Region *allocate(offset_t size);
6.27 + virtual Region *allocate(offset_t size);
6.28
6.29 - Region *region(offset_t size);
6.30 + virtual Region *region() = 0;
6.31
6.32 - Region *region();
6.33 -
6.34 - void release(Region *region);
6.35 + virtual void release(Region *region) = 0;
6.36 };
6.37
6.38 // vim: tabstop=4 expandtab shiftwidth=4
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/memory_incremental.cc Sat Mar 20 01:41:43 2021 +0100
7.3 @@ -0,0 +1,71 @@
7.4 +#include "memory_incremental.h"
7.5 +
7.6 +/* Initialise the memory pool with an optional 'limit' in pages. */
7.7 +
7.8 +MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size)
7.9 +: Memory(region_size), _limit(limit)
7.10 +{
7.11 + _limited = true;
7.12 +}
7.13 +
7.14 +MemoryIncremental::MemoryIncremental()
7.15 +: Memory()
7.16 +{
7.17 + _limited = false;
7.18 +}
7.19 +
7.20 +/* Allocate a new region of the configured size rounded to the nearest page. */
7.21 +
7.22 +Region *MemoryIncremental::region()
7.23 +{
7.24 + return region(_region_size);
7.25 +}
7.26 +
7.27 +/* Allocate a new region of the given 'size' rounded to the nearest page. */
7.28 +
7.29 +Region *MemoryIncremental::region(offset_t size)
7.30 +{
7.31 + std::lock_guard<std::mutex> guard(_lock);
7.32 +
7.33 + offset_t rounded = round(size, PAGE_SIZE);
7.34 + offset_t pages = rounded / PAGE_SIZE;
7.35 +
7.36 + /* Check for sufficient pages. */
7.37 +
7.38 + if (!_limited || (_limit >= pages))
7.39 + {
7.40 + /* Attempt to allocate aligned memory. */
7.41 +
7.42 + Region *region = allocate(rounded);
7.43 +
7.44 + if (region == NULL)
7.45 + return NULL;
7.46 +
7.47 + if (_limited)
7.48 + _limit -= pages;
7.49 +
7.50 + return region;
7.51 + }
7.52 +
7.53 + /* Return no region without sufficient pages. */
7.54 +
7.55 + else
7.56 + return NULL;
7.57 +}
7.58 +
7.59 +/* Release the allocated 'region'. */
7.60 +
7.61 +void MemoryIncremental::release(Region *region)
7.62 +{
7.63 + std::lock_guard<std::mutex> guard(_lock);
7.64 +
7.65 + offset_t rounded = round(region->size(), PAGE_SIZE);
7.66 + offset_t pages = rounded / PAGE_SIZE;
7.67 +
7.68 + if (_limited)
7.69 + _limit += pages;
7.70 +
7.71 + delete region;
7.72 +}
7.73 +
7.74 +// vim: tabstop=4 expandtab shiftwidth=4
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/memory_incremental.h Sat Mar 20 01:41:43 2021 +0100
8.3 @@ -0,0 +1,27 @@
8.4 +#pragma once
8.5 +
8.6 +#include "memory.h"
8.7 +
8.8 +
8.9 +
8.10 +/* A memory pool incrementally requesting new regions. */
8.11 +
8.12 +class MemoryIncremental : public Memory
8.13 +{
8.14 +protected:
8.15 + unsigned int _limit;
8.16 + bool _limited;
8.17 +
8.18 +public:
8.19 + explicit MemoryIncremental(unsigned int limit, offset_t region_size=PAGE_SIZE);
8.20 +
8.21 + explicit MemoryIncremental();
8.22 +
8.23 + virtual Region *region();
8.24 +
8.25 + virtual Region *region(offset_t size);
8.26 +
8.27 + virtual void release(Region *region);
8.28 +};
8.29 +
8.30 +// vim: tabstop=4 expandtab shiftwidth=4
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/memory_preallocated.cc Sat Mar 20 01:41:43 2021 +0100
9.3 @@ -0,0 +1,53 @@
9.4 +#include "memory_preallocated.h"
9.5 +
9.6 +/* Initialise the memory pool with a mandatory 'limit' in pages. */
9.7 +
9.8 +MemoryPreallocated::MemoryPreallocated(unsigned int limit, offset_t region_size)
9.9 +: Memory(region_size), _limit(limit)
9.10 +{
9.11 + while (limit > 0)
9.12 + {
9.13 + Region *r = region();
9.14 +
9.15 + if (r != NULL)
9.16 + _free.push_back(r);
9.17 + else
9.18 + break;
9.19 +
9.20 + limit--;
9.21 + }
9.22 +}
9.23 +
9.24 +/* Allocate a new region of the configured size rounded to the nearest page. */
9.25 +
9.26 +Region *MemoryPreallocated::region()
9.27 +{
9.28 + std::lock_guard<std::mutex> guard(_lock);
9.29 +
9.30 + std::list<Region *>::iterator it = _free.begin();
9.31 +
9.32 + if (it == _free.end())
9.33 + return NULL;
9.34 +
9.35 + Region *r = *it;
9.36 +
9.37 + _free.erase(it);
9.38 + _used.insert(r);
9.39 +}
9.40 +
9.41 +/* Release the allocated 'region'. */
9.42 +
9.43 +void MemoryPreallocated::release(Region *region)
9.44 +{
9.45 + std::lock_guard<std::mutex> guard(_lock);
9.46 +
9.47 + std::set<Region *>::iterator it = _used.find(region);
9.48 +
9.49 + if (it != _used.end())
9.50 + {
9.51 + _used.erase(it);
9.52 + _free.push_back(region);
9.53 + }
9.54 +}
9.55 +
9.56 +// vim: tabstop=4 expandtab shiftwidth=4
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/memory_preallocated.h Sat Mar 20 01:41:43 2021 +0100
10.3 @@ -0,0 +1,27 @@
10.4 +#pragma once
10.5 +
10.6 +#include <list>
10.7 +#include <set>
10.8 +
10.9 +#include "memory.h"
10.10 +
10.11 +
10.12 +
10.13 +/* A memory pool preallocating its memory. */
10.14 +
10.15 +class MemoryPreallocated : public Memory
10.16 +{
10.17 +protected:
10.18 + unsigned int _limit;
10.19 + std::list<Region *> _free;
10.20 + std::set<Region *> _used;
10.21 +
10.22 +public:
10.23 + explicit MemoryPreallocated(unsigned int limit, offset_t region_size=PAGE_SIZE);
10.24 +
10.25 + virtual Region *region();
10.26 +
10.27 + virtual void release(Region *region);
10.28 +};
10.29 +
10.30 +// vim: tabstop=4 expandtab shiftwidth=4
11.1 --- a/pages.cc Sat Mar 20 01:40:39 2021 +0100
11.2 +++ b/pages.cc Sat Mar 20 01:41:43 2021 +0100
11.3 @@ -1,3 +1,4 @@
11.4 +#include "memory_incremental.h"
11.5 #include "pages.h"
11.6
11.7 Pages::Pages(Memory *memory)
11.8 @@ -7,7 +8,7 @@
11.9
11.10 Pages::Pages()
11.11 {
11.12 - _memory = new Memory();
11.13 + _memory = new MemoryIncremental();
11.14 }
11.15
11.16 /* Remove the first flexpage in the queue. If its owner exists, remove it from
12.1 --- a/simple_pager.cc Sat Mar 20 01:40:39 2021 +0100
12.2 +++ b/simple_pager.cc Sat Mar 20 01:41:43 2021 +0100
12.3 @@ -2,6 +2,7 @@
12.4
12.5 #include "dataspace_server.h"
12.6 #include "ipc.h"
12.7 +#include "memory_incremental.h"
12.8 #include "simple_pager.h"
12.9
12.10
12.11 @@ -9,7 +10,7 @@
12.12 SimplePager::SimplePager(Memory *memory)
12.13 {
12.14 if (memory == NULL)
12.15 - _memory = new Memory();
12.16 + _memory = new MemoryIncremental();
12.17 else
12.18 _memory = memory;
12.19