Skip to content

Commit d8a8131

Browse files
author
Nithin Chintala
committed
more docs
1 parent 450abea commit d8a8131

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

README.md

+37-4
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,55 @@ later time. Wikipedia also has a [summary](https://en.wikipedia.org/wiki/Memory_
2222
## Example
2323
![](amalloc.gif)
2424

25+
## Usage
26+
```
27+
usage: amalloc [-speed={step|slow|norm|fast|inst}]
28+
-speed The time between each frame in the animation
29+
step: wait for user input
30+
slow: wait 1.5s
31+
norm: wait 1.0s (default)
32+
fast: wait 0.5s
33+
inst: wait 0.0s
34+
```
35+
Once you are running the code the following commands are valid. To keep
36+
the animation less crowded, variables are limited to single characters.
37+
```
38+
>>> <char> = malloc(<size>)
39+
>>> free(<char>)
40+
41+
>>> x = malloc(4)
42+
>>> y = malloc(7)
43+
>>> free(x)
44+
>>> free(y)
45+
```
46+
2547
## Install
2648
You can build the executable
2749
```
28-
$ git clone https://github.com/NithinChintala/amalloc.git
50+
$ git clone https://github.com/NithinChintala/amalloc.git && cd amalloc
2951
$ go build
3052
$ ./amalloc
3153
```
3254
Or run it as a script
3355
```
34-
$ git clone https://github.com/NithinChintala/amalloc.git
56+
$ git clone https://github.com/NithinChintala/amalloc.git && cd amalloc
3557
$ go run main.go
3658
```
3759

3860
# How Does it Work?
61+
At a high level the code is running a Finite State Machine. Each state is
62+
rendered in the animation and calling malloc / free alters the state of
63+
the animation. For all the transitions see below. You always start in the
64+
Idle state and wait for some user input. All other states go on to the
65+
next state based on the input speed. The animation will always show what
66+
state is currently being rendered.
67+
![](fsm.png)
68+
69+
70+
These are the ways the headers and cells are represented. The next and prev
71+
sections represent indexes each byte in the 16 byte heap. Since it takes
72+
4 bits to represent 16 location, a NULL pointer is represented as 11111 or
73+
5 ones in a row.
3974
**Header** - 1 byte
4075
```
4176
used unused
@@ -64,5 +99,3 @@ $ go run main.go
6499
65100
[0b00111111, 0b00000100] == {used: false, index: 1, prev: 31, next: 4}
66101
```
67-
68-
![](fsm.png)

fsm.png

-1.03 KB
Loading

main.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ func (dn *DevNull) Write(p []byte) (n int, err error) { return 0, nil }
1515

1616
func main() {
1717
interact()
18-
//debug()
1918
}
2019

2120
func interact() {

memsim/anim.go

-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ func Anim(h *Heap, waitFunc func()) {
213213
reader := bufio.NewReader(os.Stdin)
214214
for {
215215
Render(h)
216-
//fmt.Printf("\n%sState: %s\nCount: %d\n%v\n>>> ", ClearEnd, h.getPrevState(), count, h)
217216
fmt.Printf("\n%sState: %s\nCount: %d\n>>> ", ClearEnd, h.getPrevState(), count)
218217
count++
219218
if h.prevState[Type] == Idle && h.state[Type] == Idle {

0 commit comments

Comments
 (0)