Welcome, Guest. Please login or register.

Author Topic: Where's da bug  (Read 3252 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Where's da bug
« on: August 31, 2004, 10:13:41 PM »
Documentation error?

As far as I recall, CreateMsgPort() should return struct MsgPort*, not CreateProc().

In any event, CreateNewProc() is a bit more useful IMHO.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Where's da bug
« Reply #1 on: August 31, 2004, 10:38:52 PM »
Quote

Jose wrote:
Hmm, I checked the structures and the Process structure starts with a task structure followed by the MsgPort.
CreateProc returns the MsgPort according to the docs, so I think if I cast it fo a Process it's an error. Should be a MsgPort after all. Which brings me back to my first error(first post)!!


If you cast any struct X* to struct Y* and the first member struct X is not an object of type struct Y, then the cast is badly broken. You need to account for the offset of struct Y within the struct X.

I don't see that a struc MsgPort* pointer is safely castable to struct Process* unless you absolutely know for a fact that the MsgPort structure pointed to is really a member of a Process. Even then you will have to do some pointer arithmetic (or use offsetof()) to properly determing the address of the Process structure within which your MsgPort object is situated.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Where's da bug
« Reply #2 on: August 31, 2004, 10:47:26 PM »
Checked the RKMs and it does indeed return struct MsgPort* :-o

It also says that it creates a struct Process and returns it's message port.

So, I'd suggest the following dodgyness to obtain the address of the Process structure within which the MsgPort resides:

#include /* for offsetof() macro */

UBYTE* portAddr = (UBYTE*)CreateProc(.....); /* UBYTE for simple pointer arithmetic */

struct Process* process = (struct Process*)(portAddr - offsetof(struct Process, pr_MsgPort));

int p; // A