Note that there are some explanatory texts on larger screens.

plurals
  1. POKernel Device Driver "dereferencing 'void *' pointer
    primarykey
    data
    text
    <p>I am learning how to write device drivers for linux, and I have a question regarding the use of generic data structures.</p> <p>I have an assignment, which I have fully functional...so I'm not asking you to do my homework...</p> <p>This assignment requires a device to be able to enqueue and dequeue elements from a fifo buffer. I made the buffer "generic" so that any element size could be used(and specified at runtime). The source is included below(note this is not the kernel version, but the error is the same)...kernel version requires kmalloc, copy_to/from_user() etc...</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; struct RB_Buffer { void* RBData; unsigned int getindex; //index to remove element unsigned int putindex; //index to put element at unsigned int capacity; //max elements buffer holds unsigned int elemCount; //num elements inserted unsigned int elemSize; //size of each element }; void* RB_kcreate(int numElements, unsigned int elementSize); int putring(struct RB_Buffer *rbptr, void* data); int getring(struct RB_Buffer *rbptr, void* data); //Creates a Ring buffer of specified number of elements and element size. //Returns void* pointer pointing to the RB_Buffer struct. This pointer can //then be used on putring and getring functions. void* RB_kcreate(int numElements, unsigned int elementSize) { struct RB_Buffer *newBuf = malloc(sizeof(struct RB_Buffer)); if(newBuf == NULL) return 0; newBuf-&gt;RBData = (void*)malloc(elementSize*numElements);//, GFP_KERNEL); if(newBuf-&gt;RBData == NULL) { free(newBuf); return 0; } newBuf-&gt;capacity = numElements; newBuf-&gt;elemSize = elementSize; newBuf-&gt;getindex = 0; newBuf-&gt;putindex = 0; newBuf-&gt;elemCount = 0; return newBuf; } //puts an element in the buffer. Returns -1 if full, 0 on success //send data through void* data argument int putring(struct RB_Buffer *rbptr, void* data) { int i = 0; if ( rbptr-&gt;elemCount &gt;= rbptr-&gt;capacity ) return -1; memcpy(&amp;rbptr-&gt;RBData[rbptr-&gt;putindex * rbptr-&gt;elemSize], data, rbptr-&gt;elemSize); rbptr-&gt;putindex++; if (rbptr-&gt;putindex &gt;= rbptr-&gt;capacity ) rbptr-&gt;putindex = 0; rbptr-&gt;elemCount++; return 0; } //removes an element in the buffer. Returns -1 if empty, 0 on success //data is returned through the data pointer int getring(struct RB_Buffer *rbptr, void *data) { if ( !rbptr-&gt;elemCount ) return -1; rbptr-&gt;elemCount--; memcpy(data, &amp;rbptr-&gt;RBData[rbptr-&gt;getindex * rbptr-&gt;elemSize], rbptr-&gt;elemSize); rbptr-&gt;getindex++; if ( rbptr-&gt;getindex &gt;= rbptr-&gt;capacity ) rbptr-&gt;getindex = 0; return 0; } </code></pre> <p>When I compile this into a kernel module, I get the warnings:</p> <p><code>kringbuf_generic.c:53: warning: dereferencing ‘void *’ pointer kringbuf_generic.c:72: warning: dereferencing ‘void *’ pointer</code></p> <p>The error occurs here in putring(in memcpy)</p> <pre><code>if ( rbptr-&gt;elemCount &gt;= rbptr-&gt;capacity ) return -1; memcpy(&amp;rbptr-&gt;RBData[rbptr-&gt;putindex * rbptr-&gt;elemSize], data, rbptr-&gt;elemSize); rbptr-&gt;putindex++; </code></pre> <p>and here in getring, in the memcpy() function</p> <pre><code>rbptr-&gt;elemCount--; memcpy(data, &amp;rbptr-&gt;RBData[rbptr-&gt;getindex * rbptr-&gt;elemSize], rbptr-&gt;elemSize); rbptr-&gt;getindex++; </code></pre> <p>Obviously since this is a kernel module, it is not really known who will use this, and the fixing the buffer element size would limit the usage of this buffer. </p> <p>Is there any way to get rid of the warnings? Or is there some fundamental thing I should be doing differently when developing such code?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload