Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Unified Diff: utils/archive/dart_archive.c

Issue 10825139: Free the C memory for ArchiveInputStream when the stream is garbage collected. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix a renamed function Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | utils/archive/input_stream.dart » ('j') | utils/archive/input_stream.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: utils/archive/dart_archive.c
diff --git a/utils/archive/dart_archive.c b/utils/archive/dart_archive.c
index be53570e70fa6cd19a00aa8298d84849d04621d5..ce7240f11255925d33f40787fd538389584f9466 100644
--- a/utils/archive/dart_archive.c
+++ b/utils/archive/dart_archive.c
@@ -201,6 +201,46 @@ static void archiveServicePort(Dart_NativeArguments arguments) {
}
/**
+ * The C callback that runs the Dart finalizer for an object. Set up by
+ * [attachDartFinalizer]. [handle] is the object that's been collected, and
+ * [peerPtr] is a Dart list containing the callback and its argument.
+ */
+static void runDartFinalizer(Dart_Handle handle, void* peerPtr) {
+ Dart_EnterScope();
+ Dart_Handle wrappedPeer = (Dart_Handle) peerPtr;
+ Dart_Handle callback = handleError(Dart_ListGetAt(wrappedPeer, 0));
+ Dart_Handle peer = handleError(Dart_ListGetAt(wrappedPeer, 1));
+
+ handleError(Dart_InvokeClosure(callback, 1, &peer));
+ Dart_DeletePersistentHandle(wrappedPeer);
+ Dart_ExitScope();
+}
+
+/**
+ * Attaches a finalizer callback to a Dart object.
+ *
+ * This takes a Dart object, a callback function, and an argument to pass to the
+ * callback function. The callback will be called with the given argument some
+ * time after the object has been garbage collected.
+ */
+static void attachDartFinalizer(Dart_NativeArguments arguments) {
+ Dart_EnterScope();
+ Dart_SetReturnValue(arguments, Dart_Null());
+ Dart_Handle object = handleError(Dart_GetNativeArgument(arguments, 0));
+ Dart_Handle callback = handleError(Dart_GetNativeArgument(arguments, 1));
+ Dart_Handle peer = handleError(Dart_GetNativeArgument(arguments, 2));
+
+ Dart_Handle wrappedPeer = handleError(Dart_NewList(2));
+ handleError(Dart_ListSetAt(wrappedPeer, 0, callback));
+ handleError(Dart_ListSetAt(wrappedPeer, 1, peer));
+ wrappedPeer = handleError(Dart_NewPersistentHandle(wrappedPeer));
+
+ handleError(Dart_NewWeakPersistentHandle(
+ object, wrappedPeer, runDartFinalizer));
+ Dart_ExitScope();
+}
+
+/**
* A struct representing a function exposed to Dart and the name under which it
* can be looked up.
*/
@@ -212,6 +252,7 @@ struct FunctionLookup {
/** The list of functions exposed to Dart. */
struct FunctionLookup function_list[] = {
{"Archive_ServicePort", archiveServicePort},
+ {"Archive_AttachFinalizer", attachDartFinalizer},
{NULL, NULL}
};
« no previous file with comments | « no previous file | utils/archive/input_stream.dart » ('j') | utils/archive/input_stream.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698