view src/array.c @ 5353:38e24b8be4ea

Improve the lexical scoping in #'block, #'return-from. lisp/ChangeLog addition: 2011-02-07 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el: * bytecomp.el (byte-compile-initial-macro-environment): Shadow `block', `return-from' here, we implement them differently when byte-compiling. * bytecomp.el (byte-compile-active-blocks): New. * bytecomp.el (byte-compile-block-1): New. * bytecomp.el (byte-compile-return-from-1): New. * bytecomp.el (return-from-1): New. * bytecomp.el (block-1): New. These are two aliases that exist to have their own associated byte-compile functions, which functions implement `block' and `return-from'. * cl-extra.el (cl-macroexpand-all): Fix a bug here when macros in the environment have been compiled. * cl-macs.el (block): * cl-macs.el (return): * cl-macs.el (return-from): Be more careful about lexical scope in these macros. * cl.el: * cl.el ('cl-block-wrapper): Removed. * cl.el ('cl-block-throw): Removed. These aren't needed in code generated by this XEmacs. They shouldn't be needed in code generated by XEmacs 21.4, but if it turns out the packages do need them, we can put them back. 2011-01-30 Mike Sperber <mike@xemacs.org> * font-lock.el (font-lock-fontify-pending-extents): Don't fail if `font-lock-mode' is unset, which can happen in the middle of `revert-buffer'. 2011-01-23 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (delete): * cl-macs.el (delq): * cl-macs.el (remove): * cl-macs.el (remq): Don't use the compiler macro if these functions were given the wrong number of arguments, as happens in lisp-tests.el. * cl-seq.el (remove, remq): Removed. I added these to subr.el, and forgot to remove them from here. 2011-01-22 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-setq, byte-compile-set): Remove kludge allowing keywords' values to be set, all the code that does that is gone. * cl-compat.el (elt-satisfies-test-p): * faces.el (set-face-parent): * faces.el (face-doc-string): * gtk-font-menu.el: * gtk-font-menu.el (gtk-reset-device-font-menus): * msw-font-menu.el: * msw-font-menu.el (mswindows-reset-device-font-menus): * package-get.el (package-get-installedp): * select.el (select-convert-from-image-data): * sound.el: * sound.el (load-sound-file): * x-font-menu.el (x-reset-device-font-menus-core): Don't quote keywords, they're self-quoting, and the win from backward-compatibility is sufficiently small now that the style problem overrides it. 2011-01-22 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (block, return-from): Require that NAME be a symbol in these macros, as always documented in the #'block docstring and as required by Common Lisp. * descr-text.el (unidata-initialize-unihan-database): Correct the use of non-symbols in #'block and #'return-from in this function. 2011-01-15 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (concatenate): Accept more complicated TYPEs in this function, handing the sequences over to #'coerce if we don't understand them here. * cl-macs.el (inline): Don't proclaim #'concatenate as inline, its compiler macro is more useful than doing that. 2011-01-11 Aidan Kehoe <kehoea@parhasard.net> * subr.el (delete, delq, remove, remq): Move #'remove, #'remq here, they don't belong in cl-seq.el; move #'delete, #'delq here from fns.c, implement them in terms of #'delete*, allowing support for sequences generally. * update-elc.el (do-autoload-commands): Use #'delete*, not #'delq here, now the latter's no longer dumped. * cl-macs.el (delete, delq): Add compiler macros transforming #'delete and #'delq to #'delete* calls. 2011-01-10 Aidan Kehoe <kehoea@parhasard.net> * dialog.el (make-dialog-box): Correct a misplaced parenthesis here, thank you Mats Lidell in 87zkr9gqrh.fsf@mail.contactor.se ! 2011-01-02 Aidan Kehoe <kehoea@parhasard.net> * dialog.el (make-dialog-box): * list-mode.el (display-completion-list): These functions used to use cl-parsing-keywords; change them to use defun* instead, fixing the build. (Not sure what led to me not including this change in d1b17a33450b!) 2011-01-02 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (define-star-compiler-macros): Make sure the form has ITEM and LIST specified before attempting to change to calls with explicit tests; necessary for some tests in lisp-tests.el to compile correctly. (stable-union, stable-intersection): Add compiler macros for these functions, in the same way we do for most of the other functions in cl-seq.el. 2011-01-01 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (dolist, dotimes, do-symbols, macrolet) (symbol-macrolet): Define these macros with defmacro* instead of parsing the argument list by hand, for the sake of style and readability; use backquote where appropriate, instead of calling #'list and and friends, for the same reason. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * x-misc.el (device-x-display): Provide this function, documented in the Lispref for years, but not existing previously. Thank you Julian Bradfield, thank you Jeff Mincy. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * cl-seq.el: Move the heavy lifting from this file to C. Dump the cl-parsing-keywords macro, but don't use defun* for the functions we define that do take keywords, dynamic scope lossage makes that not practical. * subr.el (sort, fillarray): Move these aliases here. (map-plist): #'nsublis is now built-in, but at this point #'eql isn't necessarily available as a test; use #'eq. * obsolete.el (cl-delete-duplicates): Make this available for old compiler macros and old code. (memql): Document that this is equivalent to #'member*, and worse. * cl.el (adjoin, subst): Removed. These are in C. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * simple.el (assoc-ignore-case): Remove a duplicate definition of this function (it's already in subr.el). * iso8859-1.el (char-width): On non-Mule, make this function equivalent to that produced by (constantly 1), but preserve its docstring. * subr.el (subst-char-in-string): Define this in terms of #'substitute, #'nsubstitute. (string-width): Define this using #'reduce and #'char-width. (char-width): Give this a simpler definition, it makes far more sense to check for mule at load time and redefine, as we do in iso8859-1.el. (store-substring): Implement this in terms of #'replace, now #'replace is cheap. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * update-elc.el (lisp-files-needed-for-byte-compilation) (lisp-files-needing-early-byte-compilation): cl-macs belongs in the former, not the latter, it is as fundamental as bytecomp.el. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * cl.el: Provde the Common Lisp program-error, type-error as error symbols. This doesn't nearly go far enough for anyone using the Common Lisp errors. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (delete-duplicates): If the form has an incorrect number of arguments, don't attempt a compiler macroexpansion. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (cl-safe-expr-p): Forms that start with the symbol lambda are also safe. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (= < > <= >=): For these functions' compiler macros, the optimisation is safe even if the first and the last arguments have side effects, since they're only used the once. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (inline-side-effect-free-compiler-macros): Unroll a loop here at macro-expansion time, so these compiler macros are compiled. Use #'eql instead of #'eq in a couple of places for better style. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (notany, notevery): Avoid some dynamic scope stupidity with local variable names in these functions, when they weren't prefixed with cl-; go into some more detail in the doc strings. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (side-effect-free-fns): #'remove, #'remq are free of side-effects. (side-effect-and-error-free-fns): Drop dot, dot-marker from the list. 2010-11-17 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (coerce): In the argument list, name the first argument OBJECT, not X; the former name was always used in the doc string and is clearer. Handle vector type specifications which include the length of the target sequence, error if there's a mismatch. * cl-macs.el (cl-make-type-test): Handle type specifications starting with the symbol 'eql. 2010-11-14 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (eql): Don't remove the byte-compile property of this symbol. That was necessary to override a bug in bytecomp.el where #'eql was confused with #'eq, which bug we no longer have. If neither expression is constant, don't attempt to handle the expression in this compiler macro, leave it to byte-compile-eql, which produces better code anyway. * bytecomp.el (eq): #'eql is not the function associated with the byte-eq byte code. (byte-compile-eql): Add an explicit compile method for this function, for cases where the cl-macs compiler macro hasn't reduced it to #'eq or #'equal. 2010-10-25 Aidan Kehoe <kehoea@parhasard.net> Add compiler macros and compilation sanity-checking for various functions that take keywords. * byte-optimize.el (side-effect-free-fns): #'symbol-value is side-effect free and not error free. * bytecomp.el (byte-compile-normal-call): Check keyword argument lists for sanity; store information about the positions where keyword arguments start using the new byte-compile-keyword-start property. * cl-macs.el (cl-const-expr-val): Take a new optional argument, cl-not-constant, defaulting to nil, in this function; return it if the expression is not constant. (cl-non-fixnum-number-p): Make this into a separate function, we want to pass it to #'every. (eql): Use it. (define-star-compiler-macros): Use the same code to generate the member*, assoc* and rassoc* compiler macros; special-case some code in #'add-to-list in subr.el. (remove, remq): Add compiler macros for these two functions, in preparation for #'remove being in C. (define-foo-if-compiler-macros): Transform (remove-if-not ...) calls to (remove ... :if-not) at compile time, which will be a real win once the latter is in C. (define-substitute-if-compiler-macros) (define-subst-if-compiler-macros): Similarly for these functions. (delete-duplicates): Change this compiler macro to use #'plists-equal; if we don't have information about the type of SEQUENCE at compile time, don't bother attempting to inline the call, the function will be in C soon enough. (equalp): Remove an old commented-out compiler macro for this, if we want to see it it's in version control. (subst-char-in-string): Transform this to a call to nsubstitute or nsubstitute, if that is appropriate. * cl.el (ldiff): Don't call setf here, this makes for a load-time dependency problem in cl-macs.el 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * term/vt100.el: Refer to XEmacs, not GNU Emacs, in permissions. * term/bg-mouse.el: * term/sup-mouse.el: Put copyright notice in canonical "Copyright DATE AUTHOR" form. Refer to XEmacs, not GNU Emacs, in permissions. * site-load.el: Add permission boilerplate. * mule/canna-leim.el: * alist.el: Refer to XEmacs, not APEL/this program, in permissions. * mule/canna-leim.el: Remove my copyright, I've assigned it to the FSF. 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * gtk.el: * gtk-widget-accessors.el: * gtk-package.el: * gtk-marshal.el: * gtk-compose.el: * gnome.el: Add copyright notice based on internal evidence. 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * easymenu.el: Add reference to COPYING to permission notice. * gutter.el: * gutter-items.el: * menubar-items.el: Fix typo "Xmacs" in permissions notice. 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * auto-save.el: * font.el: * fontconfig.el: * mule/kinsoku.el: Add "part of XEmacs" text to permission notice. 2010-10-14 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (side-effect-free-fns): * cl-macs.el (remf, getf): * cl-extra.el (tailp, cl-set-getf, cl-do-remf): * cl.el (ldiff, endp): Tighten up Common Lisp compatibility for #'ldiff, #'endp, #'tailp; add circularity checking for the first two. #'cl-set-getf and #'cl-do-remf were Lisp implementations of #'plist-put and #'plist-remprop; change the names to aliases, changes the macros that use them to using #'plist-put and #'plist-remprop directly. 2010-10-12 Aidan Kehoe <kehoea@parhasard.net> * abbrev.el (fundamental-mode-abbrev-table, global-abbrev-table): Create both these abbrev tables using the usual #'define-abbrev-table calls, rather than attempting to special-case them. * cl-extra.el: Force cl-macs to be loaded here, if cl-extra.el is being loaded interpreted. Previously other, later files would redundantly call (load "cl-macs") when interpreted, it's more reasonable to do it here, once. * cmdloop.el (read-quoted-char-radix): Use defcustom here, we don't have any dump-order dependencies that would prevent that. * custom.el (eval-when-compile): Don't load cl-macs when interpreted or when byte-compiling, rely on cl-extra.el in the former case and the appropriate entry in bytecomp-load-hook in the latter. Get rid of custom-declare-variable-list, we have no dump-time dependencies that would require it. * faces.el (eval-when-compile): Don't load cl-macs when interpreted or when byte-compiling. * packages.el: Remove some inaccurate comments. * post-gc.el (cleanup-simple-finalizers): Use #'delete-if-not here, now the order of preloaded-file-list has been changed to make it available. * subr.el (custom-declare-variable-list): Remove. No need for it. Also remove a stub define-abbrev-table from this file, given the current order of preloaded-file-list there's no need for it. 2010-10-10 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-constp) Forms quoted with FUNCTION are also constant. (byte-compile-initial-macro-environment): In #'the, if FORM is constant and does not match TYPE, warn at byte-compile time. 2010-10-10 Aidan Kehoe <kehoea@parhasard.net> * backquote.el (bq-vector-contents, bq-list*): Remove; the former is equivalent to (append VECTOR nil), the latter to (list* ...). (bq-process-2): Use (append VECTOR nil) instead of using #'bq-vector-contents to convert to a list. (bq-process-1): Now we use list* instead of bq-list * subr.el (list*): Moved from cl.el, since it is now required to be available the first time a backquoted form is encountered. * cl.el (list*): Move to subr.el. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * test-harness.el (Check-Message): Add an omitted comma here, thank you the buildbot. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * hash-table.el (hash-table-key-list, hash-table-value-list) (hash-table-key-value-alist, hash-table-key-value-plist): Remove some useless #'nreverse calls in these files; our hash tables have no order, it's not helpful to pretend they do. * behavior.el (read-behavior): Do the same in this file, in some code evidently copied from hash-table.el. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * info.el (Info-insert-dir): * format.el (format-deannotate-region): * files.el (cd, save-buffers-kill-emacs): Use #'some, #'every and related functions for applying boolean operations to lists, instead of rolling our own ones that cons and don't short-circuit. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-initial-macro-environment): * cl-macs.el (the): Rephrase the docstring, make its implementation when compiling files a little nicer. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * descr-text.el (unidata-initialize-unicodedata-database) (unidata-initialize-unihan-database, describe-char-unicode-data) (describe-char-unicode-data): Wrap calls to the database functions with (with-fboundp ...), avoiding byte compile warnings on builds without support for the database functions. (describe-char): (reduce #'max ...), not (apply #'max ...), no need to cons needlessly. (describe-char): Remove a redundant lambda wrapping #'extent-properties. (describe-char-unicode-data): Call #'nsubst when replacing "" with nil in the result of #'split-string, instead of consing inside mapcar. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * x-faces.el (x-available-font-sizes): * specifier.el (let-specifier): * package-ui.el (pui-add-required-packages): * msw-faces.el (mswindows-available-font-sizes): * modeline.el (modeline-minor-mode-menu): * minibuf.el (minibuf-directory-files): Replace the O2N (delq nil (mapcar (lambda (W) (and X Y)) Z)) with the ON (mapcan (lambda (W) (and X (list Y))) Z) in these files. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (= < > <= >=): When these functions are handed more than two arguments, and those arguments have no side effects, transform to a series of two argument calls, avoiding funcall in the byte-compiled code. * mule/mule-cmds.el (finish-set-language-environment): Take advantage of this change in a function called 256 times at startup. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-function-form, byte-compile-quote) (byte-compile-quote-form): Warn at compile time, and error at runtime, if a (quote ...) or a (function ...) form attempts to quote more than one object. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (byte-optimize-apply): Transform (apply 'nconc (mapcar ...)) to (mapcan ...); warn about use of the first idiom. * update-elc.el (do-autoload-commands): * packages.el (packages-find-package-library-path): * frame.el (frame-list): * extents.el (extent-descendants): * etags.el (buffer-tag-table-files): * dumped-lisp.el (preloaded-file-list): * device.el (device-list): * bytecomp-runtime.el (proclaim-inline, proclaim-notinline) Use #'mapcan, not (apply #'nconc (mapcar ...) in all these files. * bytecomp-runtime.el (eval-when-compile, eval-and-compile): In passing, mention that these macros also evaluate the body when interpreted. tests/ChangeLog addition: 2011-02-07 Aidan Kehoe <kehoea@parhasard.net> * automated/lisp-tests.el: Test lexical scope for `block', `return-from'; add a Known-Bug-Expect-Failure for a contorted example that fails when byte-compiled.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 07 Feb 2011 12:01:24 +0000
parents 5ddbab03b0e6
children 308d34e9f07d
line wrap: on
line source

