"You can copy a range of bytes from memory into a byte array:"
{ $subsection memory>byte-array }
"You can copy a byte array to memory unsafely:"
-{ $subsection byte-array>memory }
-"A wrapper for temporarily allocating a block of memory:"
-{ $subsection with-malloc } ;
+{ $subsection byte-array>memory } ;
ARTICLE: "c-data" "Passing data between Factor and C"
"Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers."
{ $values { "alien" c-ptr } }
{ $description "Deallocates a block of memory allocated by " { $link malloc } ", " { $link calloc } " or " { $link realloc } "." } ;
-HELP: with-malloc
-{ $values { "size" "a positive integer" } { "quot" { $quotation "( c-ptr -- )" } } }
-{ $description "Allocates a zeroed block of " { $snippet "n" } " bytes and passes it to the quotation. When the quotation returns, the block is freed." } ;
-
HELP: &free
{ $values { "alien" c-ptr } }
{ $description "Marks the block of memory for unconditional deallocation at the end of the current " { $link with-destructors } " scope." } ;
: memcpy ( dst src size -- )
"void" "libc" "memcpy" { "void*" "void*" "ulong" } alien-invoke ;
-: with-malloc ( size quot -- )
- swap 1 calloc [ swap keep ] [ free ] [ ] cleanup ; inline
-
: strlen ( alien -- len )
"size_t" "libc" "strlen" { "char*" } alien-invoke ;
HRESULT Drop ( IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect ) ;\r
\r
: com-query-interface ( interface iid -- interface' )\r
- "void*" heap-size [\r
+ [\r
+ "void*" malloc-object &free\r
[ IUnknown::QueryInterface ole32-error ] keep *void*\r
- ] with-malloc ;\r
+ ] with-destructors ;\r
\r
: com-add-ref ( interface -- interface )\r
[ IUnknown::AddRef drop ] keep ; inline\r
: gl-shader-info-log ( shader -- log )
dup gl-shader-info-log-length dup [
+ 1 calloc &free
[ 0 <int> swap glGetShaderInfoLog ] keep
ascii alien>string
- ] with-malloc ;
+ ] with-destructors ;
: check-gl-shader ( shader -- shader )
dup gl-shader-ok? [ dup gl-shader-info-log throw ] unless ;
: gl-program-info-log ( program -- log )
dup gl-program-info-log-length dup [
+ 1 calloc &free
[ 0 <int> swap glGetProgramInfoLog ] keep
ascii alien>string
- ] with-malloc ;
+ ] with-destructors ;
: check-gl-program ( program -- program )
dup gl-program-ok? [ dup gl-program-info-log throw ] unless ;