Created
April 25, 2013 15:49
-
-
Save back2dos/5460793 to your computer and use it in GitHub Desktop.
Manually manage memory with a pool.
This file contains hidden or 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
| package ; | |
| class Manual { | |
| static var pool = []; | |
| static var check = new Map<Manual, Manual>(); | |
| var count:Int = 0; | |
| function new() {} | |
| function init() {/*set up*/} | |
| function free() {/*clean up*/} | |
| public function retain() | |
| if (++count > 0) | |
| check.remove(this); | |
| public function release() | |
| if (--count <= 0) | |
| check.set(this, this); | |
| public function releaseAll() { | |
| count = 1; | |
| release(); | |
| } | |
| static public function alloc(?retain = false) { | |
| var ret = | |
| if (pool.length > 0) pool.pop(); | |
| else new Poolable(); | |
| ret.count = 1; | |
| if (!retain) | |
| ret.release(); | |
| ret.init(); | |
| return ret; | |
| } | |
| static public function collect() { | |
| for (r in check) { | |
| pool.push(r); | |
| r.free(); | |
| } | |
| check = new Map(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment