Continue the implementation

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13818 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2004-05-27 05:51:31 +00:00
parent a2f45b2802
commit 9772629794

View File

@ -18,37 +18,7 @@
#include "../GCInterface.h" #include "../GCInterface.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
/* FIXME: This should be in a code-generator specific library!
*/
typedef struct GCRoot {
void **RootPtr;
void *Meta;
} GCRoot;
typedef struct GCRoots {
struct GCRoots *Next;
unsigned NumRoots;
GCRoot RootRecords[];
} GCRoots;
GCRoots *llvm_gc_root_chain;
static void llvm_cg_walk_gcroots(void (*FP)(void **Root, void *Meta)) {
GCRoots *R = llvm_gc_root_chain;
for (; R; R = R->Next) {
unsigned i, e;
for (i = 0, e = R->NumRoots; i != e; ++i)
FP(R->RootRecords[i].RootPtr, R->RootRecords[i].Meta);
}
}
/* END FIXME! */
/* We use no read/write barriers */
void *llvm_gc_read(void **P) { return *P; }
void llvm_gc_write(void *V, void **P) { *P = V; }
/* AllocPtr - This points to the next byte that is available for allocation. /* AllocPtr - This points to the next byte that is available for allocation.
*/ */
@ -59,9 +29,22 @@ static char *AllocPtr;
*/ */
static char *AllocEnd; static char *AllocEnd;
void llvm_gc_initialize() { /* CurSpace/OtherSpace - These pointers point to the two regions of memory that
AllocPtr = calloc(1, 1000); * we switch between. The unallocated portion of the CurSpace is known to be
AllocEnd = AllocPtr + 1000; * 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 /* We always want to inline the fast path, but never want to inline the slow
@ -81,18 +64,59 @@ void *llvm_gc_allocate(unsigned Size) {
static void* llvm_gc_alloc_slow(unsigned Size) { static void* llvm_gc_alloc_slow(unsigned Size) {
llvm_gc_collect(); 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); return llvm_gc_allocate(Size);
} }
static void process_root(void **Root, void *Meta) { static void process_pointer(void **Root, void *Meta) {
printf("process_root[0x%X] = 0x%X\n", (unsigned) Root, (unsigned) *Root); printf("process_root[0x%X] = 0x%X\n", (unsigned) Root, (unsigned) *Root);
} }
void llvm_gc_collect() { void llvm_gc_collect() {
printf("Garbage collecting!!\n"); // Clear out the space we will be copying into.
llvm_cg_walk_gcroots(process_root); // FIXME: This should do the copy, then clear out whatever space is left.
abort(); memset(OtherSpace, 0, SpaceSize);
/* TODO: Iterate through roots. */ printf("Garbage collecting!!\n");
llvm_cg_walk_gcroots(process_pointer);
abort();
} }
/* We use no read/write barriers */
void *llvm_gc_read(void **P) { return *P; }
void llvm_gc_write(void *V, void **P) { *P = V; }
/*===----------------------------------------------------------------------===**
* FIXME: This should be in a code-generator specific library, but for now this
* will work for all code generators.
*/
typedef struct GCRoot {
void **RootPtr;
void *Meta;
} GCRoot;
typedef struct GCRoots {
struct GCRoots *Next;
unsigned NumRoots;
GCRoot RootRecords[];
} GCRoots;
GCRoots *llvm_gc_root_chain;
void llvm_cg_walk_gcroots(void (*FP)(void **Root, void *Meta)) {
GCRoots *R = llvm_gc_root_chain;
for (; R; R = R->Next) {
unsigned i, e;
for (i = 0, e = R->NumRoots; i != e; ++i)
FP(R->RootRecords[i].RootPtr, R->RootRecords[i].Meta);
}
}
/* END FIXME! */