September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
10
Task/Memory-allocation/ALGOL-W/memory-allocation.alg
Normal file
10
Task/Memory-allocation/ALGOL-W/memory-allocation.alg
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
begin
|
||||
% define a record structure - instances must be created dynamically %
|
||||
record Element ( integer atomicNumber; string(16) name );
|
||||
reference(Element) X;
|
||||
% allocate and initialise memory for X - heap storage is the only option %
|
||||
X := Element( 1, "Hydrogen" );
|
||||
% allocate new memory for X, the original could now be garbage collected %
|
||||
X := Element( 2, "Helium" )
|
||||
% the memory allocated will now be garbage collected - there is no explicit de-allocation %
|
||||
end.
|
||||
30
Task/Memory-allocation/Kotlin/memory-allocation.kotlin
Normal file
30
Task/Memory-allocation/Kotlin/memory-allocation.kotlin
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// version 1.1.2
|
||||
|
||||
class MyClass(val myInt: Int) {
|
||||
// in theory this method should be called automatically prior to GC
|
||||
protected fun finalize() {
|
||||
println("MyClass being finalized...")
|
||||
}
|
||||
}
|
||||
|
||||
fun myFun() {
|
||||
val mc: MyClass = MyClass(2) // new non-nullable MyClass object allocated on the heap
|
||||
println(mc.myInt)
|
||||
var mc2: MyClass? = MyClass(3) // new nullable MyClass object allocated on the heap
|
||||
println(mc2?.myInt)
|
||||
mc2 = null // allowed as mc2 is nullable
|
||||
println(mc2?.myInt)
|
||||
// 'mc' and 'mc2' both become eligible for garbage collection here as no longer used
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
myFun()
|
||||
Thread.sleep(3000) // allow time for GC to execute
|
||||
val i: Int = 4 // new non-nullable Int allocated on stack
|
||||
println(i)
|
||||
var j: Int? = 5 // new nullable Int allocated on heap
|
||||
println(j)
|
||||
j = null // allowed as 'j' is nullable
|
||||
println(j)
|
||||
// 'j' becomes eligible for garbage collection here as no longer used
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
PRINT PEEK 16388+256*16389
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
10 REM ABCDEFGH
|
||||
20 LET P$="3E03010400814FC9"
|
||||
30 LET ADDR=16514
|
||||
40 POKE ADDR,CODE P$*16+CODE P$(2)-476
|
||||
50 LET P$=P$(3 TO )
|
||||
60 LET ADDR=ADDR+1
|
||||
70 IF P$<>"" THEN GOTO 40
|
||||
80 CLEAR
|
||||
90 PRINT USR 16514
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
3e 03 ld a, 3
|
||||
01 04 00 ld bc,0004
|
||||
81 add a, c
|
||||
4f ld c, a
|
||||
c9 ret
|
||||
|
|
@ -0,0 +1 @@
|
|||
10 REM Y▀▀:▖ ▟?TAN
|
||||
104
Task/Memory-allocation/X86-Assembly/memory-allocation.x86
Normal file
104
Task/Memory-allocation/X86-Assembly/memory-allocation.x86
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
; linux x86_64
|
||||
|
||||
struc block
|
||||
free: resb 1 ; whether or not this block is free
|
||||
size: resb 2 ; size of the chunk of memory
|
||||
next: resb 8 ; the next chunk after this one
|
||||
mem:
|
||||
endstruc
|
||||
|
||||
section .data
|
||||
hStart: dq 0 ; the beginning of our heap space
|
||||
break: dq 0 ; the current end of our heap space
|
||||
|
||||
|
||||
section .text
|
||||
|
||||
Allocate:
|
||||
|
||||
push rdi ; save the size argument
|
||||
|
||||
cmp qword [break], 0 ; if breakpoint is zero this
|
||||
je firstAlloc ; is the first call to allocate
|
||||
|
||||
mov rdi, qword [hStart] ; else address of heap start
|
||||
|
||||
findBlock: ; look for a suitable block of memory
|
||||
|
||||
cmp byte [rdi + free], 2
|
||||
je newBlock ; end of heap reached, create new block
|
||||
|
||||
cmp byte [rdi + free], 0
|
||||
je skipBlock ; this block taken
|
||||
|
||||
; this block is free, make
|
||||
; sure it's big enough
|
||||
mov bx, word [rdi + size]
|
||||
mov rcx, qword [rsp] ; compare against our size arg
|
||||
cmp cx, bx
|
||||
jg skipBlock ; keep looking if not big enough
|
||||
|
||||
mov byte [rdi + free], 0 ; else mark as taken
|
||||
add rdi, mem
|
||||
add rsp, 8 ; discard size arg, we didn't need it
|
||||
mov rax, rdi ; return pointer to this block
|
||||
ret
|
||||
|
||||
skipBlock:
|
||||
mov rsi, qword [rdi + next] ; load next
|
||||
mov rdi, rsi ' block address
|
||||
jmp findBlock
|
||||
|
||||
newBlock:
|
||||
mov rax, rdi
|
||||
add rdi, 1024
|
||||
cmp rdi, qword [break]
|
||||
jl initAndAllocate
|
||||
push rax
|
||||
mov rdi, qword [break] ; if we are getting low on
|
||||
add rdi, 4096 ; heap space, we ask OS for
|
||||
mov rax, 12 ; more memory with brk syscall
|
||||
syscall
|
||||
cmp rax, qword [break] ; if breakpoint has not increased,
|
||||
je allocFail ; then memory could not be allocated
|
||||
mov qword [break], rax
|
||||
pop rax
|
||||
jmp initAndAllocate
|
||||
|
||||
|
||||
firstAlloc: ; extra work has to be done on first
|
||||
mov rax, 12 ; call to this subroutine
|
||||
mov rdi, 0
|
||||
syscall
|
||||
mov qword [hStart], rax ; init heap start
|
||||
add rax, 4096
|
||||
mov rdi, rax
|
||||
mov rax, 12 ; get heap memory with sys brk
|
||||
syscall
|
||||
cmp rax, qword [hStart]
|
||||
je allocFail
|
||||
mov qword [break], rax
|
||||
mov rax, qword [hStart]
|
||||
|
||||
initAndAllocate:
|
||||
mov byte [rax + free], 0 ; mark block free
|
||||
pop rdi ; pop size arg off stack
|
||||
mov word [rax + size], di ; mark it's size
|
||||
lea rsi, [rax + mem + rdi]
|
||||
mov byte [rsi + free], 2 ; mark heap end block
|
||||
mov qword [rax + next], rsi ; mark next block
|
||||
add rax, mem ; return pointer to block's memory space
|
||||
ret
|
||||
|
||||
allocFail: ; exit(1) when allocation fails
|
||||
mov rax, 60
|
||||
mov rdi, 1
|
||||
syscall
|
||||
ret
|
||||
|
||||
; free this block so it can be
|
||||
; reused in a subsequent call to allocate
|
||||
Release:
|
||||
sub rdi, mem
|
||||
mov byte [rdi + free], 1
|
||||
ret
|
||||
2
Task/Memory-allocation/Zkl/memory-allocation.zkl
Normal file
2
Task/Memory-allocation/Zkl/memory-allocation.zkl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Data(123); // this bit bucket expects hold 123 bytes
|
||||
List.createLong(123); // this list expects to hold 123 elements
|
||||
Loading…
Add table
Add a link
Reference in a new issue