 : preserve-errno ( quot -- )
     errno [ call ] dip set-errno ; inline
 
-: (malloc) ( size -- alien )
-    void* "libc" "malloc" { ulong } alien-invoke ;
+LIBRARY: libc
 
-: (calloc) ( count size -- alien )
-    void* "libc" "calloc" { ulong ulong } alien-invoke ;
+FUNCTION-ALIAS: (malloc)
+    void* malloc ( ulong size ) ;
 
-: (free) ( alien -- )
-    void "libc" "free" { void* } alien-invoke ;
+FUNCTION-ALIAS: (calloc)
+    void* calloc ( ulong count,  ulong size ) ;
 
-: (realloc) ( alien size -- newalien )
-    void* "libc" "realloc" { void* ulong } alien-invoke ;
+FUNCTION-ALIAS: (free)
+    void free ( void* alien ) ;
+
+FUNCTION-ALIAS: (realloc)
+    void* realloc ( void* alien, ulong size ) ;
 
 <PRIVATE
 

: free ( alien -- )
    >c-ptr [ delete-malloc ] [ (free) ] bi ;

: memcpy ( dst src size -- )
    void "libc" "memcpy" { void* void* ulong } alien-invoke ;

: memcmp ( a b size -- cmp )
    int "libc" "memcmp" { void* void* ulong } alien-invoke ;

: memory= ( a b size -- ? )
    memcmp 0 = ;

: strlen ( alien -- len )
    size_t "libc" "strlen" { c-string } alien-invoke ;

DESTRUCTOR: free
DESTRUCTOR: (free)

! Copyright (C) 2007, 2010 Slava Pestov
! Copyright (C) 2007, 2008 Doug Coleman
! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types alien.syntax assocs continuations
alien.destructors kernel namespaces accessors sets summary
destructors destructors.private ;
IN: libc

LIBRARY: factor

FUNCTION-ALIAS: errno
    int err_no ( ) ;

FUNCTION-ALIAS: set-errno
    void set_err_no ( int err-no ) ;

: clear-errno ( -- )
    0 set-errno ;
