Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #24744

Re: Function factories

From jacob navia <jacob@spamsink.net>
Newsgroups comp.lang.c
Subject Re: Function factories
Date 2012-08-23 11:08 +0200
Organization lccwin
Message-ID <k14rtn$3rg$1@speranza.aioe.org> (permalink)
References <k10rgf$oep$1@speranza.aioe.org> <k12cgb$k4g$1@speranza.aioe.org> <58c40758-f7d1-4272-b4ce-ed5c185bc56b@p5g2000vbl.googlegroups.com> <5034F54B.9050403@verizon.net>

Show all headers | View raw


Le 22/08/12 17:05, James Kuyper a écrit :
> On 08/22/2012 10:40 AM, tom st denis wrote:
>> On Aug 22, 6:32 am, jacob navia <ja...@spamsink.net> wrote:
> ...
>>> If you have the JIT of lcc-win you write:
>>>
>>> typedef int (*foo)(int);
>>>
>>> foo adder_factory(int value)
>>> {
>>>          foo fnPtr;
>>>          char buf[512];
>>>
>>>          sprintf(buf,"int fn(int a) { return a + %d;}",value);
>>>          fnPtr = (foo)compile(buf);
>>>          return fnPtr;
>>
>> You could accomplish similiar [though more longwinded] via compiling
>> code and using dlopen() to load it into your process space.
>
> The intent was for the definition of the adder to be contained inside
> the definition of adder_factory().
>
>> Both of which are completely OT for this group though.
>>
>> BTW: your code doesn't seem to do any sort of error checking.  What if
>> compile() fails?
>
>  From the way it's used, it would appear to return a pointer.

By convention it returns a pointer to the first function it finds.
This is highly dependent on the application and customer setup.

Some customers want to have a "main" function that starts the thing.
In that case the JIT will return a pointer to that function. Others
will put the main function at the start of the file and the Jit returns
a function to the first function it compiles.

In case of error (syntax error, no more memory, whatever catastrophe
occurs), it returns NULL. Diagnostics are printed in stderr, whatever 
that is. In most applications stderr is a pipe connected to some
file.

Obviously in most applications the code buffer is machine generated.
It could be however that it is used as a C interpreter.

The resulting code is dynamically linked to the running program. There
is no preprocessor.

> If
> compile() fails, it might return a null pointer, in which case
> adder_factory() will also return a null pointer, leaving it for the
> caller to decide what to do about it. That's a perfectly reasonable way
> of passing on the information that the call failed.
>
> However, I think that compile() looks horribly type-unsafe.
>

In a sense yes, since it can return a pointer to ANY kind of function
by definition... It is up to you to cast the result pointer into
the right stuff. I do not see how to improve that without destroying
the generality of the program.

But we are used to it. fread() will read ANY kind of stuff into its
buffer, it is up to you to interpret the data correctly.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-21 20:36 +0000
  Re: Function factories Nobody <nobody@nowhere.com> - 2012-08-21 21:55 +0100
    Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 16:36 +0000
      Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 13:01 -0400
        Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 18:33 +0000
        Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 22:26 -0400
  Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 08:34 +0200
    Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-22 01:52 -0700
      Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 11:34 +0200
      Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:30 +0100
    Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:29 +0100
  Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-22 12:32 +0200
    Re: Function factories tom st denis <tom@iahu.ca> - 2012-08-22 07:40 -0700
      Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 11:05 -0400
        Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 11:08 +0200
        Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 13:27 +0100
          Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 11:13 -0400
            Re: Function factories "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-23 18:18 +0200
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:40 -0400
            Re: Function factories Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-08-23 09:29 -0700
              Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 18:44 +0200
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:46 -0400
            Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 18:02 +0100
              Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 19:24 +0200
                Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 18:07 -0700
                Re: Function factories Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-23 21:30 -0400
                Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 21:46 -0700
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 13:35 -0400
                Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 20:13 +0200
              Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 17:57 -0700
              Re: Function factories Andrew Cooper <amc96@cam.ac.uk> - 2012-08-25 02:35 +0100
            Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 21:21 +0200
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 15:36 -0400
                Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 22:09 +0200
                Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 16:20 -0400
                Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-23 22:33 +0200
                Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 17:07 -0400
                Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 14:51 -0700
                Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 00:09 +0200
                Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 19:01 -0400
                Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 16:31 -0700
                Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 18:31 +0200
                Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-24 02:37 -0700

csiph-web