-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1845 from sebastianbaginski/bytealloc
support for custom allocator in ByteBuffer
- Loading branch information
Showing
4 changed files
with
74 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* This file is part of magic-sdk, an sdk for the open source programming language magic. | ||
* | ||
* Copyright (C) 2016 magic-lang | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
import Owner | ||
|
||
AbstractAllocator: abstract class { | ||
_owner: Owner | ||
free: func ~withCriteria (owner: Owner) { | ||
if (owner == this _owner) | ||
this free() | ||
} | ||
allocate: abstract func (size: SizeT) -> Pointer | ||
deallocate: abstract func (pointer: Pointer) | ||
} | ||
|
||
MallocAllocator: class extends AbstractAllocator { | ||
init: func (owner := Owner Receiver) { this _owner = owner } | ||
allocate: override func (size: SizeT) -> Pointer { malloc(size) } | ||
deallocate: override func (pointer: Pointer) { memfree(pointer) } | ||
} | ||
|
||
Allocator: abstract class { | ||
_defaultAllocator: static AbstractAllocator = null | ||
mallocAllocator := static MallocAllocator new(Owner Sender) | ||
defaultAllocator: static func -> AbstractAllocator { | ||
This _defaultAllocator ?? mallocAllocator as AbstractAllocator | ||
} | ||
free: static func ~all { | ||
This mallocAllocator free(Owner Sender) | ||
} | ||
} |
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