Page 1 of 1

Device Management

Posted: Fri Feb 07, 2003 1:27 pm
by Therx
I'm writing my DM and have chosen to keep a 3D array of pointers for the device open, read, write, close, seek etc. functions. My code (below) goes to the read(...) function but causes an Invalid Opcode (0x06) exception before geting to the beginning of the function pointed to. Has anyone got any ideas why?

Thanks

Main DM File

Code: Select all

static volatile dev_t devices[NUM_TYPES][MAX_DEVS];
static volatile unsigned int totaldev[NUM_TYPES];

int add_dev(int type, dev_t *device)
{
   memcpy(&devices[type][totaldev[type]], &device, sizeof(dev_t));
   totaldev[type]++;
   return totaldev[type] - 1;
}

void read(int type, int id, int location, char *buffer)
{
   devices[type][id].p_read(location, buffer);
}
Header file

Code: Select all

typedef void (*ptr_t)(int location, char *buffer);

typedef struct
{
   ptr_t p_open, p_read, p_write, p_seek, p_close;
} dev_t;

#define NUM_TYPES      7
#define MAX_DEVS       10
#define FDD            0
#define HDD            1
#define NIC            2
#define SOUND          3
#define PRINT          4
#define VIDEO          5
#define CDD            6

int add_dev(int type, dev_t *device);
void read(int type, int id, int location, char *buffer);

Re:Device Management

Posted: Fri Feb 07, 2003 3:09 pm
by Tim
Therx wrote:

Code: Select all

   memcpy(&devices[type][totaldev[type]], &device, sizeof(dev_t));
Shouldn't this line be:

Code: Select all

   memcpy(&devices[type][totaldev[type]], device, sizeof(dev_t));
device is already a pointer; &device is a pointer to a pointer, which is now what you want.

Re:Device Management

Posted: Fri Feb 07, 2003 4:23 pm
by Therx
Thanks that made the difference. How can you spot such small mistakes? I'd have spent days trying find that :-[

Re:Device Management

Posted: Fri Feb 07, 2003 4:42 pm
by Tim
Experience :). I've seen exactly the same bug (passing a pointer to a pointer to memcpy) a couple of times recently.