Skip to content

Commit d096a39

Browse files
committed
Add freeContext
1 parent 29ccbc7 commit d096a39

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

lib/src/ensemble_llama_base.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void main() async {
2222
print(model);
2323

2424
final ctx = await llama.newContext(model, params);
25-
// await llama.freeContext(ctx);
25+
await llama.freeContext(ctx);
2626

2727
await llama.freeModel(model);
2828
llama.dispose();
@@ -104,9 +104,9 @@ class Llama {
104104
return resp.ctx!;
105105
}
106106

107-
// Future<void> freeContext(Context ctx) async {
108-
// final ctl = FreeContextCtl(ctx);
109-
// _controlPort.send(ctl);
110-
// await _response.firstWhere((e) => e is FreeContextResp && e.id == ctl.id);
111-
// }
107+
Future<void> freeContext(Context ctx) async {
108+
final ctl = FreeContextCtl(ctx);
109+
_controlPort.send(ctl);
110+
await _response.firstWhere((e) => e is FreeContextResp && e.id == ctl.id);
111+
}
112112
}

lib/src/llama_cpp_isolate_wrapper.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,10 @@ class Model {
201201
}
202202

203203
class Context {
204-
final int rawPointer;
205-
const Context._(this.rawPointer);
204+
final int _rawPointer;
205+
const Context._(this._rawPointer);
206+
Pointer<llama_context> get _ffiPointer =>
207+
Pointer.fromAddress(_rawPointer).cast<llama_context>();
206208
}
207209

208210
class _Allocations<E> {
@@ -313,5 +315,7 @@ void _onControl(ControlMessage ctl) {
313315
_response.send(ctl.done(Context._(rawCtx)));
314316

315317
case FreeContextCtl():
318+
libllama.llama_free(ctl.ctx._ffiPointer);
319+
_response.send(ctl.done());
316320
}
317321
}

0 commit comments

Comments
 (0)