mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-05 22:38:44 +00:00
add clang-format; reformat files
This commit is contained in:
parent
f20d351dd0
commit
ccd04c2159
10 changed files with 1885 additions and 1837 deletions
144
runtime/.clang-format
Normal file
144
runtime/.clang-format
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
# Common settings
|
||||
BasedOnStyle: LLVM
|
||||
TabWidth: 2
|
||||
IndentWidth: 2
|
||||
UseTab: Never
|
||||
ColumnLimit: 100
|
||||
IndentCaseLabels: true
|
||||
|
||||
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
||||
---
|
||||
Language: Cpp
|
||||
|
||||
DisableFormat: false
|
||||
Standard: Cpp11
|
||||
|
||||
AccessModifierOffset: -4
|
||||
AlignAfterOpenBracket: true
|
||||
AlignConsecutiveAssignments: true
|
||||
AlignConsecutiveDeclarations: true
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: true
|
||||
AlignTrailingComments: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: true
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AlwaysBreakAfterDefinitionReturnType: false
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
BinPackArguments: false
|
||||
BinPackParameters: true
|
||||
BitFieldColonSpacing: Both
|
||||
|
||||
|
||||
# Configure each individual brace in BraceWrapping
|
||||
BreakBeforeBraces: Attach
|
||||
# Control of individual brace wrapping cases
|
||||
BraceWrapping: {
|
||||
AfterClass: 'true'
|
||||
AfterControlStatement: 'true'
|
||||
AfterEnum : 'true'
|
||||
AfterFunction : 'true'
|
||||
AfterNamespace : 'true'
|
||||
AfterStruct : 'true'
|
||||
AfterUnion : 'true'
|
||||
BeforeCatch : 'true'
|
||||
BeforeElse : 'true'
|
||||
IndentBraces : 'false'
|
||||
AfterExternBlock : 'true'
|
||||
SplitEmptyFunction : 'false'
|
||||
SplitEmptyRecord : 'false'
|
||||
SplitEmptyNamespace : 'true'
|
||||
}
|
||||
|
||||
BreakAfterJavaFieldAnnotations: true
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakArrays: false
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: true
|
||||
BreakStringLiterals: true
|
||||
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
DerivePointerAlignment: false
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
|
||||
IndentCaseLabels: true
|
||||
FixNamespaceComments: true
|
||||
IndentWrappedFunctionNames: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
JavaScriptQuotes: Double
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBlockIndentWidth: 4
|
||||
ObjCSpaceAfterProperty: true
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakBeforeFirstCallParameter: 19
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 60
|
||||
PointerAlignment: Right
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeParens: Custom
|
||||
SpaceBeforeParensOptions: {
|
||||
AfterControlStatements: 'true'
|
||||
AfterForeachMacros: 'true'
|
||||
AfterFunctionDeclarationName: 'true'
|
||||
AfterFunctionDefinitionName: 'true'
|
||||
AfterIfMacros: 'true'
|
||||
AfterOverloadedOperator: 'true'
|
||||
AfterRequiresInClause: 'true'
|
||||
AfterRequiresInExpression: 'true'
|
||||
BeforeNonEmptyParentheses: 'false'
|
||||
}
|
||||
SpaceBeforeRangeBasedForLoopColon: false
|
||||
SpaceInEmptyBlock: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 3
|
||||
SpacesInAngles: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
VerilogBreakBetweenInstancePorts: true
|
||||
|
||||
SortUsingDeclarations: true
|
||||
SortIncludes: CaseInsensitive
|
||||
|
||||
IndentGotoLabels: false
|
||||
InsertBraces: false
|
||||
InsertNewlineAtEOF: true
|
||||
|
||||
# Comments are for developers, they should arrange them
|
||||
ReflowComments: false
|
||||
|
||||
IncludeBlocks: Regroup
|
||||
IndentPPDirectives: AfterHash
|
||||
SeparateDefinitionBlocks: Always
|
||||
|
||||
IntegerLiteralSeparator:
|
||||
Binary: 4
|
||||
Decimal: 0
|
||||
Hex: 0
|
||||
|
||||
---
|
||||
953
runtime/gc.c
953
runtime/gc.c
File diff suppressed because it is too large
Load diff
177
runtime/gc.h
177
runtime/gc.h
|
|
@ -3,99 +3,105 @@
|
|||
|
||||
#include "runtime_common.h"
|
||||
|
||||
# define GET_MARK_BIT(x) (((int) (x)) & 1)
|
||||
# define SET_MARK_BIT(x) (x = (((int) (x)) | 1))
|
||||
# define IS_ENQUEUED(x) (((int) (x)) & 2)
|
||||
# define MAKE_ENQUEUED(x) (x = (((int) (x)) | 2))
|
||||
# define MAKE_DEQUEUED(x) (x = (((int) (x)) & (~2)))
|
||||
# define RESET_MARK_BIT(x) (x = (((int) (x)) & (~1)))
|
||||
# define GET_FORWARD_ADDRESS(x) (((size_t) (x)) & (~3)) // since last 2 bits are used for mark-bit and enqueued-bit and due to correct alignment we can expect that last 2 bits don't influence address (they should always be zero)
|
||||
# define SET_FORWARD_ADDRESS(x, addr) (x = ((x & 3) | ((int) (addr)))) // take the last two bits as they are and make all others zero
|
||||
# define EXTRA_ROOM_HEAP_COEFFICIENT 2 // TODO: tune this parameter
|
||||
#define GET_MARK_BIT(x) (((int)(x)) & 1)
|
||||
#define SET_MARK_BIT(x) (x = (((int)(x)) | 1))
|
||||
#define IS_ENQUEUED(x) (((int)(x)) & 2)
|
||||
#define MAKE_ENQUEUED(x) (x = (((int)(x)) | 2))
|
||||
#define MAKE_DEQUEUED(x) (x = (((int)(x)) & (~2)))
|
||||
#define RESET_MARK_BIT(x) (x = (((int)(x)) & (~1)))
|
||||
#define GET_FORWARD_ADDRESS(x) \
|
||||
(((size_t)(x)) \
|
||||
& (~3)) // since last 2 bits are used for mark-bit and enqueued-bit and due to correct alignment we can expect that last 2 bits don't influence address (they should always be zero)
|
||||
#define SET_FORWARD_ADDRESS(x, addr) \
|
||||
(x = ((x & 3) | ((int)(addr)))) // take the last two bits as they are and make all others zero
|
||||
#define EXTRA_ROOM_HEAP_COEFFICIENT 2 // TODO: tune this parameter
|
||||
#ifdef DEBUG_VERSION
|
||||
# define MINIMUM_HEAP_CAPACITY (8)
|
||||
# define MINIMUM_HEAP_CAPACITY (8)
|
||||
#else
|
||||
# define MINIMUM_HEAP_CAPACITY (1<<10)
|
||||
# define MINIMUM_HEAP_CAPACITY (1 << 10)
|
||||
#endif
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum { ARRAY, CLOSURE, STRING, SEXP } lama_type;
|
||||
|
||||
typedef struct {
|
||||
size_t *current;
|
||||
size_t *current;
|
||||
} heap_iterator;
|
||||
|
||||
typedef struct {
|
||||
// holds type of object, which fields we are iterating over
|
||||
lama_type type;
|
||||
// here a pointer to the object header is stored
|
||||
void *obj_ptr;
|
||||
void *cur_field;
|
||||
// holds type of object, which fields we are iterating over
|
||||
lama_type type;
|
||||
// here a pointer to the object header is stored
|
||||
void *obj_ptr;
|
||||
void *cur_field;
|
||||
} obj_field_iterator;
|
||||
|
||||
typedef struct {
|
||||
size_t * begin;
|
||||
size_t * end;
|
||||
size_t * current;
|
||||
size_t size;
|
||||
size_t *begin;
|
||||
size_t *end;
|
||||
size_t *current;
|
||||
size_t size;
|
||||
} memory_chunk;
|
||||
|
||||
/* GC extra roots */
|
||||
# define MAX_EXTRA_ROOTS_NUMBER 32
|
||||
#define MAX_EXTRA_ROOTS_NUMBER 32
|
||||
|
||||
typedef struct {
|
||||
int current_free;
|
||||
void ** roots[MAX_EXTRA_ROOTS_NUMBER];
|
||||
int current_free;
|
||||
void **roots[MAX_EXTRA_ROOTS_NUMBER];
|
||||
} extra_roots_pool;
|
||||
|
||||
// the only GC-related function that should be exposed, others are useful for tests and internal implementation
|
||||
// allocates object of the given size on the heap
|
||||
void* alloc(size_t);
|
||||
void *alloc(size_t);
|
||||
// takes number of words as a parameter
|
||||
void* gc_alloc(size_t);
|
||||
void *gc_alloc(size_t);
|
||||
// takes number of words as a parameter
|
||||
void *gc_alloc_on_existing_heap(size_t);
|
||||
|
||||
// specific for mark-and-compact_phase gc
|
||||
void mark(void *obj);
|
||||
void mark_phase(void);
|
||||
void mark (void *obj);
|
||||
void mark_phase (void);
|
||||
// written in ASM, scans stack for pointers to the heap and starts marking process
|
||||
extern void __gc_root_scan_stack(void); // TODO: write without ASM, since it is absolutely not necessary
|
||||
extern void
|
||||
__gc_root_scan_stack (void); // TODO: write without ASM, since it is absolutely not necessary
|
||||
// marks each pointer from extra roots
|
||||
void scan_extra_roots(void);
|
||||
void scan_extra_roots (void);
|
||||
#ifndef DEBUG_VERSION
|
||||
// marks each valid pointer from global area
|
||||
void scan_global_area(void);
|
||||
void scan_global_area (void);
|
||||
#endif
|
||||
// takes number of words that are required to be allocated somewhere on the heap
|
||||
void compact_phase(size_t additional_size);
|
||||
void compact_phase (size_t additional_size);
|
||||
// specific for Lisp-2 algorithm
|
||||
size_t compute_locations();
|
||||
void update_references(memory_chunk *);
|
||||
void physically_relocate(memory_chunk *);
|
||||
size_t compute_locations ();
|
||||
void update_references (memory_chunk *);
|
||||
void physically_relocate (memory_chunk *);
|
||||
|
||||
// written in ASM
|
||||
extern void __gc_init (void); // MANDATORY TO CALL BEFORE ANY INTERACTION WITH GC (apart from cases where we are working with virtual stack as happens in tests)
|
||||
extern void __init (void); // should be called before interaction with GC in case of using in tests with virtual stack, otherwise it is automatically invoked by __gc_init
|
||||
extern void __shutdown (void); // mostly useful for tests but basically you want to call this in case you want to deallocate all object allocated via GC
|
||||
extern void __gc_init (
|
||||
void); // MANDATORY TO CALL BEFORE ANY INTERACTION WITH GC (apart from cases where we are working with virtual stack as happens in tests)
|
||||
extern void __init (
|
||||
void); // should be called before interaction with GC in case of using in tests with virtual stack, otherwise it is automatically invoked by __gc_init
|
||||
extern void __shutdown (
|
||||
void); // mostly useful for tests but basically you want to call this in case you want to deallocate all object allocated via GC
|
||||
// written in ASM
|
||||
extern void __pre_gc (void);
|
||||
extern void __pre_gc (void);
|
||||
// written in ASM
|
||||
extern void __post_gc (void);
|
||||
extern void __post_gc (void);
|
||||
|
||||
// invoked from ASM
|
||||
extern void gc_test_and_mark_root(size_t ** root);
|
||||
inline bool is_valid_heap_pointer(const size_t *);
|
||||
inline bool is_valid_pointer(const size_t *);
|
||||
extern void gc_test_and_mark_root (size_t **root);
|
||||
inline bool is_valid_heap_pointer (const size_t *);
|
||||
inline bool is_valid_pointer (const size_t *);
|
||||
|
||||
void clear_extra_roots (void);
|
||||
|
||||
void push_extra_root (void ** p);
|
||||
|
||||
void pop_extra_root (void ** p);
|
||||
void push_extra_root (void **p);
|
||||
|
||||
void pop_extra_root (void **p);
|
||||
|
||||
/* Functions for tests */
|
||||
|
||||
|
|
@ -105,96 +111,95 @@ void pop_extra_root (void ** p);
|
|||
// returns number of ids dumped
|
||||
// object_ids_buf is pointer to area preallocated by user for dumping ids of objects in heap
|
||||
// object_ids_buf_size is in WORDS, NOT BYTES
|
||||
size_t objects_snapshot(int *object_ids_buf, size_t object_ids_buf_size);
|
||||
size_t objects_snapshot (int *object_ids_buf, size_t object_ids_buf_size);
|
||||
|
||||
// essential function to mock program stack
|
||||
void set_stack(size_t stack_top, size_t stack_bottom);
|
||||
void set_stack (size_t stack_top, size_t stack_bottom);
|
||||
|
||||
// function to mock extra roots (Lama specific)
|
||||
void set_extra_roots(size_t extra_roots_size, void** extra_roots_ptr);
|
||||
void set_extra_roots (size_t extra_roots_size, void **extra_roots_ptr);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* Utility functions */
|
||||
|
||||
// accepts pointer to the start of the region and to the end of the region
|
||||
// scans it and if it meets a pointer, it should be modified in according to forward address
|
||||
void scan_and_fix_region(memory_chunk *old_heap, void *start, void *end);
|
||||
void scan_and_fix_region (memory_chunk *old_heap, void *start, void *end);
|
||||
|
||||
// takes a pointer to an object content as an argument, returns forwarding address
|
||||
size_t get_forward_address(void *obj);
|
||||
size_t get_forward_address (void *obj);
|
||||
|
||||
// takes a pointer to an object content as an argument, sets forwarding address to value 'addr'
|
||||
void set_forward_address(void *obj, size_t addr);
|
||||
void set_forward_address (void *obj, size_t addr);
|
||||
|
||||
// takes a pointer to an object content as an argument, returns whether this object was marked as live
|
||||
bool is_marked(void *obj);
|
||||
bool is_marked (void *obj);
|
||||
|
||||
// takes a pointer to an object content as an argument, marks the object as live
|
||||
void mark_object(void *obj);
|
||||
void mark_object (void *obj);
|
||||
|
||||
// takes a pointer to an object content as an argument, marks the object as dead
|
||||
void unmark_object(void *obj);
|
||||
void unmark_object (void *obj);
|
||||
|
||||
// takes a pointer to an object content as an argument, returns whether this object was enqueued to the queue (which is used in mark phase)
|
||||
bool is_enqueued(void *obj);
|
||||
bool is_enqueued (void *obj);
|
||||
|
||||
// takes a pointer to an object content as an argument, marks object as enqueued
|
||||
void make_enqueued(void *obj);
|
||||
void make_enqueued (void *obj);
|
||||
|
||||
// takes a pointer to an object content as an argument, unmarks object as enqueued
|
||||
void make_dequeued(void *obj);
|
||||
void make_dequeued (void *obj);
|
||||
|
||||
// returns iterator to an object with the lowest address
|
||||
heap_iterator heap_begin_iterator();
|
||||
void heap_next_obj_iterator(heap_iterator *it);
|
||||
bool heap_is_done_iterator(heap_iterator *it);
|
||||
heap_iterator heap_begin_iterator ();
|
||||
void heap_next_obj_iterator (heap_iterator *it);
|
||||
bool heap_is_done_iterator (heap_iterator *it);
|
||||
|
||||
// returns correct type when pointer to actual data is passed (header is excluded)
|
||||
lama_type get_type_row_ptr(void *ptr);
|
||||
lama_type get_type_row_ptr (void *ptr);
|
||||
// returns correct type when pointer to an object header is passed
|
||||
lama_type get_type_header_ptr(void *ptr);
|
||||
lama_type get_type_header_ptr (void *ptr);
|
||||
|
||||
// returns correct object size (together with header) of an object, ptr is pointer to an actual data is passed (header is excluded)
|
||||
size_t obj_size_row_ptr(void *ptr);
|
||||
size_t obj_size_row_ptr (void *ptr);
|
||||
// returns correct object size (together with header) of an object, ptr is pointer to an object header
|
||||
size_t obj_size_header_ptr(void *ptr);
|
||||
size_t obj_size_header_ptr (void *ptr);
|
||||
|
||||
// returns total padding size that we need to store given object type
|
||||
size_t get_header_size(lama_type type);
|
||||
size_t get_header_size (lama_type type);
|
||||
// returns number of bytes that are required to allocate array with 'sz' elements (header included)
|
||||
size_t array_size(size_t sz);
|
||||
size_t array_size (size_t sz);
|
||||
// returns number of bytes that are required to allocate string of length 'l' (header included)
|
||||
size_t string_size(size_t len);
|
||||
size_t string_size (size_t len);
|
||||
// TODO: ask if it is actually so? number of captured elements is actually sz-1 and 1 extra word is code ptr?
|
||||
// returns number of bytes that are required to allocate closure with 'sz-1' captured values (header included)
|
||||
size_t closure_size(size_t sz);
|
||||
size_t closure_size (size_t sz);
|
||||
// returns number of bytes that are required to allocate s-expression with 'members' fields (header included)
|
||||
size_t sexp_size(size_t members);
|
||||
size_t sexp_size (size_t members);
|
||||
|
||||
// returns an iterator over object fields, obj is ptr to object header
|
||||
// (in case of s-exp, it is mandatory that obj ptr is very beginning of the object,
|
||||
// considering that now we store two versions of header in there)
|
||||
obj_field_iterator field_begin_iterator(void *obj);
|
||||
obj_field_iterator field_begin_iterator (void *obj);
|
||||
// returns an iterator over object fields which are actual pointers, obj is ptr to object header
|
||||
// (in case of s-exp, it is mandatory that obj ptr is very beginning of the object,
|
||||
// considering that now we store two versions of header in there)
|
||||
obj_field_iterator ptr_field_begin_iterator(void *obj);
|
||||
obj_field_iterator ptr_field_begin_iterator (void *obj);
|
||||
// moves the iterator to next object field
|
||||
void obj_next_field_iterator(obj_field_iterator *it);
|
||||
void obj_next_field_iterator (obj_field_iterator *it);
|
||||
// moves the iterator to the next object field which is an actual pointer
|
||||
void obj_next_ptr_field_iterator(obj_field_iterator *it);
|
||||
void obj_next_ptr_field_iterator (obj_field_iterator *it);
|
||||
// returns if we are done iterating over fields of the object
|
||||
bool field_is_done_iterator(obj_field_iterator *it);
|
||||
bool field_is_done_iterator (obj_field_iterator *it);
|
||||
// ptr is pointer to the actual object content, returns pointer to the very beginning of the object (header)
|
||||
void* get_obj_header_ptr(void *ptr, lama_type type);
|
||||
void* get_object_content_ptr(void *header_ptr);
|
||||
void* get_end_of_obj(void *header_ptr);
|
||||
void *get_obj_header_ptr (void *ptr, lama_type type);
|
||||
void *get_object_content_ptr (void *header_ptr);
|
||||
void *get_end_of_obj (void *header_ptr);
|
||||
|
||||
void *alloc_string(int len);
|
||||
void *alloc_array(int len);
|
||||
void *alloc_sexp(int members);
|
||||
void *alloc_closure(int captured);
|
||||
void *alloc_string (int len);
|
||||
void *alloc_array (int len);
|
||||
void *alloc_sexp (int members);
|
||||
void *alloc_closure (int captured);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,67 +1,63 @@
|
|||
.data
|
||||
printf_format: .string "Stack root: %lx\n"
|
||||
printf_format2: .string "BOT: %lx\n"
|
||||
printf_format3: .string "TOP: %lx\n"
|
||||
printf_format4: .string "EAX: %lx\n"
|
||||
printf_format5: .string "LOL\n"
|
||||
.data
|
||||
__gc_stack_bottom: .long 0
|
||||
__gc_stack_top: .long 0
|
||||
__gc_stack_top: .long 0
|
||||
|
||||
.globl __pre_gc
|
||||
.globl __post_gc
|
||||
.globl __gc_init
|
||||
.globl __gc_root_scan_stack
|
||||
.globl __gc_stack_top
|
||||
.globl __gc_stack_bottom
|
||||
.extern init_pool
|
||||
.extern gc_test_and_copy_root
|
||||
.text
|
||||
.globl __pre_gc
|
||||
.globl __post_gc
|
||||
.globl __gc_init
|
||||
.globl __gc_root_scan_stack
|
||||
.globl __gc_stack_top
|
||||
.globl __gc_stack_bottom
|
||||
.extern __init
|
||||
.extern gc_test_and_mark_root
|
||||
|
||||
.text
|
||||
|
||||
__gc_init:
|
||||
movl %ebp, __gc_stack_bottom
|
||||
addl $4, __gc_stack_bottom
|
||||
call __init
|
||||
ret
|
||||
movl %ebp, __gc_stack_bottom
|
||||
addl $4, __gc_stack_bottom
|
||||
call __init
|
||||
ret
|
||||
|
||||
// if __gc_stack_top is equal to 0
|
||||
// then set __gc_stack_top to %ebp
|
||||
// else return
|
||||
__pre_gc:
|
||||
pushl %eax
|
||||
movl __gc_stack_top, %eax
|
||||
cmpl $0, %eax
|
||||
jne __pre_gc_2
|
||||
movl %ebp, %eax
|
||||
// addl $8, %eax
|
||||
movl %eax, __gc_stack_top
|
||||
pushl %eax
|
||||
movl __gc_stack_top, %eax
|
||||
cmpl $0, %eax
|
||||
jne __pre_gc_2
|
||||
movl %ebp, %eax
|
||||
// addl $8, %eax
|
||||
movl %eax, __gc_stack_top
|
||||
__pre_gc_2:
|
||||
popl %eax
|
||||
ret
|
||||
popl %eax
|
||||
ret
|
||||
|
||||
// if __gc_stack_top has been set by the caller
|
||||
// (i.e. it is equal to its %ebp)
|
||||
// then set __gc_stack_top to 0
|
||||
// else return
|
||||
__post_gc:
|
||||
pushl %eax
|
||||
movl __gc_stack_top, %eax
|
||||
cmpl %eax, %ebp
|
||||
jnz __post_gc2
|
||||
movl $0, __gc_stack_top
|
||||
pushl %eax
|
||||
movl __gc_stack_top, %eax
|
||||
cmpl %eax, %ebp
|
||||
jnz __post_gc2
|
||||
movl $0, __gc_stack_top
|
||||
__post_gc2:
|
||||
popl %eax
|
||||
ret
|
||||
popl %eax
|
||||
ret
|
||||
|
||||
// Scan stack for roots
|
||||
// strting from __gc_stack_top
|
||||
// till __gc_stack_bottom
|
||||
__gc_root_scan_stack:
|
||||
pushl %ebp
|
||||
movl %esp, %ebp
|
||||
pushl %ebx
|
||||
pushl %edx
|
||||
movl __gc_stack_top, %eax
|
||||
jmp next
|
||||
pushl %ebp
|
||||
movl %esp, %ebp
|
||||
pushl %ebx
|
||||
pushl %edx
|
||||
movl __gc_stack_top, %eax
|
||||
jmp next
|
||||
|
||||
loop:
|
||||
movl (%eax), %ebx
|
||||
|
|
@ -70,48 +66,48 @@ loop:
|
|||
// i.e. the following is not true:
|
||||
// __executable_start <= (%eax) <= __etext
|
||||
check11:
|
||||
leal __executable_start, %edx
|
||||
cmpl %ebx, %edx
|
||||
jna check12
|
||||
jmp check21
|
||||
leal __executable_start, %edx
|
||||
cmpl %ebx, %edx
|
||||
jna check12
|
||||
jmp check21
|
||||
|
||||
check12:
|
||||
leal __etext, %edx
|
||||
cmpl %ebx, %edx
|
||||
jnb next
|
||||
leal __etext, %edx
|
||||
cmpl %ebx, %edx
|
||||
jnb next
|
||||
|
||||
// check that it is not a pointer into the program stack
|
||||
// i.e. the following is not true:
|
||||
// __gc_stack_bottom <= (%eax) <= __gc_stack_top
|
||||
check21:
|
||||
cmpl %ebx, __gc_stack_top
|
||||
jna check22
|
||||
jmp loop2
|
||||
cmpl %ebx, __gc_stack_top
|
||||
jna check22
|
||||
jmp loop2
|
||||
|
||||
check22:
|
||||
cmpl %ebx, __gc_stack_bottom
|
||||
jnb next
|
||||
cmpl %ebx, __gc_stack_bottom
|
||||
jnb next
|
||||
|
||||
// check if it a valid pointer
|
||||
// i.e. the lastest bit is set to zero
|
||||
loop2:
|
||||
andl $0x00000001, %ebx
|
||||
jnz next
|
||||
andl $0x00000001, %ebx
|
||||
jnz next
|
||||
gc_run_t:
|
||||
pushl %eax
|
||||
pushl %eax
|
||||
call gc_test_and_mark_root
|
||||
addl $4, %esp
|
||||
popl %eax
|
||||
pushl %eax
|
||||
pushl %eax
|
||||
call gc_test_and_mark_root
|
||||
addl $4, %esp
|
||||
popl %eax
|
||||
|
||||
next:
|
||||
addl $4, %eax
|
||||
cmpl %eax, __gc_stack_bottom
|
||||
jne loop
|
||||
addl $4, %eax
|
||||
cmpl %eax, __gc_stack_bottom
|
||||
jne loop
|
||||
returnn:
|
||||
movl $0, %eax
|
||||
popl %edx
|
||||
popl %ebx
|
||||
movl %ebp, %esp
|
||||
popl %ebp
|
||||
ret
|
||||
movl $0, %eax
|
||||
popl %edx
|
||||
popl %ebx
|
||||
movl %ebp, %esp
|
||||
popl %ebp
|
||||
ret
|
||||
|
|
|
|||
1742
runtime/runtime.c
1742
runtime/runtime.c
File diff suppressed because it is too large
Load diff
|
|
@ -1,21 +1,20 @@
|
|||
# ifndef __LAMA_RUNTIME__
|
||||
# define __LAMA_RUNTIME__
|
||||
#ifndef __LAMA_RUNTIME__
|
||||
#define __LAMA_RUNTIME__
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
# include <stdarg.h>
|
||||
# include <stdlib.h>
|
||||
# include <sys/mman.h>
|
||||
# include <assert.h>
|
||||
# include <errno.h>
|
||||
# include <regex.h>
|
||||
# include <time.h>
|
||||
# include <limits.h>
|
||||
# include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <regex.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
|
||||
# define WORD_SIZE (CHAR_BIT * sizeof(int))
|
||||
#define WORD_SIZE (CHAR_BIT * sizeof(int))
|
||||
|
||||
void failure (char *s, ...);
|
||||
|
||||
# endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -5,67 +5,65 @@
|
|||
// this flag makes GC behavior a bit different for testing purposes.
|
||||
//#define DEBUG_VERSION
|
||||
|
||||
# define STRING_TAG 0x00000001
|
||||
#define STRING_TAG 0x00000001
|
||||
//# define STRING_TAG 0x00000000
|
||||
# define ARRAY_TAG 0x00000003
|
||||
#define ARRAY_TAG 0x00000003
|
||||
//# define ARRAY_TAG 0x00000002
|
||||
# define SEXP_TAG 0x00000005
|
||||
#define SEXP_TAG 0x00000005
|
||||
//# define SEXP_TAG 0x00000004
|
||||
# define CLOSURE_TAG 0x00000007
|
||||
#define CLOSURE_TAG 0x00000007
|
||||
//# define CLOSURE_TAG 0x00000006
|
||||
# define UNBOXED_TAG 0x00000009 // Not actually a data_header; used to return from LkindOf
|
||||
#define UNBOXED_TAG 0x00000009 // Not actually a data_header; used to return from LkindOf
|
||||
|
||||
# define LEN(x) ((x & 0xFFFFFFF8) >> 3)
|
||||
# define TAG(x) (x & 0x00000007)
|
||||
#define LEN(x) ((x & 0xFFFFFFF8) >> 3)
|
||||
#define TAG(x) (x & 0x00000007)
|
||||
//# define TAG(x) (x & 0x00000006)
|
||||
|
||||
#define SEXP_ONLY_HEADER_SZ (2 * sizeof(int))
|
||||
|
||||
# define SEXP_ONLY_HEADER_SZ (2 * sizeof(int))
|
||||
|
||||
# ifndef DEBUG_VERSION
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(int))
|
||||
# else
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(size_t) + sizeof(int))
|
||||
#ifndef DEBUG_VERSION
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(int))
|
||||
#else
|
||||
# define DATA_HEADER_SZ (sizeof(size_t) + sizeof(size_t) + sizeof(int))
|
||||
#endif
|
||||
|
||||
# define MEMBER_SIZE sizeof(int)
|
||||
#define MEMBER_SIZE sizeof(int)
|
||||
|
||||
# define TO_DATA(x) ((data*)((char*)(x)-DATA_HEADER_SZ))
|
||||
# define TO_SEXP(x) ((sexp*)((char*)(x)-DATA_HEADER_SZ-SEXP_ONLY_HEADER_SZ))
|
||||
#define TO_DATA(x) ((data *)((char *)(x)-DATA_HEADER_SZ))
|
||||
#define TO_SEXP(x) ((sexp *)((char *)(x)-DATA_HEADER_SZ - SEXP_ONLY_HEADER_SZ))
|
||||
|
||||
# define UNBOXED(x) (((int) (x)) & 0x0001)
|
||||
# define UNBOX(x) (((int) (x)) >> 1)
|
||||
# define BOX(x) ((((int) (x)) << 1) | 0x0001)
|
||||
#define UNBOXED(x) (((int)(x)) & 0x0001)
|
||||
#define UNBOX(x) (((int)(x)) >> 1)
|
||||
#define BOX(x) ((((int)(x)) << 1) | 0x0001)
|
||||
|
||||
# define BYTES_TO_WORDS(bytes) (((bytes) - 1) / sizeof(size_t) + 1)
|
||||
# define WORDS_TO_BYTES(words) ((words) * sizeof(size_t))
|
||||
#define BYTES_TO_WORDS(bytes) (((bytes)-1) / sizeof(size_t) + 1)
|
||||
#define WORDS_TO_BYTES(words) ((words) * sizeof(size_t))
|
||||
|
||||
// CAREFUL WITH DOUBLE EVALUATION!
|
||||
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
|
||||
|
||||
typedef struct {
|
||||
// store tag in the last three bits to understand what structure this is, other bits are filled with
|
||||
// other utility info (i.e., size for array, number of fields for s-expression)
|
||||
int data_header;
|
||||
// store tag in the last three bits to understand what structure this is, other bits are filled with
|
||||
// other utility info (i.e., size for array, number of fields for s-expression)
|
||||
int data_header;
|
||||
|
||||
#ifdef DEBUG_VERSION
|
||||
size_t id;
|
||||
size_t id;
|
||||
#endif
|
||||
|
||||
// last bit is used as MARK-BIT, the rest are used to store address where object should move
|
||||
// last bit can be used because due to alignment we can assume that last two bits are always 0's
|
||||
size_t forward_address;
|
||||
char contents[0];
|
||||
// last bit is used as MARK-BIT, the rest are used to store address where object should move
|
||||
// last bit can be used because due to alignment we can assume that last two bits are always 0's
|
||||
size_t forward_address;
|
||||
char contents[0];
|
||||
} data;
|
||||
|
||||
typedef struct {
|
||||
// duplicates contents.data_header in order to be able to understand if it is s-exp during iteration over heap
|
||||
int sexp_header;
|
||||
// stores hashed s-expression constructor name
|
||||
int tag;
|
||||
data contents;
|
||||
// duplicates contents.data_header in order to be able to understand if it is s-exp during iteration over heap
|
||||
int sexp_header;
|
||||
// stores hashed s-expression constructor name
|
||||
int tag;
|
||||
data contents;
|
||||
} sexp;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,273 +1,275 @@
|
|||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "gc.h"
|
||||
#include "runtime_common.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef DEBUG_VERSION
|
||||
|
||||
// function from runtime that maps string to int value
|
||||
extern int LtagHash (char *s);
|
||||
|
||||
extern void* Bsexp (int n, ...);
|
||||
extern void* Barray (int bn, ...);
|
||||
extern void* Bstring (void*);
|
||||
extern void* Bclosure (int bn, void *entry, ...);
|
||||
extern void *Bsexp (int n, ...);
|
||||
extern void *Barray (int bn, ...);
|
||||
extern void *Bstring (void *);
|
||||
extern void *Bclosure (int bn, void *entry, ...);
|
||||
|
||||
extern size_t __gc_stack_top, __gc_stack_bottom;
|
||||
|
||||
void test_correct_structure_sizes(void) {
|
||||
// something like induction base
|
||||
assert((array_size(0) == get_header_size(ARRAY)));
|
||||
assert((string_size(0) == get_header_size(STRING) + 1)); // +1 is because of '\0'
|
||||
assert((sexp_size(0) == get_header_size(SEXP)));
|
||||
assert((closure_size(0) == get_header_size(CLOSURE)));
|
||||
void test_correct_structure_sizes (void) {
|
||||
// something like induction base
|
||||
assert((array_size(0) == get_header_size(ARRAY)));
|
||||
assert((string_size(0) == get_header_size(STRING) + 1)); // +1 is because of '\0'
|
||||
assert((sexp_size(0) == get_header_size(SEXP)));
|
||||
assert((closure_size(0) == get_header_size(CLOSURE)));
|
||||
|
||||
// just check correctness for some small sizes
|
||||
for (int k = 1; k < 20; ++k) {
|
||||
assert((array_size(k) == get_header_size(ARRAY) + sizeof (int) * k));
|
||||
assert((string_size(k) == get_header_size(STRING) + k + 1));
|
||||
assert((sexp_size(k) == get_header_size(SEXP) + sizeof (int) * k));
|
||||
assert((closure_size(k) == get_header_size(CLOSURE) + sizeof (int) * k));
|
||||
}
|
||||
// just check correctness for some small sizes
|
||||
for (int k = 1; k < 20; ++k) {
|
||||
assert((array_size(k) == get_header_size(ARRAY) + sizeof(int) * k));
|
||||
assert((string_size(k) == get_header_size(STRING) + k + 1));
|
||||
assert((sexp_size(k) == get_header_size(SEXP) + sizeof(int) * k));
|
||||
assert((closure_size(k) == get_header_size(CLOSURE) + sizeof(int) * k));
|
||||
}
|
||||
}
|
||||
|
||||
void no_gc_tests(void) {
|
||||
test_correct_structure_sizes();
|
||||
}
|
||||
void no_gc_tests (void) { test_correct_structure_sizes(); }
|
||||
|
||||
// unfortunately there is no generic function pointer that can hold pointer to function with arbitrary signature
|
||||
extern size_t call_runtime_function(void *virt_stack_pointer, void *function_pointer, size_t num_args, ...);
|
||||
extern size_t call_runtime_function (void *virt_stack_pointer, void *function_pointer,
|
||||
size_t num_args, ...);
|
||||
|
||||
#include "virt_stack.h"
|
||||
# include "virt_stack.h"
|
||||
|
||||
virt_stack* init_test() {
|
||||
__init();
|
||||
virt_stack *st = vstack_create();
|
||||
vstack_init(st);
|
||||
__gc_stack_bottom = (size_t) vstack_top(st);
|
||||
return st;
|
||||
virt_stack *init_test () {
|
||||
__init();
|
||||
virt_stack *st = vstack_create();
|
||||
vstack_init(st);
|
||||
__gc_stack_bottom = (size_t)vstack_top(st);
|
||||
return st;
|
||||
}
|
||||
|
||||
void cleanup_test(virt_stack *st) {
|
||||
vstack_destruct(st);
|
||||
__shutdown();
|
||||
}
|
||||
void force_gc_cycle(virt_stack *st) {
|
||||
__gc_stack_top = (size_t) vstack_top(st) - 4;
|
||||
gc_alloc(0);
|
||||
__gc_stack_top = 0;
|
||||
void cleanup_test (virt_stack *st) {
|
||||
vstack_destruct(st);
|
||||
__shutdown();
|
||||
}
|
||||
|
||||
void test_simple_string_alloc(void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
vstack_push(st, BOX(i));
|
||||
}
|
||||
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "abc"));
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
cleanup_test(st);
|
||||
void force_gc_cycle (virt_stack *st) {
|
||||
__gc_stack_top = (size_t)vstack_top(st) - 4;
|
||||
gc_alloc(0);
|
||||
__gc_stack_top = 0;
|
||||
}
|
||||
|
||||
void test_simple_array_alloc(void) {
|
||||
virt_stack* st = init_test();
|
||||
void test_simple_string_alloc (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
// allocate array [ BOX(1) ] and push it onto the stack
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Barray, 2, BOX(1), BOX(1)));
|
||||
for (int i = 0; i < 5; ++i) { vstack_push(st, BOX(i)); }
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "abc"));
|
||||
|
||||
cleanup_test(st);
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_simple_sexp_alloc(void) {
|
||||
virt_stack* st = init_test();
|
||||
void test_simple_array_alloc (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
// allocate sexp with one boxed field and push it onto the stack
|
||||
// calling runtime function Bsexp(BOX(2), BOX(1), LtagHash("test"))
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bsexp, 3, BOX(2), BOX(1), LtagHash("test")));
|
||||
// allocate array [ BOX(1) ] and push it onto the stack
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Barray, 2, BOX(1), BOX(1)));
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
cleanup_test(st);
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_simple_closure_alloc(void) {
|
||||
virt_stack* st = init_test();
|
||||
void test_simple_sexp_alloc (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
// allocate closure with boxed captured value and push it onto the stack
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bclosure, 3, BOX(1), NULL, BOX(1)));
|
||||
// allocate sexp with one boxed field and push it onto the stack
|
||||
// calling runtime function Bsexp(BOX(2), BOX(1), LtagHash("test"))
|
||||
vstack_push(
|
||||
st, call_runtime_function(vstack_top(st) - 4, Bsexp, 3, BOX(2), BOX(1), LtagHash("test")));
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
cleanup_test(st);
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_single_object_allocation_with_collection_virtual_stack(void) {
|
||||
virt_stack *st = init_test();
|
||||
void test_simple_closure_alloc (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
|
||||
// allocate closure with boxed captured value and push it onto the stack
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bclosure, 3, BOX(1), NULL, BOX(1)));
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
cleanup_test(st);
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_garbage_is_reclaimed(void) {
|
||||
virt_stack *st = init_test();
|
||||
void test_single_object_allocation_with_collection_virtual_stack (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
call_runtime_function(vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
||||
vstack_push(st,
|
||||
call_runtime_function(
|
||||
vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
|
||||
|
||||
force_gc_cycle(st);
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 0));
|
||||
|
||||
cleanup_test(st);
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_alive_are_not_reclaimed(void) {
|
||||
virt_stack *st = init_test();
|
||||
void test_garbage_is_reclaimed (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
|
||||
call_runtime_function(vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
||||
|
||||
force_gc_cycle(st);
|
||||
force_gc_cycle(st);
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 0));
|
||||
|
||||
cleanup_test(st);
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_small_tree_compaction(void) {
|
||||
virt_stack *st = init_test();
|
||||
// this one will increase heap size
|
||||
call_runtime_function(vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaa");
|
||||
void test_alive_are_not_reclaimed (void) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "left-s"));
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "right-s"));
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bsexp, 4, BOX(3), vstack_kth_from_start(st, 0), vstack_kth_from_start(st, 1), LtagHash("tree")));
|
||||
force_gc_cycle(st);
|
||||
const int SZ = 10;
|
||||
int ids[SZ];
|
||||
size_t alive = objects_snapshot(ids, SZ);
|
||||
assert((alive == 3));
|
||||
vstack_push(st,
|
||||
call_runtime_function(
|
||||
vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
|
||||
|
||||
// check that order is indeed preserved
|
||||
for (int i = 0; i < alive - 1; ++i) {
|
||||
assert((ids[i] < ids[i + 1]));
|
||||
}
|
||||
cleanup_test(st);
|
||||
force_gc_cycle(st);
|
||||
|
||||
const int N = 10;
|
||||
int ids[N];
|
||||
size_t alive = objects_snapshot(ids, N);
|
||||
assert((alive == 1));
|
||||
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
void test_small_tree_compaction (void) {
|
||||
virt_stack *st = init_test();
|
||||
// this one will increase heap size
|
||||
call_runtime_function(vstack_top(st) - 4, Bstring, 1, "aaaaaaaaaaaaaaaaaaaaaa");
|
||||
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "left-s"));
|
||||
vstack_push(st, call_runtime_function(vstack_top(st) - 4, Bstring, 1, "right-s"));
|
||||
vstack_push(st,
|
||||
call_runtime_function(vstack_top(st) - 4,
|
||||
Bsexp,
|
||||
4,
|
||||
BOX(3),
|
||||
vstack_kth_from_start(st, 0),
|
||||
vstack_kth_from_start(st, 1),
|
||||
LtagHash("tree")));
|
||||
force_gc_cycle(st);
|
||||
const int SZ = 10;
|
||||
int ids[SZ];
|
||||
size_t alive = objects_snapshot(ids, SZ);
|
||||
assert((alive == 3));
|
||||
|
||||
// check that order is indeed preserved
|
||||
for (int i = 0; i < alive - 1; ++i) { assert((ids[i] < ids[i + 1])); }
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
extern size_t cur_id;
|
||||
|
||||
size_t generate_random_obj_forest(virt_stack *st, int cnt, int seed) {
|
||||
srand(seed);
|
||||
int cur_sz = 0;
|
||||
size_t alive = 0;
|
||||
while (cnt) {
|
||||
--cnt;
|
||||
if (cur_sz == 0) {
|
||||
vstack_push(st, BOX(1));
|
||||
++cur_sz;
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t pos[2] = {rand() % vstack_size(st), rand() % vstack_size(st)};
|
||||
size_t field[2];
|
||||
for (int t = 0; t < 2; ++t) {
|
||||
field[t] = vstack_kth_from_start(st, pos[t]);
|
||||
}
|
||||
size_t obj;
|
||||
|
||||
if (rand() % 2) {
|
||||
obj = call_runtime_function(vstack_top(st) - 4, Bsexp, 4, BOX(3), field[0], field[1], LtagHash("test"));
|
||||
} else {
|
||||
obj = BOX(1);
|
||||
}
|
||||
// whether object is stored on stack
|
||||
if (rand() % 2 != 0) {
|
||||
vstack_push(st, obj);
|
||||
if ((obj & 1) == 0) {
|
||||
++alive;
|
||||
}
|
||||
}
|
||||
++cur_sz;
|
||||
size_t generate_random_obj_forest (virt_stack *st, int cnt, int seed) {
|
||||
srand(seed);
|
||||
int cur_sz = 0;
|
||||
size_t alive = 0;
|
||||
while (cnt) {
|
||||
--cnt;
|
||||
if (cur_sz == 0) {
|
||||
vstack_push(st, BOX(1));
|
||||
++cur_sz;
|
||||
continue;
|
||||
}
|
||||
force_gc_cycle(st);
|
||||
return alive;
|
||||
|
||||
size_t pos[2] = {rand() % vstack_size(st), rand() % vstack_size(st)};
|
||||
size_t field[2];
|
||||
for (int t = 0; t < 2; ++t) { field[t] = vstack_kth_from_start(st, pos[t]); }
|
||||
size_t obj;
|
||||
|
||||
if (rand() % 2) {
|
||||
obj = call_runtime_function(
|
||||
vstack_top(st) - 4, Bsexp, 4, BOX(3), field[0], field[1], LtagHash("test"));
|
||||
} else {
|
||||
obj = BOX(1);
|
||||
}
|
||||
// whether object is stored on stack
|
||||
if (rand() % 2 != 0) {
|
||||
vstack_push(st, obj);
|
||||
if ((obj & 1) == 0) { ++alive; }
|
||||
}
|
||||
++cur_sz;
|
||||
}
|
||||
force_gc_cycle(st);
|
||||
return alive;
|
||||
}
|
||||
|
||||
void run_stress_test_random_obj_forest(int seed) {
|
||||
virt_stack *st = init_test();
|
||||
void run_stress_test_random_obj_forest (int seed) {
|
||||
virt_stack *st = init_test();
|
||||
|
||||
const int SZ = 100000;
|
||||
const int SZ = 100000;
|
||||
|
||||
size_t expectedAlive = generate_random_obj_forest(st, SZ, seed);
|
||||
size_t expectedAlive = generate_random_obj_forest(st, SZ, seed);
|
||||
|
||||
int ids[SZ];
|
||||
size_t alive = objects_snapshot(ids, SZ);
|
||||
assert(alive == expectedAlive);
|
||||
int ids[SZ];
|
||||
size_t alive = objects_snapshot(ids, SZ);
|
||||
assert(alive == expectedAlive);
|
||||
|
||||
// check that order is indeed preserved
|
||||
for (int i = 0; i < alive - 1; ++i) {
|
||||
assert((ids[i] < ids[i + 1]));
|
||||
}
|
||||
// check that order is indeed preserved
|
||||
for (int i = 0; i < alive - 1; ++i) { assert((ids[i] < ids[i + 1])); }
|
||||
|
||||
cleanup_test(st);
|
||||
cleanup_test(st);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
int main (int argc, char **argv) {
|
||||
#ifdef DEBUG_VERSION
|
||||
no_gc_tests();
|
||||
no_gc_tests();
|
||||
|
||||
test_simple_string_alloc();
|
||||
test_simple_array_alloc();
|
||||
test_simple_sexp_alloc();
|
||||
test_simple_closure_alloc();
|
||||
test_single_object_allocation_with_collection_virtual_stack();
|
||||
test_garbage_is_reclaimed();
|
||||
test_alive_are_not_reclaimed();
|
||||
test_small_tree_compaction();
|
||||
test_simple_string_alloc();
|
||||
test_simple_array_alloc();
|
||||
test_simple_sexp_alloc();
|
||||
test_simple_closure_alloc();
|
||||
test_single_object_allocation_with_collection_virtual_stack();
|
||||
test_garbage_is_reclaimed();
|
||||
test_alive_are_not_reclaimed();
|
||||
test_small_tree_compaction();
|
||||
|
||||
time_t start, end;
|
||||
double diff;
|
||||
time(&start);
|
||||
// stress test
|
||||
for (int s = 0; s < 100; ++s) {
|
||||
run_stress_test_random_obj_forest(s);
|
||||
}
|
||||
time(&end);
|
||||
diff = difftime(end, start);
|
||||
printf ("Stress tests took %.2lf seconds to complete\n", diff);
|
||||
time_t start, end;
|
||||
double diff;
|
||||
time(&start);
|
||||
// stress test
|
||||
for (int s = 0; s < 100; ++s) { run_stress_test_random_obj_forest(s); }
|
||||
time(&end);
|
||||
diff = difftime(end, start);
|
||||
printf("Stress tests took %.2lf seconds to complete\n", diff);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,45 +1,34 @@
|
|||
#include "virt_stack.h"
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
virt_stack *vstack_create() {
|
||||
return malloc(sizeof (virt_stack));
|
||||
virt_stack *vstack_create () { return malloc(sizeof(virt_stack)); }
|
||||
|
||||
void vstack_destruct (virt_stack *st) { free(st); }
|
||||
|
||||
void vstack_init (virt_stack *st) {
|
||||
st->cur = RUNTIME_VSTACK_SIZE;
|
||||
st->buf[st->cur] = 0;
|
||||
}
|
||||
|
||||
void vstack_destruct(virt_stack *st) {
|
||||
free(st);
|
||||
void vstack_push (virt_stack *st, size_t value) {
|
||||
if (st->cur == 0) { assert(0); }
|
||||
--st->cur;
|
||||
st->buf[st->cur] = value;
|
||||
}
|
||||
|
||||
void vstack_init(virt_stack *st) {
|
||||
st->cur = RUNTIME_VSTACK_SIZE;
|
||||
st->buf[st->cur] = 0;
|
||||
size_t vstack_pop (virt_stack *st) {
|
||||
if (st->cur == RUNTIME_VSTACK_SIZE) { assert(0); }
|
||||
size_t value = st->buf[st->cur];
|
||||
++st->cur;
|
||||
return value;
|
||||
}
|
||||
|
||||
void vstack_push(virt_stack *st, size_t value) {
|
||||
if (st->cur == 0) {
|
||||
assert(0);
|
||||
}
|
||||
--st->cur;
|
||||
st->buf[st->cur] = value;
|
||||
}
|
||||
void *vstack_top (virt_stack *st) { return st->buf + st->cur; }
|
||||
|
||||
size_t vstack_pop(virt_stack *st) {
|
||||
if (st->cur == RUNTIME_VSTACK_SIZE) {
|
||||
assert(0);
|
||||
}
|
||||
size_t value = st->buf[st->cur];
|
||||
++st->cur;
|
||||
return value;
|
||||
}
|
||||
size_t vstack_size (virt_stack *st) { return RUNTIME_VSTACK_SIZE - st->cur; }
|
||||
|
||||
void* vstack_top(virt_stack *st) {
|
||||
return st->buf + st->cur;
|
||||
}
|
||||
|
||||
size_t vstack_size(virt_stack *st) {
|
||||
return RUNTIME_VSTACK_SIZE - st->cur;
|
||||
}
|
||||
|
||||
size_t vstack_kth_from_start(virt_stack *st, size_t k) {
|
||||
assert(vstack_size(st) > k);
|
||||
return st->buf[RUNTIME_VSTACK_SIZE - 1 - k];
|
||||
size_t vstack_kth_from_start (virt_stack *st, size_t k) {
|
||||
assert(vstack_size(st) > k);
|
||||
return st->buf[RUNTIME_VSTACK_SIZE - 1 - k];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,28 +6,28 @@
|
|||
#define LAMA_RUNTIME_VIRT_STACK_H
|
||||
#define RUNTIME_VSTACK_SIZE 100000
|
||||
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct {
|
||||
size_t buf[RUNTIME_VSTACK_SIZE + 1];
|
||||
size_t cur;
|
||||
size_t buf[RUNTIME_VSTACK_SIZE + 1];
|
||||
size_t cur;
|
||||
} typedef virt_stack;
|
||||
|
||||
virt_stack *vstack_create();
|
||||
virt_stack *vstack_create ();
|
||||
|
||||
void vstack_destruct(virt_stack *st);
|
||||
void vstack_destruct (virt_stack *st);
|
||||
|
||||
void vstack_init(virt_stack *st);
|
||||
void vstack_init (virt_stack *st);
|
||||
|
||||
void vstack_push(virt_stack *st, size_t value);
|
||||
void vstack_push (virt_stack *st, size_t value);
|
||||
|
||||
size_t vstack_pop(virt_stack *st);
|
||||
size_t vstack_pop (virt_stack *st);
|
||||
|
||||
void* vstack_top(virt_stack *st);
|
||||
void *vstack_top (virt_stack *st);
|
||||
|
||||
size_t vstack_size(virt_stack *st);
|
||||
size_t vstack_size (virt_stack *st);
|
||||
|
||||
size_t vstack_kth_from_start(virt_stack *st, size_t k);
|
||||
size_t vstack_kth_from_start (virt_stack *st, size_t k);
|
||||
|
||||
#endif //LAMA_RUNTIME_VIRT_STACK_H
|
||||
#endif //LAMA_RUNTIME_VIRT_STACK_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue