Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created April 25, 2013 15:49
Show Gist options
  • Select an option

  • Save back2dos/5460793 to your computer and use it in GitHub Desktop.

Select an option

Save back2dos/5460793 to your computer and use it in GitHub Desktop.
Manually manage memory with a pool.
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