vxworks vxmemprobe源码
时间: 2023-11-09 22:08:43 浏览: 165
VxWorks 操作系统中的 vxMemProbe 函数用于在指定地址中测试内存访问是否成功。它的源码位于目录 `vxworks-6.9/target/src/os/memLib.c` 中,以下是该函数的源码:
```c
/******************************************************************************
*
* vxMemProbe - probe a task's memory for readability and/or writability
*
* This routine probes a task's memory for readability and/or writability.
* The memory is probed by attempting to read and/or write a byte at the
* specified address.
*
* This routine can be used to determine whether or not a task's memory is
* readable and/or writable. If the memory is not readable and/or writable,
* this routine will return ERROR.
*
* RETURNS: OK, or ERROR if the specified address is invalid or not accessible.
*/
STATUS vxMemProbe
(
char * addr, /* address to be probed */
int mode, /* VX_READ or VX_WRITE */
int length, /* 1, 2, or 4 bytes */
char * pVal /* where to return value, if read */
)
{
STATUS status = OK; /* status to return */
char val; /* value read from memory */
/* check parameters */
if ((mode != VX_READ) && (mode != VX_WRITE))
return (ERROR);
if ((length != 1) && (length != 2) && (length != 4))
return (ERROR);
/* probe memory */
if (mode == VX_READ)
{
switch (length)
{
case 1:
if (vxTas((char *) addr) == ERROR) /* read byte */
status = ERROR;
break;
case 2:
if (vxTas((short *) addr) == ERROR) /* read short */
status = ERROR;
break;
case 4:
if (vxTas((int *) addr) == ERROR) /* read long */
status = ERROR;
break;
}
if ((status != ERROR) && (pVal != NULL))
{
switch (length)
{
case 1:
*pVal = *((char *) addr); /* return byte */
break;
case 2:
*((short *) pVal) = *((short *) addr); /* return short */
break;
case 4:
*((int *) pVal) = *((int *) addr); /* return long */
break;
}
}
}
else /* mode == VX_WRITE */
{
val = *pVal; /* value to be written */
switch (length)
{
case 1:
if (vxTas((char *) addr) == ERROR) /* write byte */
status = ERROR;
else
*((char *) addr) = val;
break;
case 2:
if (vxTas((short *) addr) == ERROR) /* write short */
status = ERROR;
else
*((short *) addr) = *((short *) &val);
break;
case 4:
if (vxTas((int *) addr) == ERROR) /* write long */
status = ERROR;
else
*((int *) addr) = *((int *) &val);
break;
}
}
return (status);
}
```
以上代码首先检查传入参数的有效性,然后通过 `vxTas` 函数尝试在指定地址读取或写入一个字节、短整型或长整型数据。如果读取或写入失败,则返回 `ERROR`,否则返回 `OK`。如果指定了 `pVal`,则会将读取的数据保存在 `pVal` 中。
阅读全文