[M3devel] open array parameter passing, to external C procedure
Rodney Bates
rodney_bates at lcwb.coop
Mon Feb 6 17:43:11 CET 2012
All the compilers pass open arrays as a pointer to metadata.
The metadata consists of a pointer to actual element zero, followed
by a "shape", which is an array of element counts in each open dimension.
The number of open dimensions is a static property of the type, so
it is not passed, but hard-coded where needed.
The actual elements can be anywhere, and indeed must be somewhere
else in the case of a SUBARRAY.
I suppose they could have made this into separate parameters for
address of element zero and for each shape component, but I'm not
sure right off hand how that would interact with heap-allocated open
arrays and subarrays of either heap-allocated or local variables.
I've been ambivalent about this representation, but it can be used
consistently everywhere. For a heap-allocated open array, the
metadata is immediately followed by the elements.
So, no, it won't work the way you hope. For your example, you
would pass ADR(events[0]) and NUMBER(events) as separate parameters.
(ADR(events) would pass the address of the metadata.)
Of course, usually, the array you will have is not necessarily entirely
full. If that were the case, to do it in the Modula-3 way you had hoped,
you would have had to pass SUBARRAY(events,0,event_count). As it is,
the actuals in this case would be ADR(events[0]) and event_count.
-Rodney Bates
--- dragisha at m3w.org wrote:
From: Dragiša Durić <dragisha at m3w.org>
To: m3devel <m3devel at elegosoft.com>
Subject: [M3devel] open array parameter passing, to external C procedure
Date: Mon, 6 Feb 2012 09:50:51 +0100
I have this case:
extern int epoll_wait (int __epfd, struct epoll_event *__events,
int __maxevents, int __timeout);
And I would like to do it like:
<*EXTERNAL*>
PROCEDURE epoll_wait (epfd: int; VAR events: ARRAY OF epoll_event; timeout: int := -1): int;
…
VAR
events: ARRAY [0..MaxEvents-1] OF epoll_event;
…
WITH res = epoll_wait(epfd, events, 2) DO
…
END;
Can I expect cm3 to do "right thing". Or I have to place parameters "by hand" using ADR and SIZE?
TIA,
dd
More information about the M3devel
mailing list