Annotation of gforth/struct.fs, revision 1.14

1.8       anton       1: \ data structures (like C structs)
                      2: 
1.11      anton       3: \ Copyright (C) 1995, 1997 Free Software Foundation, Inc.
1.8       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
1.14    ! anton      21: : naligned ( addr1 n -- addr2 ) \ gforth
1.12      anton      22: \g @code{addr2} is the aligned version of @code{addr1} wrt the
                     23: \g alignment @code{n}.
1.1       anton      24:  1- tuck +  swap invert and ;
1.14    ! anton      25: 
        !            26: ' naligned alias nalign \ old name, obsolete
1.1       anton      27: 
1.11      anton      28: : dozerofield ( -- )
                     29:     \ a field that makes no change
                     30:     \ to enable accessing the offset with "['] <field> >body @" this
                     31:     \ is not implemented with "['] noop alias"
                     32:     last @
                     33:     if
                     34:        immediate
                     35:     then
1.12      anton      36: does> ( name execution: -- )
1.11      anton      37:     drop ;
                     38: 
1.12      anton      39: : field, ( align1 offset1 align size --  align2 offset2 )
1.13      anton      40:     swap rot over nalign dup , ( align1 size align offset )
                     41:     rot + >r nalign r> ;
1.12      anton      42: 
                     43: : create-field ( align1 offset1 align size --  align2 offset2 )
                     44:     create field, ;
                     45: 
                     46: : field ( align1 offset1 align size "name" --  align2 offset2 ) \ gforth
1.11      anton      47:     \G name execution: ( addr1 -- addr2 )
1.12      anton      48:     2 pick 
1.11      anton      49:     if \ field offset <> 0
                     50:        [IFDEF]  (Field)
                     51:            (Field)
                     52:        [ELSE]
                     53:            Header reveal dofield: cfa,
                     54:        [THEN]
                     55:     else
                     56:        create dozerofield
1.12      anton      57:     then
                     58:     field, ;
1.1       anton      59: 
1.12      anton      60: : end-struct ( align size "name" -- ) \ gforth
                     61: \g @code{name} execution: @code{addr1 -- addr1+offset1}@*
                     62: \g create a field @code{name} with offset @code{offset1}, and the type
                     63: \g given by @code{size align}. @code{offset2} is the offset of the
                     64: \g next field, and @code{align2} is the alignment of all fields.
                     65:     over nalign \ pad size to full alignment
                     66:     2constant ;
                     67: 
                     68: 1 chars 0 end-struct struct ( -- align size ) \ gforth
                     69: \g an empty structure, used to start a structure definition.
                     70: 
                     71: \ type descriptors
                     72: 1 aligned   1 cells   2constant cell% ( -- align size ) \ gforth
                     73: 1 chars     1 chars   2constant char% ( -- align size ) \ gforth
                     74: 1 faligned  1 floats  2constant float% ( -- align size ) \ gforth
                     75: 1 dfaligned 1 dfloats 2constant dfloat% ( -- align size ) \ gforth
                     76: 1 sfaligned 1 sfloats 2constant sfloat% ( -- align size ) \ gforth
                     77: cell% 2*              2constant double% ( -- align size ) \ gforth
                     78: 
                     79: \ memory allocation words
                     80: ' drop alias %alignment ( align size -- align ) \ gforth
                     81: \g the alignment of the structure
                     82: ' nip alias %size ( align size -- size ) \ gforth
                     83: \g the size of the structure
                     84: 
                     85: : %align ( align size -- ) \ gforth
                     86:     \G align the data space pointer to the alignment @code{align}. 
                     87:     drop here swap nalign here - allot ;
                     88: 
                     89: : %allot ( align size -- addr ) \ gforth
                     90:     \g allot @code{size} address units of data space with alignment
                     91:     \g @code{align}; the resulting block of data is found at
                     92:     \g @code{addr}.
                     93:     tuck %align
1.2       anton      94:     here swap allot ;
                     95: 
1.12      anton      96: : %allocate ( align size -- addr ior ) \ gforth
                     97:     \g allocate @code{size} address units with alignment @code{align},
                     98:     \g similar to @code{allocate}.
                     99:     nip allocate ;
                    100: 
                    101: : %alloc ( size align -- addr ) \ gforth
                    102:     \g allocate @code{size} address units with alignment @code{align},
                    103:     \g giving a data block at @code{addr}; @code{throw}s an ior code
                    104:     \g if not successful.
                    105:     %allocate throw ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>