In my epic adventures of mixing Haskell with C, I stumbled across
the following thing: From Haskell-land I want to register an
IO-Action which should be called periodically from C-land. After
reading previous mails, consulting some fortune-tellers, looking
into crystal balls, etc., I conjured up the following code:

-- Main.hs ------------------------------------------------------
{-# OPTIONS -#include "callhaskell.h" #-}

import Foreign(makeStablePtr)

haskellCB :: IO ()
haskellCB = putStrLn "haskellCB"

main :: IO ()
main = do sp <- makeStablePtr haskellCB
          _casm_GC_ ``callback = %0; loop();'' sp

-- callhaskell.h ------------------------------------------------
#include "rtsdefs.h"
extern StgStablePtr callback;
extern void loop(void);

-- callhaskell.c ------------------------------------------------
#include "callhaskell.h"

StgStablePtr callback;

void loop(void) {
  while (1) performIO(callback);
}


The problem with this code is the fact that every callback seems
to eat 8 bytes from the stack:

panne@liesl: > make
rm -f Main.o
ghc-3.01 -recomp -fglasgow-exts -c Main.hs -o Main.o
ghc-3.01: module version changed to 1; reason: no old .hi file
ghc-3.01 -c callhaskell.c -o callhaskell.o
ghc-3.01 -o leaky -recomp -fglasgow-exts Main.o callhaskell.o 

panne@liesl: > ./leaky +RTS -K8k | wc
Stack space overflow: current size 8000 bytes.
Use `+RTS -Ksize' to increase it.
     57      58     579

panne@liesl: > ./leaky +RTS -K16k | wc
Stack space overflow: current size 16000 bytes.
Use `+RTS -Ksize' to increase it.
   1081    1082   10819

panne@liesl: > ./leaky +RTS -K24k | wc
Stack space overflow: current size 24000 bytes.
Use `+RTS -Ksize' to increase it.
   2105    2106   21059


Am I doing something wrong here or is this a bug?

-- 
Sven Panne                                        Tel.: +49/89/2178-2235
LMU, Institut fuer Informatik                     FAX : +49/89/2178-2211
LFE Programmier- und Modellierungssprachen              Oettingenstr. 67
mailto:[EMAIL PROTECTED]            D-80538 Muenchen
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne

Reply via email to