diff options
| author | Paul Eggert <eggert@cs.ucla.edu> | 2017-04-04 14:06:25 -0700 |
|---|---|---|
| committer | Paul Eggert <eggert@cs.ucla.edu> | 2017-04-04 14:07:54 -0700 |
| commit | 82b9efc869dddcbd5677867b84b20ae155b5b9da (patch) | |
| tree | c570068c6e5801726a2ea05474a3779bf5cd4fd6 /src | |
| parent | 12317ff4fb53f889a69a858a629df6beffacd051 (diff) | |
| download | emacs-82b9efc869dddcbd5677867b84b20ae155b5b9da.tar.gz | |
Minor simplifications and doc for records
* doc/lispref/records.texi (Records): Mention size limit.
* etc/NEWS: Mention records.
* src/alloc.c (allocate_pseudovector, allocate_record):
Prefer 'PSEUDOVECTOR_SIZE_MASK' to its definiens.
(allocate_record): Check arg range here, not in callers, as this
simplifies the code. Use allocate_vectorlike instead of
allocate_vector, to avoid duplicate runtime tests.
(Fmake_record, record): Don't mention PSEUDOVECTOR_SIZE_BITS in
the doc string, as it is not visible to the user.
(Fmake_record, record, Fcopy_record):
Prefer make_lisp_ptr to XSETVECTOR.
(record): Broaden memcpy to copy the type, too.
Diffstat (limited to 'src')
| -rw-r--r-- | src/alloc.c | 67 |
1 files changed, 19 insertions, 48 deletions
diff --git a/src/alloc.c b/src/alloc.c index 5024b1211e5..a58824fb0f3 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -3371,7 +3371,7 @@ allocate_pseudovector (int memlen, int lisplen, eassert (0 <= tag && tag <= PVEC_FONT); eassert (0 <= lisplen && lisplen <= zerolen && zerolen <= memlen); eassert (memlen - lisplen <= (1 << PSEUDOVECTOR_REST_BITS) - 1); - eassert (lisplen <= (1 << PSEUDOVECTOR_SIZE_BITS) - 1); + eassert (lisplen <= PSEUDOVECTOR_SIZE_MASK); /* Only the first LISPLEN slots will be traced normally by the GC. */ memclear (v->contents, zerolen * word_size); @@ -3393,16 +3393,17 @@ allocate_buffer (void) } -/* Allocate a new record with COUNT slots. Return NULL if COUNT is - too large. */ +/* Allocate a record with COUNT slots. COUNT must be positive, and + includes the type slot. */ static struct Lisp_Vector * -allocate_record (int count) +allocate_record (EMACS_INT count) { - if (count >= (1 << PSEUDOVECTOR_SIZE_BITS)) - return NULL; - - struct Lisp_Vector *p = allocate_vector (count); + if (count > PSEUDOVECTOR_SIZE_MASK) + error ("Attempt to allocate a record of %"pI"d slots; max is %d", + count, PSEUDOVECTOR_SIZE_MASK); + struct Lisp_Vector *p = allocate_vectorlike (count); + p->header.size = count; XSETPVECTYPE (p, PVEC_RECORD); return p; } @@ -3411,53 +3412,29 @@ allocate_record (int count) DEFUN ("make-record", Fmake_record, Smake_record, 3, 3, 0, doc: /* Create a new record. TYPE is its type as returned by `type-of'. SLOTS is the number of -slots, each initialized to INIT. The number of slots, including the -type slot, must fit in PSEUDOVECTOR_SIZE_BITS. */) +non-type slots, each initialized to INIT. */) (Lisp_Object type, Lisp_Object slots, Lisp_Object init) { - Lisp_Object record; - ptrdiff_t size, i; - struct Lisp_Vector *p; - CHECK_NATNUM (slots); - - size = XFASTINT (slots) + 1; - p = allocate_record (size); - if (p == NULL) - error ("Attempt to allocate a record of %"pD"d slots; max is %d", - size, (1 << PSEUDOVECTOR_SIZE_BITS) - 1); - + EMACS_INT size = XFASTINT (slots) + 1; + struct Lisp_Vector *p = allocate_record (size); p->contents[0] = type; - for (i = 1; i < size; i++) + for (ptrdiff_t i = 1; i < size; i++) p->contents[i] = init; - - XSETVECTOR (record, p); - return record; + return make_lisp_ptr (p, Lisp_Vectorlike); } DEFUN ("record", Frecord, Srecord, 1, MANY, 0, doc: /* Create a new record. TYPE is its type as returned by `type-of'. SLOTS is used to -initialize the record slots with shallow copies of the arguments. The -number of slots, including the type slot, must fit in -PSEUDOVECTOR_SIZE_BITS. +initialize the record slots with shallow copies of the arguments. usage: (record TYPE &rest SLOTS) */) (ptrdiff_t nargs, Lisp_Object *args) { struct Lisp_Vector *p = allocate_record (nargs); - if (p == NULL) - error ("Attempt to allocate a record of %"pD"d slots; max is %d", - nargs, (1 << PSEUDOVECTOR_SIZE_BITS) - 1); - - Lisp_Object type = args[0]; - Lisp_Object record; - - p->contents[0] = type; - memcpy (p->contents + 1, args + 1, (nargs - 1) * sizeof *args); - - XSETVECTOR (record, p); - return record; + memcpy (p->contents, args, nargs * sizeof *args); + return make_lisp_ptr (p, Lisp_Vectorlike); } @@ -3466,17 +3443,11 @@ DEFUN ("copy-record", Fcopy_record, Scopy_record, 1, 1, 0, (Lisp_Object record) { CHECK_RECORD (record); - struct Lisp_Vector *src = XVECTOR (record); ptrdiff_t size = ASIZE (record) & PSEUDOVECTOR_SIZE_MASK; struct Lisp_Vector *new = allocate_record (size); - if (new == NULL) - error ("Attempt to allocate a record of %"pD"d slots; max is %d", - size, (1 << PSEUDOVECTOR_SIZE_BITS) - 1); - - memcpy (&(new->contents[0]), &(src->contents[0]), + memcpy (new->contents, XVECTOR (record)->contents, size * sizeof (Lisp_Object)); - XSETVECTOR (record, new); - return record; + return make_lisp_ptr (new, Lisp_Vectorlike); } |
