-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5dc88e
commit bc6ab38
Showing
8 changed files
with
1,273 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package allocator_test | ||
|
||
import ( | ||
"fmt" | ||
"unsafe" | ||
|
||
"github.com/joetifa2003/mm-go" | ||
"github.com/joetifa2003/mm-go/allocator" | ||
) | ||
|
||
func ExampleNewC() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
ptr := allocator.Alloc[int](alloc) | ||
defer allocator.Free(alloc, ptr) | ||
|
||
*ptr = 15 | ||
fmt.Println(*ptr) | ||
|
||
// Output: 15 | ||
} | ||
|
||
func ExampleAlloc() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
// So you can do this: | ||
ptr := allocator.Alloc[int](alloc) // allocates a single int and returns a ptr to it | ||
defer allocator.Free(alloc, ptr) // frees the int (defer recommended to prevent leaks) | ||
*ptr = 15 | ||
fmt.Println(*ptr) | ||
|
||
// instead of doing this: | ||
ptr2 := (*int)(alloc.Alloc(mm.SizeOf[int]())) | ||
defer alloc.Free(unsafe.Pointer(ptr2)) | ||
*ptr2 = 15 | ||
|
||
fmt.Println(*ptr2) | ||
|
||
// Output: | ||
// 15 | ||
// 15 | ||
} | ||
|
||
func ExampleAllocMany() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
heap := allocator.AllocMany[int](alloc, 2) // allocates 2 ints and returns it as a slice of ints with length 2 | ||
defer allocator.FreeMany(alloc, heap) // it's recommended to make sure the data gets deallocated (defer recommended to prevent leaks) | ||
|
||
heap[0] = 15 // changes the data in the slice (aka the heap) | ||
ptr := &heap[0] // takes a pointer to the first int in the heap | ||
// Be careful if you do ptr := heap[0] this will take a copy from the data on the heap | ||
*ptr = 45 // changes the value from 15 to 45 | ||
heap[1] = 70 | ||
|
||
fmt.Println(heap[0]) | ||
fmt.Println(heap[1]) | ||
|
||
// Output: | ||
// 45 | ||
// 70 | ||
} | ||
|
||
func ExampleRealloc() { | ||
alloc := allocator.NewC() | ||
defer alloc.Destroy() | ||
|
||
heap := allocator.AllocMany[int](alloc, 2) // allocates 2 int and returns it as a slice of ints with length 2 | ||
|
||
heap[0] = 15 | ||
heap[1] = 70 | ||
|
||
heap = allocator.Realloc(alloc, heap, 3) | ||
allocator.FreeMany(alloc, heap) | ||
|
||
heap[3] = 100 | ||
|
||
fmt.Println(heap[0]) | ||
fmt.Println(heap[1]) | ||
fmt.Println(heap[3]) | ||
|
||
// Output: | ||
// 15 | ||
// 70 | ||
// 100 | ||
} | ||
|
||
func ExampleNewAllocator() { | ||
// Create a custom allocator | ||
alloc := allocator.NewAllocator( | ||
nil, | ||
myallocator_alloc, | ||
myallocator_free, | ||
myallocator_realloc, | ||
myallocator_destroy, | ||
) | ||
|
||
// Check how C allocator is implemented | ||
// or batchallocator soruce for a reference | ||
|
||
_ = alloc | ||
} | ||
|
||
func myallocator_alloc(allocator unsafe.Pointer, size int) unsafe.Pointer { | ||
return nil | ||
} | ||
|
||
func myallocator_free(allocator unsafe.Pointer, ptr unsafe.Pointer) { | ||
} | ||
|
||
func myallocator_realloc(allocator unsafe.Pointer, ptr unsafe.Pointer, size int) unsafe.Pointer { | ||
return nil | ||
} | ||
|
||
func myallocator_destroy(allocator unsafe.Pointer) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package batchallocator_test | ||
|
||
import ( | ||
"github.com/joetifa2003/mm-go" | ||
"github.com/joetifa2003/mm-go/allocator" | ||
"github.com/joetifa2003/mm-go/batchallocator" | ||
) | ||
|
||
func Example() { | ||
alloc := batchallocator.New(allocator.NewC()) // by default it allocates page, which is usually 4kb | ||
defer alloc.Destroy() // this frees all memory allocated by the allocator automatically | ||
|
||
ptr := allocator.Alloc[int](alloc) | ||
// but you can still free the pointers manually if you want (will free buckets of memory if all pointers depending on it is freed) | ||
defer allocator.Free(alloc, ptr) // this can removed and the memory will be freed. | ||
} | ||
|
||
func ExampleWithBucketSize() { | ||
alloc := batchallocator.New( | ||
allocator.NewC(), | ||
batchallocator.WithBucketSize(mm.SizeOf[int]()*15), // configure the allocator to allocate size of 15 ints in one go. | ||
) | ||
defer alloc.Destroy() | ||
|
||
ptr := allocator.Alloc[int](alloc) | ||
defer allocator.Free(alloc, ptr) // this can be removed and the memory will still be freed on Destroy. | ||
|
||
ptr2 := allocator.Alloc[int](alloc) // will not call CGO because there is still enough memory in the Bucket. | ||
defer allocator.Free(alloc, ptr2) // this can be removed and the memory will still be freed on Destroy. | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mm_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/joetifa2003/mm-go" | ||
) | ||
|
||
func ExampleSizeOf() { | ||
fmt.Println(mm.SizeOf[int32]()) | ||
fmt.Println(mm.SizeOf[int64]()) | ||
// Output: | ||
// 4 | ||
// 8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters