Date: Mon, 3 Jan 2005 14:53:51 +0200 From: Peter Pentchev <roam@ringlet.net> To: Tejas Sumant <tejas.sumant@gmail.com> Cc: freebsd-hackers@freebsd.org Subject: Re: Freeing Volatile Pointer Message-ID: <20050103125351.GA1242@straylight.m.ringlet.net> In-Reply-To: <009e01c4f191$50433430$1500a8c0@indranet.local> References: <009e01c4f191$50433430$1500a8c0@indranet.local>
next in thread | previous in thread | raw e-mail | index | archive | help
--tKW2IUtsqtDRztdT Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jan 03, 2005 at 06:09:45PM +0530, Tejas Sumant wrote: > Hi, >=20 > I have a volatile pointer declared in my device driver. > I am allocating memory using kernel malloc function. >=20 > host_mem_resp_ptr =3D (volatile unsigned long *)malloc(sizeof(volatile > unsigned long), M_DEVBUF, M_NOWAIT); >=20 > When I try to free it, I get following warning while compiling the driver. >=20 > "warning: passing arg 1 of free discards qualifiers from pointer target > type" >=20 > The statment to free the memory is >=20 > free(host_mem_resp_ptr, M_DEVBUF); >=20 > 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) =2E..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 --=20 Peter Pentchev roam@ringlet.net roam@cnsys.bg roam@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? --tKW2IUtsqtDRztdT Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (FreeBSD) iD8DBQFB2UBf7Ri2jRYZRVMRAuRrAJ9LxGaNChBnrbXbAwd3q7uWoBwvowCgwLck lza2+Yjf61UH4SiXkB7iy2w= =Nh4u -----END PGP SIGNATURE----- --tKW2IUtsqtDRztdT--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050103125351.GA1242>