Skip to content

Commit

Permalink
More godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Cully committed Oct 8, 2016
1 parent 8f685f0 commit dd8cab3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package freespacetree

// Node represents a range of free space in the tree.
type Node struct {
from uint64
to uint64
left *Node
right *Node
}

// NewNode returns a pointer to a new Node with the specified range of free space.
func NewNode(from, to uint64) *Node {
inst := &Node{
left: nil,
Expand All @@ -17,6 +19,8 @@ func NewNode(from, to uint64) *Node {
return inst
}

// Allocate attempts to allocate a continuous range of blocks as specified, returning
// the first blockid in the allocation, and whether the space was successfully allocated.
func (nd *Node) Allocate(blocks uint64) (uint64, bool) {
if nd.left != nil {
blockid, found := nd.left.Allocate(blocks)
Expand All @@ -40,14 +44,15 @@ func (nd *Node) Allocate(blocks uint64) (uint64, bool) {
return 0, false
}

// Deallocate frees the continuous space referenced by the specified blockid and length, returning the new node
// representing the free space.
func (nd *Node) Deallocate(blockid uint64, blocklength uint64) *Node {
node := NewNode(blockid, blockid+blocklength)
nd.AddNode(node)
nd = nd.AddNode(node)
return nd
}

// Add an existing node into the tree, merging if necessary
// Returns the new root of the tree
// AddNode adds an existing node into the tree, merging nodes if necessary and returning the new root of the tree.
func (nd *Node) AddNode(node *Node) *Node {
// Detect node engulfed by nd
if nd.from <= node.from && nd.to >= node.to {
Expand Down
5 changes: 5 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package freespacetree

// Tree represents a tree of free space.
type Tree struct {
root *Node
capacity uint64
}

// New returns a pointer to a new Tree with the specified capacity in blocks.
func New(capacity uint64) *Tree {
inst := &Tree{
root: NewNode(0, capacity),
Expand All @@ -13,10 +15,13 @@ func New(capacity uint64) *Tree {
return inst
}

// Allocate attempts to allocate a continuous range of blocks in the Tree as specified, returning
// the first blockid in the allocation, and whether the space was successfully allocated.
func (tr *Tree) Allocate(blocks uint64) (uint64, bool) {
return tr.root.Allocate(blocks)
}

// Deallocate frees the continuous space in the Tree referenced by the specified blockid and length.
func (tr *Tree) Deallocate(blockid uint64, blocklength uint64) {
tr.root = tr.root.Deallocate(blockid, blocklength)
}

0 comments on commit dd8cab3

Please sign in to comment.