Search code examples
cmmap

C: Warning about munmap to struct


I have a struct:

typedef struct {
  uint32_t  length_offset;
  uint32_t  tms_offset;
  uint32_t  tdi_offset;
  uint32_t  tdo_offset;
  uint32_t  ctrl_offset;
} jtag_t;

and I create memory mapping with it:

volatile jtag_t* jtag_mem_loc = (volatile jtag_t*) mmap(
  NULL, kMapSize, PROT_READ | PROT_WRITE, 
  MAP_SHARED, fd_uio, 0);

No problem so far, until I munmap:

if ( munmap(jtag_mem_loc, kMapSize) == (fd_t) -1 ) 
  perror("close error: ");

I get warning message

clang -std=c11  ../src/sw/app/xvc_server.c -o ./t                             
../src/sw/app/xvc_server.c:55:17: warning: passing 'volatile jtag_t *' to parameter of type
      'void *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
    if ( munmap(jtag_mem_loc, kMapSize) == (fd_t) -1 ) 
                ^~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/mman.h:247:22: note: 
      passing argument to parameter here
int     munmap(void *, size_t) __DARWIN_ALIAS(munmap);
                     ^
1 warning generated.

I cast type to void* then warning can be solve.

if ( munmap( (void*) jtag_mem_loc, kMapSize) == (fd_t) -1 ) 
  perror("close error: ");

AFAIK, convert AnyType* to void* generally is bad idea. Does I have other solutions?


Solution

  • Your code with the cast is correct and neccessary.