/* Support for dynarrs and other types of dynamic arrays.
   Copyright (c) 1994, 1995 Free Software Foundation, Inc.
   Copyright (c) 1993, 1995 Sun Microsystems, Inc.
   Copyright (c) 1995, 1996, 2000, 2002, 2003, 2004, 2005, 2010 Ben Wing.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with:  Not in FSF. */

/* Written by Ben Wing, December 1993. */

#include <config.h>
#include "lisp.h"

#include "insdel.h"


/*****************************************************************************/
/*                       "dynarr" a.k.a. dynamic array                       */
/*****************************************************************************/

/*
A "dynamic array" or "dynarr" is a contiguous array of fixed-size elements
where there is no upper limit (except available memory) on the number of
elements in the array.  Because the elements are maintained contiguously,
space is used efficiently (no per-element pointers necessary) and random
access to a particular element is in constant time.  At any one point, the
block of memory that holds the array has an upper limit; if this limit is
exceeded, the memory is realloc()ed into a new array that is twice as big.
Assuming that the time to grow the array is on the order of the new size of
the array block, this scheme has a provably constant amortized time
\(i.e. average time over all additions).

When you add elements or retrieve elements, pointers are used.  Note that
the element itself (of whatever size it is), and not the pointer to it,
is stored in the array; thus you do not have to allocate any heap memory
on your own.  Also, returned pointers are only guaranteed to be valid
until the next operation that changes the length of the array.

This is a container object.  Declare a dynamic array of a specific type
as follows:

  typedef struct
  {
    Dynarr_declare (mytype);
  } mytype_dynarr;

Use the following functions/macros:


  ************* Dynarr creation *************

   void *Dynarr_new(type)
      [MACRO] Create a new dynamic-array object, with each element of the
      specified type.  The return value is cast to (type##_dynarr).
      This requires following the convention that types are declared in
      such a way that this type concatenation works.  In particular, TYPE
      must be a symbol, not an arbitrary C type.  To make dynarrs of
      complex types, a typedef must be declared, e.g.

      typedef unsigned char *unsigned_char_ptr;

      and then you can say

      unsigned_char_ptr_dynarr *dyn = Dynarr_new (unsigned_char_ptr);

   void *Dynarr_new2(dynarr_type, type)
      [MACRO] Create a new dynamic-array object, with each element of the
      specified type.  The array itself is of type DYNARR_TYPE.  This makes
      it possible to create dynarrs over complex types without the need
      to create typedefs, as described above.  Use is as follows:

      ucharptr_dynarr *dyn = Dynarr_new2 (ucharptr_dynarr *, unsigned char *);

   Dynarr_free(d)
      Destroy a dynamic array and the memory allocated to it.

  ************* Dynarr access *************

   type Dynarr_at(d, i)
      [MACRO] Return the element at the specified index.  The index must be
      between 0 and Dynarr_largest(d), inclusive.  With error-checking
      enabled, bounds checking on the index is in the form of asserts() --
      an out-of-bounds index causes an abort.  The element itself is
      returned, not a pointer to it.

   type *Dynarr_atp(d, i)
      [MACRO] Return a pointer to the element at the specified index.
      Restrictions and bounds checking on the index is as for Dynarr_at.
      The pointer may not be valid after an element is added to or
      (conceivably) removed from the array, because this may trigger a
      realloc() performed on the underlying dynarr storage, which may
      involve moving the entire underlying storage to a new location in
      memory.

   type *Dynarr_begin(d)
      [MACRO] Return a pointer to the first element in the dynarr.  See
      Dynarr_atp() for warnings about when the pointer might become invalid.

   type *Dynarr_lastp(d)
      [MACRO] Return a pointer to the last element in the dynarr.  See
      Dynarr_atp() for warnings about when the pointer might become invalid.

   type *Dynarr_past_lastp(d)
      [MACRO] Return a pointer to the beginning of the element just past the
      last one.  WARNING: This may not point to valid memory; however, the
      byte directly before will be pointer will be valid memory.  This macro
      might be useful for various reasons, e.g. as a stopping point in a loop
      (although Dynarr_lastp() could be used just as well) or as a place to
      start writing elements if Dynarr_length() < Dynarr_largest().

  ************* Dynarr length/size retrieval and setting *************

   int Dynarr_length(d)
      [MACRO] Return the number of elements currently in a dynamic array.

   int Dynarr_largest(d)
      [MACRO] Return the maximum value that Dynarr_length(d) would
      ever have returned.  This is used esp. in the redisplay code,
      which reuses dynarrs for performance reasons.

   int Dynarr_max(d)
      [MACRO] Return the maximum number of elements that can fit in the
      dynarr before it needs to be resized.

      Note that Dynarr_length(d) <= Dynarr_largest(d) <= Dynarr_max(d).
   
   Bytecount Dynarr_sizeof(d)
      [MACRO] Return the total size of the elements currently in dynarr
      D.  This 

   Dynarr_set_lengthr(d, len)
      [MACRO] Set the length of D to LEN, which must be between 0 and
      Dynarr_largest(d), inclusive.  With error-checking enabled, an
      assertion failure will result from trying to set the length
      to less than zero or greater than Dynarr_largest(d).  The
      restriction to Dynarr_largest() is to ensure that

   Dynarr_set_length(d, len)
      [MACRO] Set the length of D to LEN, resizing the dynarr as
      necessary to make sure enough space is available.  there are no
      restrictions on LEN other than available memory and that it must
      be at least 0.  Note that

   Dynarr_set_length_and_zero(d, len)
      [MACRO] Like Dynarr_set_length(d, len) but also, if increasing
      the length, zero out the memory between the old and new lengths,
      i.e. starting just past the previous last element and up through
      the new last element.

   Dynarr_incrementr(d)
      [MACRO] Increments the length of D by 1.  Equivalent to
      Dynarr_set_lengthr(d, Dynarr_length(d) + 1).

   Dynarr_increment(d)
      [MACRO] Increments the length of D by 1.  Equivalent to
      Dynarr_set_length(d, Dynarr_length(d) + 1).

   Dynarr_reset(d)
      [MACRO] Reset the length of a dynamic array to 0.

   Dynarr_resize(d, maxval)
      Resize the internal dynarr storage to so that it can hold at least
      MAXVAL elements.  Resizing is done using a geometric series
      (repeatedly multiply the old maximum by a constant, normally 1.5,
      till a large enough size is reached), so this will be efficient
      even if resizing larger by one element at a time.  This is mostly
      an internal function.



  ************* Adding/deleting elements to/from a dynarr *************

   Dynarr_add(d, el)
      [MACRO] Add an element to the end of a dynamic array.  EL is a pointer
      to the element; the element itself is stored in the array, however.
      No function call is performed unless the array needs to be resized.

   Dynarr_add_many(d, base, len)
      [MACRO] Add LEN elements to the end of the dynamic array.  The elements
      should be contiguous in memory, starting at BASE.  If BASE if NULL,
      just make space for the elements; don't actually add them.

   Dynarr_prepend_many(d, base, len)
      [MACRO] Prepend LEN elements to the beginning of the dynamic array.
      The elements should be contiguous in memory, starting at BASE.
      If BASE if NULL, just make space for the elements; don't actually
      add them.

   Dynarr_insert_many(d, base, len, pos)
      Insert LEN elements to the dynamic array starting at position
      POS.  The elements should be contiguous in memory, starting at BASE.
      If BASE if NULL, just make space for the elements; don't actually
      add them.

   type Dynarr_pop(d)
      [MACRO] Pop the last element off the dynarr and return it.

   Dynarr_delete(d, i)
      [MACRO] Delete an element from the dynamic array at position I.

   Dynarr_delete_many(d, pos, len)
      Delete LEN elements from the dynamic array starting at position
      POS.

   Dynarr_zero_many(d, pos, len)
      Zero out LEN elements in the dynarr D starting at position POS.

   Dynarr_delete_by_pointer(d, p)
      [MACRO] Delete an element from the dynamic array at pointer P,
      which must point within the block of memory that stores the data.
      P should be obtained using Dynarr_atp().

  ************* Dynarr locking *************

   Dynarr_lock(d)
      Lock the dynarr against further locking or writing.  With error-checking
      enabled, any attempts to write into a locked dynarr or re-lock an
      already locked one will cause an assertion failure and abort.

   Dynarr_unlock(d)
      Unlock a locked dynarr, allowing writing into it.

  ************* Dynarr global variables *************

   Dynarr_min_size
      Minimum allowable size for a dynamic array when it is resized.

*/

