www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/11/02:52:53

Xref: news2.mv.net comp.os.msdos.djgpp:4825
From: Jonathan Markland <jrm101 AT tower DOT york DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Grabbing interrupts in C++
Date: Tue, 11 Jun 1996 00:03:11 +0100
Organization: The University of York, UK
Lines: 66
Message-ID: <31BCA9AF.B37@tower.york.ac.uk>
References: <01BB5489 DOT 0EBADA80 AT psp3>
NNTP-Posting-Host: cst108.york.ac.uk
Mime-Version: 1.0
To: Steve Higgins <steve AT psp-digital DOT co DOT uk>
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Steve Higgins wrote:

[snip]

> void VectorIntercept::globInterrupt(struct _MyRegs *r)
> {
>     r->me->InterruptRoutine(r->regs);
> }
> 
> and hey presto my virtual class function InterruptRoutine() is called with
> the _go32_dpmi_regs as its parameter.
> 
> This all works great and I can set up any number of interrupt routines.

[snip]

Ok, so I accept that this *happens* to work, but I'm not convinced that it's
entirely safe! You state, and your code suggests, that 
VectorIntercept::globInterrupt is called when an interrupt is received. How
can you be so sure that the calling runtime system software knows that
your function is a C++ member function rather than a standard C function?

Member functions are always (albeit implicitly) passed the 'this' pointer. I'm
not convinced that this will happen in this case, and you may have been lucky
because:

1. VectorIntercept::globInterrupt accesses no class data members that would
   need the use of the 'this' pointer. (You are only actually using the _MyRegs *
   which is received as a parameter)

2. I expect that you've been fortunate, because (guessing now!) the compiler
   usually pushes the 'this' pointer as the first parameter, or perhaps
   passes it in a register, and as such the stack frame for globInterrupt
   is identical/similar enough to a standard C function to cause no problems.

I suggest that you make globInterrupt a friend function, if you're going to
continue this approach:

class VectorIntercept
{
   // class blurb etc.

  virtual void InterruptRoutine(_go32_dpmi_regs *r);

   struct _MyRegs
   {
      _go32_dpmi_regs *regs;  // processor registers
      VectorIntercept    *me;    // tag my own stuff at the end of the block.
   } Regs;

private:
   friend void globInterrupt(struct _MyRegs *r);
};


void globInterrupt(struct VectorInterrupt::_MyRegs *r)
{
   r->me->InterruptRoutine(r->regs);
}

I use this sort of technique for an entirely different (but non interrupt
related) situation, where a remote procedure call from a client can only
call a standard C function, and the struct 'trick' helps locate the missing
class!

Jonathan Markland   (jrm101 AT tower DOT york DOT ac DOT uk)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019