change malloc-struct to initialize struct from initial values; add (malloc-struct) and (struct) words that leave their memory uninitialized

db4
Joe Groff 2009-08-29 22:40:13 -05:00
parent 7276fe44d7
commit 3e51bde484
3 changed files with 32 additions and 8 deletions

View File

@ -9,6 +9,15 @@ HELP: <struct-boa>
}
{ $description "This macro implements " { $link boa } " for " { $link struct } " classes. A struct of the given class is constructed, and its slots are initialized using values off the top of the datastack." } ;
HELP: (struct)
{ $values
{ "class" class }
{ "struct" struct }
}
{ $description "Allocates garbage-collected heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are left uninitialized; in most cases, the " { $link <struct> } " word, which initializes the struct's slots with their initial values, should be used instead." } ;
{ (struct) (malloc-struct) } related-words
HELP: <struct>
{ $values
{ "class" class }
@ -55,7 +64,14 @@ HELP: malloc-struct
{ "class" class }
{ "struct" struct }
}
{ $description "Allocates unmanaged C heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are zeroed out. The struct should be " { $link free } "d when it is no longer needed." } ;
{ $description "Allocates unmanaged C heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are initialized to their initial values. The struct should be " { $link free } "d when it is no longer needed." } ;
HELP: (malloc-struct)
{ $values
{ "class" class }
{ "struct" struct }
}
{ $description "Allocates unmanaged C heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are left uninitialized; to initialize the allocated memory with the slots' initial values, use " { $link malloc-struct } ". The struct should be " { $link free } "d when it is no longer needed." } ;
HELP: memory>struct
{ $values
@ -80,6 +96,9 @@ ARTICLE: "classes.struct" "Struct classes"
{ $subsection <struct-boa> }
{ $subsection malloc-struct }
{ $subsection memory>struct }
"When the contents of a struct will be immediately reset, faster primitive words are available that will create a struct without initializing its contents:"
{ $subsection (struct) }
{ $subsection (malloc-struct) }
"Structs have literal syntax like tuples:"
{ $subsection POSTPONE: S{ }
"Union structs are also supported, which behave like structs but share the same memory for all the type's slots."

View File

@ -63,7 +63,7 @@ UNION-STRUCT: struct-test-float-and-bits
[ 1.0 ] [ struct-test-float-and-bits <struct> 1.0 float>bits >>bits f>> ] unit-test
[ 4 ] [ struct-test-float-and-bits heap-size ] unit-test
[ ] [ [ struct-test-foo malloc-struct &free drop ] with-destructors ] unit-test
[ 123 ] [ [ struct-test-foo malloc-struct &free y>> ] with-destructors ] unit-test
STRUCT: struct-test-string-ptr
{ x char* } ;

View File

@ -37,6 +37,8 @@ M: struct equal?
[ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
} 2&& ;
: struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
: memory>struct ( ptr class -- struct )
[ 1array ] dip slots>tuple ;
@ -44,17 +46,20 @@ M: struct equal?
dup struct-class? [ '[ _ boa ] ] [ drop f ] if
] 1 define-partial-eval
: (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
'[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
: (malloc-struct) ( class -- struct )
[ heap-size malloc ] keep memory>struct ; inline
: malloc-struct ( class -- struct )
[ 1 swap heap-size calloc ] keep memory>struct ; inline
[ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ;
: (struct) ( class -- struct )
[ heap-size <byte-array> ] keep memory>struct ; inline
: struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
[ heap-size (byte-array) ] keep memory>struct ; inline
: <struct> ( class -- struct )
dup struct-prototype
[ >c-ptr clone swap memory>struct ] [ (struct) ] if* ; inline
[ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ;
MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
[