codon/stdlib/internal/gc.codon

77 lines
1.5 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
2021-09-27 18:02:44 +00:00
from C import seq_realloc(cobj, int) -> cobj
from C import seq_free(cobj)
2021-10-20 19:02:34 +00:00
from C import seq_register_finalizer(cobj, cobj)
2021-09-27 18:02:44 +00:00
from C import seq_gc_add_roots(cobj, cobj)
from C import seq_gc_remove_roots(cobj, cobj)
from C import seq_gc_clear_roots()
from C import seq_gc_exclude_static_roots(cobj, cobj)
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__())