Skip to content

Commit

Permalink
New memory persistent allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
dsouzai committed Feb 25, 2020
1 parent dea02bd commit e38af6e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
58 changes: 58 additions & 0 deletions runtime/compiler/env/PersistentAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@ namespace J9 {

PersistentAllocator::PersistentAllocator(const PersistentAllocatorKit &creationKit) :
_minimumSegmentSize(creationKit.minimumSegmentSize),
#if defined(NEW_MEMORY)
_rawAllocator(&creationKit.javaVM),
_segmentAllocator(MEMORY_TYPE_JIT_PERSISTENT, creationKit.javaVM, _rawAllocator),
#else
_segmentAllocator(MEMORY_TYPE_JIT_PERSISTENT, creationKit.javaVM),
#endif
_freeBlocks(),
#if defined(NEW_MEMORY)
_segments(SegmentContainerAllocator(_rawAllocator))
#else
_segments(SegmentContainerAllocator(RawAllocator(&creationKit.javaVM)))
#endif
{
}

PersistentAllocator::~PersistentAllocator() throw()
{
while (!_segments.empty())
{
#if defined(NEW_MEMORY)
TR::MemorySegment &segment = _segments.front();
#else
J9MemorySegment &segment = _segments.front();
#endif
_segments.pop_front();
_segmentAllocator.deallocate(segment);
}
Expand Down Expand Up @@ -122,6 +135,33 @@ PersistentAllocator::allocateLocked(size_t requestedSize)

// Find the first persistent segment with enough free space
//
#if defined(NEW_MEMORY)
SegmentContainer::const_iterator iter = findUsableSegment(allocSize);
if (iter == _segments.end())
{
size_t const segmentSize = allocSize < _minimumSegmentSize ? _minimumSegmentSize : allocSize;
TR::MemorySegment &segment = _segmentAllocator.allocate(segmentSize);
try
{
_segments.push_front(TR::ref(segment));
}
catch(const std::exception &e)
{
_segmentAllocator.deallocate(segment);
return 0;
}

TR_ASSERT(segment.remaining() >= allocSize, "Failed to acquire a segment");
block = new(segment.allocate(allocSize)) Block(allocSize);
}
else
{
TR::MemorySegment &segment = TR::ref(*iter);

TR_ASSERT(segment.remaining() >= allocSize, "Failed to acquire a segment");
block = new(segment.allocate(allocSize)) Block(allocSize);
}
#else
J9MemorySegment *segment = findUsableSegment(allocSize);
if (!segment)
{
Expand All @@ -140,9 +180,26 @@ PersistentAllocator::allocateLocked(size_t requestedSize)
}
TR_ASSERT(segment && remainingSpace(*segment) >= allocSize, "Failed to acquire a segment");
block = new(operator new(allocSize, *segment)) Block(allocSize);
#endif

return block + 1;
}

#if defined(NEW_MEMORY)
PersistentAllocator::SegmentContainer::const_iterator
PersistentAllocator::findUsableSegment(size_t requiredSize)
{
for (auto i = _segments.begin(); i != _segments.end(); ++i)
{
TR::MemorySegment &candidate = TR::ref(*i);
if (candidate.remaining() >= requiredSize )
{
return i;
}
}
return _segments.end();
}
#else
J9MemorySegment *
PersistentAllocator::findUsableSegment(size_t requiredSize)
{
Expand All @@ -162,6 +219,7 @@ PersistentAllocator::remainingSpace(J9MemorySegment &segment) throw()
{
return (segment.heapTop - segment.heapAlloc);
}
#endif

void
PersistentAllocator::freeBlock(Block * block)
Expand Down
30 changes: 27 additions & 3 deletions runtime/compiler/env/PersistentAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,27 @@ namespace TR { using J9::PersistentAllocator; }

#include <new>
#include "env/PersistentAllocatorKit.hpp"

#if defined(NEW_MEMORY)
#include "env/J9TestRawAllocator.hpp"
#else
#include "env/RawAllocator.hpp"
#endif
#include "env/TypedAllocator.hpp"
#if defined(NEW_MEMORY)
#include "env/J9TestSegmentAllocator.hpp"
#else
#include "env/J9SegmentAllocator.hpp"
#endif
#include "infra/ReferenceWrapper.hpp"
#include "env/MemorySegment.hpp"
#include <deque>

#if !defined(NEW_MEMORY)
extern "C" {
struct J9MemorySegment;
}
#endif

namespace J9 {

Expand Down Expand Up @@ -88,16 +99,29 @@ class PersistentAllocator
void * allocateLocked(size_t);
void freeBlock(Block *);

J9MemorySegment * findUsableSegment(size_t requiredSize);
#if defined(NEW_MEMORY)
typedef TR::typed_allocator<TR::reference_wrapper<TR::MemorySegment>, TestAlloc::RawAllocator> SegmentContainerAllocator;
typedef std::deque<TR::reference_wrapper<TR::MemorySegment>, SegmentContainerAllocator> SegmentContainer;
SegmentContainer::const_iterator findUsableSegment(size_t requiredSize);
#else
typedef TR::typed_allocator<TR::reference_wrapper<J9MemorySegment>, TR::RawAllocator> SegmentContainerAllocator;
typedef std::deque<TR::reference_wrapper<J9MemorySegment>, SegmentContainerAllocator> SegmentContainer;

J9MemorySegment * findUsableSegment(size_t requiredSize);
static void * allocate(J9MemorySegment &memorySegment, size_t size) throw();
static size_t remainingSpace(J9MemorySegment &memorySegment) throw();
#endif

size_t const _minimumSegmentSize;

#if defined(NEW_MEMORY)
TestAlloc::J9RA _rawAllocator;
TestAlloc::J9SA _segmentAllocator;
#else
SegmentAllocator _segmentAllocator;
#endif

Block * _freeBlocks[PERSISTANT_BLOCK_SIZE_BUCKETS];
typedef TR::typed_allocator<TR::reference_wrapper<J9MemorySegment>, TR::RawAllocator> SegmentContainerAllocator;
typedef std::deque<TR::reference_wrapper<J9MemorySegment>, SegmentContainerAllocator> SegmentContainer;
SegmentContainer _segments;
};

Expand Down

0 comments on commit e38af6e

Please sign in to comment.