September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -5,3 +5,14 @@ dealloc(a)
|
|||
# Allocate memory block on shared heap
|
||||
var b = allocShared(1000)
|
||||
deallocShared(b)
|
||||
|
||||
# Allocate and Dellocate a single int on the thread local heap
|
||||
var p = create(int, sizeof(int)) # allocate memory
|
||||
# create zeroes memory; createU does not.
|
||||
echo p[] # 0
|
||||
p[] = 123 # assign a value
|
||||
echo p[] # 123
|
||||
discard resize(p, 0) # deallocate it
|
||||
# p is now invalid. Let's set it to nil
|
||||
p = nil # set pointer to nil
|
||||
echo isNil(p) # true
|
||||
|
|
|
|||
|
|
@ -1,14 +1,29 @@
|
|||
( display total memory available )
|
||||
@memory putn
|
||||
display total memory available
|
||||
|
||||
( display unused memory )
|
||||
@memory here - putn
|
||||
~~~
|
||||
EOM n:put
|
||||
~~~
|
||||
|
||||
( display next free address )
|
||||
here putn
|
||||
display unused memory
|
||||
|
||||
( allocate 1000 cells )
|
||||
1000 allot
|
||||
~~~
|
||||
EOM here - n:put
|
||||
~~~
|
||||
|
||||
( free 500 cells )
|
||||
-500 allot
|
||||
display next free address
|
||||
|
||||
~~~
|
||||
here n:put
|
||||
~~~
|
||||
|
||||
allocate 1000 cells
|
||||
|
||||
~~~
|
||||
#1000 allot
|
||||
~~~
|
||||
|
||||
free 500 cells
|
||||
|
||||
~~~
|
||||
#-500 allot
|
||||
~~~
|
||||
|
|
|
|||
17
Task/Memory-allocation/Rust/memory-allocation.rust
Normal file
17
Task/Memory-allocation/Rust/memory-allocation.rust
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// we have to use `unsafe` here because
|
||||
// we will be dereferencing a raw pointer
|
||||
unsafe {
|
||||
use std::alloc::{Layout, alloc, dealloc};
|
||||
// define a layout of a block of memory
|
||||
let int_layout = Layout::new::<i32>();
|
||||
|
||||
// memory is allocated here
|
||||
let ptr = alloc(int_layout);
|
||||
|
||||
// let us point to some data
|
||||
*ptr = 123;
|
||||
assert_eq!(*ptr, 123);
|
||||
|
||||
// deallocate `ptr` with associated layout `int_layout`
|
||||
dealloc(ptr, int_layout);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue