From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.devel Subject: Preferring ptrdiff_t to ssize_t Date: Wed, 01 Oct 2014 13:29:40 -0700 Organization: UCLA Computer Science Department Message-ID: <542C6434.8020505@cs.ucla.edu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1412195431 22860 80.91.229.3 (1 Oct 2014 20:30:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 1 Oct 2014 20:30:31 +0000 (UTC) To: Emacs development discussions Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Oct 01 22:30:24 2014 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XZQXX-0001XZ-OC for ged-emacs-devel@m.gmane.org; Wed, 01 Oct 2014 22:30:23 +0200 Original-Received: from localhost ([::1]:58582 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XZQXX-00065n-5T for ged-emacs-devel@m.gmane.org; Wed, 01 Oct 2014 16:30:23 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XZQXE-000619-B7 for emacs-devel@gnu.org; Wed, 01 Oct 2014 16:30:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XZQX6-0004Yt-OR for emacs-devel@gnu.org; Wed, 01 Oct 2014 16:30:04 -0400 Original-Received: from smtp.cs.ucla.edu ([131.179.128.62]:52728) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XZQX6-0004Xs-HI for emacs-devel@gnu.org; Wed, 01 Oct 2014 16:29:56 -0400 Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.cs.ucla.edu (Postfix) with ESMTP id 98086A60013 for ; Wed, 1 Oct 2014 13:29:49 -0700 (PDT) X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu Original-Received: from smtp.cs.ucla.edu ([127.0.0.1]) by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id F0uAmN58DWg2 for ; Wed, 1 Oct 2014 13:29:40 -0700 (PDT) Original-Received: from penguin.cs.ucla.edu (Penguin.CS.UCLA.EDU [131.179.64.200]) by smtp.cs.ucla.edu (Postfix) with ESMTPSA id D9B8639E8012 for ; Wed, 1 Oct 2014 13:29:40 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 131.179.128.62 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:174916 Archived-At: The Emacs C code prefers signed integer types to unsigned, so it typically uses ptrdiff_t for indexes that might not fit in 'int'. At first glance, another type ssize_t is a plausible alternative, as the two types are equivalent on typical platforms. On less-common platforms, though, ptrdiff_t works better than ssize_t, so for size-related integers Emacs's portable C code should continue to prefer ptrdiff_t. Here are some details about this: 1. Historically, some 64-bit Unix-based platforms had 32-bit ssize_t even though size_t and ptrdiff_t were both 64-bit, because they had 32-bit 'int' and wanted system calls like 'read' to support the traditional default API where 'read' returned 'int'. I don't know whether these platforms have died out entirely, but I suspect they haven't. 2. A few platforms even now have ptrdiff_t wider than size_t, thus avoiding the problem of integer overflow when subtracting pointers. Using ptrdiff_t could therefore improve the quality of Emacs ports to these platforms, even if Emacs doesn't happen to run there now. 3. ptrdiff_t is more ubiquitous and better-standardized than ssize_t, as ptrdiff_t is required by the C standard whereas ssize_t is required only by POSIX. 4. There are standard printf formats for prtdiff_t (e.g, "%td") but not for ssize_t. 5. To avoid problems with integer overflows in ptrdiff_t-related calculations, Emacs never allocates objects larger than PTRDIFF_MAX bytes. On platforms where PTRDIFF_MAX != SSIZE_MAX this approach wouldn't work if ssize_t started to be used extensively. 6. POSIX requires only that ssize_t be able to store values in the range [-1, SSIZE_MAX], which makes it an iffy choice (at least in theory) for containing negative values other than -1. I'm bringing this up now because in emacs-24 bzr 117515 Eli introduced a ssize_t local variable, in trunk bzr 117993 I changed it to ptrdiff_t, and Eli reverted the latter change in trunk bzr 117997. In this particular case the value happens to be an 'int', so on second thought perhaps Emacs should just declare it as 'int' rather than mess with *_t types. More generally, though, when size-related values might not fit in 'int', Emacs should continue to prefer ptrdiff_t to ssize_t in portable code. I plan to add a paragraph to internals.texi to help document this.