From 74b63d27a629db96b73a83f205d8a256911abc1c Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 1 Apr 2019 11:54:23 -0700 Subject: [PATCH] Make struct Lisp_Objfwd etc. objects read-only MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Initialize these objects statically, and make them constants. This is a bit safer and more efficient. * src/data.c (XBOOLFWD, XKBOARD_OBJFWD, XFIXNUMFWD, XOBJFWD): * src/lisp.h (XBUFFER_OBJFWD): Return a pointer-to-const instead of an unrestricted pointer. (lispfwd): fwdptr is now a pointer-to-const instead of an unrestricted pointer. All uses changed. (SET_SYMBOL_FWD): Accept pointer-to-const instead of an unrestricted pointer. (DEFVAR_LISP, DEFVAR_LISP_NOPRO, DEFVAR_BOOL, DEFVAR_INT) (DEFVAR_KBOARD): Initialize static structures statically instead of dynamically, and make them const. * src/lread.c (defvar_int, defvar_bool, defvar_lisp_nopro) (defvar_lisp, defvar_kboard): Accept pointer-to-const instead of an unrestricted pointer; it’s now the caller’s responsibility to initialize the pointed-to storage. No need for a separate address argument any more. All callers changed. --- src/data.c | 8 ++++---- src/lisp.h | 43 ++++++++++++++++++++++++------------------- src/lread.c | 42 ++++++++++++------------------------------ 3 files changed, 40 insertions(+), 53 deletions(-) diff --git a/src/data.c b/src/data.c index 936bb74f69a..11cd598ed85 100644 --- a/src/data.c +++ b/src/data.c @@ -62,25 +62,25 @@ OBJFWDP (lispfwd a) return XFWDTYPE (a) == Lisp_Fwd_Obj; } -static struct Lisp_Boolfwd * +static struct Lisp_Boolfwd const * XBOOLFWD (lispfwd a) { eassert (BOOLFWDP (a)); return a.fwdptr; } -static struct Lisp_Kboard_Objfwd * +static struct Lisp_Kboard_Objfwd const * XKBOARD_OBJFWD (lispfwd a) { eassert (KBOARD_OBJFWDP (a)); return a.fwdptr; } -static struct Lisp_Intfwd * +static struct Lisp_Intfwd const * XFIXNUMFWD (lispfwd a) { eassert (INTFWDP (a)); return a.fwdptr; } -static struct Lisp_Objfwd * +static struct Lisp_Objfwd const * XOBJFWD (lispfwd a) { eassert (OBJFWDP (a)); diff --git a/src/lisp.h b/src/lisp.h index 62c3230a148..a0a7cbdf518 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -803,7 +803,7 @@ INLINE void union of the possible values (struct Lisp_Objfwd, struct Lisp_Intfwd, etc.). The pointer is packaged inside a struct to help static checking. */ -typedef struct { void *fwdptr; } lispfwd; +typedef struct { void const *fwdptr; } lispfwd; /* Interned state of a symbol. */ @@ -2204,7 +2204,7 @@ SET_SYMBOL_BLV (struct Lisp_Symbol *sym, struct Lisp_Buffer_Local_Value *v) sym->u.s.val.blv = v; } INLINE void -SET_SYMBOL_FWD (struct Lisp_Symbol *sym, void *v) +SET_SYMBOL_FWD (struct Lisp_Symbol *sym, void const *v) { eassume (sym->u.s.redirect == SYMBOL_FORWARDED && v); sym->u.s.val.fwd.fwdptr = v; @@ -2759,7 +2759,7 @@ struct Lisp_Kboard_Objfwd INLINE enum Lisp_Fwd_Type XFWDTYPE (lispfwd a) { - enum Lisp_Fwd_Type *p = a.fwdptr; + enum Lisp_Fwd_Type const *p = a.fwdptr; return *p; } @@ -2769,7 +2769,7 @@ BUFFER_OBJFWDP (lispfwd a) return XFWDTYPE (a) == Lisp_Fwd_Buffer_Obj; } -INLINE struct Lisp_Buffer_Objfwd * +INLINE struct Lisp_Buffer_Objfwd const * XBUFFER_OBJFWD (lispfwd a) { eassert (BUFFER_OBJFWDP (a)); @@ -3096,11 +3096,11 @@ enum maxargs CALLN is overkill for simple usages like 'Finsert (1, &text);'. */ #define CALLN(f, ...) CALLMANY (f, ((Lisp_Object []) {__VA_ARGS__})) -extern void defvar_lisp (struct Lisp_Objfwd *, const char *, Lisp_Object *); -extern void defvar_lisp_nopro (struct Lisp_Objfwd *, const char *, Lisp_Object *); -extern void defvar_bool (struct Lisp_Boolfwd *, const char *, bool *); -extern void defvar_int (struct Lisp_Intfwd *, const char *, intmax_t *); -extern void defvar_kboard (struct Lisp_Kboard_Objfwd *, const char *, int); +extern void defvar_lisp (struct Lisp_Objfwd const *, char const *); +extern void defvar_lisp_nopro (struct Lisp_Objfwd const *, char const *); +extern void defvar_bool (struct Lisp_Boolfwd const *, char const *); +extern void defvar_int (struct Lisp_Intfwd const *, char const *); +extern void defvar_kboard (struct Lisp_Kboard_Objfwd const *, char const *); /* Macros we use to define forwarded Lisp variables. These are used in the syms_of_FILENAME functions. @@ -3121,29 +3121,34 @@ extern void defvar_kboard (struct Lisp_Kboard_Objfwd *, const char *, int); #define DEFVAR_LISP(lname, vname, doc) \ do { \ - static struct Lisp_Objfwd o_fwd; \ - defvar_lisp (&o_fwd, lname, &globals.f_ ## vname); \ + static struct Lisp_Objfwd const o_fwd \ + = {Lisp_Fwd_Obj, &globals.f_##vname}; \ + defvar_lisp (&o_fwd, lname); \ } while (false) #define DEFVAR_LISP_NOPRO(lname, vname, doc) \ do { \ - static struct Lisp_Objfwd o_fwd; \ - defvar_lisp_nopro (&o_fwd, lname, &globals.f_ ## vname); \ + static struct Lisp_Objfwd const o_fwd \ + = {Lisp_Fwd_Obj, &globals.f_##vname}; \ + defvar_lisp_nopro (&o_fwd, lname); \ } while (false) #define DEFVAR_BOOL(lname, vname, doc) \ do { \ - static struct Lisp_Boolfwd b_fwd; \ - defvar_bool (&b_fwd, lname, &globals.f_ ## vname); \ + static struct Lisp_Boolfwd const b_fwd \ + = {Lisp_Fwd_Bool, &globals.f_##vname}; \ + defvar_bool (&b_fwd, lname); \ } while (false) #define DEFVAR_INT(lname, vname, doc) \ do { \ - static struct Lisp_Intfwd i_fwd; \ - defvar_int (&i_fwd, lname, &globals.f_ ## vname); \ + static struct Lisp_Intfwd const i_fwd \ + = {Lisp_Fwd_Int, &globals.f_##vname}; \ + defvar_int (&i_fwd, lname); \ } while (false) #define DEFVAR_KBOARD(lname, vname, doc) \ do { \ - static struct Lisp_Kboard_Objfwd ko_fwd; \ - defvar_kboard (&ko_fwd, lname, offsetof (KBOARD, vname ## _)); \ + static struct Lisp_Kboard_Objfwd const ko_fwd \ + = {Lisp_Fwd_Kboard_Obj, offsetof (KBOARD, vname##_)}; \ + defvar_kboard (&ko_fwd, lname); \ } while (false) diff --git a/src/lread.c b/src/lread.c index dd35cf9063e..5f33fcd6957 100644 --- a/src/lread.c +++ b/src/lread.c @@ -4425,28 +4425,19 @@ defalias (struct Lisp_Subr *sname, char *string) C variable of type intmax_t. Sample call (with "xx" to fool make-docfile): DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */ void -defvar_int (struct Lisp_Intfwd *i_fwd, - const char *namestring, intmax_t *address) +defvar_int (struct Lisp_Intfwd const *i_fwd, char const *namestring) { - Lisp_Object sym; - sym = intern_c_string (namestring); - i_fwd->type = Lisp_Fwd_Int; - i_fwd->intvar = address; + Lisp_Object sym = intern_c_string (namestring); XSYMBOL (sym)->u.s.declared_special = true; XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED; SET_SYMBOL_FWD (XSYMBOL (sym), i_fwd); } -/* Similar but define a variable whose value is t if address contains 1, - nil if address contains 0. */ +/* Similar but define a variable whose value is t if 1, nil if 0. */ void -defvar_bool (struct Lisp_Boolfwd *b_fwd, - const char *namestring, bool *address) +defvar_bool (struct Lisp_Boolfwd const *b_fwd, char const *namestring) { - Lisp_Object sym; - sym = intern_c_string (namestring); - b_fwd->type = Lisp_Fwd_Bool; - b_fwd->boolvar = address; + Lisp_Object sym = intern_c_string (namestring); XSYMBOL (sym)->u.s.declared_special = true; XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED; SET_SYMBOL_FWD (XSYMBOL (sym), b_fwd); @@ -4459,37 +4450,28 @@ defvar_bool (struct Lisp_Boolfwd *b_fwd, gc-marked for some other reason, since marking the same slot twice can cause trouble with strings. */ void -defvar_lisp_nopro (struct Lisp_Objfwd *o_fwd, - const char *namestring, Lisp_Object *address) +defvar_lisp_nopro (struct Lisp_Objfwd const *o_fwd, char const *namestring) { - Lisp_Object sym; - sym = intern_c_string (namestring); - o_fwd->type = Lisp_Fwd_Obj; - o_fwd->objvar = address; + Lisp_Object sym = intern_c_string (namestring); XSYMBOL (sym)->u.s.declared_special = true; XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED; SET_SYMBOL_FWD (XSYMBOL (sym), o_fwd); } void -defvar_lisp (struct Lisp_Objfwd *o_fwd, - const char *namestring, Lisp_Object *address) +defvar_lisp (struct Lisp_Objfwd const *o_fwd, char const *namestring) { - defvar_lisp_nopro (o_fwd, namestring, address); - staticpro (address); + defvar_lisp_nopro (o_fwd, namestring); + staticpro (o_fwd->objvar); } /* Similar but define a variable whose value is the Lisp Object stored at a particular offset in the current kboard object. */ void -defvar_kboard (struct Lisp_Kboard_Objfwd *ko_fwd, - const char *namestring, int offset) +defvar_kboard (struct Lisp_Kboard_Objfwd const *ko_fwd, char const *namestring) { - Lisp_Object sym; - sym = intern_c_string (namestring); - ko_fwd->type = Lisp_Fwd_Kboard_Obj; - ko_fwd->offset = offset; + Lisp_Object sym = intern_c_string (namestring); XSYMBOL (sym)->u.s.declared_special = true; XSYMBOL (sym)->u.s.redirect = SYMBOL_FORWARDED; SET_SYMBOL_FWD (XSYMBOL (sym), ko_fwd); -- 2.39.2