From owner-freebsd-hackers@FreeBSD.ORG Mon Jan 3 12:53:48 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6A1E416A4CE for ; Mon, 3 Jan 2005 12:53:48 +0000 (GMT) Received: from gandalf.online.bg (gandalf.online.bg [217.75.128.9]) by mx1.FreeBSD.org (Postfix) with SMTP id 0823E43D54 for ; Mon, 3 Jan 2005 12:53:47 +0000 (GMT) (envelope-from roam@ringlet.net) Received: (qmail 1157 invoked from network); 3 Jan 2005 12:53:41 -0000 Received: from unknown (HELO straylight.ringlet.net) (217.75.135.134) by gandalf.online.bg with SMTP; 3 Jan 2005 12:53:41 -0000 Received: (qmail 6124 invoked by uid 1000); 3 Jan 2005 12:53:51 -0000 Date: Mon, 3 Jan 2005 14:53:51 +0200 From: Peter Pentchev To: Tejas Sumant Message-ID: <20050103125351.GA1242@straylight.m.ringlet.net> Mail-Followup-To: Tejas Sumant , freebsd-hackers@freebsd.org References: <009e01c4f191$50433430$1500a8c0@indranet.local> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="tKW2IUtsqtDRztdT" Content-Disposition: inline In-Reply-To: <009e01c4f191$50433430$1500a8c0@indranet.local> User-Agent: Mutt/1.5.6i cc: freebsd-hackers@freebsd.org Subject: Re: Freeing Volatile Pointer X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 Jan 2005 12:53:48 -0000 --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--