From cef90102cb0366f26a9cf618497793d223d60a66 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 4 Jun 2017 23:52:10 -0700 Subject: [PATCH] SCHARS and STRING_BYTES are nonnegative Tell the compiler that SCHARS and STRING_BYTES are nonnegative, in the hopes that this will optimize a bit better. Also, check this at runtime if ENABLE_CHECKING. * src/lisp.h (SCHARS, STRING_BYTES): eassume that these functions return nonnegative values. (STRING_SET_CHARS) [ENABLE_CHECKING]: eassert that newsize is nonnegative. --- src/lisp.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lisp.h b/src/lisp.h index ce939fcee62..c35bd1f6df1 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -1346,7 +1346,9 @@ SSET (Lisp_Object string, ptrdiff_t index, unsigned char new) INLINE ptrdiff_t SCHARS (Lisp_Object string) { - return XSTRING (string)->size; + ptrdiff_t nchars = XSTRING (string)->size; + eassume (0 <= nchars); + return nchars; } #ifdef GC_CHECK_STRING_BYTES @@ -1356,10 +1358,12 @@ INLINE ptrdiff_t STRING_BYTES (struct Lisp_String *s) { #ifdef GC_CHECK_STRING_BYTES - return string_bytes (s); + ptrdiff_t nbytes = string_bytes (s); #else - return s->size_byte < 0 ? s->size : s->size_byte; + ptrdiff_t nbytes = s->size_byte < 0 ? s->size : s->size_byte; #endif + eassume (0 <= nbytes); + return nbytes; } INLINE ptrdiff_t @@ -1373,7 +1377,7 @@ STRING_SET_CHARS (Lisp_Object string, ptrdiff_t newsize) /* This function cannot change the size of data allocated for the string when it was created. */ eassert (STRING_MULTIBYTE (string) - ? newsize <= SBYTES (string) + ? 0 <= newsize && newsize <= SBYTES (string) : newsize == SCHARS (string)); XSTRING (string)->size = newsize; } -- 2.11.4.GIT