How to resolve the algorithm Arena storage pool step by step in the Tcl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arena storage pool step by step in the Tcl programming language

Table of Contents

Problem Statement

Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as where   T   is the type of an allocated object,   and   P   is a reference to the object. The storage pool chosen by the allocator can be determined by either:

In the former case objects can be allocated only in one storage pool. In the latter case objects of the type can be allocated in any storage pool or on the stack.

The task is to show how allocators and user-defined storage pools are supported by the language. In particular:

Explain what controls the storage pool choice in the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arena storage pool step by step in the Tcl programming language

Source code in the tcl programming language

package require Tcl 8.6
oo::class create Pool {
    superclass oo::class
    variable capacity pool busy
    unexport create
    constructor args {
	next {*}$args
	set capacity 100
	set pool [set busy {}]
    }
    method new {args} {
	if {[llength $pool]} {
	    set pool [lassign $pool obj]
	} else {
	    if {[llength $busy] >= $capacity} {
		throw {POOL CAPACITY} "exceeded capacity: $capacity"
	    }
	    set obj [next]
	    set newobj [namespace current]::[namespace tail $obj]
	    rename $obj $newobj
	    set obj $newobj
	}
	try {
	    [info object namespace $obj]::my Init {*}$args
	} on error {msg opt} {
	    lappend pool $obj
	    return -options $opt $msg
	}
	lappend busy $obj
	return $obj
    }
    method ReturnToPool obj {
	try {
	    if {"Finalize" in [info object methods $obj -all -private]} {
		[info object namespace $obj]::my Finalize
	    }
	} on error {msg opt} {
	    after 0 [list return -options $opt $msg]
	    return false
	}
	set idx [lsearch -exact $busy $obj]
	set busy [lreplace $busy $idx $idx]
	if {[llength $pool] + [llength $busy] + 1 <= $capacity} {
	    lappend pool $obj
	    return true
	} else {
	    return false
	}
    }
    method capacity {{value {}}} {
	if {[llength [info level 0]] == 3} {
	    if {$value < $capacity} {
		while {[llength $pool] > 0 && [llength $pool] + [llength $busy] > $value} {
		    set pool [lassign $pool obj]
		    rename $obj {}
		}
	    }
	    set capacity [expr {$value >> 0}]
	} else {
	    return $capacity
	}
    }
    method clearPool {} {
	foreach obj $busy {
	    $obj destroy
	}
    }
    method destroy {} {
	my clearPool
	next
    }
    self method create {class {definition {}}} {
	set cls [next $class $definition]
	oo::define $cls method destroy {} {
	    if {![[info object namespace [self class]]::my ReturnToPool [self]]} {
		next
	    }
	}
	return $cls
    }
}


Pool create PoolExample {
    variable int

    method Init value {
	puts stderr "Initializing [self] with $value"
	set int $value
	incr int 0
    }
    method Finalize {} {
	puts stderr "Finalizing [self] which held $int"
    }

    method value {{newValue {}}} {
	if {[llength [info level 0]] == 3} {
	    set int [incr newValue 0]
	} else {
	    return $int
	}
    }
}

PoolExample capacity 10
set objs {}
try {
    for {set i 0} {$i < 20} {incr i} {
	lappend objs [PoolExample new $i]
    }
} trap {POOL CAPACITY} msg {
    puts "trapped: $msg"
}
puts -nonewline "number of objects: [llength $objs]\n\t"
foreach o $objs {
    puts -nonewline "[$o value] "
}
puts ""
set objs [lassign $objs a b c]
$a destroy
$b destroy
$c destroy
PoolExample capacity 9
try {
    for {} {$i < 20} {incr i} {
	lappend objs [PoolExample new $i]
    }
} trap {POOL CAPACITY} msg {
    puts "trapped: $msg"
}
puts -nonewline "number of objects: [llength $objs]\n\t"
foreach o $objs {
    puts -nonewline "[$o value] "
}
puts ""
PoolExample clearPool


  

You may also check:How to resolve the algorithm Find Chess960 starting position identifier step by step in the Phix programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Array length step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm File input/output step by step in the Wren programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the Cowgol programming language