内存单元定义

runtime/allocator.reds

;-- cell header bits layout --
;   31:     lock                            ;-- lock series for active thread access only
;   30:     new-line                        ;-- new-line (LF) marker (before the slot)
;   29-25:  arity                           ;-- arity for routine! functions.
;   24:     self?                           ;-- self-aware context flag
;   23:     node-body                       ;-- op! body points to a block node (instead of native code)
;   22-19:  tuple-size                      ;-- size of tuple
;   18:     series-owned                    ;-- mark a series owned by an object
;   17:     owner                           ;-- indicate that an object is an owner
;   16:     native! op                      ;-- operator is made from a native! function
;   15:     extern flag                     ;-- routine code is external to Red (from FFI)
;   14-8:   <reserved>
;   7-0:    datatype ID                     ;-- datatype number

cell!: alias struct! [
    header  [integer!]                      ;-- cell's header flags
    data1   [integer!]                      ;-- placeholders to make a 128-bit cell
    data2   [integer!]
    data3   [integer!]
]

;-- series flags --
;   31:     used                            ;-- 1 = used, 0 = free
;   30:     type                            ;-- always 0 for series-buffer!
;   29-28:  insert-opt                      ;-- optimized insertions: 2 = head, 1 = tail, 0 = both
;   27:     mark                            ;-- mark as referenced for the GC (mark phase)
;   26:     lock                            ;-- lock series for active thread access only
;   25:     immutable                       ;-- mark as read-only
;   24:     big                             ;-- indicates a big series (big-frame!)
;   23:     small                           ;-- reserved
;   22:     stack                           ;-- series buffer is allocated on stack
;   21:     permanent                       ;-- protected from GC (system-critical series)
;   20:     fixed                           ;-- series cannot be relocated (system-critical series)
;   19:     complement                      ;-- complement flag for bitsets
;   18:     UTF-16 cache                    ;-- signifies that the string cache is UTF-16 encoded (UTF-8 by default)
;   17:     owned                           ;-- series is owned by an object
;   16-3:   <reserved>
;   4-0:    unit                            ;-- size in bytes of atomic element stored in buffer
                                            ;-- 0: UTF-8, 1: Latin1/binary, 2: UCS-2, 4: UCS-4, 16: block! cell
series-buffer!: alias struct! [
    flags   [integer!]                      ;-- series flags
    node    [int-ptr!]                      ;-- point back to referring node
    size    [integer!]                      ;-- usable buffer size (series-buffer! struct excluded)
    offset  [cell!]                         ;-- series buffer offset pointer (insert at head optimization)
    tail    [cell!]                         ;-- series buffer tail pointer 
]

series-frame!: alias struct! [              ;-- series frame header
    next    [series-frame!]                 ;-- next frame or null
    prev    [series-frame!]                 ;-- previous frame or null
    heap    [series-buffer!]                ;-- point to allocatable region
    tail    [byte-ptr!]                     ;-- point to last byte in allocatable region
]

node-frame!: alias struct! [                ;-- node frame header
    next    [node-frame!]                   ;-- next frame or null
    prev    [node-frame!]                   ;-- previous frame or null
    nodes   [integer!]                      ;-- number of nodes
    bottom  [int-ptr!]                      ;-- bottom of stack (last entry, fixed)
    top     [int-ptr!]                      ;-- top of stack (first entry, moving)
]

big-frame!: alias struct! [                 ;-- big frame header (for >= 2MB series)
    flags   [integer!]                      ;-- bit 30: 1 (type = big)
    next    [big-frame!]                    ;-- next frame or null
    size    [integer!]                      ;-- size (up to 4GB - size? header)
    padding [integer!]                      ;-- make this header same size as series-buffer! header
]

关系

  • node: int-ptr!
  • series-buffer!: 管理nodeheadtail使用cell!单位
  • node-frame!: 分配node
  • series-frame!: 分配series-buffer!
#define node!               int-ptr!
#define series!             series-buffer!