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.
|
2014-07-06 13:46:02 -04:00
|
|
|
USING: accessors alien alien.c-types alien.destructors
|
|
|
|
alien.syntax destructors destructors.private kernel math
|
2014-10-24 20:09:27 -04:00
|
|
|
namespaces sequences sets summary system vocabs ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: libc
|
|
|
|
|
2014-07-04 06:19:29 -04:00
|
|
|
HOOK: strerror os ( errno -- str )
|
|
|
|
|
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
|
2015-07-19 19:25:30 -04:00
|
|
|
int err_no ( )
|
2010-04-15 00:50:30 -04:00
|
|
|
|
|
|
|
FUNCTION-ALIAS: set-errno
|
2015-07-19 19:25:30 -04:00
|
|
|
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)
|
2015-07-19 19:25:30 -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)
|
2015-07-19 19:25:30 -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)
|
2015-07-19 19:25:30 -04:00
|
|
|
void free ( void* alien )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-04-15 00:50:30 -04:00
|
|
|
FUNCTION-ALIAS: (realloc)
|
2015-07-19 19:25:30 -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
|
2015-07-19 19:25:30 -04:00
|
|
|
char* strerror ( int errno )
|
2014-07-04 06:31:11 -04:00
|
|
|
|
|
|
|
! 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 ;
|
|
|
|
|
2015-08-13 19:13:05 -04:00
|
|
|
: (throw-errno) ( errno -- * ) dup strerror libc-error ;
|
2014-11-21 13:19:12 -05:00
|
|
|
|
|
|
|
: throw-errno ( -- * ) errno (throw-errno) ;
|
2014-07-04 06:11:45 -04:00
|
|
|
|
2014-11-21 12:29:45 -05:00
|
|
|
: io-error ( n -- ) 0 < [ throw-errno ] when ;
|
2014-07-04 06:11:45 -04:00
|
|
|
|
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 )
|
2015-08-13 19:13:05 -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
|
2015-08-13 19:13:05 -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
|
|
|
|
2015-07-19 19:25:30 -04:00
|
|
|
FUNCTION: void memset ( void* buf, int char, size_t size )
|
2010-07-16 17:13:44 -04:00
|
|
|
|
2015-07-19 19:25:30 -04:00
|
|
|
FUNCTION: void memcpy ( void* dst, void* src, ulong size )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2015-07-19 19:25: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
|
|
|
|
2015-07-19 19:25:30 -04:00
|
|
|
FUNCTION: size_t strlen ( c-string alien )
|
2008-05-15 00:23:12 -04:00
|
|
|
|
2015-07-19 19:25:30 -04:00
|
|
|
FUNCTION: int system ( c-string command )
|
2010-06-02 02:58:04 -04:00
|
|
|
|
2009-01-17 18:58:31 -05:00
|
|
|
DESTRUCTOR: free
|
2014-07-04 06:31:11 -04:00
|
|
|
|
2014-07-06 13:46:02 -04:00
|
|
|
! For libc.linux, libc.windows, libc.macosx...
|
2014-10-24 20:09:27 -04:00
|
|
|
<< "libc." os name>> append require >>
|