caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Help interfacing with C
@ 2006-08-16 19:34 Nathaniel Gray
  2006-08-17  3:49 ` [Caml-list] " malc
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Nathaniel Gray @ 2006-08-16 19:34 UTC (permalink / raw)
  To: Caml List

Hi folks,

I'm having a heck of a time figuring out why this code *doesn't* work:

/* fdlist is a list of (file_descr * 'a) pairs.  Filter by FD_ISSET */
static value fdset_to_fdlist2(value fdlist, fd_set *fdset)
{
  value l, p, newres;
  value res = Val_int(0);

  Begin_roots4(l, p, res, newres);  /* I know, this is aggressive */
    for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
      p = Field(l, 0);
      int fd = Int_val(Field(p, 0));
      if (FD_ISSET(fd, fdset)) {
        newres = alloc_small(2, 0);
        Field(newres, 0) = p;
        Field(newres, 1) = res;
        res = newres;
      }
    }
  End_roots();
  return res;
}

But this code *does* work:

/* fdlist is a list of file_descr.  Filter by FD_ISSET */
static value fdset_to_fdlist(value fdlist, fd_set *fdset)
{
  value l;
  value res = Val_int(0);

  Begin_roots2(l, res);
    for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
      int fd = Int_val(Field(l, 0));
      if (FD_ISSET(fd, fdset)) {
        value newres = alloc_small(2, 0);
        Field(newres, 0) = Val_int(fd);
        Field(newres, 1) = res;
        res = newres;
      }
    }
  End_roots();
  return res;
}

The second sample comes directly from otherlibs/unix/select.c in the
ocaml compiler, and the first is part of a modification I'm writing to
have select work on (file_descr * 'a) pairs instead of plain file
descriptors.  The Begin_roots4 line was originally Begin_roots3(l,
res, newres), but it doesn't seem to make any difference.  When I run
in gdb the failure looks like this:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
0x000051ac in caml_array_unsafe_get (array=1, index=1) at array.c:101
101       if (Tag_val(array) == Double_array_tag)
(gdb) bt
#0  0x000051ac in caml_array_unsafe_get (array=1, index=1) at array.c:101
#1  0x0002dd84 in caml_interprete (prog=0x205000, prog_size=949932) at
interp.c:869
#2  0x00021bd8 in caml_main (argv=0xbfffe934) at startup.c:414
#3  0x00002630 in main (argc=2, argv=0xbfffe934) at main.c:35

It fails on line 869 of this code in caml_interprete:
867         Instruct(C_CALL2):
868           Setup_for_c_call;
869           accu = Primitive(*pc)(accu, sp[1]);
870           Restore_after_c_call;

I've tried poking in gdb, but as far as I can tell the result of the
function is a well-formed list.  Any suggestions?

Thanks!
-n8

-- 
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2006-08-24 14:48 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-16 19:34 Help interfacing with C Nathaniel Gray
2006-08-17  3:49 ` [Caml-list] " malc
2006-08-18 21:00   ` Nathaniel Gray
2006-08-17  5:56 ` Bardur Arantsson
2006-08-18  7:10   ` [Caml-list] " Olivier Andrieu
2006-08-18 15:50     ` Bardur Arantsson
2006-08-18 21:33   ` [Caml-list] " Nathaniel Gray
2006-08-18 22:24     ` Bardur Arantsson
2006-08-19  0:33       ` [Caml-list] " Nathaniel Gray
2006-08-19  6:03         ` Bardur Arantsson
2006-08-21 22:45           ` [Caml-list] " Nathaniel Gray
2006-08-19  8:30         ` Olivier Andrieu
2006-08-21 22:35           ` Nathaniel Gray
2006-08-19  9:03     ` Richard Jones
2006-08-19  9:41       ` skaller
2006-08-18  8:46 ` [Caml-list] " Damien Doligez
2006-08-18 20:09   ` Nathaniel Gray
2006-08-23 23:36 ` Nathaniel Gray

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 NNTP newsgroup(s).