codon/stdlib/internal/gc.codon

116 lines
1.7 KiB
Python
Raw Normal View History

2022-01-24 07:01:09 +00:00
# (c) 2022 Exaloop Inc. All rights reserved.
2021-09-27 18:02:44 +00:00
# Primarily for internal use. Regular users should not use this module.
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
@pure
@C
2022-01-24 07:01:09 +00:00
def seq_alloc(a: int) -> cobj:
pass
2021-09-27 18:02:44 +00:00
@pure
@C
2022-01-24 07:01:09 +00:00
def seq_alloc_atomic(a: int) -> cobj:
pass
@nocapture
@derives
@C
def seq_realloc(p: cobj, a: int) -> cobj:
pass
@nocapture
@C
def seq_free(p: cobj) -> void:
pass
@nocapture
@C
def seq_register_finalizer(p: cobj, f: cobj) -> void:
pass
@nocapture
@C
def seq_gc_add_roots(p: cobj, q: cobj) -> void:
pass
@nocapture
@C
def seq_gc_remove_roots(p: cobj, q: cobj) -> void:
pass
@C
def seq_gc_clear_roots() -> void:
pass
@nocapture
@C
def seq_gc_exclude_static_roots(p: cobj, q: cobj) -> void:
pass
2021-09-27 18:02:44 +00:00
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def sizeof(T: type):
return T.__elemsize__
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def atomic(T: type):
return T.__atomic__
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def alloc(sz: int):
return seq_alloc(sz)
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
# Allocates a block of memory via GC, where the
# caller guarantees that this block will not store
# pointers to other GC-allocated data.
def alloc_atomic(sz: int):
return seq_alloc_atomic(sz)
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def realloc(p: cobj, sz: int):
return seq_realloc(p, sz)
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def free(p: cobj):
seq_free(p)
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def add_roots(start: cobj, end: cobj):
seq_gc_add_roots(start, end)
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def remove_roots(start: cobj, end: cobj):
seq_gc_remove_roots(start, end)
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def clear_roots():
seq_gc_clear_roots()
2022-01-24 07:01:09 +00:00
2021-09-27 18:02:44 +00:00
def exclude_static_roots(start: cobj, end: cobj):
seq_gc_exclude_static_roots(start, end)
2022-01-24 07:01:09 +00:00
def register_finalizer(p):
2022-01-24 07:01:09 +00:00
if hasattr(p, "__del__"):
def f(x: cobj, data: cobj, T: type):
Ptr[T](__ptr__(x).as_byte())[0].__del__()
2022-01-24 07:01:09 +00:00
seq_register_finalizer(p.__raw__(), f(T=type(p), ...).__raw__())
2022-05-21 00:44:49 +00:00
def construct_ref[T](args) -> T:
p = T.__new__()
p.__init__(*args)
return p