From owner-freebsd-arch@FreeBSD.ORG Sat Mar 25 08:06:35 2006 Return-Path: X-Original-To: freebsd-arch@freebsd.org Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4694216A401; Sat, 25 Mar 2006 08:06:35 +0000 (UTC) (envelope-from dillon@apollo.backplane.com) Received: from apollo.backplane.com (apollo.backplane.com [216.240.41.2]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0E21843D45; Sat, 25 Mar 2006 08:06:34 +0000 (GMT) (envelope-from dillon@apollo.backplane.com) Received: from apollo.backplane.com (localhost [127.0.0.1]) by apollo.backplane.com (8.13.4.20060308/8.13.4) with ESMTP id k2P86Y3U011862; Sat, 25 Mar 2006 00:06:34 -0800 (PST) Received: (from dillon@localhost) by apollo.backplane.com (8.13.4.20060308/8.13.4/Submit) id k2P86YJU011861; Sat, 25 Mar 2006 00:06:34 -0800 (PST) Date: Sat, 25 Mar 2006 00:06:34 -0800 (PST) From: Matthew Dillon Message-Id: <200603250806.k2P86YJU011861@apollo.backplane.com> To: Jason Evans References: <44247DF1.8000002@FreeBSD.org> Cc: freebsd-arch@freebsd.org Subject: Re: Proposed addition of malloc_size_np() X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Mar 2006 08:06:35 -0000 : :=== Proposal === :Add malloc_size_np() to libc, and provide the prototype in malloc_np.h: : : size_t : malloc_size_np(const void *ptr); : :This function would return the usable size of the allocation pointed to :by ptr, which is always greater than or equal to the size that was :specified in the call to malloc(), calloc(), posix_memalign(), or :... That creates a serious race condition for any threaded program, since multiple threads might be malloc()ing memory at the same time and one could wind up using the 'extra' space that abuts an allocation being tested by some other thread. Maybe something like: n = realloc_try(ptr, bytes) This function attempts to extend the size of a previously allocated memory block without changing its pointer. The total size of the (now possibly extended) memory pointed to by pointer is returned. The total size returned will always be at least min(bytes, previous_size) and will never exceed (bytes). The function cannot fail, but may not return the requested number of bytes. This function may also be used to truncate the size of a previously allocated memory block without changing its pointer. This function will always return (bytes) when the passed bytes is less then or equal to the size of the current allocation. The function does NOT guarentee that the memory allocator can actually use the freed memory past the truncation point for other unrelated allocations, but it DOES guarentee that, baring other allocations that reuse the memory, the current allocation can be extended back into the truncated area with another call to realloc_try(). -- That would give you the most flexibility and be relatively opaque to the actual malloc implementation. The degenerate case would only have to determine the size of the current allocation to return a meaningful result (even if it doesn't actually extend or truncate it), and that information clearly must already be attainable since the original allocation size is not specified when you free() the memory. -Matt