Pointer

is a data type for representing C pointers.

add

template

(Fn [(Ptr a), Long] (Ptr a))

adds a long integer value to a pointer.

address

template

(Fn [(Ref a b)] (Ptr a))

returns the memory address of a reference. Equivalent to the & operator when placed before a variable in C.

blit

defn

(Fn [(Ptr a)] (Ptr a))

                        (blit x)
                    

copy

template

(Fn [(Ref (Ptr a) b)] (Ptr a))

copies a pointer p.

dec

defn

(Fn [(Ptr a)] (Ptr a))

                        (dec a)
                    

eq

template

(Fn [(Ptr a), (Ptr a)] Bool)

checks two pointer references for equality.

free

template

(Fn [(Ptr a)] ())

Free a pointer, deallocating the memory associated with it. Carp's borrow checker handles deallocation automatically for managed types. The Ptr type is unmanaged, so pointers allocated with functions such as Pointer.unsafe-alloc need to be deallocated manually using this function. Users need to manually verify that this operation is safe.

(let-do [x (Pointer.unsafe-alloc @"c")]
  (Pointer.set x @"carp")
  (println* (Pointer.to-value x))
  (Pointer.free x))

from-long

template

(Fn [Long] (Ptr a))

converts a long integer to a pointer.

inc

defn

(Fn [(Ptr a)] (Ptr a))

                        (inc a)
                    

prn

defn

(Fn [(Ptr a)] String)

                        (prn a)
                    

ref-eq

template

(Fn [(Ref (Ptr a) b), (Ref (Ptr a) c)] Bool)

set

template

(Fn [(Ptr a), a] ())

Sets the value of a pointer.

str

defn

(Fn [(Ptr a)] String)

                        (str a)
                    

strp

external

(Fn [(Ptr a)] String)

sub

template

(Fn [(Ptr a), Long] (Ptr a))

subtracts a long integer value from a pointer.

to-long

template

(Fn [(Ptr a)] Long)

converts a pointer to a long integer.

to-ref

template

(Fn [(Ptr a)] (Ref a b))

converts a pointer to a reference type.

The user will have to ensure themselves that this is a safe operation.

to-value

template

(Fn [(Ptr a)] a)

converts a pointer to a value.

The user will have to ensure themselves that this is a safe operation.

unsafe-alloc

template

(Fn [a] (Ptr a))

Allocate a new pointer to the value a. This pointer won't be managed by Carp's borrow checker and will cause memory leaks unless explicitly freed using Pointer.free.

See Unsafe.leak if you want to prevent Carp's automatic memory management from deallocating a value without performing an allocation.

(let-do [x (Pointer.unsafe-alloc @"c")]
  (Pointer.set x @"carp")
  (println* (Pointer.to-value x))
  (Pointer.free x))

unsafe-set

template

(Fn [(Ptr a), b] ())

Sets the value of a pointer to a value of any type. The user will have to ensure this operation is safe.

width

template

(Fn [(Ptr a)] Long)

gets the byte size of a pointer.