[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master cef9010 1/3: SCHARS and STRING_BYTES are nonnegativ
From: |
Paul Eggert |
Subject: |
[Emacs-diffs] master cef9010 1/3: SCHARS and STRING_BYTES are nonnegative |
Date: |
Mon, 5 Jun 2017 02:53:53 -0400 (EDT) |
branch: master
commit cef90102cb0366f26a9cf618497793d223d60a66
Author: Paul Eggert <address@hidden>
Commit: Paul Eggert <address@hidden>
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 ce939fc..c35bd1f 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;
}