Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ld.nape;
- $(import);
- class Allocator {
- $(define local private var pool<T>:T;);
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- public function new() {}
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- $(define global public inline function alloc<T>() {
- if(pool<T>==null) return new T();
- else {
- var ret = pool<T>;
- pool<T> = ret.next;
- return ret;
- }
- });
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- $(define global public inline function free<T>(obj:T) {
- obj.next = pool<T>;
- pool<T> = obj;
- });
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- $(define global public inline function reserve<T>(num:UInt) {
- while(num-->0) free<T>(new T());
- });
- }
- /* example
- import ld.nape.Allocator;
- class A {
- public var next:A;
- public function new() {}
- }
- class Main {
- static function main() {
- var alloc = new Allocator();
- alloc.free<A>(new A());
- var obj = alloc.alloc<A>();
- alloc.reserve<A>(100);
- //no conflict with macro when using alloc as a variable name
- //as alloc macro must take an argument.
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement