diff --git a/runtime/compiler/env/PersistentAllocator.cpp b/runtime/compiler/env/PersistentAllocator.cpp index f046c51b29f..1008c8212c2 100644 --- a/runtime/compiler/env/PersistentAllocator.cpp +++ b/runtime/compiler/env/PersistentAllocator.cpp @@ -30,9 +30,18 @@ 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 { } @@ -40,7 +49,11 @@ 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); } @@ -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) { @@ -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) { @@ -162,6 +219,7 @@ PersistentAllocator::remainingSpace(J9MemorySegment &segment) throw() { return (segment.heapTop - segment.heapAlloc); } +#endif void PersistentAllocator::freeBlock(Block * block) diff --git a/runtime/compiler/env/PersistentAllocator.hpp b/runtime/compiler/env/PersistentAllocator.hpp index b6d1412fb25..31dcab2aa2d 100644 --- a/runtime/compiler/env/PersistentAllocator.hpp +++ b/runtime/compiler/env/PersistentAllocator.hpp @@ -30,16 +30,27 @@ namespace TR { using J9::PersistentAllocator; } #include #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 +#if !defined(NEW_MEMORY) extern "C" { struct J9MemorySegment; } +#endif namespace J9 { @@ -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, TestAlloc::RawAllocator> SegmentContainerAllocator; + typedef std::deque, SegmentContainerAllocator> SegmentContainer; + SegmentContainer::const_iterator findUsableSegment(size_t requiredSize); +#else + typedef TR::typed_allocator, TR::RawAllocator> SegmentContainerAllocator; + typedef std::deque, 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::RawAllocator> SegmentContainerAllocator; - typedef std::deque, SegmentContainerAllocator> SegmentContainer; SegmentContainer _segments; };