-
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
3c45a26
commit bf51e14
Showing
14 changed files
with
177 additions
and
312 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package allocator | ||
|
||
import ( | ||
"context" | ||
"unsafe" | ||
) | ||
|
||
type ctxKey int | ||
|
||
const ( | ||
AllocatorKey ctxKey = iota | ||
) | ||
|
||
type Allocator interface { | ||
Alloc(size int) unsafe.Pointer | ||
Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer | ||
Free(ptr unsafe.Pointer) | ||
Destroy() | ||
} | ||
|
||
func WithAllocator(ctx context.Context, allocator Allocator) context.Context { | ||
return context.WithValue(ctx, AllocatorKey, allocator) | ||
} | ||
|
||
func GetAllocator(ctx context.Context) Allocator { | ||
allocator, ok := ctx.Value(AllocatorKey).(Allocator) | ||
if !ok { | ||
return CAllocator{} | ||
} | ||
|
||
return allocator | ||
} |
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,23 @@ | ||
package allocator | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/joetifa2003/mm-go/malloc" | ||
) | ||
|
||
type CAllocator struct{} | ||
|
||
func (c CAllocator) Alloc(size int) unsafe.Pointer { | ||
return malloc.Malloc(size) | ||
} | ||
|
||
func (c CAllocator) Free(ptr unsafe.Pointer) { | ||
malloc.Free(ptr) | ||
} | ||
|
||
func (c CAllocator) Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer { | ||
return malloc.Realloc(ptr, int(size)) | ||
} | ||
|
||
func (c CAllocator) 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
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
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
Oops, something went wrong.