2006-11-17 03:32:33 +00:00
|
|
|
/*===-- semispace.c - Simple semi-space copying garbage collector ---------===*\
|
|
|
|
|*
|
|
|
|
|* The LLVM Compiler Infrastructure
|
|
|
|
|*
|
2007-12-29 22:59:10 +00:00
|
|
|
|* This file is distributed under the University of Illinois Open Source
|
|
|
|
|* License. See LICENSE.TXT for details.
|
2006-11-17 03:32:33 +00:00
|
|
|
|*
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|*
|
|
|
|
|* This garbage collector is an extremely simple copying collector. It splits
|
|
|
|
|* the managed region of memory into two pieces: the current space to allocate
|
|
|
|
|* from, and the copying space. When the portion being allocated from fills up,
|
|
|
|
|* a garbage collection cycle happens, which copies all live blocks to the other
|
|
|
|
|* half of the managed space.
|
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "../GCInterface.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* AllocPtr - This points to the next byte that is available for allocation.
|
|
|
|
*/
|
|
|
|
static char *AllocPtr;
|
|
|
|
|
|
|
|
/* AllocEnd - This points to the first byte not available for allocation. When
|
|
|
|
* AllocPtr passes this, we have run out of space.
|
|
|
|
*/
|
|
|
|
static char *AllocEnd;
|
|
|
|
|
|
|
|
/* CurSpace/OtherSpace - These pointers point to the two regions of memory that
|
|
|
|
* we switch between. The unallocated portion of the CurSpace is known to be
|
|
|
|
* zero'd out, but the OtherSpace contains junk.
|
|
|
|
*/
|
|
|
|
static void *CurSpace, *OtherSpace;
|
|
|
|
|
|
|
|
/* SpaceSize - The size of each space. */
|
|
|
|
static unsigned SpaceSize;
|
|
|
|
|
|
|
|
/* llvm_gc_initialize - Allocate the two spaces that we plan to switch between.
|
|
|
|
*/
|
|
|
|
void llvm_gc_initialize(unsigned InitialHeapSize) {
|
|
|
|
SpaceSize = InitialHeapSize/2;
|
|
|
|
CurSpace = AllocPtr = calloc(1, SpaceSize);
|
|
|
|
OtherSpace = malloc(SpaceSize);
|
|
|
|
AllocEnd = AllocPtr + SpaceSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We always want to inline the fast path, but never want to inline the slow
|
|
|
|
* path.
|
|
|
|
*/
|
|
|
|
void *llvm_gc_allocate(unsigned Size) __attribute__((always_inline));
|
|
|
|
static void* llvm_gc_alloc_slow(unsigned Size) __attribute__((noinline));
|
|
|
|
|
|
|
|
void *llvm_gc_allocate(unsigned Size) {
|
|
|
|
char *OldAP = AllocPtr;
|
|
|
|
char *NewEnd = OldAP+Size;
|
|
|
|
if (NewEnd > AllocEnd)
|
|
|
|
return llvm_gc_alloc_slow(Size);
|
|
|
|
AllocPtr = NewEnd;
|
|
|
|
return OldAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* llvm_gc_alloc_slow(unsigned Size) {
|
|
|
|
llvm_gc_collect();
|
|
|
|
if (AllocPtr+Size > AllocEnd) {
|
|
|
|
fprintf(stderr, "Garbage collector ran out of memory "
|
|
|
|
"allocating object of size: %d\n", Size);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm_gc_allocate(Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void process_pointer(void **Root, void *Meta) {
|
|
|
|
printf("process_root[0x%p] = 0x%p\n", (void*) Root, (void*) *Root);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm_gc_collect() {
|
|
|
|
// Clear out the space we will be copying into.
|
|
|
|
// FIXME: This should do the copy, then clear out whatever space is left.
|
|
|
|
memset(OtherSpace, 0, SpaceSize);
|
|
|
|
|
|
|
|
printf("Garbage collecting!!\n");
|
|
|
|
llvm_cg_walk_gcroots(process_pointer);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We use no read/write barriers */
|
|
|
|
void *llvm_gc_read(void *ObjPtr, void **FieldPtr) { return *FieldPtr; }
|
|
|
|
void llvm_gc_write(void *V, void *ObjPtr, void **FieldPtr) { *FieldPtr = V; }
|
|
|
|
|
|
|
|
|
|
|
|
/*===----------------------------------------------------------------------===**
|
|
|
|
* FIXME: This should be in a code-generator specific library, but for now this
|
|
|
|
* will work for all code generators.
|
|
|
|
*/
|
2008-01-24 05:16:36 +00:00
|
|
|
typedef struct FrameMap FrameMap;
|
With this patch, the LowerGC transformation becomes the
ShadowStackCollector, which additionally has reduced overhead with
no sacrifice in portability.
Considering a function @fun with 8 loop-local roots,
ShadowStackCollector introduces the following overhead
(x86):
; shadowstack prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl $___gc_fun, 20(%esp)
movl $0, 24(%esp)
movl $0, 28(%esp)
movl $0, 32(%esp)
movl $0, 36(%esp)
movl $0, 40(%esp)
movl $0, 44(%esp)
movl $0, 48(%esp)
movl $0, 52(%esp)
movl %ecx, 16(%esp)
leal 16(%esp), %ecx
movl %ecx, (%eax)
; shadowstack loop overhead
(none)
; shadowstack epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; shadowstack metadata
.align 3
___gc_fun: # __gc_fun
.long 8
.space 4
In comparison to LowerGC:
; lowergc prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl %ecx, 48(%esp)
movl $8, 52(%esp)
movl $0, 60(%esp)
movl $0, 56(%esp)
movl $0, 68(%esp)
movl $0, 64(%esp)
movl $0, 76(%esp)
movl $0, 72(%esp)
movl $0, 84(%esp)
movl $0, 80(%esp)
movl $0, 92(%esp)
movl $0, 88(%esp)
movl $0, 100(%esp)
movl $0, 96(%esp)
movl $0, 108(%esp)
movl $0, 104(%esp)
movl $0, 116(%esp)
movl $0, 112(%esp)
; lowergc loop overhead
leal 44(%esp), %eax
movl %eax, 56(%esp)
leal 40(%esp), %eax
movl %eax, 64(%esp)
leal 36(%esp), %eax
movl %eax, 72(%esp)
leal 32(%esp), %eax
movl %eax, 80(%esp)
leal 28(%esp), %eax
movl %eax, 88(%esp)
leal 24(%esp), %eax
movl %eax, 96(%esp)
leal 20(%esp), %eax
movl %eax, 104(%esp)
leal 16(%esp), %eax
movl %eax, 112(%esp)
; lowergc epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; lowergc metadata
(none)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45670 91177308-0d34-0410-b5e6-96231b3b80d8
2008-01-07 01:30:53 +00:00
|
|
|
struct FrameMap {
|
|
|
|
int32_t NumRoots; // Number of roots in stack frame.
|
|
|
|
int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots.
|
|
|
|
void *Meta[]; // May be absent for roots without metadata.
|
|
|
|
};
|
|
|
|
|
2008-01-24 05:16:36 +00:00
|
|
|
typedef struct StackEntry StackEntry;
|
With this patch, the LowerGC transformation becomes the
ShadowStackCollector, which additionally has reduced overhead with
no sacrifice in portability.
Considering a function @fun with 8 loop-local roots,
ShadowStackCollector introduces the following overhead
(x86):
; shadowstack prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl $___gc_fun, 20(%esp)
movl $0, 24(%esp)
movl $0, 28(%esp)
movl $0, 32(%esp)
movl $0, 36(%esp)
movl $0, 40(%esp)
movl $0, 44(%esp)
movl $0, 48(%esp)
movl $0, 52(%esp)
movl %ecx, 16(%esp)
leal 16(%esp), %ecx
movl %ecx, (%eax)
; shadowstack loop overhead
(none)
; shadowstack epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; shadowstack metadata
.align 3
___gc_fun: # __gc_fun
.long 8
.space 4
In comparison to LowerGC:
; lowergc prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl %ecx, 48(%esp)
movl $8, 52(%esp)
movl $0, 60(%esp)
movl $0, 56(%esp)
movl $0, 68(%esp)
movl $0, 64(%esp)
movl $0, 76(%esp)
movl $0, 72(%esp)
movl $0, 84(%esp)
movl $0, 80(%esp)
movl $0, 92(%esp)
movl $0, 88(%esp)
movl $0, 100(%esp)
movl $0, 96(%esp)
movl $0, 108(%esp)
movl $0, 104(%esp)
movl $0, 116(%esp)
movl $0, 112(%esp)
; lowergc loop overhead
leal 44(%esp), %eax
movl %eax, 56(%esp)
leal 40(%esp), %eax
movl %eax, 64(%esp)
leal 36(%esp), %eax
movl %eax, 72(%esp)
leal 32(%esp), %eax
movl %eax, 80(%esp)
leal 28(%esp), %eax
movl %eax, 88(%esp)
leal 24(%esp), %eax
movl %eax, 96(%esp)
leal 20(%esp), %eax
movl %eax, 104(%esp)
leal 16(%esp), %eax
movl %eax, 112(%esp)
; lowergc epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; lowergc metadata
(none)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45670 91177308-0d34-0410-b5e6-96231b3b80d8
2008-01-07 01:30:53 +00:00
|
|
|
struct StackEntry {
|
2008-01-24 05:16:36 +00:00
|
|
|
StackEntry *Next; // Caller's stack entry.
|
With this patch, the LowerGC transformation becomes the
ShadowStackCollector, which additionally has reduced overhead with
no sacrifice in portability.
Considering a function @fun with 8 loop-local roots,
ShadowStackCollector introduces the following overhead
(x86):
; shadowstack prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl $___gc_fun, 20(%esp)
movl $0, 24(%esp)
movl $0, 28(%esp)
movl $0, 32(%esp)
movl $0, 36(%esp)
movl $0, 40(%esp)
movl $0, 44(%esp)
movl $0, 48(%esp)
movl $0, 52(%esp)
movl %ecx, 16(%esp)
leal 16(%esp), %ecx
movl %ecx, (%eax)
; shadowstack loop overhead
(none)
; shadowstack epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; shadowstack metadata
.align 3
___gc_fun: # __gc_fun
.long 8
.space 4
In comparison to LowerGC:
; lowergc prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl %ecx, 48(%esp)
movl $8, 52(%esp)
movl $0, 60(%esp)
movl $0, 56(%esp)
movl $0, 68(%esp)
movl $0, 64(%esp)
movl $0, 76(%esp)
movl $0, 72(%esp)
movl $0, 84(%esp)
movl $0, 80(%esp)
movl $0, 92(%esp)
movl $0, 88(%esp)
movl $0, 100(%esp)
movl $0, 96(%esp)
movl $0, 108(%esp)
movl $0, 104(%esp)
movl $0, 116(%esp)
movl $0, 112(%esp)
; lowergc loop overhead
leal 44(%esp), %eax
movl %eax, 56(%esp)
leal 40(%esp), %eax
movl %eax, 64(%esp)
leal 36(%esp), %eax
movl %eax, 72(%esp)
leal 32(%esp), %eax
movl %eax, 80(%esp)
leal 28(%esp), %eax
movl %eax, 88(%esp)
leal 24(%esp), %eax
movl %eax, 96(%esp)
leal 20(%esp), %eax
movl %eax, 104(%esp)
leal 16(%esp), %eax
movl %eax, 112(%esp)
; lowergc epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; lowergc metadata
(none)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45670 91177308-0d34-0410-b5e6-96231b3b80d8
2008-01-07 01:30:53 +00:00
|
|
|
const FrameMap *Map; // Pointer to constant FrameMap.
|
|
|
|
void *Roots[]; // Stack roots (in-place array).
|
|
|
|
};
|
|
|
|
StackEntry *llvm_gc_root_chain;
|
2006-11-17 03:32:33 +00:00
|
|
|
|
|
|
|
void llvm_cg_walk_gcroots(void (*FP)(void **Root, void *Meta)) {
|
2008-01-24 05:16:36 +00:00
|
|
|
StackEntry *R;
|
|
|
|
for (R = llvm_gc_root_chain; R; R = R->Next) {
|
2006-11-17 03:32:33 +00:00
|
|
|
unsigned i, e;
|
2008-01-24 05:16:36 +00:00
|
|
|
for (i = 0, e = R->Map->NumMeta; i != e; ++i)
|
With this patch, the LowerGC transformation becomes the
ShadowStackCollector, which additionally has reduced overhead with
no sacrifice in portability.
Considering a function @fun with 8 loop-local roots,
ShadowStackCollector introduces the following overhead
(x86):
; shadowstack prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl $___gc_fun, 20(%esp)
movl $0, 24(%esp)
movl $0, 28(%esp)
movl $0, 32(%esp)
movl $0, 36(%esp)
movl $0, 40(%esp)
movl $0, 44(%esp)
movl $0, 48(%esp)
movl $0, 52(%esp)
movl %ecx, 16(%esp)
leal 16(%esp), %ecx
movl %ecx, (%eax)
; shadowstack loop overhead
(none)
; shadowstack epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; shadowstack metadata
.align 3
___gc_fun: # __gc_fun
.long 8
.space 4
In comparison to LowerGC:
; lowergc prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl %ecx, 48(%esp)
movl $8, 52(%esp)
movl $0, 60(%esp)
movl $0, 56(%esp)
movl $0, 68(%esp)
movl $0, 64(%esp)
movl $0, 76(%esp)
movl $0, 72(%esp)
movl $0, 84(%esp)
movl $0, 80(%esp)
movl $0, 92(%esp)
movl $0, 88(%esp)
movl $0, 100(%esp)
movl $0, 96(%esp)
movl $0, 108(%esp)
movl $0, 104(%esp)
movl $0, 116(%esp)
movl $0, 112(%esp)
; lowergc loop overhead
leal 44(%esp), %eax
movl %eax, 56(%esp)
leal 40(%esp), %eax
movl %eax, 64(%esp)
leal 36(%esp), %eax
movl %eax, 72(%esp)
leal 32(%esp), %eax
movl %eax, 80(%esp)
leal 28(%esp), %eax
movl %eax, 88(%esp)
leal 24(%esp), %eax
movl %eax, 96(%esp)
leal 20(%esp), %eax
movl %eax, 104(%esp)
leal 16(%esp), %eax
movl %eax, 112(%esp)
; lowergc epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; lowergc metadata
(none)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45670 91177308-0d34-0410-b5e6-96231b3b80d8
2008-01-07 01:30:53 +00:00
|
|
|
FP(&R->Roots[i], R->Map->Meta[i]);
|
2008-01-24 05:16:36 +00:00
|
|
|
for (e = R->Map->NumRoots; i != e; ++i)
|
With this patch, the LowerGC transformation becomes the
ShadowStackCollector, which additionally has reduced overhead with
no sacrifice in portability.
Considering a function @fun with 8 loop-local roots,
ShadowStackCollector introduces the following overhead
(x86):
; shadowstack prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl $___gc_fun, 20(%esp)
movl $0, 24(%esp)
movl $0, 28(%esp)
movl $0, 32(%esp)
movl $0, 36(%esp)
movl $0, 40(%esp)
movl $0, 44(%esp)
movl $0, 48(%esp)
movl $0, 52(%esp)
movl %ecx, 16(%esp)
leal 16(%esp), %ecx
movl %ecx, (%eax)
; shadowstack loop overhead
(none)
; shadowstack epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; shadowstack metadata
.align 3
___gc_fun: # __gc_fun
.long 8
.space 4
In comparison to LowerGC:
; lowergc prologue
movl L_llvm_gc_root_chain$non_lazy_ptr, %eax
movl (%eax), %ecx
movl %ecx, 48(%esp)
movl $8, 52(%esp)
movl $0, 60(%esp)
movl $0, 56(%esp)
movl $0, 68(%esp)
movl $0, 64(%esp)
movl $0, 76(%esp)
movl $0, 72(%esp)
movl $0, 84(%esp)
movl $0, 80(%esp)
movl $0, 92(%esp)
movl $0, 88(%esp)
movl $0, 100(%esp)
movl $0, 96(%esp)
movl $0, 108(%esp)
movl $0, 104(%esp)
movl $0, 116(%esp)
movl $0, 112(%esp)
; lowergc loop overhead
leal 44(%esp), %eax
movl %eax, 56(%esp)
leal 40(%esp), %eax
movl %eax, 64(%esp)
leal 36(%esp), %eax
movl %eax, 72(%esp)
leal 32(%esp), %eax
movl %eax, 80(%esp)
leal 28(%esp), %eax
movl %eax, 88(%esp)
leal 24(%esp), %eax
movl %eax, 96(%esp)
leal 20(%esp), %eax
movl %eax, 104(%esp)
leal 16(%esp), %eax
movl %eax, 112(%esp)
; lowergc epilogue
movl 48(%esp), %edx
movl %edx, (%ecx)
; lowergc metadata
(none)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45670 91177308-0d34-0410-b5e6-96231b3b80d8
2008-01-07 01:30:53 +00:00
|
|
|
FP(&R->Roots[i], NULL);
|
2006-11-17 03:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END FIXME! */
|
|
|
|
|
|
|
|
|