[Pdx-pm] Designing a C library specifically for HLL binding
Eric Wilhelm
enobacon at gmail.com
Thu Oct 30 13:07:37 PDT 2008
Hi all,
I'm working on adding .dwg support to VectorSection
(http://vectorsection.org/), which will involve
a lot of twiddly bitwise navigation, seeks, unpacks and such of the
binary format. It is also likely to hit a wall on speed rather quickly
in Perl, but the code is also likely to be forced into looking like "C
with dollar signs" because of the low-level nature of the task.
I'm thinking that perhaps the way to go is to implement small parts in
C, probably involving Inline to hook them into the Perl code.
Keep in mind that I don't like C well enough to convince myself that
I "know" it. Or rather, I don't want to spend all of my development
time dealing with memory allocation. And just to keep things
interesting, almost all of the data involved is going to be arrays and
arrays of arrays.
I'm wondering if someone can give me some feedback on how this would
work and/or point me at some examples or reading material.
My thought is that the C functions would all have one of the following
signatures:
bool gets_complex_data(source_struct, dest_struct);
int gets_simple_data(source_struct);
Where 'source_struct' is a pointer to a struct which essentially
contains just the filehandle and maybe a bit of positional data (the
input is stateful.) Then dest_struct is block allocated. The HLL
binding would then be doing all of the memory allocation (part of the
plan being to minimize the memory footprint, but also to make
a 'Perlish' API.)
I suppose this same scheme could be applied to a streaming processor in
C. As I'm currently imagining it, the XS for extracting a polygon
would resemble:
int i;
int points;
pointstruct a_point;
points = get_polygon_point_count(input);
for(i=0; i < points; i++) {
if(get_polygon_point(input, i, a_point)) {
// do something with a_point
}
else {
// fail, retry, whatever
}
}
Where "do something" in this case is to create an AV and push it to
the 'points' AV in the HV which gets returned. Thus, the library only
sets values in the pre-allocated temporary 'a_point' struct and the
rest of the memory management happens within the Perl interpreter.
Thus, the Perl code will be somewhat buffered from the fine-grained
details of "$pts = number_of_points" and "foreach (0..$pts) {...}".
Now remember, I only know enough C to be dangerously amusing. But, I
have never heard of a C library being created specifically for binding
to an HLL interpreter, so I might be lacking the vocabulary needed to
clarify what I think I'm trying to do. Part of the goal here would be
to allow it to be easily bound to various other interpreters - hence
not simply a big XS library.
So, is this making any sense? Seem like a reasonable approach? Can
anyone point me to a codebase which does something similar? And
possibly a Perl binding?
Thanks,
Eric
--
I arise in the morning torn between a desire to improve the world and a
desire to enjoy the world. This makes it hard to plan the day.
--E.B. White
---------------------------------------------------
http://scratchcomputing.com
---------------------------------------------------
More information about the Pdx-pm-list
mailing list