From owner-freebsd-arch@FreeBSD.ORG Mon Jun 16 18:07:25 2003 Return-Path: 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 ED0A937B401; Mon, 16 Jun 2003 18:07:25 -0700 (PDT) Received: from c104-254.bas1.prp.dublin.eircom.net (c104-254.bas1.prp.dublin.eircom.net [159.134.104.254]) by mx1.FreeBSD.org (Postfix) with SMTP id 65AE243FA3; Mon, 16 Jun 2003 18:07:24 -0700 (PDT) (envelope-from iedowse@maths.tcd.ie) To: Bruce Evans In-Reply-To: Your message of "Mon, 16 Jun 2003 21:13:13 +1000." <20030616205631.F28116@gamplex.bde.org> Date: Tue, 17 Jun 2003 01:59:34 +0100 From: Ian Dowse Message-ID: <200306170159.aa26127@salmon.maths.tcd.ie> cc: Don Lewis cc: iedowse@maths.tcd.ie cc: freebsd-arch@FreeBSD.org Subject: Re: Message buffer and printf reentrancy patch X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Jun 2003 01:07:26 -0000 In message <20030616205631.F28116@gamplex.bde.org>, Bruce Evans writes: >On Mon, 16 Jun 2003, Don Lewis wrote: >> It looks like MSGBUF_SEQNORM() could avoid the conditional code and any >> questions about signed remainders if it was defined like this: >> >> #define MSGBUF_SEQNORM(mbp, seq) (((seq) + (mbp)->msg_seqmod) % \ >> (mbp)->msg_seqmod) >> >> as long as msg_seqmod < INT_MAX/2. MSGBUF_SEQNORM() could be simplified >> further if msg_seqmod was added by the caller (such as MSGBUF_SEQSUB()) >> if the argument could be negative. > >Yes. The negative numbers of interest seem to be limited to at most >differences of sequence numbers (or maybe differeces of indexes, which >are smaller), so they are larger than -msg_seqmod. MSGBUF_SEQSUB() >shouldn't add the bias, however, since it is used in contexts where >we really want to see the negative values. The only minor problem I see with the above is that it is fragile with respect to arbitrary input sequence numbers, in that it could return a negative value. However, the property of guaranteeing to return a normalised sequence number can be achieved by forcing an unsigned division like in MSGBUF_SEQ_TO_POS, i.e.: #define MSGBUF_SEQNORM(mbp, seq) ((int)((u_int)((seq) + \ (mbp)->msg_seqmod) % (mbp)->msg_seqmod)) This should do the right thing for the expected ranges, but also ensures that the macro itself can never return an out-of-range sequence number, whatever the input value. Ian