factor/library/compiler/alien/malloc.facts

42 lines
3.1 KiB
Plaintext
Raw Normal View History

2006-03-26 21:08:02 -05:00
IN: libc
USING: help ;
2006-08-16 21:55:53 -04:00
HELP: malloc ( size -- alien )
2006-03-26 21:08:02 -05:00
{ $values { "size" "a non-negative integer" } { "alien" "an alien address" } }
{ $description "Allocates a block of " { $snippet "size" } " bytes from the operating system. The contents of the block are undefined."
$terpri
"Outputs " { $link f } " if memory allocation failed, so calls to this word should be followed by a call to " { $link check-ptr } "." }
2006-03-27 02:34:07 -05:00
{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } ;
2006-03-26 21:08:02 -05:00
2006-08-16 21:55:53 -04:00
HELP: calloc ( count size -- alien )
2006-03-26 21:08:02 -05:00
{ $values { "count" "a non-negative integer" } { "size" "a non-negative integer" } { "alien" "an alien address" } }
{ $description "Allocates a block of " { $snippet "count * size" } " bytes from the operating system. The contents of the block are initially zero."
$terpri
"Outputs " { $link f } " if memory allocation failed, so calls to this word should be followed by a call to " { $link check-ptr } "." }
2006-03-27 02:34:07 -05:00
{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } ;
2006-03-26 21:08:02 -05:00
2006-08-16 21:55:53 -04:00
HELP: realloc ( alien size -- newalien )
2006-03-26 21:08:02 -05:00
{ $values { "alien" "an alien address" } { "size" "a non-negative integer" } { "newalien" "an alien address" } }
{ $description "Allocates a new block of " { $snippet "size" } " bytes from the operating system. The contents of " { $snippet "alien" } ", which itself must be a block previously returned by " { $link malloc } " or " { $link realloc } ", are copied into the new block, and the old block is freed."
$terpri
"Outputs " { $link f } " if memory allocation failed, so calls to this word should be followed by a call to " { $link check-ptr } "." }
2006-03-27 02:34:07 -05:00
{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } ;
2006-03-26 21:08:02 -05:00
2006-08-17 23:53:51 -04:00
HELP: memcpy ( dst src size -- )
2006-03-26 21:08:02 -05:00
{ $values { "dst" "an alien address" } { "src" "an alien address" } { "size" "a non-negative integer" } }
{ $description "Copies " { $snippet "size" } " bytes from " { $snippet "src" } " to " { $snippet "dst" } "." }
{ $warning "As per the BSD C library documentation, the behavior is undefined if the source and destination overlap." } ;
2006-08-16 21:55:53 -04:00
HELP: check-ptr
2006-03-26 21:08:02 -05:00
{ $values { "c-ptr" "an alien address, byte array, or " { $link f } } { "checked" "an alien address or byte array with non-zero address" } }
2006-08-01 17:35:00 -04:00
{ $description "Throws an error if the input is " { $link f } ". Otherwise the object remains on the data stack. This word should be used to check the return values of " { $link malloc } " and " { $link realloc } " before use." }
2006-08-16 21:55:53 -04:00
{ $error-description "Callers of " { $link malloc } " and " { $link realloc } " should use " { $link check-ptr } " to throw an error in the case of a memory allocation failure." } ;
2006-03-29 17:44:04 -05:00
2006-08-16 21:55:53 -04:00
HELP: free ( ptr -- )
2006-03-29 17:44:04 -05:00
{ $values { "ptr" "an alien address" } }
{ $description "Deallocates a block of memory allocated by " { $link malloc } ", " { $link calloc } " or " { $link realloc } "." } ;
2006-08-16 21:55:53 -04:00
HELP: with-malloc
{ $values { "n" "a positive integer" } { "quot" "a quotation with stack effect " { $snippet "( 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." } ;