4.2. sys
rtl/sys is implicitly merged into ordinary DQ modules. You can call its public
helpers without writing use sys. --no-use-sys opts out for low-level modules.
| Reexported module | Services |
|---|---|
rtl/rtlint |
Runtime intrinsics, type helpers, runtime error hook. |
rtl/mem |
Allocation and byte-block operations. |
rtl/strfunc |
Runtime string operations. |
rtl/exception |
Exception and exception helpers, when exceptions are enabled. |
rtl/prgargs |
Arguments and exit_code, on hosted targets. |
String methods, anyvalue, and formatting are documented with their language
chapters: String Functions,
anyvalue, and
Text Formatting.
4.2.1 Arguments and Process Status
func ArgCount() -> int
func ArgStr(i : int) -> ^char
func Exit(status : int)
func Errno() -> int32
On hosted targets, ArgCount() includes the program name at index zero.
ArgStr(i) returns a borrowed C string or nil for an invalid index. Read the
string before the process ends; do not free the pointer.
Set exit_code and return normally from *Main to select a process status.
Exit(status) terminates immediately; it does not unwind DQ scopes.
Errno() reads the C runtime's error value. Interpret it only according to the
native operation that just failed, since successful calls need not clear it.
See Runtime and Program Execution for a complete argument-handling example and target differences.
4.2.2 Raw Memory
func MemAlloc(allocbytes : int, zeromem : bool = false) -> pointer
func MemFree(memptr : pointer)
func MemFreeRef(memptrvar : ref pointer)
func MemRealloc(memptr : pointer, newsize : uint) -> pointer
func MemReallocRef(memptr : ref pointer, newsize : uint)
func MemCopy(dest : pointer, src : pointer, size : uint)
func MemSet(dest : pointer, value : int, size : uint)
func MemBlockSize(memptr : pointer) -> uint
MemAlloc returns raw storage, optionally zeroed. Check for nil before use.
MemFreeRef frees an allocation and clears the pointer variable. MemFree
does not clear other aliases. Neither function runs object destructors.
use print
func *Main():
var buffer : ^byte = ^byte(MemAlloc(64, true))
if buffer == nil:
exit_code = 1
return
endif
buffer^ = 42
PrintLn("First byte: {}", [buffer^])
MemFree(buffer)
buffer = nil
endfunc
MemCopy copies bytes and handles overlapping source and destination ranges.
MemSet fills a block with the low byte of value. Sizes are measured in bytes;
these operations do not check the size or lifetime of the supplied storage.
Do not use byte copying to duplicate managed strings, arrays, or owning fields.
MemRealloc can move an allocation. On allocation failure with a nonzero size,
it returns nil while the old block remains allocated; keep the old pointer
until the result has been checked. MemReallocRef directly replaces the pointer
and therefore does not preserve that recovery handle on failure.
MemBlockSize reports the allocator's usable block size on Linux and Windows;
the fallback on other targets returns zero. It is not a portable way to track
application buffer lengths.
Prefer managed strings, arrays, and objects for ordinary storage. See Memory Management and Autofree for cleanup and owning pointers.
4.2.3 Exceptions and Backtraces
With exceptions enabled, Exception supplies a formatted message and a captured
backtrace. A handler can inspect error.message and call
error.PrintBacktrace(). Compile with -g -O0 for useful source-line diagnostics.
The complete rules and examples are in
Exceptions and Error Handling.