mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-16 18:50:23 +02:00
Attempt to start creating a proper API
This commit is contained in:
parent
c824f17bd9
commit
2e6dde66b3
7 changed files with 87 additions and 45 deletions
70
gc-api.h
70
gc-api.h
|
@ -1,26 +1,62 @@
|
|||
#ifndef GC_TYPES_H_
|
||||
#define GC_TYPES_H_
|
||||
#ifndef GC_API_H_
|
||||
#define GC_API_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef GC_DEBUG
|
||||
#define GC_DEBUG 0
|
||||
#endif
|
||||
|
||||
#define GC_UNLIKELY(e) __builtin_expect(e, 0)
|
||||
#define GC_LIKELY(e) __builtin_expect(e, 1)
|
||||
|
||||
#if GC_DEBUG
|
||||
#define GC_ASSERT(x) do { if (GC_UNLIKELY(!(x))) __builtin_trap(); } while (0)
|
||||
#else
|
||||
#define GC_ASSERT(x) do { } while (0)
|
||||
#endif
|
||||
|
||||
struct gc_ref {
|
||||
uintptr_t value;
|
||||
};
|
||||
|
||||
static inline struct gc_ref gc_ref(uintptr_t value) {
|
||||
return (struct gc_ref){value};
|
||||
}
|
||||
static inline uintptr_t gc_ref_value(struct gc_ref ref) {
|
||||
return ref.value;
|
||||
}
|
||||
|
||||
static inline struct gc_ref gc_ref_null(void) {
|
||||
return gc_ref(0);
|
||||
}
|
||||
static inline int gc_ref_is_heap_object(struct gc_ref ref) {
|
||||
return ref.value != 0;
|
||||
}
|
||||
static inline struct gc_ref gc_ref_from_heap_object_or_null(void *obj) {
|
||||
return gc_ref((uintptr_t) obj);
|
||||
}
|
||||
static inline struct gc_ref gc_ref_from_heap_object(void *obj) {
|
||||
GC_ASSERT(obj);
|
||||
return gc_ref_from_heap_object_or_null(obj);
|
||||
}
|
||||
static inline void* gc_ref_heap_object(struct gc_ref ref) {
|
||||
GC_ASSERT(gc_ref_is_heap_object(ref));
|
||||
return (void *) gc_ref_value(ref);
|
||||
}
|
||||
|
||||
struct gc_edge {
|
||||
union {
|
||||
void *addr;
|
||||
void **loc;
|
||||
};
|
||||
struct gc_ref *dst;
|
||||
};
|
||||
|
||||
static inline struct gc_edge gc_edge(void* addr) {
|
||||
struct gc_edge edge;
|
||||
edge.addr = addr;
|
||||
return edge;
|
||||
return (struct gc_edge){addr};
|
||||
}
|
||||
static inline struct gc_edge object_field(void* addr) {
|
||||
return gc_edge(addr);
|
||||
static struct gc_ref gc_edge_ref(struct gc_edge edge) {
|
||||
return *edge.dst;
|
||||
}
|
||||
static inline void* dereference_edge(struct gc_edge edge) {
|
||||
return *edge.loc;
|
||||
}
|
||||
static inline void update_edge(struct gc_edge edge, void *value) {
|
||||
*edge.loc = value;
|
||||
static inline void gc_edge_update(struct gc_edge edge, struct gc_ref ref) {
|
||||
*edge.dst = ref;
|
||||
}
|
||||
|
||||
#endif // GC_TYPES_H_
|
||||
#endif // GC_API_H_
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue