Remove with-malloc, use destructors instead

db4
Slava Pestov 2008-12-02 21:51:21 -06:00
parent c4ff034229
commit 22be7e5928
5 changed files with 8 additions and 14 deletions

View File

@ -220,9 +220,7 @@ $nl
"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."

View File

@ -32,10 +32,6 @@ HELP: free
{ $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." } ;

View File

@ -87,9 +87,6 @@ PRIVATE>
: 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 ;

View File

@ -28,9 +28,10 @@ COM-INTERFACE: IDropTarget IUnknown {00000122-0000-0000-C000-000000000046}
HRESULT Drop ( IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect ) ;
: com-query-interface ( interface iid -- interface' )
"void*" heap-size [
[
"void*" malloc-object &free
[ IUnknown::QueryInterface ole32-error ] keep *void*
] with-malloc ;
] with-destructors ;
: com-add-ref ( interface -- interface )
[ IUnknown::AddRef drop ] keep ; inline

View File

@ -44,9 +44,10 @@ IN: opengl.shaders
: 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 ;
@ -79,9 +80,10 @@ PREDICATE: fragment-shader < gl-shader (fragment-shader?) ;
: 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 ;