From 22be7e59284f0999bcc59f8f4b4036d06fc454fc Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 2 Dec 2008 21:51:21 -0600 Subject: [PATCH] Remove with-malloc, use destructors instead --- basis/alien/c-types/c-types-docs.factor | 4 +--- basis/libc/libc-docs.factor | 4 ---- basis/libc/libc.factor | 3 --- basis/windows/com/com.factor | 5 +++-- extra/opengl/shaders/shaders.factor | 6 ++++-- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/basis/alien/c-types/c-types-docs.factor b/basis/alien/c-types/c-types-docs.factor index 102d7b9612..1ec9a82169 100644 --- a/basis/alien/c-types/c-types-docs.factor +++ b/basis/alien/c-types/c-types-docs.factor @@ -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." diff --git a/basis/libc/libc-docs.factor b/basis/libc/libc-docs.factor index 37a3b7068f..b89f4174bf 100644 --- a/basis/libc/libc-docs.factor +++ b/basis/libc/libc-docs.factor @@ -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." } ; diff --git a/basis/libc/libc.factor b/basis/libc/libc.factor index cf4e2fb722..c4d351e6a0 100644 --- a/basis/libc/libc.factor +++ b/basis/libc/libc.factor @@ -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 ; diff --git a/basis/windows/com/com.factor b/basis/windows/com/com.factor index 9649de6402..0e1a907ca7 100644 --- a/basis/windows/com/com.factor +++ b/basis/windows/com/com.factor @@ -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 diff --git a/extra/opengl/shaders/shaders.factor b/extra/opengl/shaders/shaders.factor index 93ca6b32cc..a88ea6de4d 100755 --- a/extra/opengl/shaders/shaders.factor +++ b/extra/opengl/shaders/shaders.factor @@ -44,9 +44,10 @@ IN: opengl.shaders : gl-shader-info-log ( shader -- log ) dup gl-shader-info-log-length dup [ + 1 calloc &free [ 0 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 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 ;