From: Ian Jackson Subject: Re: [PATCH 22/28] libxl: ocaml: event management Date: Tue, 23 Apr 2013 17:14:03 +0100 Message-ID: <20854.45899.833064.41965@mariner.uk.xensource.com> References: <1364222729-6982-1-git-send-email-rob.hoes@citrix.com> <1364222729-6982-23-git-send-email-rob.hoes@citrix.com> <1365684085.8036.99.camel@zakaz.uk.xensource.com> <7EA643C653F17F4C80DE959E978F10ED010F729E0194@LONPMAILBOX01.citrite.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <7EA643C653F17F4C80DE959E978F10ED010F729E0194@LONPMAILBOX01.citrite.net> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Rob Hoes Cc: Ian Campbell , "xen-devel@lists.xen.org" List-Id: xen-devel@lists.xenproject.org > > > +value stub_xl_osevent_register_hooks(value ctx, value user) { > > > + CAMLparam2(ctx, user); > > > + libxl_osevent_hooks *hooks; > > > + hooks = malloc(sizeof(*hooks)); > > > + > > > + hooks->fd_register = fd_register; > > > + hooks->fd_modify = fd_modify; > > > + hooks->fd_deregister = fd_deregister; > > > + hooks->timeout_register = timeout_register; > > > + hooks->timeout_modify = timeout_modify; > > > + hooks->timeout_deregister = timeout_deregister; > > > + > > > + libxl_osevent_register_hooks(CTX, hooks, (void *) user); > > > > This user thing will be retained by libxl -- is that safe from an ocaml gc point > > of view? > > Good point. The original value may go out of scope in the OCaml program and will then be GC'ed. We should copy the value to avoid trouble. To do that, though, we need to know the type of the thing, which is currently polymorphic. I'll just go ahead and make it a string instead, because that seems to be the most useful. > > Are these hooks and associated data every cleaned up by libxl? Or is the assumption that libxl_osevent_register_hooks is just called once at the beginning of the program, and everything starts till the end? >From the doc comment (libxl_event.h line 353): * osevent_register_hooks may be called only once for each libxl_ctx. So you are not allowed to call it with NULL to deregister or change the hooks. The "user" should normally be a pointer whatever structure you have that contains the libxl_ctx*. > > > +void event_occurs(void *user, const libxl_event *event) { > > > + CAMLparam0(); > > > + CAMLlocalN(args, 2); > > > + value *func = caml_named_value("xl_event_occurs_callback"); > > > + > > > + args[0] = (value) user; > > > + args[1] = Val_event((libxl_event *) event); > > > + //libxl_event_free(CTX, event); // no ctx here! > > > > Is it leaked or do you free it somewhere else? I suppose "func" must do it? > > (which makes sense actually) > > Hmm... This is awkward. The thing we are giving to "func" is the event translated into an Ocaml type, and not the C libxl_event*. And even if we give the libxl_event* to "func" as well, it still needs to know the ctx in order to free it (which it probably would, but won't make things easier to use). Is there no way to ask libxl to which ctx the event belongs? No, there isn't such a way. Indeed it's not recorded. I think part of the problem here is that you may be trying to map the C functions to ocaml too directly. Your ocaml system already has an event loop, doesn't it ? You should provide plumbing to glue it to that. Ian.