static const struct memory_description const_Ascbyte_ptr_description_1[] = {
  { XD_ASCII_STRING, 0 },
  { XD_END }
};

const struct sized_memory_description const_Ascbyte_ptr_description = {
  sizeof (const Ascbyte *),
  const_Ascbyte_ptr_description_1
};

static const struct memory_description const_Ascbyte_ptr_dynarr_description_1[] = {
  XD_DYNARR_DESC (const_Ascbyte_ptr_dynarr, &const_Ascbyte_ptr_description),
  { XD_END }
};

const struct sized_memory_description const_Ascbyte_ptr_dynarr_description = {
  sizeof (const_Ascbyte_ptr_dynarr),
  const_Ascbyte_ptr_dynarr_description_1
};


static Elemcount Dynarr_min_size = 8;

static void
Dynarr_realloc (Dynarr *dy, Elemcount new_size)
{
  if (DUMPEDP (dy->base))
    {
      void *new_base = malloc (new_size * Dynarr_elsize (dy));
      memcpy (new_base, dy->base, 
	      (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
	      Dynarr_elsize (dy));
      dy->base = new_base;
    }
  else
    dy->base = xrealloc (dy->base, new_size * Dynarr_elsize (dy));
}

void *
Dynarr_newf (Bytecount elsize)
{
  Dynarr *d = xnew_and_zero (Dynarr);
  d->elsize_ = elsize;

  return d;
}

#ifdef NEW_GC
DEFINE_DUMPABLE_INTERNAL_LISP_OBJECT ("dynarr", dynarr,
				      0, 0,
				      Dynarr);

static void
Dynarr_lisp_realloc (Dynarr *dy, Elemcount new_size)
{
  void *new_base =
    XPNTR (alloc_sized_lrecord_array (Dynarr_elsize (dy), new_size,
				      dy->lisp_imp));
  if (dy->base)
    memcpy (new_base, dy->base, 
	    (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
	    Dynarr_elsize (dy));
  dy->base = new_base;
}

void *
Dynarr_lisp_newf (Bytecount elsize, 
		  const struct lrecord_implementation *dynarr_imp, 
		  const struct lrecord_implementation *imp)
{
  Dynarr *d = (Dynarr *) XPNTR (alloc_sized_lrecord (sizeof (Dynarr),
                                                     dynarr_imp));
  d->elsize_ = elsize;
  d->lisp_imp = imp;

  return d;
}
#endif /* not NEW_GC */

void
Dynarr_resize (void *d, Elemcount size)
{
  Elemcount newsize;
  double multiplier;
  Dynarr *dy = (Dynarr *) Dynarr_verify (d);

  if (Dynarr_max (dy) <= 8)
    multiplier = 2;
  else
    multiplier = 1.5;

  for (newsize = Dynarr_max (dy); newsize < size;)
    newsize = max (Dynarr_min_size, (Elemcount) (multiplier * newsize));

  /* Don't do anything if the array is already big enough. */
  if (newsize > Dynarr_max (dy))
    {
#ifdef NEW_GC
      if (dy->lisp_imp)
	Dynarr_lisp_realloc (dy, newsize);
      else
	Dynarr_realloc (dy, newsize);
#else /* not NEW_GC */
      Dynarr_realloc (dy, newsize);
#endif /* not NEW_GC */
      dy->max_ = newsize;
    }
}

/* Add a number of contiguous elements to the array starting at POS. */

void
Dynarr_insert_many (void *d, const void *base, Elemcount len, Elemcount pos)
{
  Dynarr *dy = Dynarr_verify_mod (d);
  Elemcount old_len = Dynarr_length (dy);

  /* #### This could conceivably be wrong, if code wants to access stuff
     between len and largest. */
  dynarr_checking_assert (pos >= 0 && pos <= old_len);
  dynarr_checking_assert (len >= 0);
  Dynarr_increase_length (dy, old_len + len);

  if (pos != old_len)
    {
      memmove ((Rawbyte *) dy->base + (pos + len)*Dynarr_elsize (dy),
	       (Rawbyte *) dy->base + pos*Dynarr_elsize (dy),
	       (old_len - pos)*Dynarr_elsize (dy));
    }
  /* Some functions call us with a value of 0 to mean "reserve space but
     don't write into it" */
  if (base)
    memcpy ((Rawbyte *) dy->base + pos*Dynarr_elsize (dy), base,
	    len*Dynarr_elsize (dy));
}

void
Dynarr_delete_many (void *d, Elemcount pos, Elemcount len)
{
  Dynarr *dy = Dynarr_verify_mod (d);

  dynarr_checking_assert (pos >= 0 && len >= 0 &&
			  pos + len <= Dynarr_length (dy));

  memmove ((Rawbyte *) dy->base + pos*Dynarr_elsize (dy),
	   (Rawbyte *) dy->base + (pos + len)*Dynarr_elsize (dy),
	   (Dynarr_length (dy) - pos - len)*Dynarr_elsize (dy));

  Dynarr_set_length_1 (dy, Dynarr_length (dy) - len);
}

void
Dynarr_free (void *d)
{
  Dynarr *dy = (Dynarr *) d;

#ifdef NEW_GC
  if (dy->base && !DUMPEDP (dy->base))
    {
      if (!dy->lisp_imp)
	{
	  xfree (dy->base);
	  dy->base = 0;
	}
    }
  if (!DUMPEDP (dy))
    {
      if (!dy->lisp_imp)
	xfree (dy);
    }
#else /* not NEW_GC */
  if (dy->base && !DUMPEDP (dy->base))
    {
      xfree (dy->base);
      dy->base = 0;
    }
  if(!DUMPEDP (dy))
    xfree (dy);
#endif /* not NEW_GC */
}

#ifdef MEMORY_USAGE_STATS

/* Return memory usage for dynarr D.  The returned value is the total
   amount of bytes actually being used for the dynarr, including all
   overhead.  The extra amount of space in the dynarr that is
   allocated beyond what was requested is returned in DYNARR_OVERHEAD
   in STATS.  The extra amount of space that malloc() allocates beyond
   what was requested of it is returned in MALLOC_OVERHEAD in STATS.
   See the comment above the definition of this structure. */

Bytecount
Dynarr_memory_usage (void *d, struct usage_stats *stats)
{
  Bytecount total = 0;
  Dynarr *dy = (Dynarr *) d;

  /* We have to be a bit tricky here because not all of the
     memory that malloc() will claim as "requested" was actually
     requested. */

  if (dy->base)
    {
      Bytecount malloc_used =
	malloced_storage_size (dy->base, Dynarr_elsize (dy) * Dynarr_max (dy),
			       0);
      /* #### This may or may not be correct.  Some dynarrs would
	 prefer that we use dy->len instead of dy->largest here. */
      Bytecount was_requested = Dynarr_elsize (dy) * Dynarr_largest (dy);
      Bytecount dynarr_overhead =
	Dynarr_elsize (dy) * (Dynarr_max (dy) - Dynarr_largest (dy));

      total += malloc_used;
      stats->was_requested += was_requested;
      stats->dynarr_overhead += dynarr_overhead;
      /* And the remainder must be malloc overhead. */
      stats->malloc_overhead +=
	malloc_used - was_requested - dynarr_overhead;
    }

  total += malloced_storage_size (d, sizeof (*dy), stats);

  return total;
}

#endif /* MEMORY_USAGE_STATS */


/*****************************************************************************/
/*                           stack-like allocation                           */
/*****************************************************************************/

/* Version of malloc() that will be extremely efficient when allocation
   nearly always occurs in LIFO (stack) order.

   #### Perhaps shouldn't be in this file, but where else? */

typedef struct
{
  Dynarr_declare (char_dynarr *);
} char_dynarr_dynarr;

char_dynarr_dynarr *stack_like_free_list;
char_dynarr_dynarr *stack_like_in_use_list;

void *
stack_like_malloc (Bytecount size)
{
  char_dynarr *this_one;
  if (!stack_like_free_list)
    {
      stack_like_free_list = Dynarr_new2 (char_dynarr_dynarr,
					  char_dynarr *);
      stack_like_in_use_list = Dynarr_new2 (char_dynarr_dynarr,
					    char_dynarr *);
    }

  if (Dynarr_length (stack_like_free_list) > 0)
    this_one = Dynarr_pop (stack_like_free_list);
  else
    this_one = Dynarr_new (char);
  Dynarr_add (stack_like_in_use_list, this_one);
  Dynarr_reset (this_one);
  Dynarr_add_many (this_one, 0, size);
  return Dynarr_begin (this_one);
}

void
stack_like_free (void *val)
{
  Elemcount len = Dynarr_length (stack_like_in_use_list);
  assert (len > 0);
  /* The vast majority of times, we will be called in a last-in first-out
     order, and the item at the end of the list will be the one we're
     looking for, so just check for this first and avoid any function
     calls. */
  if (Dynarr_begin (Dynarr_at (stack_like_in_use_list, len - 1)) == val)
    {
      char_dynarr *this_one = Dynarr_pop (stack_like_in_use_list);
      Dynarr_add (stack_like_free_list, this_one);
    }
  else
    {
      /* Find the item and delete it. */
      int i;
      assert (len >= 2);
      for (i = len - 2; i >= 0; i--)
	if (Dynarr_begin (Dynarr_at (stack_like_in_use_list, i)) ==
	    val)
	  {
	    char_dynarr *this_one = Dynarr_at (stack_like_in_use_list, i);
	    Dynarr_add (stack_like_free_list, this_one);
	    Dynarr_delete (stack_like_in_use_list, i);
	    return;
	  }

      ABORT ();
    }
}


/*****************************************************************************/
/*                           Generalized gap array                           */
/*****************************************************************************/

/* A "gap array" is an array that has a "gap" somewhere in the middle of it,
   so that insertions and deletions near the gap -- or in general, highly
   localized insertions and deletions -- are very fast.  Inserting or
   deleting works by first moving the gap to the insertion or deletion
   position and then shortening or lengthening the gap as necessary.  The
   idea comes from the gap used in storing text in a buffer.

   The gap array interface differs in a number of ways from dynarrs (####
   and should be changed so that it works the same as dynarrs):

   (1) There aren't separate type-specific gap array types.  As a result,
       operations like gap_array_at() require that the type be specified as
       one of the arguments.  It is often more convenient to use a macro
       wrapper around this operation.

   (2) The gap array type is itself a stretchy array rather than using a
       separate block of memory to store the array.  This means that certain
       operations (especially insertions) may relocate the the gap array,
       and as a result return a pointer to the (possibly) moved gap array,
       which must be stored back into the location where the gap array
       pointer resides.  This also means that the caller must worry about
       cloning the gap array in the case where it has been dumped, or you
       will get an ABORT() inside of xrealloc().

   (3) Fewer operations are available than for dynarrs, and may have
       different names and/or different calling conventions.

   (4) The mechanism for creating "Lisp-object gap arrays" isn't completely
       developed.  Currently it's only possible to create a gap-array Lisp
       object that wraps Lisp_Object pointers (not Lisp object structures
       directly), and only under NEW_GC.

   (5) Gap arrays have a concept of a "gap array marker" that properly
       tracks insertions and deletions; no such thing exists in dynarrs.
       It exists in gap arrays because it's necessary for their use in
       implementing extent lists.
 */

extern const struct sized_memory_description gap_array_marker_description;

static const struct memory_description gap_array_marker_description_1[] = { 
#ifdef NEW_GC
  { XD_LISP_OBJECT, offsetof (Gap_Array_Marker, next) },
#else /* not NEW_GC */
  { XD_BLOCK_PTR, offsetof (Gap_Array_Marker, next), 1,
    { &gap_array_marker_description } },
#endif /* not NEW_GC */
  { XD_END }
};

#ifdef NEW_GC
DEFINE_NODUMP_INTERNAL_LISP_OBJECT ("gap-array-marker", gap_array_marker,
				    0, gap_array_marker_description_1,
				    struct gap_array_marker);
#else /* not NEW_GC */
const struct sized_memory_description gap_array_marker_description = {
  sizeof (Gap_Array_Marker),
  gap_array_marker_description_1
};
#endif /* not NEW_GC */

static const struct memory_description lispobj_gap_array_description_1[] = {
  XD_GAP_ARRAY_DESC (&lisp_object_description),
  { XD_END }
};

#ifdef NEW_GC

static Bytecount
size_gap_array (Lisp_Object obj)
{
  Gap_Array *ga = XGAP_ARRAY (obj);
  return gap_array_byte_size (ga);
}

DEFINE_DUMPABLE_SIZABLE_INTERNAL_LISP_OBJECT ("gap-array", gap_array,
					      0,
					      lispobj_gap_array_description_1,
					      size_gap_array,
					      struct gap_array);
#else /* not NEW_GC */
const struct sized_memory_description lispobj_gap_array_description = {
  0, lispobj_gap_array_description_1
};
#endif /* (not) NEW_GC */

#ifndef NEW_GC
static Gap_Array_Marker *gap_array_marker_freelist;
#endif /* not NEW_GC */

/* This generalizes the "array with a gap" model used to store buffer
   characters.  This is based on the stuff in insdel.c and should
   probably be merged with it.  This is not extent-specific and should
   perhaps be moved into a separate file. */

/* ------------------------------- */
/*        internal functions       */
/* ------------------------------- */

/* Adjust the gap array markers in the range (FROM, TO].  Parallel to
   adjust_markers() in insdel.c. */

static void
gap_array_adjust_markers (Gap_Array *ga, Memxpos from,
			  Memxpos to, Elemcount amount)
{
  Gap_Array_Marker *m;

  for (m = ga->markers; m; m = m->next)
    m->pos = do_marker_adjustment (m->pos, from, to, amount);
}

static void
gap_array_recompute_derived_values (Gap_Array *ga)
{
  ga->offset_past_gap = ga->elsize * (ga->gap + ga->gapsize);
  ga->els_past_gap = ga->numels - ga->gap;
}

/* Move the gap to array position POS.  Parallel to move_gap() in
   insdel.c but somewhat simplified. */

static void
gap_array_move_gap (Gap_Array *ga, Elemcount pos)
{
  Elemcount gap = ga->gap;
  Elemcount gapsize = ga->gapsize;

  if (pos < gap)
    {
      memmove (GAP_ARRAY_MEMEL_ADDR (ga, pos + gapsize),
	       GAP_ARRAY_MEMEL_ADDR (ga, pos),
	       (gap - pos)*ga->elsize);
      gap_array_adjust_markers (ga, (Memxpos) pos, (Memxpos) gap,
				gapsize);
    }
  else if (pos > gap)
    {
      memmove (GAP_ARRAY_MEMEL_ADDR (ga, gap),
	       GAP_ARRAY_MEMEL_ADDR (ga, gap + gapsize),
	       (pos - gap)*ga->elsize);
      gap_array_adjust_markers (ga, (Memxpos) (gap + gapsize),
				(Memxpos) (pos + gapsize), - gapsize);
    }
  ga->gap = pos;

  gap_array_recompute_derived_values (ga);
}

/* Make the gap INCREMENT characters longer.  Parallel to make_gap() in
   insdel.c.  The gap array may be moved, so assign the return value back
   to the array pointer. */

static Gap_Array *
gap_array_make_gap (Gap_Array *ga, Elemcount increment)
{
  Elemcount real_gap_loc;
  Elemcount old_gap_size;

  /* If we have to get more space, get enough to last a while.  We use
     a geometric progression that saves on realloc space. */
  increment += 100 + ga->numels / 8;

#ifdef NEW_GC
  if (ga->is_lisp)
    ga = (Gap_Array *) mc_realloc (ga,
				   offsetof (Gap_Array, array) +
				   (ga->numels + ga->gapsize + increment) *
				   ga->elsize);
  else
#endif /* not NEW_GC */
    ga = (Gap_Array *) xrealloc (ga,
				 offsetof (Gap_Array, array) +
				 (ga->numels + ga->gapsize + increment) *
				 ga->elsize);
  if (ga == 0)
    memory_full ();

  real_gap_loc = ga->gap;
  old_gap_size = ga->gapsize;

  /* Call the newly allocated space a gap at the end of the whole space.  */
  ga->gap = ga->numels + ga->gapsize;
  ga->gapsize = increment;

  /* Move the new gap down to be consecutive with the end of the old one.
     This adjusts the markers properly too.  */
  gap_array_move_gap (ga, real_gap_loc + old_gap_size);

  /* Now combine the two into one large gap.  */
  ga->gapsize += old_gap_size;
  ga->gap = real_gap_loc;

  gap_array_recompute_derived_values (ga);

  return ga;
}

/* ------------------------------- */
/*        external functions       */
/* ------------------------------- */

Bytecount
gap_array_byte_size (Gap_Array *ga)
{
  return offsetof (Gap_Array, array) + (ga->numels + ga->gapsize) * ga->elsize;
}

/* Insert NUMELS elements (pointed to by ELPTR) into the specified
   gap array at POS.  The gap array may be moved, so assign the
   return value back to the array pointer. */

Gap_Array *
gap_array_insert_els (Gap_Array *ga, Elemcount pos, void *elptr,
		      Elemcount numels)
{
  assert (pos >= 0 && pos <= ga->numels);
  if (ga->gapsize < numels)
    ga = gap_array_make_gap (ga, numels - ga->gapsize);
  if (pos != ga->gap)
    gap_array_move_gap (ga, pos);

  memcpy (GAP_ARRAY_MEMEL_ADDR (ga, ga->gap), (char *) elptr,
	  numels*ga->elsize);
  ga->gapsize -= numels;
  ga->gap += numels;
  ga->numels += numels;
  gap_array_recompute_derived_values (ga);
  /* This is the equivalent of insert-before-markers.

     #### Should only happen if marker is "moves forward at insert" type.
     */

  gap_array_adjust_markers (ga, pos - 1, pos, numels);
  return ga;
}

/* Delete NUMELS elements from the specified gap array, starting at FROM. */

void
gap_array_delete_els (Gap_Array *ga, Elemcount from, Elemcount numdel)
{
  Elemcount to = from + numdel;
  Elemcount gapsize = ga->gapsize;

  assert (from >= 0);
  assert (numdel >= 0);
  assert (to <= ga->numels);

  /* Make sure the gap is somewhere in or next to what we are deleting.  */
  if (to < ga->gap)
    gap_array_move_gap (ga, to);
  if (from > ga->gap)
    gap_array_move_gap (ga, from);

  /* Relocate all markers pointing into the new, larger gap
     to point at the end of the text before the gap.  */
  gap_array_adjust_markers (ga, to + gapsize, to + gapsize,
			    - numdel - gapsize);

  ga->gapsize += numdel;
  ga->numels -= numdel;
  ga->gap = from;
  gap_array_recompute_derived_values (ga);
}

Gap_Array_Marker *
gap_array_make_marker (Gap_Array *ga, Elemcount pos)
{
  Gap_Array_Marker *m;

  assert (pos >= 0 && pos <= ga->numels);
#ifdef NEW_GC
    m = XGAP_ARRAY_MARKER (ALLOC_NORMAL_LISP_OBJECT (gap_array_marker));
#else /* not NEW_GC */
  if (gap_array_marker_freelist)
    {
      m = gap_array_marker_freelist;
      gap_array_marker_freelist = gap_array_marker_freelist->next;
    }
  else
    m = xnew (Gap_Array_Marker);
#endif /* not NEW_GC */

  m->pos = GAP_ARRAY_ARRAY_TO_MEMORY_POS (ga, pos);
  m->next = ga->markers;
  ga->markers = m;
  return m;
}

void
gap_array_delete_marker (Gap_Array *ga, Gap_Array_Marker *m)
{
  Gap_Array_Marker *p, *prev;

  for (prev = 0, p = ga->markers; p && p != m; prev = p, p = p->next)
    ;
  assert (p);
  if (prev)
    prev->next = p->next;
  else
    ga->markers = p->next;
#ifndef NEW_GC
  m->next = gap_array_marker_freelist;
  m->pos = 0xDEADBEEF; /* -559038737 base 10 */
  gap_array_marker_freelist = m;
#endif /* not NEW_GC */
}

#ifndef NEW_GC
void
gap_array_delete_all_markers (Gap_Array *ga)
{
  Gap_Array_Marker *p, *next;

  for (p = ga->markers; p; p = next)
    {
      next = p->next;
      p->next = gap_array_marker_freelist;
      p->pos = 0xDEADBEEF; /* -559038737 as an int */
      gap_array_marker_freelist = p;
    }
}
#endif /* not NEW_GC */

void
gap_array_move_marker (Gap_Array *ga, Gap_Array_Marker *m, Elemcount pos)
{
  assert (pos >= 0 && pos <= ga->numels);
  m->pos = GAP_ARRAY_ARRAY_TO_MEMORY_POS (ga, pos);
}

Gap_Array *
make_gap_array (Elemcount elsize, int USED_IF_NEW_GC (do_lisp))
{
  Gap_Array *ga;
#ifdef NEW_GC
  /* #### I don't quite understand why it's necessary to make all these
     internal objects into Lisp objects under NEW_GC.  It's a pain in the
     ass to code around this.  I'm proceeding on the assumption that it's
     not really necessary to do it after all, and so we only make a Lisp-
     object gap array when the object being held is a Lisp_Object, i.e. a
     pointer to a Lisp object.  In the case where instead we hold a `struct
     range_table_entry', just blow it off.  Otherwise we either need to do
     a bunch of painful and/or boring rewriting. --ben */
  if (do_lisp)
    {
      ga = XGAP_ARRAY (ALLOC_SIZED_LISP_OBJECT (sizeof (Gap_Array),
						gap_array));
      ga->is_lisp = 1;
    }
  else
#endif /* not NEW_GC */
    ga = xnew_and_zero (Gap_Array);
  ga->elsize = elsize;
  return ga;
}

Gap_Array *
gap_array_clone (Gap_Array *ga)
{
  Bytecount size = gap_array_byte_size (ga);
  Gap_Array *ga2;
  Gap_Array_Marker *m;

#ifdef NEW_GC
  if (ga->is_lisp)
    {
      ga2 = XGAP_ARRAY (ALLOC_SIZED_LISP_OBJECT (size, gap_array));
      copy_lisp_object (wrap_gap_array (ga2), wrap_gap_array (ga));
    }
  else
#endif
    {
      ga2 = (Gap_Array *) xmalloc (size);
      memcpy (ga2, ga, size);
    }
  ga2->markers = NULL;
  for (m = ga->markers; m; m = m->next)
    gap_array_make_marker (ga2, m->pos);
  return ga2;
}

#ifndef NEW_GC
void
free_gap_array (Gap_Array *ga)
{
  gap_array_delete_all_markers (ga);
  xfree (ga);
}
#endif /* not NEW_GC */

#ifdef MEMORY_USAGE_STATS

/* Return memory usage for gap array GA.  The returned value is the total
   amount of bytes actually being used for the gap array, including all
   overhead.  The extra amount of space in the gap array that is used
   for the gap is counted in GAP_OVERHEAD, not in WAS_REQUESTED.
   If NEW_GC, space for gap-array markers is returned through MARKER_ANCILLARY;
   otherwise it's added into the gap array usage. */

Bytecount
gap_array_memory_usage (Gap_Array *ga, struct usage_stats *stats,
			Bytecount *marker_ancillary)
{
  Bytecount total = 0;

  /* We have to be a bit tricky here because not all of the
     memory that malloc() will claim as "requested" was actually
     requested -- some of it makes up the gap. */

  Bytecount size = gap_array_byte_size (ga);
  Bytecount gap_size = ga->gapsize * ga->elsize;
  Bytecount malloc_used = malloced_storage_size (ga, size, 0);
  total += malloc_used;
  stats->was_requested += size - gap_size;
  stats->gap_overhead += gap_size;
  stats->malloc_overhead += malloc_used - size;

#ifdef NEW_GC
  {
    Bytecount marker_usage = 0;
    Gap_Array_Marker *p;

    for (p = ga->markers; p; p = p->next)
      marker_usage += lisp_object_memory_usage (wrap_gap_array_marker (p));
    if (marker_ancillary)
      *marker_ancillary = marker_usage;
  }
#else
  {
    Gap_Array_Marker *p;

    for (p = ga->markers; p; p = p->next)
      total += malloced_storage_size (p, sizeof (p), stats);
    if (marker_ancillary)
      *marker_ancillary = 0;
  }
#endif /* (not) NEW_GC */
  
  return total;
}

#endif /* MEMORY_USAGE_STATS */


/*****************************************************************************/
/*                              Initialization                               */
/*****************************************************************************/

void
syms_of_array (void)
{
#ifdef NEW_GC
  INIT_LISP_OBJECT (gap_array_marker);
  INIT_LISP_OBJECT (gap_array);
#endif /* NEW_GC */
}