factor/basis/libc/libc.factor

107 lines
2.2 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2004, 2005 Mackenzie Straight
2008-05-15 00:23:12 -04:00
! Copyright (C) 2007, 2008 Slava Pestov
! Copyright (C) 2007, 2008 Doug Coleman
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-07-28 23:57:43 -04:00
USING: alien assocs continuations destructors kernel
2008-07-30 05:12:17 -04:00
namespaces accessors sets summary ;
2007-09-20 18:09:08 -04:00
IN: libc
<PRIVATE
: (malloc) ( size -- alien )
"void*" "libc" "malloc" { "ulong" } alien-invoke ;
: (calloc) ( count size -- alien )
"void*" "libc" "calloc" { "ulong" "ulong" } alien-invoke ;
: (free) ( alien -- )
"void" "libc" "free" { "void*" } alien-invoke ;
: (realloc) ( alien size -- newalien )
"void*" "libc" "realloc" { "void*" "ulong" } alien-invoke ;
2008-07-28 23:57:43 -04:00
SYMBOL: malloc-expiry
: mallocs ( -- assoc )
malloc-expiry get-global expired? [
2008-07-28 23:57:43 -04:00
-1 <alien> malloc-expiry set-global
H{ } clone dup \ mallocs set-global
] [
\ mallocs get-global
] if ;
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: double-free ;
2007-09-20 18:09:08 -04:00
2008-07-30 05:12:17 -04:00
M: double-free summary
drop "Free failed since memory is not allocated" ;
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
: add-malloc ( alien -- )
2008-07-28 23:57:43 -04:00
mallocs conjoin ;
2007-09-20 18:09:08 -04:00
: delete-malloc ( alien -- )
[
2008-07-28 23:57:43 -04:00
mallocs delete-at*
2007-09-20 18:09:08 -04:00
[ double-free ] unless drop
] when* ;
: malloc-exists? ( alien -- ? )
2008-07-28 23:57:43 -04:00
mallocs key? ;
2007-09-20 18:09:08 -04:00
PRIVATE>
: malloc ( size -- alien )
(malloc) check-ptr
dup add-malloc ;
: calloc ( count size -- alien )
(calloc) check-ptr
dup add-malloc ;
: realloc ( alien size -- newalien )
over malloc-exists? [ realloc-error ] unless
dupd (realloc) check-ptr
swap delete-malloc
dup add-malloc ;
: free ( alien -- )
dup delete-malloc
(free) ;
: memcpy ( dst src size -- )
"void" "libc" "memcpy" { "void*" "void*" "ulong" } alien-invoke ;
2008-05-11 18:43:34 -04:00
: strlen ( alien -- len )
"size_t" "libc" "strlen" { "char*" } alien-invoke ;
2008-05-15 00:23:12 -04:00
<PRIVATE
! Memory allocations
TUPLE: memory-destructor alien disposed ;
2008-05-15 00:23:12 -04:00
M: memory-destructor dispose* alien>> free ;
PRIVATE>
: &free ( alien -- alien )
dup f memory-destructor boa &dispose drop ; inline
2008-05-15 00:23:12 -04:00
: |free ( alien -- alien )
dup f memory-destructor boa |dispose drop ; inline