corda/src/heap.h

85 lines
2.3 KiB
C
Raw Normal View History

2012-05-11 23:43:27 +00:00
/* Copyright (c) 2008-2011, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
2007-06-02 23:36:55 +00:00
#ifndef HEAP_H
#define HEAP_H
2007-06-20 19:20:25 +00:00
#include "system.h"
#include "allocator.h"
2007-06-20 19:20:25 +00:00
namespace vm {
// an object must survive TenureThreshold + 2 garbage collections
// before being copied to gen2 (must be at least 1):
const unsigned TenureThreshold = 3;
const unsigned FixieTenureThreshold = TenureThreshold + 2;
class Heap: public Allocator {
2007-06-02 23:36:55 +00:00
public:
enum CollectionType {
MinorCollection,
MajorCollection
};
enum Status {
Null,
Reachable,
Unreachable,
Tenured
};
2007-06-02 23:36:55 +00:00
class Visitor {
public:
virtual void visit(void*) = 0;
2007-06-02 23:36:55 +00:00
};
2007-06-20 16:58:35 +00:00
class Walker {
public:
2007-06-20 16:58:35 +00:00
virtual bool visit(unsigned) = 0;
};
class Client {
public:
virtual void collect(void* context, CollectionType type) = 0;
2007-06-20 16:58:35 +00:00
virtual void visitRoots(Visitor*) = 0;
virtual bool isFixed(void*) = 0;
virtual unsigned sizeInWords(void*) = 0;
virtual unsigned copiedSizeInWords(void*) = 0;
virtual void copy(void*, void*) = 0;
2007-06-20 16:58:35 +00:00
virtual void walk(void*, Walker*) = 0;
2007-06-02 23:36:55 +00:00
};
virtual void setClient(Client* client) = 0;
2008-12-02 02:38:00 +00:00
virtual void setImmortalHeap(uintptr_t* start, unsigned sizeInWords) = 0;
virtual unsigned limit() = 0;
rework VM exception handling; throw OOMEs when appropriate This rather large commit modifies the VM to use non-local returns to throw exceptions instead of simply setting Thread::exception and returning frame-by-frame as it used to. This has several benefits: * Functions no longer need to check Thread::exception after each call which might throw an exception (which would be especially tedious and error-prone now that any function which allocates objects directly or indirectly might throw an OutOfMemoryError) * There's no need to audit the code for calls to functions which previously did not throw exceptions but later do * Performance should be improved slightly due to both the reduced need for conditionals and because undwinding now occurs in a single jump instead of a series of returns The main disadvantages are: * Slightly higher overhead for entering and leaving the VM via the JNI and JDK methods * Non-local returns can make the code harder to read * We must be careful to register destructors for stack-allocated resources with the Thread so they can be called prior to a non-local return The non-local return implementation is similar to setjmp/longjmp, except it uses continuation-passing style to avoid the need for cooperation from the C/C++ compiler. Native C++ exceptions would have also been an option, but that would introduce a dependence on libstdc++, which we're trying to avoid for portability reasons. Finally, this commit ensures that the VM throws an OutOfMemoryError instead of aborting when it reaches its memory ceiling. Currently, we treat the ceiling as a soft limit and temporarily exceed it as necessary to allow garbage collection and certain internal allocations to succeed, but refuse to allocate any Java objects until the heap size drops back below the ceiling.
2010-12-27 22:55:23 +00:00
virtual bool limitExceeded() = 0;
virtual void collect(CollectionType type, unsigned footprint) = 0;
virtual void* allocateFixed(Allocator* allocator, unsigned sizeInWords,
bool objectMask, unsigned* totalInBytes) = 0;
2008-11-29 02:31:06 +00:00
virtual void* allocateImmortalFixed(Allocator* allocator,
unsigned sizeInWords, bool objectMask,
unsigned* totalInBytes) = 0;
virtual void mark(void* p, unsigned offset, unsigned count) = 0;
virtual void pad(void* p) = 0;
2007-06-20 17:42:13 +00:00
virtual void* follow(void* p) = 0;
virtual void postVisit() = 0;
virtual Status status(void* p) = 0;
virtual CollectionType collectionType() = 0;
virtual void disposeFixies() = 0;
virtual void dispose() = 0;
2007-06-02 23:36:55 +00:00
};
Heap* makeHeap(System* system, unsigned limit);
2007-06-20 19:20:25 +00:00
} // namespace vm
2007-06-02 23:36:55 +00:00
#endif//HEAP_H