Modula-2 Reloaded ⋄ Replaced Items

Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Replaced Items

Predefined Procedures NEW and DISPOSE

Predefined procedures NEW and DISPOSE have been replaced by the NEW and RELEASE statements.

NEW(p) invokes ALLOCATE(p, TSIZE(TypeOf(p^)))
NEW(p, n) invokes ALLOCATE(p, TSIZE(TypeOf(p^)) + n * TSIZE(ElemType))
DISPOSE(p) invokes DEALLOCATE(p, SIZE(p^))

Procedure NEW

Procedure NEW is used to dynamically allocate storage at runtime. The procedure is defined as:

<*INLINE*> PROCEDURE NEW ( VAR p : <PointerType>; (* OPTIONAL *) n : <unsignedTypes> );
  (* calculates the required storage size for the base type of p, allocates a block of
     storage of the determined size and passes its address back in p, if p points to
     an indeterminate type, then the determinant value must be passed in n *)
Static Semantics:

A call of NEW must have one or two actual parameters. The first parameter may be a variable of any pointer type. If the first parameter is a pointer to an indeterminate type, then a second parameter must be passed in. The second parameter may be a value of type OCTET, CARDINAL or LONGCARD. A library procedure named ALLOCATE must be visible in the lexical scope where NEW is called. The library procedure must conform to the following signature:

PROCEDURE ALLOCATE ( VAR p : ADDRESS; size : LONGCARD );

Dynamic Semantics:

Procedure NEW allocates storage at runtime and passes a pointer to the allocated storage back in its first parameter. It calls function TSIZE to calculate the required storage size and it calls library procedure ALLOCATE to allocate storage. The semantics are defined as:

macro NEW ( byref p : pointer; optional n : anyCardinalType )

  targetType := baseTypeOf(p)
  baseSize := evaluate TSIZE(targetType)
  if isDeterminateType(targetType) then
    insert ALLOCATE(p, baseSize)
  elsif isIndeterminateType(targetType) and isPresent(n) then
    indeterminateComponent := symTabLookup(targetType, indeterminateField, name)
    componentType :=  baseTypeOf(p^.indeterminateComponent)
    insert ALLOCATE(p, baseSize + n * TSIZE(componentType))
    determinant := symTabLookup(targetType, determinantField, name)
    insert p^.determinant := n
  else
    raiseCompileTimeError
  endif

endm

Procedure DISPOSE

Procedure DISPOSE is used to release dynamically allocated storage that was reserved by a call to procedure NEW. The procedure is defined as:

<*INLINE*> PROCEDURE DISPOSE ( VAR p : <AnyPointerType> );
  (* release the storage previously allocated by a call to NEW and pointed to by p *)
Static Semantics:

A call of DISPOSE must have one actual parameter. The parameter may be a variable of any pointer type. A library procedure named DEALLOCATE must be visible in the lexical scope where DISPOSE is called. The library procedure must conform to the following signature:

PROCEDURE DEALLOCATE ( VAR p : ADDRESS; size : LONGCARD );

Dynamic Semantics:

Procedure DISPOSE first calls function SIZE to determine the allocation size of the storage block pointed to by its first argument and then it deallocates the storage block by calling library procedure DEALLOCATE passing the pointer and allocated size. The semantics are defined as:

macro DISPOSE ( byref p : pointer )

  insert DEALLOCATE(p, SIZE (p^))

endm