1.1 --- a/Makefile Wed Feb 03 22:22:30 2021 +0100
1.2 +++ b/Makefile Wed Feb 03 23:33:48 2021 +0100
1.3 @@ -42,7 +42,7 @@
1.4 PLAIN_SRC_CC_dstest_server = \
1.5 access_map.cc accessing.cc accessor.cc \
1.6 dstest_server.cc flexpage.cc file_pager.cc \
1.7 - ipc.cc memory.cc memory_map.cc \
1.8 + ipc.cc memory.cc \
1.9 opener_resource.cc opener_context_resource.cc \
1.10 page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.11 region.cc resource_server.cc simple_pager.cc \
2.1 --- a/memory.cc Wed Feb 03 22:22:30 2021 +0100
2.2 +++ b/memory.cc Wed Feb 03 23:33:48 2021 +0100
2.3 @@ -1,12 +1,6 @@
2.4 #include "memory.h"
2.5 -#include "memory_map.h"
2.6 -
2.7 -
2.8
2.9 -/* Global memory allocator. */
2.10 -
2.11 -extern Allocator allocator;
2.12 -
2.13 +#include <stdlib.h>
2.14
2.15
2.16 /* Initialise the memory pool with an optional 'limit' in pages. */
2.17 @@ -23,6 +17,24 @@
2.18 _limited = false;
2.19 }
2.20
2.21 +/* Allocate a block of the given 'size'. */
2.22 +
2.23 +Region *Memory::allocate(offset_t size)
2.24 +{
2.25 + /* Attempt to allocate aligned memory. */
2.26 +
2.27 + void *current;
2.28 +
2.29 + /* Make the size appropriate for the invocation. */
2.30 +
2.31 + size = round_multiple(size, PAGE_SIZE);
2.32 +
2.33 + if (posix_memalign(¤t, size, size))
2.34 + return NULL;
2.35 +
2.36 + return new Region((offset_t) current, (offset_t) current + size);
2.37 +}
2.38 +
2.39 /* Allocate a new region of the given 'size' rounded to the nearest page. */
2.40
2.41 Region *Memory::region(offset_t size)
2.42 @@ -38,7 +50,7 @@
2.43 {
2.44 /* Attempt to allocate aligned memory. */
2.45
2.46 - Region *region = allocator.region(rounded);
2.47 + Region *region = allocate(rounded);
2.48
2.49 if (region == NULL)
2.50 return NULL;
2.51 @@ -72,8 +84,6 @@
2.52 if (_limited)
2.53 _limit += pages;
2.54
2.55 - allocator.remove(region);
2.56 -
2.57 delete region;
2.58 }
2.59
3.1 --- a/memory.h Wed Feb 03 22:22:30 2021 +0100
3.2 +++ b/memory.h Wed Feb 03 23:33:48 2021 +0100
3.3 @@ -25,6 +25,8 @@
3.4
3.5 explicit Memory();
3.6
3.7 + Region *allocate(offset_t size);
3.8 +
3.9 Region *region(offset_t size);
3.10
3.11 Region *region();
4.1 --- a/memory_map.cc Wed Feb 03 22:22:30 2021 +0100
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,83 +0,0 @@
4.4 -#include <stdlib.h>
4.5 -
4.6 -#include "memory_map.h"
4.7 -#include "memory_utils.h"
4.8 -
4.9 -
4.10 -
4.11 -/* Global memory allocator. */
4.12 -
4.13 -Allocator allocator;
4.14 -
4.15 -
4.16 -
4.17 -/* Region management. */
4.18 -
4.19 -void MemoryMap::insert(Region *region)
4.20 -{
4.21 - std::lock_guard<std::mutex> guard(_lock);
4.22 -
4.23 - _regions.insert(_MemoryMapEntry(region->start, region));
4.24 -}
4.25 -
4.26 -void MemoryMap::remove(Region *region)
4.27 -{
4.28 - std::lock_guard<std::mutex> guard(_lock);
4.29 -
4.30 - _MemoryMap::iterator entry = _regions.find(region->start);
4.31 -
4.32 - if (entry != _regions.end())
4.33 - _regions.erase(entry);
4.34 -}
4.35 -
4.36 -/* Show the memory state. */
4.37 -
4.38 -void MemoryMap::show(std::ostringstream &buffer)
4.39 -{
4.40 - std::lock_guard<std::mutex> guard(_lock);
4.41 -
4.42 - _MemoryMap::iterator it;
4.43 -
4.44 - buffer << "Mem: ";
4.45 -
4.46 - for (it = _regions.begin(); it != _regions.end(); it++)
4.47 - {
4.48 - it->second->state.show(buffer);
4.49 - buffer << " ";
4.50 - }
4.51 -
4.52 - buffer << std::endl;
4.53 -}
4.54 -
4.55 -
4.56 -
4.57 -/* Allocate a block of the given 'size'. */
4.58 -
4.59 -Region *Allocator::region(offset_t size)
4.60 -{
4.61 - /* Attempt to allocate aligned memory. */
4.62 -
4.63 - void *current;
4.64 -
4.65 - /* Make the size appropriate for the invocation. */
4.66 -
4.67 - size = round_multiple(size, PAGE_SIZE);
4.68 -
4.69 - if (posix_memalign(¤t, size, size))
4.70 - return NULL;
4.71 -
4.72 - Region *region = new Region((offset_t) current, (offset_t) current + size);
4.73 -
4.74 - insert(region);
4.75 - return region;
4.76 -}
4.77 -
4.78 -/* Release the allocated 'region'. */
4.79 -
4.80 -void Allocator::release(Region *region)
4.81 -{
4.82 - remove(region);
4.83 - delete region;
4.84 -}
4.85 -
4.86 -// vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/memory_map.h Wed Feb 03 22:22:30 2021 +0100
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,51 +0,0 @@
5.4 -#pragma once
5.5 -
5.6 -#include <map>
5.7 -#include <mutex>
5.8 -#include <sstream>
5.9 -
5.10 -#include "region.h"
5.11 -#include "types.h"
5.12 -
5.13 -
5.14 -
5.15 -/* Memory map entry. */
5.16 -
5.17 -typedef std::map<offset_t, Region *> _MemoryMap;
5.18 -typedef std::pair<offset_t, Region *> _MemoryMapEntry;
5.19 -
5.20 -
5.21 -
5.22 -/* Memory organised as a collection of regions. */
5.23 -
5.24 -class MemoryMap
5.25 -{
5.26 -protected:
5.27 - std::mutex _lock;
5.28 - _MemoryMap _regions;
5.29 -
5.30 -public:
5.31 - /* Region maintenance. */
5.32 -
5.33 - void insert(Region *region);
5.34 -
5.35 - void remove(Region *region);
5.36 -
5.37 - /* Debugging methods. */
5.38 -
5.39 - void show(std::ostringstream &buffer);
5.40 -};
5.41 -
5.42 -
5.43 -
5.44 -/* An allocator abstraction. */
5.45 -
5.46 -class Allocator : public MemoryMap
5.47 -{
5.48 -public:
5.49 - Region *region(offset_t size);
5.50 -
5.51 - void release(Region *region);
5.52 -};
5.53 -
5.54 -// vim: tabstop=4 expandtab shiftwidth=4