2007-09-20 18:09:08 -04:00
|
|
|
! Copyright (C) 2004, 2005 Mackenzie Straight
|
2010-03-30 17:31:41 -04:00
|
|
|
! Copyright (C) 2007, 2010 Slava Pestov
|
2008-05-15 00:23:12 -04:00
|
|
|
! Copyright (C) 2007, 2008 Doug Coleman
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2013-10-17 11:05:26 -04:00
|
|
|
USING: accessors alien alien.c-types alien.destructors alien.syntax assocs
|
|
|
|
combinators continuations destructors destructors.private kernel math
|
2013-10-21 16:58:33 -04:00
|
|
|
namespaces prettyprint sequences sets summary system vocabs vocabs.parser ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: libc
|
|
|
|
|
2014-07-04 06:19:29 -04:00
|
|
|
HOOK: strerror os ( errno -- str )
|
|
|
|
|
2014-07-04 06:25:57 -04:00
|
|
|
! For libc.linux, libc.windows, libc.macosx...
|
2013-10-23 09:45:45 -04:00
|
|
|
<< "libc." os unparse append require >>
|
2013-10-17 11:05:26 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
LIBRARY: factor
|
2009-02-06 19:22:28 -05:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION-ALIAS: errno
|
|
|
|
int err_no ( ) ;
|
|
|
|
|
|
|
|
FUNCTION-ALIAS: set-errno
|
|
|
|
void set_err_no ( int err-no ) ;
|
2010-02-03 18:20:24 -05:00
|
|
|
|
2009-02-06 19:22:28 -05:00
|
|
|
: clear-errno ( -- )
|
2010-02-03 18:20:24 -05:00
|
|
|
0 set-errno ;
|
|
|
|
|
|
|
|
: preserve-errno ( quot -- )
|
|
|
|
errno [ call ] dip set-errno ; inline
|
2009-02-06 19:22:28 -05:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
LIBRARY: libc
|
|
|
|
|
|
|
|
FUNCTION-ALIAS: (malloc)
|
2010-05-18 22:34:52 -04:00
|
|
|
void* malloc ( size_t size ) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION-ALIAS: (calloc)
|
2010-05-18 22:34:52 -04:00
|
|
|
void* calloc ( size_t count, size_t size ) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION-ALIAS: (free)
|
|
|
|
void free ( void* alien ) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION-ALIAS: (realloc)
|
2010-05-18 22:34:52 -04:00
|
|
|
void* realloc ( void* alien, size_t size ) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2014-07-04 06:31:11 -04:00
|
|
|
FUNCTION-ALIAS: strerror_unsafe
|
|
|
|
char* strerror ( int errno ) ;
|
|
|
|
|
|
|
|
! Add a default strerror even though it's not threadsafe
|
|
|
|
M: object strerror strerror_unsafe ;
|
|
|
|
|
2014-07-04 06:11:45 -04:00
|
|
|
ERROR: libc-error errno message ;
|
|
|
|
|
|
|
|
: (io-error) ( -- * ) errno dup strerror libc-error ;
|
|
|
|
|
|
|
|
: io-error ( n -- ) 0 < [ (io-error) ] when ;
|
|
|
|
|
2010-03-30 17:31:41 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-08-24 21:21:03 -04:00
|
|
|
! We stick malloc-ptr instances in the global disposables set
|
|
|
|
TUPLE: malloc-ptr value continuation ;
|
|
|
|
|
|
|
|
M: malloc-ptr hashcode* value>> hashcode* ;
|
|
|
|
|
|
|
|
M: malloc-ptr equal?
|
2012-07-21 13:22:44 -04:00
|
|
|
over malloc-ptr? [ [ value>> ] same? ] [ 2drop f ] if ;
|
2009-08-24 21:21:03 -04:00
|
|
|
|
|
|
|
: <malloc-ptr> ( value -- malloc-ptr )
|
|
|
|
malloc-ptr new swap >>value ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2008-03-20 16:00:49 -04:00
|
|
|
ERROR: bad-ptr ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-07-30 05:12:17 -04:00
|
|
|
M: bad-ptr summary
|
|
|
|
drop "Memory allocation failed" ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: check-ptr ( c-ptr -- c-ptr )
|
2008-03-20 16:00:49 -04:00
|
|
|
[ bad-ptr ] unless* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-03-20 16:00:49 -04:00
|
|
|
ERROR: realloc-error ptr size ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-07-30 05:12:17 -04:00
|
|
|
M: realloc-error summary
|
|
|
|
drop "Memory reallocation failed" ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-01-17 21:10:56 -05:00
|
|
|
: add-malloc ( alien -- alien )
|
2009-08-24 21:21:03 -04:00
|
|
|
dup <malloc-ptr> register-disposable ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: delete-malloc ( alien -- )
|
2009-08-24 21:21:03 -04:00
|
|
|
[ <malloc-ptr> unregister-disposable ] when* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: malloc-exists? ( alien -- ? )
|
2013-03-08 19:30:33 -05:00
|
|
|
<malloc-ptr> disposables get in? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: malloc ( size -- alien )
|
2009-01-17 21:10:56 -05:00
|
|
|
(malloc) check-ptr add-malloc ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: calloc ( count size -- alien )
|
2009-01-17 21:10:56 -05:00
|
|
|
(calloc) check-ptr add-malloc ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: realloc ( alien size -- newalien )
|
2009-02-06 05:37:28 -05:00
|
|
|
[ >c-ptr ] dip
|
2007-09-20 18:09:08 -04:00
|
|
|
over malloc-exists? [ realloc-error ] unless
|
2009-01-17 21:10:56 -05:00
|
|
|
[ drop ] [ (realloc) check-ptr ] 2bi
|
|
|
|
[ delete-malloc ] [ add-malloc ] bi* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: free ( alien -- )
|
2009-02-06 05:37:28 -05:00
|
|
|
>c-ptr [ delete-malloc ] [ (free) ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-07-16 17:13:44 -04:00
|
|
|
FUNCTION: void memset ( void* buf, int char, size_t size ) ;
|
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION: void memcpy ( void* dst, void* src, ulong size ) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION: int memcmp ( void* a, void* b, ulong size ) ;
|
2009-08-26 19:05:38 -04:00
|
|
|
|
2010-08-06 03:16:25 -04:00
|
|
|
: memory= ( a b size -- ? ) memcmp 0 = ; inline
|
2009-08-26 19:05:38 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION: size_t strlen ( c-string alien ) ;
|
2008-05-15 00:23:12 -04:00
|
|
|
|
2010-06-02 02:58:04 -04:00
|
|
|
FUNCTION: int system ( c-string command ) ;
|
|
|
|
|
2009-01-17 18:58:31 -05:00
|
|
|
DESTRUCTOR: free
|
2010-04-14 16:08:45 -04:00
|
|
|
DESTRUCTOR: (free)
|
2014-07-04 06:31:11 -04:00
|
|
|
|
|
|
|
! For strerror on Unix all platforms
|
|
|
|
<< os windows? [ "libc.unix" require ] unless >>
|