Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Freeing Volatile Pointer

47 views
Skip to first unread message

Tejas Sumant

unread,
Jan 3, 2005, 7:40:17 AM1/3/05
to freebsd...@freebsd.org
Hi,

I have a volatile pointer declared in my device driver.
I am allocating memory using kernel malloc function.

host_mem_resp_ptr = (volatile unsigned long *)malloc(sizeof(volatile
unsigned long), M_DEVBUF, M_NOWAIT);

When I try to free it, I get following warning while compiling the driver.

"warning: passing arg 1 of free discards qualifiers from pointer target
type"

The statment to free the memory is

free(host_mem_resp_ptr, M_DEVBUF);

Can anybody please tell me reason and solution?

Tejas


_______________________________________________
freebsd...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hacke...@freebsd.org"

Peter Pentchev

unread,
Jan 3, 2005, 7:54:16 AM1/3/05
to Tejas Sumant, freebsd...@freebsd.org
On Mon, Jan 03, 2005 at 06:09:45PM +0530, Tejas Sumant wrote:
> Hi,
>
> I have a volatile pointer declared in my device driver.
> I am allocating memory using kernel malloc function.
>
> host_mem_resp_ptr = (volatile unsigned long *)malloc(sizeof(volatile
> unsigned long), M_DEVBUF, M_NOWAIT);
>
> When I try to free it, I get following warning while compiling the driver.
>
> "warning: passing arg 1 of free discards qualifiers from pointer target
> type"
>
> The statment to free the memory is
>
> free(host_mem_resp_ptr, M_DEVBUF);
>
> Can anybody please tell me reason and solution?

The reason is that the C compiler treats 'void *' and 'volatile void *'
as different types. In this case, the compiler casts your 'unsigned long *'
to 'void *' and still warns you that this might not necessarily be what
you want, although it is harmless in this case.

In theory, if a function is declared as accepting a non-volatile pointer,
passing a volatile pointer could be dangerous - the function could stash it
somewhere and then attempt to reuse it later when its value has actually
been changed. This might cause the function to access memory that is no
longer allocated, or just the wrong chunk of memory, or something similar.

In your case the only danger lies in some other thread modifying the
pointer *and invalidating the memory* that it previously pointed to,
after you call free() and before free() has actually freed the memory.
It is up to you to decide whether this is a risk - or rather, to make
sure that it isn't :) Then use something like:

free((void *)host_mem_resp_ptr, M_DEVBUF)

...and you should be okay, unless you specifically pass -Wcast-qual to
the compiler. If you do, it will again give this warning, just because
you have explicitly asked it to :)

G'luck,
Peter

--
Peter Pentchev ro...@ringlet.net ro...@cnsys.bg ro...@FreeBSD.org
PGP key: http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint FDBA FD79 C26F 3C51 C95E DF9E ED18 B68D 1619 4553
What would this sentence be like if pi were 3?

Joerg Sonnenberger

unread,
Jan 3, 2005, 11:03:10 AM1/3/05
to freebsd...@freebsd.org, Tejas Sumant
On Mon, Jan 03, 2005 at 02:53:51PM +0200, Peter Pentchev wrote:
> free((void *)host_mem_resp_ptr, M_DEVBUF)
>
> ...and you should be okay, unless you specifically pass -Wcast-qual to
> the compiler. If you do, it will again give this warning, just because
> you have explicitly asked it to :)

Still better is using __DEVOLATILE from sys/cdefs.h, which does a cast to
uintptr_t first. This way, you even avoid this warning :)

Joerg

Tejas Sumant

unread,
Jan 4, 2005, 2:55:24 AM1/4/05
to freebsd...@freebsd.org, Joerg Sonnenberger

>
> Still better is using __DEVOLATILE from sys/cdefs.h, which does a cast to
> uintptr_t first. This way, you even avoid this warning :)

Is it available with FreeBSD4.10 release?
I am working on 4.10.
I couldnt find this __DEVOLATILE.

OR
Is it defined in any other file for 4.10 release?

Tejas

Joerg Sonnenberger

unread,
Jan 4, 2005, 11:47:10 AM1/4/05
to freebsd...@freebsd.org
On Tue, Jan 04, 2005 at 01:24:17PM +0530, Tejas Sumant wrote:
> Is it available with FreeBSD4.10 release?
> I am working on 4.10.
> I couldnt find this __DEVOLATILE.

__DEVOLATILE, __DECONST and the like haven't been MFCd.

Joerg

0 new messages