From: Mathieu Othacehe <m.othacehe@gmail.com>
To: "Ludovic Courtès" <ludo@gnu.org>,
Amirouche <amirouche@hypermove.net>,
erik.edrosa@gmail.com, "Andy Wingo" <wingo@igalia.com>
Cc: guix-devel <guix-devel@gnu.org>
Subject: Re: (guix git) and guile-git finalizers.
Date: Mon, 19 Jun 2017 18:01:23 +0200 [thread overview]
Message-ID: <86vans0xws.fsf@gmail.com> (raw)
In-Reply-To: <87zidc2thj.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 581 bytes --]
Hi !
I may have found something that replaces finalizers. I followed Andy and
Amirouche advices and used guardians, as in guile-sqlite3.
The idea here is to create a guardian per pointer-type to finalize. A
pumper function that operates on this guardian is also created. This
pumper function knows the git_libgit2_xxx function to call to free the
pointers stored in guardians.
Finally, all pumpers are registered into a hook that is run before
shutting down.
I tried it on "repository" pointers but it can be easily extended to the
other finalizers.
WDYT ?
Thanks,
Mathieu
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Replace-pointer-finalizer-by-guardians.patch --]
[-- Type: text/x-diff, Size: 3269 bytes --]
From 84bd5aa7f1c5bd01c721d475f8fdfad5533c71b5 Mon Sep 17 00:00:00 2001
From: Mathieu Othacehe <m.othacehe@gmail.com>
Date: Mon, 19 Jun 2017 17:48:08 +0200
Subject: [PATCH] Replace pointer finalizer by guardians.
* git/bindings.scm (%pumpers-hook): New exported variable,
(define-element-guardian): New exported macro.
* git/repository.scm (%repository-free): Remove and replace by a call
to define-element-guardian macro.
(pointer->repository): Do not set a pointer finalizer and add the
pointer to the guardian generated by define-element-guardian
instead.
---
git/bindings.scm | 37 ++++++++++++++++++++++++++++++++++---
git/repository.scm | 4 ++--
2 files changed, 36 insertions(+), 5 deletions(-)
diff --git a/git/bindings.scm b/git/bindings.scm
index 189369e..29eab73 100644
--- a/git/bindings.scm
+++ b/git/bindings.scm
@@ -30,7 +30,9 @@
make-buffer
free-buffer
buffer-content
- buffer-content/string))
+ buffer-content/string
+ %pumpers-hook
+ define-element-guardian))
;; DRAFT!
@@ -207,8 +209,37 @@
(define libgit2-opts
(libgit2->procedure int "git_libgit2_init" `(,int)))
-(define-public libgit2-shutdown
- (libgit2->procedure int "git_libgit2_shutdown" '()))
+(define %pumpers-hook (make-hook))
+
+(define-syntax define-element-guardian
+ (lambda (x)
+ (define-syntax-rule (id ctx parts ...)
+ "Assemble PARTS into a raw (unhygienic) identifier."
+ (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
+ (syntax-case x ()
+ ((_ element finalizer hook)
+ (with-syntax
+ ((finalizer-name (id #'element #'element #'-free))
+ (guardian-name (id #'element #'element #'-guardian))
+ (pumper-name (id #'element #'pump- #'element #'-guardian)))
+ #'(begin
+ (define finalizer-name
+ (let ((proc (libgit2->procedure void finalizer '(*))))
+ (lambda (pointer)
+ (proc pointer))))
+ (define guardian-name (make-guardian))
+ (define (pumper-name)
+ (let ((pointer (guardian-name)))
+ (when pointer
+ (finalizer-name pointer)
+ (pumper-name))))
+ (add-hook! hook pumper-name)))))))
+
+(define-public (libgit2-shutdown)
+ ;; Before shutting down, try to finalize pointers kept in guardians.
+ (run-hook %pumpers-hook)
+ (let ((proc (libgit2->procedure int "git_libgit2_shutdown" '())))
+ (proc)))
(define libgit2-version
(let ((proc (libgit2->procedure void "git_libgit2_version" '(* * *))))
diff --git a/git/repository.scm b/git/repository.scm
index f82dff4..646dbe4 100644
--- a/git/repository.scm
+++ b/git/repository.scm
@@ -89,10 +89,10 @@
;; FIXME: https://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_fetchhead_foreach
-(define %repository-free (dynamic-func "git_repository_free" libgit2))
+(define-element-guardian repository "git_repository_free" %pumpers-hook)
(define (pointer->repository! pointer)
- (set-pointer-finalizer! pointer %repository-free)
+ (repository-guardian pointer)
(pointer->repository pointer))
(define repository-get-namespace
--
2.1.4
next prev parent reply other threads:[~2017-06-19 16:01 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-12 12:44 (guix git) and guile-git finalizers Mathieu Othacehe
2017-06-13 8:15 ` Ludovic Courtès
2017-06-13 16:41 ` Mathieu Othacehe
2017-06-19 16:01 ` Mathieu Othacehe [this message]
2017-06-22 11:40 ` Andy Wingo
2017-07-01 10:16 ` Mathieu Othacehe
2017-07-01 12:50 ` Ludovic Courtès
2017-07-01 14:33 ` Mathieu Othacehe
2017-06-13 8:47 ` Andy Wingo
2017-06-13 16:39 ` Mathieu Othacehe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86vans0xws.fsf@gmail.com \
--to=m.othacehe@gmail.com \
--cc=amirouche@hypermove.net \
--cc=erik.edrosa@gmail.com \
--cc=guix-devel@gnu.org \
--cc=ludo@gnu.org \
--cc=wingo@igalia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).