aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/mem.pk
blob: 5ad3c7a933ef913620c70261bf3cb8ac95a56588 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## std.mem: Unsafe functions for working with raw memory.
## This module is imported by default.

## Raw pointers. Extremely unsafe.
## While they are removed from the memory model by default (and hence can
## cause memory errors), pointers must know what *type* they point at.
## This cuts down on errors somewhat - not much, but somewhat.
@[magic]
pub type ptr[T] =
  addr: uint
## Heap-allocated, memory-safe references.
@[magic]
pub type ref[T]
## Heap-allocated, reference-counted references.
## These are a special type - rather than anything handled by the type system
## alone - because there are significant optimizations the compiler can make.
@[magic]
pub type refc[T]

## Tells the compiler to interpret data of one type directly as data of another.
## Extremely unsafe. Take caution to memory representations when using.
## Compiler magic. The type signature is left here for documentation purposes.
@[unsafe, magic]
pub func cast[T, U](self: T): U

## Sets the sizeof(T) bytes following data + offset * sizeof(T) to T.
## Extremely unsafe.
@[unsafe]
pub func set[T](data: ptr T, val: T, offset: uint = 0) =
  ...

## Gets the value at data + offset * sizeof(T), and interprets it as type T.
## Extremely unsafe.
@[unsafe]
pub func get[T](data: ptr T, offset: uint = 0) =
  ...

## Returns a pointer offset by offset * sizeof(T).
## Extremely unsafe.
@[unsafe]
pub func offset[T](data: ptr T, offset: uint): ptr T =
  ...

@[unsafe]
pub func malloc[T](size: uint = sizeof(T)): ptr T =
  ...

@[unsafe]
pub func calloc[T](size: uint = sizeof(T)): ptr T =
  ...

@[unsafe]
pub func realloc[T](data: ptr T): ptr T =
  ...

@[unsafe]
pub func copy[T](from: ptr T, to: ptr T, length: uint) =
  ...