summaryrefslogtreecommitdiff
path: root/external/gpl2/libmalloc
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2016-01-13 21:42:18 +0000
committerchristos <christos@NetBSD.org>2016-01-13 21:42:18 +0000
commit07c3c4887b6ca12fb2ee712a9f89fb4e7a75b4e5 (patch)
tree0a4afc40e383d939e52cada6427e512b886b8527 /external/gpl2/libmalloc
parent0f5ae28b58110241e3d973f20905da6caf41740c (diff)
import the latest standalone libmalloc.
Diffstat (limited to 'external/gpl2/libmalloc')
-rw-r--r--external/gpl2/libmalloc/dist/COPYING.LIB481
-rw-r--r--external/gpl2/libmalloc/dist/ChangeLog2
-rw-r--r--external/gpl2/libmalloc/dist/Makefile57
-rw-r--r--external/gpl2/libmalloc/dist/OChangeLog34
-rw-r--r--external/gpl2/libmalloc/dist/README12
-rw-r--r--external/gpl2/libmalloc/dist/calloc.c41
-rw-r--r--external/gpl2/libmalloc/dist/cfree.c45
-rw-r--r--external/gpl2/libmalloc/dist/free.c212
-rw-r--r--external/gpl2/libmalloc/dist/getpagesize.h41
-rw-r--r--external/gpl2/libmalloc/dist/gmalloc-head.c8
-rw-r--r--external/gpl2/libmalloc/dist/gmalloc.c1352
-rw-r--r--external/gpl2/libmalloc/dist/malloc-find.c61
-rw-r--r--external/gpl2/libmalloc/dist/malloc.c377
-rw-r--r--external/gpl2/libmalloc/dist/malloc.h296
-rw-r--r--external/gpl2/libmalloc/dist/mcheck.c217
-rw-r--r--external/gpl2/libmalloc/dist/mem-limits.h186
-rw-r--r--external/gpl2/libmalloc/dist/memalign.c68
-rw-r--r--external/gpl2/libmalloc/dist/morecore.c54
-rw-r--r--external/gpl2/libmalloc/dist/mstats.c41
-rw-r--r--external/gpl2/libmalloc/dist/mtrace.awk50
-rw-r--r--external/gpl2/libmalloc/dist/mtrace.c189
-rw-r--r--external/gpl2/libmalloc/dist/ralloc.c1088
-rw-r--r--external/gpl2/libmalloc/dist/realloc.c221
-rw-r--r--external/gpl2/libmalloc/dist/valloc.c48
-rw-r--r--external/gpl2/libmalloc/dist/vm-limit.c137
25 files changed, 5318 insertions, 0 deletions
diff --git a/external/gpl2/libmalloc/dist/COPYING.LIB b/external/gpl2/libmalloc/dist/COPYING.LIB
new file mode 100644
index 00000000000..eb685a5ec98
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/COPYING.LIB
@@ -0,0 +1,481 @@
+ GNU LIBRARY GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1991 Free Software Foundation, Inc.
+ 675 Mass Ave, Cambridge, MA 02139, USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the library GPL. It is
+ numbered 2 because it goes with version 2 of the ordinary GPL.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Library General Public License, applies to some
+specially designated Free Software Foundation software, and to any
+other libraries whose authors decide to use it. You can use it for
+your libraries, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if
+you distribute copies of the library, or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link a program with the library, you must provide
+complete object files to the recipients so that they can relink them
+with the library, after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ Our method of protecting your rights has two steps: (1) copyright
+the library, and (2) offer you this license which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ Also, for each distributor's protection, we want to make certain
+that everyone understands that there is no warranty for this free
+library. If the library is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original
+version, so that any problems introduced by others will not reflect on
+the original authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that companies distributing free
+software will individually obtain patent licenses, thus in effect
+transforming the program into proprietary software. To prevent this,
+we have made it clear that any patent must be licensed for everyone's
+free use or not licensed at all.
+
+ Most GNU software, including some libraries, is covered by the ordinary
+GNU General Public License, which was designed for utility programs. This
+license, the GNU Library General Public License, applies to certain
+designated libraries. This license is quite different from the ordinary
+one; be sure to read it in full, and don't assume that anything in it is
+the same as in the ordinary license.
+
+ The reason we have a separate public license for some libraries is that
+they blur the distinction we usually make between modifying or adding to a
+program and simply using it. Linking a program with a library, without
+changing the library, is in some sense simply using the library, and is
+analogous to running a utility program or application program. However, in
+a textual and legal sense, the linked executable is a combined work, a
+derivative of the original library, and the ordinary General Public License
+treats it as such.
+
+ Because of this blurred distinction, using the ordinary General
+Public License for libraries did not effectively promote software
+sharing, because most developers did not use the libraries. We
+concluded that weaker conditions might promote sharing better.
+
+ However, unrestricted linking of non-free programs would deprive the
+users of those programs of all benefit from the free status of the
+libraries themselves. This Library General Public License is intended to
+permit developers of non-free programs to use free libraries, while
+preserving your freedom as a user of such programs to change the free
+libraries that are incorporated in them. (We have not seen how to achieve
+this as regards changes in header files, but we have achieved it as regards
+changes in the actual functions of the Library.) The hope is that this
+will lead to faster development of free libraries.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, while the latter only
+works together with the library.
+
+ Note that it is possible for a library to be covered by the ordinary
+General Public License rather than by this special one.
+
+ GNU LIBRARY GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library which
+contains a notice placed by the copyright holder or other authorized
+party saying it may be distributed under the terms of this Library
+General Public License (also called "this License"). Each licensee is
+addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also compile or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ c) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ d) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the source code distributed need not include anything that is normally
+distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Library General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ Appendix: How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with this library; if not, write to the Free
+ Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
diff --git a/external/gpl2/libmalloc/dist/ChangeLog b/external/gpl2/libmalloc/dist/ChangeLog
new file mode 100644
index 00000000000..74a985aa42d
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/ChangeLog
@@ -0,0 +1,2 @@
+
+Find older changes in OChangeLog.
diff --git a/external/gpl2/libmalloc/dist/Makefile b/external/gpl2/libmalloc/dist/Makefile
new file mode 100644
index 00000000000..36f66aa7d6b
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/Makefile
@@ -0,0 +1,57 @@
+# Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Library General Public License
+# as published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+
+# The GNU C Library 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
+# Library General Public License for more details.
+
+# You should have received a copy of the GNU Library General Public
+# License along with the GNU C Library; see the file COPYING.LIB. If
+# not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+# Cambridge, MA 02139, USA.
+
+# Makefile for standalone distribution of malloc.
+
+# Use this on System V.
+#CPPFLAGS = -DUSG
+
+.PHONY: all
+all: libmalloc.a gmalloc.o
+
+gmalloc = valloc.c malloc.c free.c cfree.c realloc.c calloc.c morecore.c memalign.c
+sources = valloc.c malloc.c free.c cfree.c realloc.c calloc.c morecore.c memalign.c mcheck.c mtrace.c mstats.c vm-limit.c ralloc.c malloc-find.c
+objects = valloc.o malloc.o free.o cfree.o realloc.o calloc.o morecore.o memalign.o mcheck.o mtrace.o mstats.o vm-limit.o ralloc.o malloc-find.o
+headers = malloc.h
+
+libmalloc.a: $(objects)
+ ar crv $@ $(objects)
+ ranlib $@
+
+$(objects): $(headers)
+
+gmalloc.c: gmalloc-head.c $(headers) $(gmalloc) Makefile
+ cat gmalloc-head.c $(headers) $(gmalloc) > $@-tmp
+ mv -f $@-tmp $@
+# Make it unwritable to avoid accidentally changing the file,
+# since it is generated and any changes would be lost.
+ chmod a-w $@
+
+.c.o:
+ $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c $< $(OUTPUT_OPTION)
+
+.PHONY: clean realclean malloc-clean malloc-realclean
+clean malloc-clean:
+ -rm -f libmalloc.a *.o core
+realclean malloc-realclean: clean
+ -rm -f TAGS tags *~
+
+# For inside the C library.
+malloc.tar malloc.tar.Z: FORCE
+ $(MAKE) -C .. $@
+FORCE:
diff --git a/external/gpl2/libmalloc/dist/OChangeLog b/external/gpl2/libmalloc/dist/OChangeLog
new file mode 100644
index 00000000000..452aa53b323
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/OChangeLog
@@ -0,0 +1,34 @@
+ **** All newer entries are in the C library ChangeLog file. ****
+
+Thu Jul 11 18:15:04 1991 Roland McGrath (roland@churchy.gnu.ai.mit.edu)
+
+ * Merged with C library version, which now has its own subdir.
+ * malloc.h, *.c: Use ansideclisms and #ifdefs for portability both
+ in and out of the C library.
+ * Makefile: New makefile for malloc subdir in libc.
+ Has targets to create malloc.tar{,.Z} by ansidecl processing on srcs.
+ * malloc/Makefile: New file; Makefile for standalone distribution.
+ * malloc/README: New file; info for same.
+
+Fri Apr 6 00:18:36 1990 Jim Kingdon (kingdon at pogo.ai.mit.edu)
+
+ * Makefile: Add comments.
+
+Thu Apr 5 23:08:14 1990 Mike Haertel (mike at albert.ai.mit.edu)
+
+ * mcheck.c (mcheck, checkhdr): Support user-supplied abort()
+ function.
+ * malloc.h: Declare __free().
+ * Makefile: New target libmalloc.a.
+
+Thu Apr 5 21:56:03 1990 Jim Kingdon (kingdon at pogo.ai.mit.edu)
+
+ * free.c (free): Split into free and __free.
+ * malloc.c (morecore): Call __free on oldinfo.
+
+Local Variables:
+mode: indented-text
+left-margin: 8
+fill-column: 76
+version-control: never
+End:
diff --git a/external/gpl2/libmalloc/dist/README b/external/gpl2/libmalloc/dist/README
new file mode 100644
index 00000000000..b5655c92820
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/README
@@ -0,0 +1,12 @@
+This is the standalone distribution of GNU malloc.
+GNU malloc is part of the GNU C Library, but is also distributed separately.
+
+If you find bugs in GNU malloc, send reports to bug-glibc@prep.ai.mit.edu.
+
+GNU malloc is free software. See the file COPYING.LIB for copying conditions.
+
+The makefile builds libmalloc.a and gmalloc.o. If you are using GNU malloc
+to replace your system's existing malloc package, it is important to make
+sure you get all GNU functions, not some of the GNU functions and some from
+the system library. gmalloc.o has all the functions in one file, so using
+that will make sure you don't accidentally mix the two malloc packages.
diff --git a/external/gpl2/libmalloc/dist/calloc.c b/external/gpl2/libmalloc/dist/calloc.c
new file mode 100644
index 00000000000..c02a7eeeb42
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/calloc.c
@@ -0,0 +1,41 @@
+/* $NetBSD: calloc.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* Allocate an array of NMEMB elements each SIZE bytes long.
+ The entire array is initialized to zeros. */
+__ptr_t
+calloc (nmemb, size)
+ register __malloc_size_t nmemb;
+ register __malloc_size_t size;
+{
+ register __ptr_t result = malloc (nmemb * size);
+
+ if (result != NULL)
+ (void) memset (result, 0, nmemb * size);
+
+ return result;
+}
diff --git a/external/gpl2/libmalloc/dist/cfree.c b/external/gpl2/libmalloc/dist/cfree.c
new file mode 100644
index 00000000000..17a4f2fe2c4
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/cfree.c
@@ -0,0 +1,45 @@
+/* $NetBSD: cfree.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#ifdef _LIBC
+
+#include <ansidecl.h>
+#include <gnu-stabs.h>
+
+#undef cfree
+
+function_alias(cfree, free, void, (ptr),
+ DEFUN(cfree, (ptr), PTR ptr))
+
+#else
+
+void
+cfree (ptr)
+ __ptr_t ptr;
+{
+ free (ptr);
+}
+
+#endif
diff --git a/external/gpl2/libmalloc/dist/free.c b/external/gpl2/libmalloc/dist/free.c
new file mode 100644
index 00000000000..7f5525fdbda
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/free.c
@@ -0,0 +1,212 @@
+/* $NetBSD: free.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Free a block of memory allocated by `malloc'.
+ Copyright 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* Debugging hook for free. */
+void (*__free_hook) __P ((__ptr_t __ptr));
+
+/* List of blocks allocated by memalign. */
+struct alignlist *_aligned_blocks = NULL;
+
+/* Return memory to the heap.
+ Like `free' but don't call a __free_hook if there is one. */
+void
+_free_internal (ptr)
+ __ptr_t ptr;
+{
+ int type;
+ __malloc_size_t block, blocks;
+ register __malloc_size_t i;
+ struct list *prev, *next;
+
+ block = BLOCK (ptr);
+
+ type = _heapinfo[block].busy.type;
+ switch (type)
+ {
+ case 0:
+ /* Get as many statistics as early as we can. */
+ --_chunks_used;
+ _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
+ _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
+
+ /* Find the free cluster previous to this one in the free list.
+ Start searching at the last block referenced; this may benefit
+ programs with locality of allocation. */
+ i = _heapindex;
+ if (i > block)
+ while (i > block)
+ i = _heapinfo[i].free.prev;
+ else
+ {
+ do
+ i = _heapinfo[i].free.next;
+ while (i > 0 && i < block);
+ i = _heapinfo[i].free.prev;
+ }
+
+ /* Determine how to link this block into the free list. */
+ if (block == i + _heapinfo[i].free.size)
+ {
+ /* Coalesce this block with its predecessor. */
+ _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
+ block = i;
+ }
+ else
+ {
+ /* Really link this block back into the free list. */
+ _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
+ _heapinfo[block].free.next = _heapinfo[i].free.next;
+ _heapinfo[block].free.prev = i;
+ _heapinfo[i].free.next = block;
+ _heapinfo[_heapinfo[block].free.next].free.prev = block;
+ ++_chunks_free;
+ }
+
+ /* Now that the block is linked in, see if we can coalesce it
+ with its successor (by deleting its successor from the list
+ and adding in its size). */
+ if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
+ {
+ _heapinfo[block].free.size
+ += _heapinfo[_heapinfo[block].free.next].free.size;
+ _heapinfo[block].free.next
+ = _heapinfo[_heapinfo[block].free.next].free.next;
+ _heapinfo[_heapinfo[block].free.next].free.prev = block;
+ --_chunks_free;
+ }
+
+ /* Now see if we can return stuff to the system. */
+ blocks = _heapinfo[block].free.size;
+ if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
+ && (*__morecore) (0) == ADDRESS (block + blocks))
+ {
+ register __malloc_size_t bytes = blocks * BLOCKSIZE;
+ _heaplimit -= blocks;
+ (*__morecore) (-bytes);
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapinfo[block].free.next;
+ _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapinfo[block].free.prev;
+ block = _heapinfo[block].free.prev;
+ --_chunks_free;
+ _bytes_free -= bytes;
+ }
+
+ /* Set the next search to begin at this block. */
+ _heapindex = block;
+ break;
+
+ default:
+ /* Do some of the statistics. */
+ --_chunks_used;
+ _bytes_used -= 1 << type;
+ ++_chunks_free;
+ _bytes_free += 1 << type;
+
+ /* Get the address of the first free fragment in this block. */
+ prev = (struct list *) ((char *) ADDRESS (block) +
+ (_heapinfo[block].busy.info.frag.first << type));
+
+ if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
+ {
+ /* If all fragments of this block are free, remove them
+ from the fragment list and free the whole block. */
+ next = prev;
+ for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
+ next = next->next;
+ prev->prev->next = next;
+ if (next != NULL)
+ next->prev = prev->prev;
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = 1;
+
+ /* Keep the statistics accurate. */
+ ++_chunks_used;
+ _bytes_used += BLOCKSIZE;
+ _chunks_free -= BLOCKSIZE >> type;
+ _bytes_free -= BLOCKSIZE;
+
+ free (ADDRESS (block));
+ }
+ else if (_heapinfo[block].busy.info.frag.nfree != 0)
+ {
+ /* If some fragments of this block are free, link this
+ fragment into the fragment list after the first free
+ fragment of this block. */
+ next = (struct list *) ptr;
+ next->next = prev->next;
+ next->prev = prev;
+ prev->next = next;
+ if (next->next != NULL)
+ next->next->prev = next;
+ ++_heapinfo[block].busy.info.frag.nfree;
+ }
+ else
+ {
+ /* No fragments of this block are free, so link this
+ fragment into the fragment list and announce that
+ it is the first free fragment of this block. */
+ prev = (struct list *) ptr;
+ _heapinfo[block].busy.info.frag.nfree = 1;
+ _heapinfo[block].busy.info.frag.first = (unsigned long int)
+ ((unsigned long int) ((char *) ptr - (char *) NULL)
+ % BLOCKSIZE >> type);
+ prev->next = _fraghead[type].next;
+ prev->prev = &_fraghead[type];
+ prev->prev->next = prev;
+ if (prev->next != NULL)
+ prev->next->prev = prev;
+ }
+ break;
+ }
+}
+
+/* Return memory to the heap. */
+void
+free (ptr)
+ __ptr_t ptr;
+{
+ register struct alignlist *l;
+
+ if (ptr == NULL)
+ return;
+
+ for (l = _aligned_blocks; l != NULL; l = l->next)
+ if (l->aligned == ptr)
+ {
+ l->aligned = NULL; /* Mark the slot in the list as free. */
+ ptr = l->exact;
+ break;
+ }
+
+ if (__free_hook != NULL)
+ (*__free_hook) (ptr);
+ else
+ _free_internal (ptr);
+}
diff --git a/external/gpl2/libmalloc/dist/getpagesize.h b/external/gpl2/libmalloc/dist/getpagesize.h
new file mode 100644
index 00000000000..4970ab7c1d2
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/getpagesize.h
@@ -0,0 +1,41 @@
+/* $NetBSD: getpagesize.h,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Emulate getpagesize on systems that lack it. */
+
+#ifndef HAVE_GETPAGESIZE
+
+#ifdef VMS
+#define getpagesize() 512
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef _SC_PAGESIZE
+#define getpagesize() sysconf(_SC_PAGESIZE)
+#else
+
+#include <sys/param.h>
+
+#ifdef EXEC_PAGESIZE
+#define getpagesize() EXEC_PAGESIZE
+#else
+#ifdef NBPG
+#define getpagesize() NBPG * CLSIZE
+#ifndef CLSIZE
+#define CLSIZE 1
+#endif /* no CLSIZE */
+#else /* no NBPG */
+#ifdef NBPC
+#define getpagesize() NBPC
+#else /* no NBPC */
+#ifdef PAGESIZE
+#define getpagesize() PAGESIZE
+#endif
+#endif /* NBPC */
+#endif /* no NBPG */
+#endif /* no EXEC_PAGESIZE */
+#endif /* no _SC_PAGESIZE */
+
+#endif /* not HAVE_GETPAGESIZE */
diff --git a/external/gpl2/libmalloc/dist/gmalloc-head.c b/external/gpl2/libmalloc/dist/gmalloc-head.c
new file mode 100644
index 00000000000..0b955efab64
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/gmalloc-head.c
@@ -0,0 +1,8 @@
+/* $NetBSD: gmalloc-head.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
+
+#define _MALLOC_INTERNAL
+
+/* The malloc headers and source files from the C library follow here. */
+
diff --git a/external/gpl2/libmalloc/dist/gmalloc.c b/external/gpl2/libmalloc/dist/gmalloc.c
new file mode 100644
index 00000000000..a2137063c0a
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/gmalloc.c
@@ -0,0 +1,1352 @@
+/* $NetBSD: gmalloc.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
+
+#define _MALLOC_INTERNAL
+
+/* The malloc headers and source files from the C library follow here. */
+
+/* Declarations for `malloc' and friends.
+ Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_H
+
+#define _MALLOC_H 1
+
+#ifdef _MALLOC_INTERNAL
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
+#include <string.h>
+#else
+#ifndef memset
+#define memset(s, zero, n) bzero ((s), (n))
+#endif
+#ifndef memcpy
+#define memcpy(d, s, n) bcopy ((s), (d), (n))
+#endif
+#endif
+
+#if defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
+#include <limits.h>
+#else
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#endif /* _MALLOC_INTERNAL. */
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
+#undef __P
+#define __P(args) args
+#undef __ptr_t
+#define __ptr_t void *
+#else /* Not C++ or ANSI C. */
+#undef __P
+#define __P(args) ()
+#undef const
+#define const
+#undef __ptr_t
+#define __ptr_t char *
+#endif /* C++ or ANSI C. */
+
+#if defined (__STDC__) && __STDC__
+#include <stddef.h>
+#define __malloc_size_t size_t
+#define __malloc_ptrdiff_t ptrdiff_t
+#else
+#define __malloc_size_t unsigned int
+#define __malloc_ptrdiff_t int
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+
+/* Allocate SIZE bytes of memory. */
+extern __ptr_t malloc __P ((__malloc_size_t __size));
+/* Re-allocate the previously allocated block
+ in __ptr_t, making the new block SIZE bytes long. */
+extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
+/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
+extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
+/* Free a block allocated by `malloc', `realloc' or `calloc'. */
+extern void free __P ((__ptr_t __ptr));
+
+/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
+extern __ptr_t memalign __P ((__malloc_size_t __alignment,
+ __malloc_size_t __size));
+
+/* Allocate SIZE bytes on a page boundary. */
+extern __ptr_t valloc __P ((__malloc_size_t __size));
+
+
+#ifdef _MALLOC_INTERNAL
+
+/* The allocator divides the heap into blocks of fixed size; large
+ requests receive one or more whole blocks, and small requests
+ receive a fragment of a block. Fragment sizes are powers of two,
+ and all fragments of a block are the same size. When all the
+ fragments in a block have been freed, the block itself is freed. */
+#define INT_BIT (CHAR_BIT * sizeof(int))
+#define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
+#define BLOCKSIZE (1 << BLOCKLOG)
+#define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
+
+/* Determine the amount of memory spanned by the initial heap table
+ (not an absolute limit). */
+#define HEAP (INT_BIT > 16 ? 4194304 : 65536)
+
+/* Number of contiguous free blocks allowed to build up at the end of
+ memory before they will be returned to the system. */
+#define FINAL_FREE_BLOCKS 8
+
+/* Data structure giving per-block information. */
+typedef union
+ {
+ /* Heap information for a busy block. */
+ struct
+ {
+ /* Zero for a large (multiblock) object, or positive giving the
+ logarithm to the base two of the fragment size. */
+ int type;
+ union
+ {
+ struct
+ {
+ __malloc_size_t nfree; /* Free frags in a fragmented block. */
+ __malloc_size_t first; /* First free fragment of the block. */
+ } frag;
+ /* For a large object, in its first block, this has the number
+ of blocks in the object. In the other blocks, this has a
+ negative number which says how far back the first block is. */
+ __malloc_ptrdiff_t size;
+ } info;
+ } busy;
+ /* Heap information for a free block
+ (that may be the first of a free cluster). */
+ struct
+ {
+ __malloc_size_t size; /* Size (in blocks) of a free cluster. */
+ __malloc_size_t next; /* Index of next free cluster. */
+ __malloc_size_t prev; /* Index of previous free cluster. */
+ } free;
+ } malloc_info;
+
+/* Pointer to first block of the heap. */
+extern char *_heapbase;
+
+/* Table indexed by block number giving per-block information. */
+extern malloc_info *_heapinfo;
+
+/* Address to block number and vice versa. */
+#define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
+#define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
+
+/* Current search index for the heap table. */
+extern __malloc_size_t _heapindex;
+
+/* Limit of valid info table indices. */
+extern __malloc_size_t _heaplimit;
+
+/* Doubly linked lists of free fragments. */
+struct list
+ {
+ struct list *next;
+ struct list *prev;
+ };
+
+/* Free list headers for each fragment size. */
+extern struct list _fraghead[];
+
+/* List of blocks allocated with `memalign' (or `valloc'). */
+struct alignlist
+ {
+ struct alignlist *next;
+ __ptr_t aligned; /* The address that memaligned returned. */
+ __ptr_t exact; /* The address that malloc returned. */
+ };
+extern struct alignlist *_aligned_blocks;
+
+/* Instrumentation. */
+extern __malloc_size_t _chunks_used;
+extern __malloc_size_t _bytes_used;
+extern __malloc_size_t _chunks_free;
+extern __malloc_size_t _bytes_free;
+
+/* Internal version of `free' used in `morecore' (malloc.c). */
+extern void _free_internal __P ((__ptr_t __ptr));
+
+#endif /* _MALLOC_INTERNAL. */
+
+/* Given an address in the middle of a malloc'd object,
+ return the address of the beginning of the object. */
+extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
+
+/* Underlying allocation function; successive calls should
+ return contiguous pieces of memory. */
+extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
+
+/* Default value of `__morecore'. */
+extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
+
+/* If not NULL, this function is called after each time
+ `__morecore' is called to increase the data size. */
+extern void (*__after_morecore_hook) __P ((void));
+
+/* Nonzero if `malloc' has been called and done its initialization. */
+extern int __malloc_initialized;
+
+/* Hooks for debugging versions. */
+extern void (*__malloc_initialize_hook) __P ((void));
+extern void (*__free_hook) __P ((__ptr_t __ptr));
+extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
+extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
+ __malloc_size_t __alignment));
+
+/* Return values for `mprobe': these are the kinds of inconsistencies that
+ `mcheck' enables detection of. */
+enum mcheck_status
+ {
+ MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
+ MCHECK_OK, /* Block is fine. */
+ MCHECK_FREE, /* Block freed twice. */
+ MCHECK_HEAD, /* Memory before the block was clobbered. */
+ MCHECK_TAIL /* Memory after the block was clobbered. */
+ };
+
+/* Activate a standard collection of debugging hooks. This must be called
+ before `malloc' is ever called. ABORTFUNC is called with an error code
+ (see enum above) when an inconsistency is detected. If ABORTFUNC is
+ null, the standard function prints on stderr and then calls `abort'. */
+extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
+
+/* Check for aberrations in a particular malloc'd block. You must have
+ called `mcheck' already. These are the same checks that `mcheck' does
+ when you free or reallocate a block. */
+extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
+
+/* Activate a standard collection of tracing hooks. */
+extern void mtrace __P ((void));
+extern void muntrace __P ((void));
+
+/* Statistics available to the user. */
+struct mstats
+ {
+ __malloc_size_t bytes_total; /* Total size of the heap. */
+ __malloc_size_t chunks_used; /* Chunks allocated by the user. */
+ __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
+ __malloc_size_t chunks_free; /* Chunks in the free list. */
+ __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
+ };
+
+/* Pick up the current statistics. */
+extern struct mstats mstats __P ((void));
+
+/* Call WARNFUN with a warning message when memory usage is high. */
+extern void memory_warnings __P ((__ptr_t __start,
+ void (*__warnfun) __P ((const char *))));
+
+
+/* Relocating allocator. */
+
+/* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
+extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
+
+/* Free the storage allocated in HANDLEPTR. */
+extern void r_alloc_free __P ((__ptr_t *__handleptr));
+
+/* Adjust the block at HANDLEPTR to be SIZE bytes long. */
+extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* malloc.h */
+/* Allocate memory on a page boundary.
+ Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#if defined (__GNU_LIBRARY__) || defined (_LIBC)
+#include <stddef.h>
+#include <sys/cdefs.h>
+extern size_t __getpagesize __P ((void));
+#else
+#include "getpagesize.h"
+#define __getpagesize() getpagesize()
+#endif
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+static __malloc_size_t pagesize;
+
+__ptr_t
+valloc (size)
+ __malloc_size_t size;
+{
+ if (pagesize == 0)
+ pagesize = __getpagesize ();
+
+ return memalign (pagesize, size);
+}
+/* Memory allocator `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* How to really get more memory. */
+__ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
+
+/* Debugging hook for `malloc'. */
+__ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
+
+/* Pointer to the base of the first block. */
+char *_heapbase;
+
+/* Block information table. Allocated with align/__free (not malloc/free). */
+malloc_info *_heapinfo;
+
+/* Number of info entries. */
+static __malloc_size_t heapsize;
+
+/* Search index in the info table. */
+__malloc_size_t _heapindex;
+
+/* Limit of valid info table indices. */
+__malloc_size_t _heaplimit;
+
+/* Free lists for each fragment size. */
+struct list _fraghead[BLOCKLOG];
+
+/* Instrumentation. */
+__malloc_size_t _chunks_used;
+__malloc_size_t _bytes_used;
+__malloc_size_t _chunks_free;
+__malloc_size_t _bytes_free;
+
+/* Are you experienced? */
+int __malloc_initialized;
+
+void (*__malloc_initialize_hook) __P ((void));
+void (*__after_morecore_hook) __P ((void));
+
+/* Aligned allocation. */
+static __ptr_t align __P ((__malloc_size_t));
+static __ptr_t
+align (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ unsigned long int adj;
+
+ result = (*__morecore) (size);
+ adj = (unsigned long int) ((unsigned long int) ((char *) result -
+ (char *) NULL)) % BLOCKSIZE;
+ if (adj != 0)
+ {
+ adj = BLOCKSIZE - adj;
+ (void) (*__morecore) (adj);
+ result = (char *) result + adj;
+ }
+
+ if (__after_morecore_hook)
+ (*__after_morecore_hook) ();
+
+ return result;
+}
+
+/* Set everything up and remember that we have. */
+static int initialize __P ((void));
+static int
+initialize ()
+{
+ if (__malloc_initialize_hook)
+ (*__malloc_initialize_hook) ();
+
+ heapsize = HEAP / BLOCKSIZE;
+ _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
+ if (_heapinfo == NULL)
+ return 0;
+ memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
+ _heapinfo[0].free.size = 0;
+ _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
+ _heapindex = 0;
+ _heapbase = (char *) _heapinfo;
+
+ /* Account for the _heapinfo block itself in the statistics. */
+ _bytes_used = heapsize * sizeof (malloc_info);
+ _chunks_used = 1;
+
+ __malloc_initialized = 1;
+ return 1;
+}
+
+/* Get neatly aligned memory, initializing or
+ growing the heap info table as necessary. */
+static __ptr_t morecore __P ((__malloc_size_t));
+static __ptr_t
+morecore (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ malloc_info *newinfo, *oldinfo;
+ __malloc_size_t newsize;
+
+ result = align (size);
+ if (result == NULL)
+ return NULL;
+
+ /* Check if we need to grow the info table. */
+ if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
+ {
+ newsize = heapsize;
+ while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize)
+ newsize *= 2;
+ newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
+ if (newinfo == NULL)
+ {
+ (*__morecore) (-size);
+ return NULL;
+ }
+ memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
+ memset (&newinfo[heapsize], 0,
+ (newsize - heapsize) * sizeof (malloc_info));
+ oldinfo = _heapinfo;
+ newinfo[BLOCK (oldinfo)].busy.type = 0;
+ newinfo[BLOCK (oldinfo)].busy.info.size
+ = BLOCKIFY (heapsize * sizeof (malloc_info));
+ _heapinfo = newinfo;
+ /* Account for the _heapinfo block itself in the statistics. */
+ _bytes_used += newsize * sizeof (malloc_info);
+ ++_chunks_used;
+ _free_internal (oldinfo);
+ heapsize = newsize;
+ }
+
+ _heaplimit = BLOCK ((char *) result + size);
+ return result;
+}
+
+/* Allocate memory from the heap. */
+__ptr_t
+malloc (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ __malloc_size_t block, blocks, lastblocks, start;
+ register __malloc_size_t i;
+ struct list *next;
+
+ /* ANSI C allows `malloc (0)' to either return NULL, or to return a
+ valid address you can realloc and free (though not dereference).
+
+ It turns out that some extant code (sunrpc, at least Ultrix's version)
+ expects `malloc (0)' to return non-NULL and breaks otherwise.
+ Be compatible. */
+
+#if 0
+ if (size == 0)
+ return NULL;
+#endif
+
+ if (__malloc_hook != NULL)
+ return (*__malloc_hook) (size);
+
+ if (!__malloc_initialized)
+ if (!initialize ())
+ return NULL;
+
+ if (size < sizeof (struct list))
+ size = sizeof (struct list);
+
+#ifdef SUNOS_LOCALTIME_BUG
+ if (size < 16)
+ size = 16;
+#endif
+
+ /* Determine the allocation policy based on the request size. */
+ if (size <= BLOCKSIZE / 2)
+ {
+ /* Small allocation to receive a fragment of a block.
+ Determine the logarithm to base two of the fragment size. */
+ register __malloc_size_t log = 1;
+ --size;
+ while ((size /= 2) != 0)
+ ++log;
+
+ /* Look in the fragment lists for a
+ free fragment of the desired size. */
+ next = _fraghead[log].next;
+ if (next != NULL)
+ {
+ /* There are free fragments of this size.
+ Pop a fragment out of the fragment list and return it.
+ Update the block's nfree and first counters. */
+ result = (__ptr_t) next;
+ next->prev->next = next->next;
+ if (next->next != NULL)
+ next->next->prev = next->prev;
+ block = BLOCK (result);
+ if (--_heapinfo[block].busy.info.frag.nfree != 0)
+ _heapinfo[block].busy.info.frag.first = (unsigned long int)
+ ((unsigned long int) ((char *) next->next - (char *) NULL)
+ % BLOCKSIZE) >> log;
+
+ /* Update the statistics. */
+ ++_chunks_used;
+ _bytes_used += 1 << log;
+ --_chunks_free;
+ _bytes_free -= 1 << log;
+ }
+ else
+ {
+ /* No free fragments of the desired size, so get a new block
+ and break it into fragments, returning the first. */
+ result = malloc (BLOCKSIZE);
+ if (result == NULL)
+ return NULL;
+
+ /* Link all fragments but the first into the free list. */
+ for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
+ {
+ next = (struct list *) ((char *) result + (i << log));
+ next->next = _fraghead[log].next;
+ next->prev = &_fraghead[log];
+ next->prev->next = next;
+ if (next->next != NULL)
+ next->next->prev = next;
+ }
+
+ /* Initialize the nfree and first counters for this block. */
+ block = BLOCK (result);
+ _heapinfo[block].busy.type = log;
+ _heapinfo[block].busy.info.frag.nfree = i - 1;
+ _heapinfo[block].busy.info.frag.first = i - 1;
+
+ _chunks_free += (BLOCKSIZE >> log) - 1;
+ _bytes_free += BLOCKSIZE - (1 << log);
+ _bytes_used -= BLOCKSIZE - (1 << log);
+ }
+ }
+ else
+ {
+ /* Large allocation to receive one or more blocks.
+ Search the free list in a circle starting at the last place visited.
+ If we loop completely around without finding a large enough
+ space we will have to get more memory from the system. */
+ blocks = BLOCKIFY (size);
+ start = block = _heapindex;
+ while (_heapinfo[block].free.size < blocks)
+ {
+ block = _heapinfo[block].free.next;
+ if (block == start)
+ {
+ /* Need to get more from the system. Check to see if
+ the new core will be contiguous with the final free
+ block; if so we don't need to get as much. */
+ block = _heapinfo[0].free.prev;
+ lastblocks = _heapinfo[block].free.size;
+ if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
+ (*__morecore) (0) == ADDRESS (block + lastblocks) &&
+ (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
+ {
+ /* Which block we are extending (the `final free
+ block' referred to above) might have changed, if
+ it got combined with a freed info table. */
+ block = _heapinfo[0].free.prev;
+ _heapinfo[block].free.size += (blocks - lastblocks);
+ _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
+ continue;
+ }
+ result = morecore (blocks * BLOCKSIZE);
+ if (result == NULL)
+ return NULL;
+ block = BLOCK (result);
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = blocks;
+ ++_chunks_used;
+ _bytes_used += blocks * BLOCKSIZE;
+ return result;
+ }
+ }
+
+ /* At this point we have found a suitable free list entry.
+ Figure out how to remove what we need from the list. */
+ result = ADDRESS (block);
+ if (_heapinfo[block].free.size > blocks)
+ {
+ /* The block we found has a bit left over,
+ so relink the tail end back into the free list. */
+ _heapinfo[block + blocks].free.size
+ = _heapinfo[block].free.size - blocks;
+ _heapinfo[block + blocks].free.next
+ = _heapinfo[block].free.next;
+ _heapinfo[block + blocks].free.prev
+ = _heapinfo[block].free.prev;
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapindex = block + blocks;
+ }
+ else
+ {
+ /* The block exactly matches our requirements,
+ so just remove it from the list. */
+ _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapinfo[block].free.prev;
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapindex = _heapinfo[block].free.next;
+ --_chunks_free;
+ }
+
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = blocks;
+ ++_chunks_used;
+ _bytes_used += blocks * BLOCKSIZE;
+ _bytes_free -= blocks * BLOCKSIZE;
+
+ /* Mark all the blocks of the object just allocated except for the
+ first with a negative number so you can find the first block by
+ adding that adjustment. */
+ while (--blocks > 0)
+ _heapinfo[block + blocks].busy.info.size = -blocks;
+ }
+
+ return result;
+}
+
+#ifndef _LIBC
+
+/* On some ANSI C systems, some libc functions call _malloc, _free
+ and _realloc. Make them use the GNU functions. */
+
+__ptr_t
+_malloc (size)
+ __malloc_size_t size;
+{
+ return malloc (size);
+}
+
+void
+_free (ptr)
+ __ptr_t ptr;
+{
+ free (ptr);
+}
+
+__ptr_t
+_realloc (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ return realloc (ptr, size);
+}
+
+#endif
+/* Free a block of memory allocated by `malloc'.
+ Copyright 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* Debugging hook for free. */
+void (*__free_hook) __P ((__ptr_t __ptr));
+
+/* List of blocks allocated by memalign. */
+struct alignlist *_aligned_blocks = NULL;
+
+/* Return memory to the heap.
+ Like `free' but don't call a __free_hook if there is one. */
+void
+_free_internal (ptr)
+ __ptr_t ptr;
+{
+ int type;
+ __malloc_size_t block, blocks;
+ register __malloc_size_t i;
+ struct list *prev, *next;
+
+ block = BLOCK (ptr);
+
+ type = _heapinfo[block].busy.type;
+ switch (type)
+ {
+ case 0:
+ /* Get as many statistics as early as we can. */
+ --_chunks_used;
+ _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
+ _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
+
+ /* Find the free cluster previous to this one in the free list.
+ Start searching at the last block referenced; this may benefit
+ programs with locality of allocation. */
+ i = _heapindex;
+ if (i > block)
+ while (i > block)
+ i = _heapinfo[i].free.prev;
+ else
+ {
+ do
+ i = _heapinfo[i].free.next;
+ while (i > 0 && i < block);
+ i = _heapinfo[i].free.prev;
+ }
+
+ /* Determine how to link this block into the free list. */
+ if (block == i + _heapinfo[i].free.size)
+ {
+ /* Coalesce this block with its predecessor. */
+ _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
+ block = i;
+ }
+ else
+ {
+ /* Really link this block back into the free list. */
+ _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
+ _heapinfo[block].free.next = _heapinfo[i].free.next;
+ _heapinfo[block].free.prev = i;
+ _heapinfo[i].free.next = block;
+ _heapinfo[_heapinfo[block].free.next].free.prev = block;
+ ++_chunks_free;
+ }
+
+ /* Now that the block is linked in, see if we can coalesce it
+ with its successor (by deleting its successor from the list
+ and adding in its size). */
+ if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
+ {
+ _heapinfo[block].free.size
+ += _heapinfo[_heapinfo[block].free.next].free.size;
+ _heapinfo[block].free.next
+ = _heapinfo[_heapinfo[block].free.next].free.next;
+ _heapinfo[_heapinfo[block].free.next].free.prev = block;
+ --_chunks_free;
+ }
+
+ /* Now see if we can return stuff to the system. */
+ blocks = _heapinfo[block].free.size;
+ if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
+ && (*__morecore) (0) == ADDRESS (block + blocks))
+ {
+ register __malloc_size_t bytes = blocks * BLOCKSIZE;
+ _heaplimit -= blocks;
+ (*__morecore) (-bytes);
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapinfo[block].free.next;
+ _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapinfo[block].free.prev;
+ block = _heapinfo[block].free.prev;
+ --_chunks_free;
+ _bytes_free -= bytes;
+ }
+
+ /* Set the next search to begin at this block. */
+ _heapindex = block;
+ break;
+
+ default:
+ /* Do some of the statistics. */
+ --_chunks_used;
+ _bytes_used -= 1 << type;
+ ++_chunks_free;
+ _bytes_free += 1 << type;
+
+ /* Get the address of the first free fragment in this block. */
+ prev = (struct list *) ((char *) ADDRESS (block) +
+ (_heapinfo[block].busy.info.frag.first << type));
+
+ if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
+ {
+ /* If all fragments of this block are free, remove them
+ from the fragment list and free the whole block. */
+ next = prev;
+ for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
+ next = next->next;
+ prev->prev->next = next;
+ if (next != NULL)
+ next->prev = prev->prev;
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = 1;
+
+ /* Keep the statistics accurate. */
+ ++_chunks_used;
+ _bytes_used += BLOCKSIZE;
+ _chunks_free -= BLOCKSIZE >> type;
+ _bytes_free -= BLOCKSIZE;
+
+ free (ADDRESS (block));
+ }
+ else if (_heapinfo[block].busy.info.frag.nfree != 0)
+ {
+ /* If some fragments of this block are free, link this
+ fragment into the fragment list after the first free
+ fragment of this block. */
+ next = (struct list *) ptr;
+ next->next = prev->next;
+ next->prev = prev;
+ prev->next = next;
+ if (next->next != NULL)
+ next->next->prev = next;
+ ++_heapinfo[block].busy.info.frag.nfree;
+ }
+ else
+ {
+ /* No fragments of this block are free, so link this
+ fragment into the fragment list and announce that
+ it is the first free fragment of this block. */
+ prev = (struct list *) ptr;
+ _heapinfo[block].busy.info.frag.nfree = 1;
+ _heapinfo[block].busy.info.frag.first = (unsigned long int)
+ ((unsigned long int) ((char *) ptr - (char *) NULL)
+ % BLOCKSIZE >> type);
+ prev->next = _fraghead[type].next;
+ prev->prev = &_fraghead[type];
+ prev->prev->next = prev;
+ if (prev->next != NULL)
+ prev->next->prev = prev;
+ }
+ break;
+ }
+}
+
+/* Return memory to the heap. */
+void
+free (ptr)
+ __ptr_t ptr;
+{
+ register struct alignlist *l;
+
+ if (ptr == NULL)
+ return;
+
+ for (l = _aligned_blocks; l != NULL; l = l->next)
+ if (l->aligned == ptr)
+ {
+ l->aligned = NULL; /* Mark the slot in the list as free. */
+ ptr = l->exact;
+ break;
+ }
+
+ if (__free_hook != NULL)
+ (*__free_hook) (ptr);
+ else
+ _free_internal (ptr);
+}
+/* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#ifdef _LIBC
+
+#include <ansidecl.h>
+#include <gnu-stabs.h>
+
+#undef cfree
+
+function_alias(cfree, free, void, (ptr),
+ DEFUN(cfree, (ptr), PTR ptr))
+
+#else
+
+void
+cfree (ptr)
+ __ptr_t ptr;
+{
+ free (ptr);
+}
+
+#endif
+/* Change the size of a block allocated by `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#if (defined (MEMMOVE_MISSING) || \
+ !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
+
+/* Snarfed directly from Emacs src/dispnew.c:
+ XXX Should use system bcopy if it handles overlap. */
+#ifndef emacs
+
+/* Like bcopy except never gets confused by overlap. */
+
+static void
+safe_bcopy (from, to, size)
+ char *from, *to;
+ int size;
+{
+ if (size <= 0 || from == to)
+ return;
+
+ /* If the source and destination don't overlap, then bcopy can
+ handle it. If they do overlap, but the destination is lower in
+ memory than the source, we'll assume bcopy can handle that. */
+ if (to < from || from + size <= to)
+ bcopy (from, to, size);
+
+ /* Otherwise, we'll copy from the end. */
+ else
+ {
+ register char *endf = from + size;
+ register char *endt = to + size;
+
+ /* If TO - FROM is large, then we should break the copy into
+ nonoverlapping chunks of TO - FROM bytes each. However, if
+ TO - FROM is small, then the bcopy function call overhead
+ makes this not worth it. The crossover point could be about
+ anywhere. Since I don't think the obvious copy loop is too
+ bad, I'm trying to err in its favor. */
+ if (to - from < 64)
+ {
+ do
+ *--endt = *--endf;
+ while (endf != from);
+ }
+ else
+ {
+ for (;;)
+ {
+ endt -= (to - from);
+ endf -= (to - from);
+
+ if (endt < to)
+ break;
+
+ bcopy (endf, endt, to - from);
+ }
+
+ /* If SIZE wasn't a multiple of TO - FROM, there will be a
+ little left over. The amount left over is
+ (endt + (to - from)) - to, which is endt - from. */
+ bcopy (from, to, endt - from);
+ }
+ }
+}
+#endif /* Not emacs. */
+
+#define memmove(to, from, size) safe_bcopy ((from), (to), (size))
+
+#endif
+
+
+#define min(A, B) ((A) < (B) ? (A) : (B))
+
+/* Debugging hook for realloc. */
+__ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+
+/* Resize the given region to the new size, returning a pointer
+ to the (possibly moved) region. This is optimized for speed;
+ some benchmarks seem to indicate that greater compactness is
+ achieved by unconditionally allocating and copying to a
+ new region. This module has incestuous knowledge of the
+ internals of both free and malloc. */
+__ptr_t
+realloc (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ int type;
+ __malloc_size_t block, blocks, oldlimit;
+
+ if (size == 0)
+ {
+ free (ptr);
+ return malloc (0);
+ }
+ else if (ptr == NULL)
+ return malloc (size);
+
+ if (__realloc_hook != NULL)
+ return (*__realloc_hook) (ptr, size);
+
+ block = BLOCK (ptr);
+
+ type = _heapinfo[block].busy.type;
+ switch (type)
+ {
+ case 0:
+ /* Maybe reallocate a large block to a small fragment. */
+ if (size <= BLOCKSIZE / 2)
+ {
+ result = malloc (size);
+ if (result != NULL)
+ {
+ memcpy (result, ptr, size);
+ _free_internal (ptr);
+ return result;
+ }
+ }
+
+ /* The new size is a large allocation as well;
+ see if we can hold it in place. */
+ blocks = BLOCKIFY (size);
+ if (blocks < _heapinfo[block].busy.info.size)
+ {
+ /* The new size is smaller; return
+ excess memory to the free list. */
+ _heapinfo[block + blocks].busy.type = 0;
+ _heapinfo[block + blocks].busy.info.size
+ = _heapinfo[block].busy.info.size - blocks;
+ _heapinfo[block].busy.info.size = blocks;
+ /* We have just created a new chunk by splitting a chunk in two.
+ Now we will free this chunk; increment the statistics counter
+ so it doesn't become wrong when _free_internal decrements it. */
+ ++_chunks_used;
+ _free_internal (ADDRESS (block + blocks));
+ result = ptr;
+ }
+ else if (blocks == _heapinfo[block].busy.info.size)
+ /* No size change necessary. */
+ result = ptr;
+ else
+ {
+ /* Won't fit, so allocate a new region that will.
+ Free the old region first in case there is sufficient
+ adjacent free space to grow without moving. */
+ blocks = _heapinfo[block].busy.info.size;
+ /* Prevent free from actually returning memory to the system. */
+ oldlimit = _heaplimit;
+ _heaplimit = 0;
+ _free_internal (ptr);
+ _heaplimit = oldlimit;
+ result = malloc (size);
+ if (result == NULL)
+ {
+ /* Now we're really in trouble. We have to unfree
+ the thing we just freed. Unfortunately it might
+ have been coalesced with its neighbors. */
+ if (_heapindex == block)
+ (void) malloc (blocks * BLOCKSIZE);
+ else
+ {
+ __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
+ (void) malloc (blocks * BLOCKSIZE);
+ _free_internal (previous);
+ }
+ return NULL;
+ }
+ if (ptr != result)
+ memmove (result, ptr, blocks * BLOCKSIZE);
+ }
+ break;
+
+ default:
+ /* Old size is a fragment; type is logarithm
+ to base two of the fragment size. */
+ if (size > (__malloc_size_t) (1 << (type - 1)) &&
+ size <= (__malloc_size_t) (1 << type))
+ /* The new size is the same kind of fragment. */
+ result = ptr;
+ else
+ {
+ /* The new size is different; allocate a new space,
+ and copy the lesser of the new size and the old. */
+ result = malloc (size);
+ if (result == NULL)
+ return NULL;
+ memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
+ free (ptr);
+ }
+ break;
+ }
+
+ return result;
+}
+/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* Allocate an array of NMEMB elements each SIZE bytes long.
+ The entire array is initialized to zeros. */
+__ptr_t
+calloc (nmemb, size)
+ register __malloc_size_t nmemb;
+ register __malloc_size_t size;
+{
+ register __ptr_t result = malloc (nmemb * size);
+
+ if (result != NULL)
+ (void) memset (result, 0, nmemb * size);
+
+ return result;
+}
+/* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library 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.
+
+The GNU C Library 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 the GNU C Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#ifndef __GNU_LIBRARY__
+#define __sbrk sbrk
+#endif
+
+#ifdef __GNU_LIBRARY__
+/* It is best not to declare this and cast its result on foreign operating
+ systems with potentially hostile include files. */
+extern __ptr_t __sbrk __P ((int increment));
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+/* Allocate INCREMENT more bytes of data space,
+ and return the start of data space, or NULL on errors.
+ If INCREMENT is negative, shrink data space. */
+__ptr_t
+__default_morecore (increment)
+#ifdef __STDC__
+ ptrdiff_t increment;
+#else
+ int increment;
+#endif
+{
+ __ptr_t result = (__ptr_t) __sbrk ((int) increment);
+ if (result == (__ptr_t) -1)
+ return NULL;
+ return result;
+}
+/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+__ptr_t (*__memalign_hook) __P ((size_t __size, size_t __alignment));
+
+__ptr_t
+memalign (alignment, size)
+ __malloc_size_t alignment;
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ unsigned long int adj;
+
+ if (__memalign_hook)
+ return (*__memalign_hook) (alignment, size);
+
+ size = ((size + alignment - 1) / alignment) * alignment;
+
+ result = malloc (size);
+ if (result == NULL)
+ return NULL;
+ adj = (unsigned long int) ((unsigned long int) ((char *) result -
+ (char *) NULL)) % alignment;
+ if (adj != 0)
+ {
+ struct alignlist *l;
+ for (l = _aligned_blocks; l != NULL; l = l->next)
+ if (l->aligned == NULL)
+ /* This slot is free. Use it. */
+ break;
+ if (l == NULL)
+ {
+ l = (struct alignlist *) malloc (sizeof (struct alignlist));
+ if (l == NULL)
+ {
+ free (result);
+ return NULL;
+ }
+ l->next = _aligned_blocks;
+ _aligned_blocks = l;
+ }
+ l->exact = result;
+ result = l->aligned = (char *) result + alignment - adj;
+ }
+
+ return result;
+}
diff --git a/external/gpl2/libmalloc/dist/malloc-find.c b/external/gpl2/libmalloc/dist/malloc-find.c
new file mode 100644
index 00000000000..a7ea8b0d188
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/malloc-find.c
@@ -0,0 +1,61 @@
+/* $NetBSD: malloc-find.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Find the starting address of a malloc'd block, from anywhere inside it.
+ Copyright (C) 1995 Free Software Foundation, Inc.
+
+This file is part of the GNU C Library. Its master source is NOT part of
+the C library, however. The master source lives in /gd/gnu/lib.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* Given an address in the middle of a malloc'd object,
+ return the address of the beginning of the object. */
+
+__ptr_t
+malloc_find_object_address (ptr)
+ __ptr_t ptr;
+{
+ __malloc_size_t block = BLOCK (ptr);
+ int type = _heapinfo[block].busy.type;
+
+ if (type == 0)
+ {
+ /* The object is one or more entire blocks. */
+ __malloc_ptrdiff_t sizevalue = _heapinfo[block].busy.info.size;
+
+ if (sizevalue < 0)
+ /* This is one of the blocks after the first. SIZEVALUE
+ says how many blocks to go back to find the first. */
+ block += sizevalue;
+
+ /* BLOCK is now the first block of the object.
+ Its start is the start of the object. */
+ return ADDRESS (block);
+ }
+ else
+ {
+ /* Get the size of fragments in this block. */
+ __malloc_size_t size = 1 << type;
+
+ /* Turn off the low bits to find the start address of the fragment. */
+ return _heapbase + (((char *) ptr - _heapbase) & ~(size - 1));
+ }
+}
diff --git a/external/gpl2/libmalloc/dist/malloc.c b/external/gpl2/libmalloc/dist/malloc.c
new file mode 100644
index 00000000000..3e1f43abff7
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/malloc.c
@@ -0,0 +1,377 @@
+/* $NetBSD: malloc.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Memory allocator `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+/* How to really get more memory. */
+__ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
+
+/* Debugging hook for `malloc'. */
+__ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
+
+/* Pointer to the base of the first block. */
+char *_heapbase;
+
+/* Block information table. Allocated with align/__free (not malloc/free). */
+malloc_info *_heapinfo;
+
+/* Number of info entries. */
+static __malloc_size_t heapsize;
+
+/* Search index in the info table. */
+__malloc_size_t _heapindex;
+
+/* Limit of valid info table indices. */
+__malloc_size_t _heaplimit;
+
+/* Free lists for each fragment size. */
+struct list _fraghead[BLOCKLOG];
+
+/* Instrumentation. */
+__malloc_size_t _chunks_used;
+__malloc_size_t _bytes_used;
+__malloc_size_t _chunks_free;
+__malloc_size_t _bytes_free;
+
+/* Are you experienced? */
+int __malloc_initialized;
+
+void (*__malloc_initialize_hook) __P ((void));
+void (*__after_morecore_hook) __P ((void));
+
+/* Aligned allocation. */
+static __ptr_t align __P ((__malloc_size_t));
+static __ptr_t
+align (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ unsigned long int adj;
+
+ result = (*__morecore) (size);
+ adj = (unsigned long int) ((unsigned long int) ((char *) result -
+ (char *) NULL)) % BLOCKSIZE;
+ if (adj != 0)
+ {
+ adj = BLOCKSIZE - adj;
+ (void) (*__morecore) (adj);
+ result = (char *) result + adj;
+ }
+
+ if (__after_morecore_hook)
+ (*__after_morecore_hook) ();
+
+ return result;
+}
+
+/* Set everything up and remember that we have. */
+static int initialize __P ((void));
+static int
+initialize ()
+{
+ if (__malloc_initialize_hook)
+ (*__malloc_initialize_hook) ();
+
+ heapsize = HEAP / BLOCKSIZE;
+ _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
+ if (_heapinfo == NULL)
+ return 0;
+ memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
+ _heapinfo[0].free.size = 0;
+ _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
+ _heapindex = 0;
+ _heapbase = (char *) _heapinfo;
+
+ /* Account for the _heapinfo block itself in the statistics. */
+ _bytes_used = heapsize * sizeof (malloc_info);
+ _chunks_used = 1;
+
+ __malloc_initialized = 1;
+ return 1;
+}
+
+/* Get neatly aligned memory, initializing or
+ growing the heap info table as necessary. */
+static __ptr_t morecore __P ((__malloc_size_t));
+static __ptr_t
+morecore (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ malloc_info *newinfo, *oldinfo;
+ __malloc_size_t newsize;
+
+ result = align (size);
+ if (result == NULL)
+ return NULL;
+
+ /* Check if we need to grow the info table. */
+ if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
+ {
+ newsize = heapsize;
+ while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize)
+ newsize *= 2;
+ newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
+ if (newinfo == NULL)
+ {
+ (*__morecore) (-size);
+ return NULL;
+ }
+ memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
+ memset (&newinfo[heapsize], 0,
+ (newsize - heapsize) * sizeof (malloc_info));
+ oldinfo = _heapinfo;
+ newinfo[BLOCK (oldinfo)].busy.type = 0;
+ newinfo[BLOCK (oldinfo)].busy.info.size
+ = BLOCKIFY (heapsize * sizeof (malloc_info));
+ _heapinfo = newinfo;
+ /* Account for the _heapinfo block itself in the statistics. */
+ _bytes_used += newsize * sizeof (malloc_info);
+ ++_chunks_used;
+ _free_internal (oldinfo);
+ heapsize = newsize;
+ }
+
+ _heaplimit = BLOCK ((char *) result + size);
+ return result;
+}
+
+/* Allocate memory from the heap. */
+__ptr_t
+malloc (size)
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ __malloc_size_t block, blocks, lastblocks, start;
+ register __malloc_size_t i;
+ struct list *next;
+
+ /* ANSI C allows `malloc (0)' to either return NULL, or to return a
+ valid address you can realloc and free (though not dereference).
+
+ It turns out that some extant code (sunrpc, at least Ultrix's version)
+ expects `malloc (0)' to return non-NULL and breaks otherwise.
+ Be compatible. */
+
+#if 0
+ if (size == 0)
+ return NULL;
+#endif
+
+ if (__malloc_hook != NULL)
+ return (*__malloc_hook) (size);
+
+ if (!__malloc_initialized)
+ if (!initialize ())
+ return NULL;
+
+ if (size < sizeof (struct list))
+ size = sizeof (struct list);
+
+#ifdef SUNOS_LOCALTIME_BUG
+ if (size < 16)
+ size = 16;
+#endif
+
+ /* Determine the allocation policy based on the request size. */
+ if (size <= BLOCKSIZE / 2)
+ {
+ /* Small allocation to receive a fragment of a block.
+ Determine the logarithm to base two of the fragment size. */
+ register __malloc_size_t log = 1;
+ --size;
+ while ((size /= 2) != 0)
+ ++log;
+
+ /* Look in the fragment lists for a
+ free fragment of the desired size. */
+ next = _fraghead[log].next;
+ if (next != NULL)
+ {
+ /* There are free fragments of this size.
+ Pop a fragment out of the fragment list and return it.
+ Update the block's nfree and first counters. */
+ result = (__ptr_t) next;
+ next->prev->next = next->next;
+ if (next->next != NULL)
+ next->next->prev = next->prev;
+ block = BLOCK (result);
+ if (--_heapinfo[block].busy.info.frag.nfree != 0)
+ _heapinfo[block].busy.info.frag.first = (unsigned long int)
+ ((unsigned long int) ((char *) next->next - (char *) NULL)
+ % BLOCKSIZE) >> log;
+
+ /* Update the statistics. */
+ ++_chunks_used;
+ _bytes_used += 1 << log;
+ --_chunks_free;
+ _bytes_free -= 1 << log;
+ }
+ else
+ {
+ /* No free fragments of the desired size, so get a new block
+ and break it into fragments, returning the first. */
+ result = malloc (BLOCKSIZE);
+ if (result == NULL)
+ return NULL;
+
+ /* Link all fragments but the first into the free list. */
+ for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
+ {
+ next = (struct list *) ((char *) result + (i << log));
+ next->next = _fraghead[log].next;
+ next->prev = &_fraghead[log];
+ next->prev->next = next;
+ if (next->next != NULL)
+ next->next->prev = next;
+ }
+
+ /* Initialize the nfree and first counters for this block. */
+ block = BLOCK (result);
+ _heapinfo[block].busy.type = log;
+ _heapinfo[block].busy.info.frag.nfree = i - 1;
+ _heapinfo[block].busy.info.frag.first = i - 1;
+
+ _chunks_free += (BLOCKSIZE >> log) - 1;
+ _bytes_free += BLOCKSIZE - (1 << log);
+ _bytes_used -= BLOCKSIZE - (1 << log);
+ }
+ }
+ else
+ {
+ /* Large allocation to receive one or more blocks.
+ Search the free list in a circle starting at the last place visited.
+ If we loop completely around without finding a large enough
+ space we will have to get more memory from the system. */
+ blocks = BLOCKIFY (size);
+ start = block = _heapindex;
+ while (_heapinfo[block].free.size < blocks)
+ {
+ block = _heapinfo[block].free.next;
+ if (block == start)
+ {
+ /* Need to get more from the system. Check to see if
+ the new core will be contiguous with the final free
+ block; if so we don't need to get as much. */
+ block = _heapinfo[0].free.prev;
+ lastblocks = _heapinfo[block].free.size;
+ if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
+ (*__morecore) (0) == ADDRESS (block + lastblocks) &&
+ (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
+ {
+ /* Which block we are extending (the `final free
+ block' referred to above) might have changed, if
+ it got combined with a freed info table. */
+ block = _heapinfo[0].free.prev;
+ _heapinfo[block].free.size += (blocks - lastblocks);
+ _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
+ continue;
+ }
+ result = morecore (blocks * BLOCKSIZE);
+ if (result == NULL)
+ return NULL;
+ block = BLOCK (result);
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = blocks;
+ ++_chunks_used;
+ _bytes_used += blocks * BLOCKSIZE;
+ return result;
+ }
+ }
+
+ /* At this point we have found a suitable free list entry.
+ Figure out how to remove what we need from the list. */
+ result = ADDRESS (block);
+ if (_heapinfo[block].free.size > blocks)
+ {
+ /* The block we found has a bit left over,
+ so relink the tail end back into the free list. */
+ _heapinfo[block + blocks].free.size
+ = _heapinfo[block].free.size - blocks;
+ _heapinfo[block + blocks].free.next
+ = _heapinfo[block].free.next;
+ _heapinfo[block + blocks].free.prev
+ = _heapinfo[block].free.prev;
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapindex = block + blocks;
+ }
+ else
+ {
+ /* The block exactly matches our requirements,
+ so just remove it from the list. */
+ _heapinfo[_heapinfo[block].free.next].free.prev
+ = _heapinfo[block].free.prev;
+ _heapinfo[_heapinfo[block].free.prev].free.next
+ = _heapindex = _heapinfo[block].free.next;
+ --_chunks_free;
+ }
+
+ _heapinfo[block].busy.type = 0;
+ _heapinfo[block].busy.info.size = blocks;
+ ++_chunks_used;
+ _bytes_used += blocks * BLOCKSIZE;
+ _bytes_free -= blocks * BLOCKSIZE;
+
+ /* Mark all the blocks of the object just allocated except for the
+ first with a negative number so you can find the first block by
+ adding that adjustment. */
+ while (--blocks > 0)
+ _heapinfo[block + blocks].busy.info.size = -blocks;
+ }
+
+ return result;
+}
+
+#ifndef _LIBC
+
+/* On some ANSI C systems, some libc functions call _malloc, _free
+ and _realloc. Make them use the GNU functions. */
+
+__ptr_t
+_malloc (size)
+ __malloc_size_t size;
+{
+ return malloc (size);
+}
+
+void
+_free (ptr)
+ __ptr_t ptr;
+{
+ free (ptr);
+}
+
+__ptr_t
+_realloc (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ return realloc (ptr, size);
+}
+
+#endif
diff --git a/external/gpl2/libmalloc/dist/malloc.h b/external/gpl2/libmalloc/dist/malloc.h
new file mode 100644
index 00000000000..07a113ef90a
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/malloc.h
@@ -0,0 +1,296 @@
+/* $NetBSD: malloc.h,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Declarations for `malloc' and friends.
+ Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_H
+
+#define _MALLOC_H 1
+
+#ifdef _MALLOC_INTERNAL
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
+#include <string.h>
+#else
+#ifndef memset
+#define memset(s, zero, n) bzero ((s), (n))
+#endif
+#ifndef memcpy
+#define memcpy(d, s, n) bcopy ((s), (d), (n))
+#endif
+#endif
+
+#if defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
+#include <limits.h>
+#else
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#endif /* _MALLOC_INTERNAL. */
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
+#undef __P
+#define __P(args) args
+#undef __ptr_t
+#define __ptr_t void *
+#else /* Not C++ or ANSI C. */
+#undef __P
+#define __P(args) ()
+#undef const
+#define const
+#undef __ptr_t
+#define __ptr_t char *
+#endif /* C++ or ANSI C. */
+
+#if defined (__STDC__) && __STDC__
+#include <stddef.h>
+#define __malloc_size_t size_t
+#define __malloc_ptrdiff_t ptrdiff_t
+#else
+#define __malloc_size_t unsigned int
+#define __malloc_ptrdiff_t int
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+
+/* Allocate SIZE bytes of memory. */
+extern __ptr_t malloc __P ((__malloc_size_t __size));
+/* Re-allocate the previously allocated block
+ in __ptr_t, making the new block SIZE bytes long. */
+extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
+/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
+extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
+/* Free a block allocated by `malloc', `realloc' or `calloc'. */
+extern void free __P ((__ptr_t __ptr));
+
+/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
+extern __ptr_t memalign __P ((__malloc_size_t __alignment,
+ __malloc_size_t __size));
+
+/* Allocate SIZE bytes on a page boundary. */
+extern __ptr_t valloc __P ((__malloc_size_t __size));
+
+
+#ifdef _MALLOC_INTERNAL
+
+/* The allocator divides the heap into blocks of fixed size; large
+ requests receive one or more whole blocks, and small requests
+ receive a fragment of a block. Fragment sizes are powers of two,
+ and all fragments of a block are the same size. When all the
+ fragments in a block have been freed, the block itself is freed. */
+#define INT_BIT (CHAR_BIT * sizeof(int))
+#define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
+#define BLOCKSIZE (1 << BLOCKLOG)
+#define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
+
+/* Determine the amount of memory spanned by the initial heap table
+ (not an absolute limit). */
+#define HEAP (INT_BIT > 16 ? 4194304 : 65536)
+
+/* Number of contiguous free blocks allowed to build up at the end of
+ memory before they will be returned to the system. */
+#define FINAL_FREE_BLOCKS 8
+
+/* Data structure giving per-block information. */
+typedef union
+ {
+ /* Heap information for a busy block. */
+ struct
+ {
+ /* Zero for a large (multiblock) object, or positive giving the
+ logarithm to the base two of the fragment size. */
+ int type;
+ union
+ {
+ struct
+ {
+ __malloc_size_t nfree; /* Free frags in a fragmented block. */
+ __malloc_size_t first; /* First free fragment of the block. */
+ } frag;
+ /* For a large object, in its first block, this has the number
+ of blocks in the object. In the other blocks, this has a
+ negative number which says how far back the first block is. */
+ __malloc_ptrdiff_t size;
+ } info;
+ } busy;
+ /* Heap information for a free block
+ (that may be the first of a free cluster). */
+ struct
+ {
+ __malloc_size_t size; /* Size (in blocks) of a free cluster. */
+ __malloc_size_t next; /* Index of next free cluster. */
+ __malloc_size_t prev; /* Index of previous free cluster. */
+ } free;
+ } malloc_info;
+
+/* Pointer to first block of the heap. */
+extern char *_heapbase;
+
+/* Table indexed by block number giving per-block information. */
+extern malloc_info *_heapinfo;
+
+/* Address to block number and vice versa. */
+#define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
+#define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
+
+/* Current search index for the heap table. */
+extern __malloc_size_t _heapindex;
+
+/* Limit of valid info table indices. */
+extern __malloc_size_t _heaplimit;
+
+/* Doubly linked lists of free fragments. */
+struct list
+ {
+ struct list *next;
+ struct list *prev;
+ };
+
+/* Free list headers for each fragment size. */
+extern struct list _fraghead[];
+
+/* List of blocks allocated with `memalign' (or `valloc'). */
+struct alignlist
+ {
+ struct alignlist *next;
+ __ptr_t aligned; /* The address that memaligned returned. */
+ __ptr_t exact; /* The address that malloc returned. */
+ };
+extern struct alignlist *_aligned_blocks;
+
+/* Instrumentation. */
+extern __malloc_size_t _chunks_used;
+extern __malloc_size_t _bytes_used;
+extern __malloc_size_t _chunks_free;
+extern __malloc_size_t _bytes_free;
+
+/* Internal version of `free' used in `morecore' (malloc.c). */
+extern void _free_internal __P ((__ptr_t __ptr));
+
+#endif /* _MALLOC_INTERNAL. */
+
+/* Given an address in the middle of a malloc'd object,
+ return the address of the beginning of the object. */
+extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
+
+/* Underlying allocation function; successive calls should
+ return contiguous pieces of memory. */
+extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
+
+/* Default value of `__morecore'. */
+extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
+
+/* If not NULL, this function is called after each time
+ `__morecore' is called to increase the data size. */
+extern void (*__after_morecore_hook) __P ((void));
+
+/* Nonzero if `malloc' has been called and done its initialization. */
+extern int __malloc_initialized;
+
+/* Hooks for debugging versions. */
+extern void (*__malloc_initialize_hook) __P ((void));
+extern void (*__free_hook) __P ((__ptr_t __ptr));
+extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
+extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
+ __malloc_size_t __alignment));
+
+/* Return values for `mprobe': these are the kinds of inconsistencies that
+ `mcheck' enables detection of. */
+enum mcheck_status
+ {
+ MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
+ MCHECK_OK, /* Block is fine. */
+ MCHECK_FREE, /* Block freed twice. */
+ MCHECK_HEAD, /* Memory before the block was clobbered. */
+ MCHECK_TAIL /* Memory after the block was clobbered. */
+ };
+
+/* Activate a standard collection of debugging hooks. This must be called
+ before `malloc' is ever called. ABORTFUNC is called with an error code
+ (see enum above) when an inconsistency is detected. If ABORTFUNC is
+ null, the standard function prints on stderr and then calls `abort'. */
+extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
+
+/* Check for aberrations in a particular malloc'd block. You must have
+ called `mcheck' already. These are the same checks that `mcheck' does
+ when you free or reallocate a block. */
+extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
+
+/* Activate a standard collection of tracing hooks. */
+extern void mtrace __P ((void));
+extern void muntrace __P ((void));
+
+/* Statistics available to the user. */
+struct mstats
+ {
+ __malloc_size_t bytes_total; /* Total size of the heap. */
+ __malloc_size_t chunks_used; /* Chunks allocated by the user. */
+ __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
+ __malloc_size_t chunks_free; /* Chunks in the free list. */
+ __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
+ };
+
+/* Pick up the current statistics. */
+extern struct mstats mstats __P ((void));
+
+/* Call WARNFUN with a warning message when memory usage is high. */
+extern void memory_warnings __P ((__ptr_t __start,
+ void (*__warnfun) __P ((const char *))));
+
+
+/* Relocating allocator. */
+
+/* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
+extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
+
+/* Free the storage allocated in HANDLEPTR. */
+extern void r_alloc_free __P ((__ptr_t *__handleptr));
+
+/* Adjust the block at HANDLEPTR to be SIZE bytes long. */
+extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* malloc.h */
diff --git a/external/gpl2/libmalloc/dist/mcheck.c b/external/gpl2/libmalloc/dist/mcheck.c
new file mode 100644
index 00000000000..36e5cfb1fd1
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/mcheck.c
@@ -0,0 +1,217 @@
+/* $NetBSD: mcheck.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Standard debugging hooks for `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#include <stdio.h>
+#endif
+
+/* Old hook values. */
+static void (*old_free_hook) __P ((__ptr_t ptr));
+static __ptr_t (*old_malloc_hook) __P ((__malloc_size_t size));
+static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size));
+
+/* Function to call when something awful happens. */
+static void (*abortfunc) __P ((enum mcheck_status));
+
+/* Arbitrary magical numbers. */
+#define MAGICWORD 0xfedabeeb
+#define MAGICFREE 0xd8675309
+#define MAGICBYTE ((char) 0xd7)
+#define MALLOCFLOOD ((char) 0x93)
+#define FREEFLOOD ((char) 0x95)
+
+struct hdr
+ {
+ __malloc_size_t size; /* Exact size requested by user. */
+ unsigned long int magic; /* Magic number to check header integrity. */
+ };
+
+#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
+#define flood memset
+#else
+static void flood __P ((__ptr_t, int, __malloc_size_t));
+static void
+flood (ptr, val, size)
+ __ptr_t ptr;
+ int val;
+ __malloc_size_t size;
+{
+ char *cp = ptr;
+ while (size--)
+ *cp++ = val;
+}
+#endif
+
+static enum mcheck_status checkhdr __P ((const struct hdr *));
+static enum mcheck_status
+checkhdr (hdr)
+ const struct hdr *hdr;
+{
+ enum mcheck_status status;
+ switch (hdr->magic)
+ {
+ default:
+ status = MCHECK_HEAD;
+ break;
+ case MAGICFREE:
+ status = MCHECK_FREE;
+ break;
+ case MAGICWORD:
+ if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
+ status = MCHECK_TAIL;
+ else
+ status = MCHECK_OK;
+ break;
+ }
+ if (status != MCHECK_OK)
+ (*abortfunc) (status);
+ return status;
+}
+
+static void freehook __P ((__ptr_t));
+static void
+freehook (ptr)
+ __ptr_t ptr;
+{
+ struct hdr *hdr = ((struct hdr *) ptr) - 1;
+ checkhdr (hdr);
+ hdr->magic = MAGICFREE;
+ flood (ptr, FREEFLOOD, hdr->size);
+ __free_hook = old_free_hook;
+ free (hdr);
+ __free_hook = freehook;
+}
+
+static __ptr_t mallochook __P ((__malloc_size_t));
+static __ptr_t
+mallochook (size)
+ __malloc_size_t size;
+{
+ struct hdr *hdr;
+
+ __malloc_hook = old_malloc_hook;
+ hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
+ __malloc_hook = mallochook;
+ if (hdr == NULL)
+ return NULL;
+
+ hdr->size = size;
+ hdr->magic = MAGICWORD;
+ ((char *) &hdr[1])[size] = MAGICBYTE;
+ flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
+ return (__ptr_t) (hdr + 1);
+}
+
+static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t));
+static __ptr_t
+reallochook (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ struct hdr *hdr = ((struct hdr *) ptr) - 1;
+ __malloc_size_t osize = hdr->size;
+
+ checkhdr (hdr);
+ if (size < osize)
+ flood ((char *) ptr + size, FREEFLOOD, osize - size);
+ __free_hook = old_free_hook;
+ __malloc_hook = old_malloc_hook;
+ __realloc_hook = old_realloc_hook;
+ hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
+ __free_hook = freehook;
+ __malloc_hook = mallochook;
+ __realloc_hook = reallochook;
+ if (hdr == NULL)
+ return NULL;
+
+ hdr->size = size;
+ hdr->magic = MAGICWORD;
+ ((char *) &hdr[1])[size] = MAGICBYTE;
+ if (size > osize)
+ flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
+ return (__ptr_t) (hdr + 1);
+}
+
+static void
+mabort (status)
+ enum mcheck_status status;
+{
+ const char *msg;
+ switch (status)
+ {
+ case MCHECK_OK:
+ msg = "memory is consistent, library is buggy";
+ break;
+ case MCHECK_HEAD:
+ msg = "memory clobbered before allocated block";
+ break;
+ case MCHECK_TAIL:
+ msg = "memory clobbered past end of allocated block";
+ break;
+ case MCHECK_FREE:
+ msg = "block freed twice";
+ break;
+ default:
+ msg = "bogus mcheck_status, library is buggy";
+ break;
+ }
+#ifdef __GNU_LIBRARY__
+ __libc_fatal (msg);
+#else
+ fprintf (stderr, "mcheck: %s\n", msg);
+ fflush (stderr);
+ abort ();
+#endif
+}
+
+static int mcheck_used = 0;
+
+int
+mcheck (func)
+ void (*func) __P ((enum mcheck_status));
+{
+ abortfunc = (func != NULL) ? func : &mabort;
+
+ /* These hooks may not be safely inserted if malloc is already in use. */
+ if (!__malloc_initialized && !mcheck_used)
+ {
+ old_free_hook = __free_hook;
+ __free_hook = freehook;
+ old_malloc_hook = __malloc_hook;
+ __malloc_hook = mallochook;
+ old_realloc_hook = __realloc_hook;
+ __realloc_hook = reallochook;
+ mcheck_used = 1;
+ }
+
+ return mcheck_used ? 0 : -1;
+}
+
+enum mcheck_status
+mprobe (__ptr_t ptr)
+{
+ return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;
+}
diff --git a/external/gpl2/libmalloc/dist/mem-limits.h b/external/gpl2/libmalloc/dist/mem-limits.h
new file mode 100644
index 00000000000..fc48f2c282a
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/mem-limits.h
@@ -0,0 +1,186 @@
+/* $NetBSD: mem-limits.h,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Includes for memory limit warnings.
+ Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
+
+
+This file is part of the GNU C Library. Its master source is NOT part of
+the C library, however. The master source lives in /gd/gnu/lib.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifdef MSDOS
+#include <dpmi.h>
+#endif
+
+/* Some systems need this before <sys/resource.h>. */
+#include <sys/types.h>
+
+#ifdef _LIBC
+
+#include <sys/resource.h>
+#define BSD4_2 /* Tell code below to use getrlimit. */
+
+#else
+
+#if defined (__osf__) && (defined (__mips) || defined (mips) || defined(__alpha))
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+
+#ifdef __bsdi__
+#define BSD4_2
+#endif
+
+#ifndef BSD4_2
+#ifndef USG
+#ifndef MSDOS
+#ifndef WINDOWSNT
+#include <sys/vlimit.h>
+#endif /* not WINDOWSNT */
+#endif /* not MSDOS */
+#endif /* not USG */
+#else /* if BSD4_2 */
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif /* BSD4_2 */
+
+#endif /* _LIBC */
+
+#ifdef emacs
+/* The important properties of this type are that 1) it's a pointer, and
+ 2) arithmetic on it should work as if the size of the object pointed
+ to has a size of 1. */
+#ifdef __STDC__
+typedef void *POINTER;
+#else
+typedef char *POINTER;
+#endif
+
+typedef unsigned long SIZE;
+
+#ifdef NULL
+#undef NULL
+#endif
+#define NULL ((POINTER) 0)
+
+extern POINTER start_of_data ();
+#ifdef DATA_SEG_BITS
+#define EXCEEDS_LISP_PTR(ptr) \
+ (((EMACS_UINT) (ptr) & ~DATA_SEG_BITS) >> VALBITS)
+#else
+#define EXCEEDS_LISP_PTR(ptr) ((EMACS_UINT) (ptr) >> VALBITS)
+#endif
+
+#ifdef BSD
+#ifndef DATA_SEG_BITS
+extern char etext;
+#define start_of_data() &etext
+#endif
+#endif
+
+#else /* Not emacs */
+extern char etext;
+#define start_of_data() &etext
+#endif /* Not emacs */
+
+
+
+/* start of data space; can be changed by calling malloc_init */
+static POINTER data_space_start;
+
+/* Number of bytes of writable memory we can expect to be able to get */
+static unsigned int lim_data;
+
+#ifdef NO_LIM_DATA
+static void
+get_lim_data ()
+{
+ lim_data = -1;
+}
+#else /* not NO_LIM_DATA */
+
+#ifdef USG
+
+static void
+get_lim_data ()
+{
+ extern long ulimit ();
+
+ lim_data = -1;
+
+ /* Use the ulimit call, if we seem to have it. */
+#if !defined (ULIMIT_BREAK_VALUE) || defined (LINUX)
+ lim_data = ulimit (3, 0);
+#endif
+
+ /* If that didn't work, just use the macro's value. */
+#ifdef ULIMIT_BREAK_VALUE
+ if (lim_data == -1)
+ lim_data = ULIMIT_BREAK_VALUE;
+#endif
+
+ lim_data -= (long) data_space_start;
+}
+
+#else /* not USG */
+#ifdef WINDOWSNT
+
+static void
+get_lim_data ()
+{
+ extern unsigned long data_region_size;
+ lim_data = data_region_size;
+}
+
+#else
+#if !defined (BSD4_2) && !defined (__osf__)
+
+#ifdef MSDOS
+void
+get_lim_data ()
+{
+ _go32_dpmi_meminfo info;
+
+ _go32_dpmi_get_free_memory_information (&info);
+ lim_data = info.available_memory;
+}
+#else /* not MSDOS */
+static void
+get_lim_data ()
+{
+ lim_data = vlimit (LIM_DATA, -1);
+}
+#endif /* not MSDOS */
+
+#else /* BSD4_2 */
+
+static void
+get_lim_data ()
+{
+ struct rlimit XXrlimit;
+
+ getrlimit (RLIMIT_DATA, &XXrlimit);
+#ifdef RLIM_INFINITY
+ lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
+#else
+ lim_data = XXrlimit.rlim_cur; /* soft limit */
+#endif
+}
+#endif /* BSD4_2 */
+#endif /* not WINDOWSNT */
+#endif /* not USG */
+#endif /* not NO_LIM_DATA */
diff --git a/external/gpl2/libmalloc/dist/memalign.c b/external/gpl2/libmalloc/dist/memalign.c
new file mode 100644
index 00000000000..a9adb3401d1
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/memalign.c
@@ -0,0 +1,68 @@
+/* $NetBSD: memalign.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+__ptr_t (*__memalign_hook) __P ((size_t __size, size_t __alignment));
+
+__ptr_t
+memalign (alignment, size)
+ __malloc_size_t alignment;
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ unsigned long int adj;
+
+ if (__memalign_hook)
+ return (*__memalign_hook) (alignment, size);
+
+ size = ((size + alignment - 1) / alignment) * alignment;
+
+ result = malloc (size);
+ if (result == NULL)
+ return NULL;
+ adj = (unsigned long int) ((unsigned long int) ((char *) result -
+ (char *) NULL)) % alignment;
+ if (adj != 0)
+ {
+ struct alignlist *l;
+ for (l = _aligned_blocks; l != NULL; l = l->next)
+ if (l->aligned == NULL)
+ /* This slot is free. Use it. */
+ break;
+ if (l == NULL)
+ {
+ l = (struct alignlist *) malloc (sizeof (struct alignlist));
+ if (l == NULL)
+ {
+ free (result);
+ return NULL;
+ }
+ l->next = _aligned_blocks;
+ _aligned_blocks = l;
+ }
+ l->exact = result;
+ result = l->aligned = (char *) result + alignment - adj;
+ }
+
+ return result;
+}
diff --git a/external/gpl2/libmalloc/dist/morecore.c b/external/gpl2/libmalloc/dist/morecore.c
new file mode 100644
index 00000000000..e7dd33ea5dd
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/morecore.c
@@ -0,0 +1,54 @@
+/* $NetBSD: morecore.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library 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.
+
+The GNU C Library 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 the GNU C Library; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#ifndef __GNU_LIBRARY__
+#define __sbrk sbrk
+#endif
+
+#ifdef __GNU_LIBRARY__
+/* It is best not to declare this and cast its result on foreign operating
+ systems with potentially hostile include files. */
+extern __ptr_t __sbrk __P ((int increment));
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+/* Allocate INCREMENT more bytes of data space,
+ and return the start of data space, or NULL on errors.
+ If INCREMENT is negative, shrink data space. */
+__ptr_t
+__default_morecore (increment)
+#ifdef __STDC__
+ ptrdiff_t increment;
+#else
+ int increment;
+#endif
+{
+ __ptr_t result = (__ptr_t) __sbrk ((int) increment);
+ if (result == (__ptr_t) -1)
+ return NULL;
+ return result;
+}
diff --git a/external/gpl2/libmalloc/dist/mstats.c b/external/gpl2/libmalloc/dist/mstats.c
new file mode 100644
index 00000000000..88fbe1c4ec6
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/mstats.c
@@ -0,0 +1,41 @@
+/* $NetBSD: mstats.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Access the statistics maintained by `malloc'.
+ Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+struct mstats
+mstats ()
+{
+ struct mstats result;
+
+ result.bytes_total = (char *) (*__morecore) (0) - _heapbase;
+ result.chunks_used = _chunks_used;
+ result.bytes_used = _bytes_used;
+ result.chunks_free = _chunks_free;
+ result.bytes_free = _bytes_free;
+ return result;
+}
diff --git a/external/gpl2/libmalloc/dist/mtrace.awk b/external/gpl2/libmalloc/dist/mtrace.awk
new file mode 100644
index 00000000000..06844d1a4b4
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/mtrace.awk
@@ -0,0 +1,50 @@
+#
+# Awk program to analyze mtrace.c output.
+#
+{
+ if ($1 == "@") {
+ where = " (" $2 ")"
+ n = 3
+ } else {
+ where = ""
+ n = 1
+ }
+ if ($n == "+") {
+ if (allocated[$(n+1)] != "")
+ print "+", $(n+1), "Alloc", NR, "duplicate:", allocated[$(n+1)], wherewas[$(n+1)], where;
+ else {
+ wherewas[$(n+1)] = where;
+ allocated[$(n+1)] = $(n+2);
+ }
+ } else if ($n == "-") {
+ if (allocated[$(n+1)] != "") {
+ wherewas[$(n+1)] = "";
+ allocated[$(n+1)] = "";
+ if (allocated[$(n+1)] != "")
+ print "DELETE FAILED", $(n+1), allocated[$(n+1)];
+ } else
+ print "-", $(n+1), "Free", NR, "was never alloc'd", where;
+ } else if ($n == "<") {
+ if (allocated[$(n+1)] != "") {
+ wherewas[$(n+1)] = "";
+ allocated[$(n+1)] = "";
+ } else
+ print "-", $(n+1), "Realloc", NR, "was never alloc'd", where;
+ } else if ($n == ">") {
+ if (allocated[$(n+1)] != "")
+ print "+", $(n+1), "Realloc", NR, "duplicate:", allocated[$(n+1)], where;
+ else {
+ wherewas[$(n+1)] = $(n+2);
+ allocated[$(n+1)] = $(n+2);
+ }
+ } else if ($n == "=") {
+ # Ignore "= Start"
+ } else if ($n == "!") {
+ # Ignore failed realloc attempts for now
+ }
+}
+END {
+ for (x in allocated)
+ if (allocated[x] != "")
+ print "+", x, allocated[x], wherewas[x];
+}
diff --git a/external/gpl2/libmalloc/dist/mtrace.c b/external/gpl2/libmalloc/dist/mtrace.c
new file mode 100644
index 00000000000..92901364953
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/mtrace.c
@@ -0,0 +1,189 @@
+/* $NetBSD: mtrace.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* More debugging hooks for `malloc'.
+ Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+ Written April 2, 1991 by John Gilmore of Cygnus Support.
+ Based on mcheck.c by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#include <stdio.h>
+
+#ifndef __GNU_LIBRARY__
+extern char *getenv ();
+#else
+#include <stdlib.h>
+#endif
+
+static FILE *mallstream;
+static char mallenv[]= "MALLOC_TRACE";
+static char mallbuf[BUFSIZ]; /* Buffer for the output. */
+
+/* Address to breakpoint on accesses to... */
+__ptr_t mallwatch;
+
+/* File name and line number information, for callers that had
+ the foresight to call through a macro. */
+char *_mtrace_file;
+int _mtrace_line;
+
+/* Old hook values. */
+static void (*tr_old_free_hook) __P ((__ptr_t ptr));
+static __ptr_t (*tr_old_malloc_hook) __P ((__malloc_size_t size));
+static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size));
+
+/* This function is called when the block being alloc'd, realloc'd, or
+ freed has an address matching the variable "mallwatch". In a debugger,
+ set "mallwatch" to the address of interest, then put a breakpoint on
+ tr_break. */
+
+void tr_break __P ((void));
+void
+tr_break ()
+{
+}
+
+static void tr_where __P ((void));
+static void
+tr_where ()
+{
+ if (_mtrace_file)
+ {
+ fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
+ _mtrace_file = NULL;
+ }
+}
+
+static void tr_freehook __P ((__ptr_t));
+static void
+tr_freehook (ptr)
+ __ptr_t ptr;
+{
+ tr_where ();
+ fprintf (mallstream, "- %p\n", ptr); /* Be sure to print it first. */
+ if (ptr == mallwatch)
+ tr_break ();
+ __free_hook = tr_old_free_hook;
+ free (ptr);
+ __free_hook = tr_freehook;
+}
+
+static __ptr_t tr_mallochook __P ((__malloc_size_t));
+static __ptr_t
+tr_mallochook (size)
+ __malloc_size_t size;
+{
+ __ptr_t hdr;
+
+ __malloc_hook = tr_old_malloc_hook;
+ hdr = (__ptr_t) malloc (size);
+ __malloc_hook = tr_mallochook;
+
+ tr_where ();
+ /* We could be printing a NULL here; that's OK. */
+ fprintf (mallstream, "+ %p %lx\n", hdr, (unsigned long)size);
+
+ if (hdr == mallwatch)
+ tr_break ();
+
+ return hdr;
+}
+
+static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t));
+static __ptr_t
+tr_reallochook (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ __ptr_t hdr;
+
+ if (ptr == mallwatch)
+ tr_break ();
+
+ __free_hook = tr_old_free_hook;
+ __malloc_hook = tr_old_malloc_hook;
+ __realloc_hook = tr_old_realloc_hook;
+ hdr = (__ptr_t) realloc (ptr, size);
+ __free_hook = tr_freehook;
+ __malloc_hook = tr_mallochook;
+ __realloc_hook = tr_reallochook;
+ tr_where ();
+ if (hdr == NULL)
+ /* Failed realloc. */
+ fprintf (mallstream, "! %p %lx\n", ptr, (unsigned long)size);
+ else
+ fprintf (mallstream, "< %p\n> %p %lx\n", ptr, hdr, (unsigned long)size);
+
+ if (hdr == mallwatch)
+ tr_break ();
+
+ return hdr;
+}
+
+/* We enable tracing if either the environment variable MALLOC_TRACE
+ is set, or if the variable mallwatch has been patched to an address
+ that the debugging user wants us to stop on. When patching mallwatch,
+ don't forget to set a breakpoint on tr_break! */
+
+void
+mtrace ()
+{
+ char *mallfile;
+
+ /* Don't panic if we're called more than once. */
+ if (mallstream != NULL)
+ return;
+
+ mallfile = getenv (mallenv);
+ if (mallfile != NULL || mallwatch != NULL)
+ {
+ mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
+ if (mallstream != NULL)
+ {
+ /* Be sure it doesn't malloc its buffer! */
+ setbuf (mallstream, mallbuf);
+ fprintf (mallstream, "= Start\n");
+ tr_old_free_hook = __free_hook;
+ __free_hook = tr_freehook;
+ tr_old_malloc_hook = __malloc_hook;
+ __malloc_hook = tr_mallochook;
+ tr_old_realloc_hook = __realloc_hook;
+ __realloc_hook = tr_reallochook;
+ }
+ }
+}
+
+void
+muntrace ()
+{
+ if (mallstream == NULL)
+ return;
+
+ fprintf (mallstream, "= End\n");
+ fclose (mallstream);
+ mallstream = NULL;
+ __free_hook = tr_old_free_hook;
+ __malloc_hook = tr_old_malloc_hook;
+ __realloc_hook = tr_old_realloc_hook;
+}
diff --git a/external/gpl2/libmalloc/dist/ralloc.c b/external/gpl2/libmalloc/dist/ralloc.c
new file mode 100644
index 00000000000..0b6bbcb57ca
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/ralloc.c
@@ -0,0 +1,1088 @@
+/* $NetBSD: ralloc.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Block-relocating memory allocator.
+ Copyright (C) 1993, 1995 Free Software Foundation, Inc.
+
+
+This file is part of the GNU C Library. Its master source is NOT part of
+the C library, however. The master source lives in /gd/gnu/lib.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+/* NOTES:
+
+ Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
+ rather than all of them. This means allowing for a possible
+ hole between the first bloc and the end of malloc storage. */
+
+#ifdef emacs
+
+#include <config.h>
+#include "lisp.h" /* Needed for VALBITS. */
+
+#undef NULL
+
+/* The important properties of this type are that 1) it's a pointer, and
+ 2) arithmetic on it should work as if the size of the object pointed
+ to has a size of 1. */
+#if 0 /* Arithmetic on void* is a GCC extension. */
+#ifdef __STDC__
+typedef void *POINTER;
+#else
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+typedef char *POINTER;
+
+#endif
+#endif /* 0 */
+
+/* Unconditionally use char * for this. */
+typedef char *POINTER;
+
+typedef unsigned long SIZE;
+
+/* Declared in dispnew.c, this version doesn't screw up if regions
+ overlap. */
+extern void safe_bcopy ();
+
+#include "getpagesize.h"
+
+#else /* Not emacs. */
+
+#include <stddef.h>
+
+typedef size_t SIZE;
+typedef void *POINTER;
+
+#include <unistd.h>
+#include <malloc.h>
+#include <string.h>
+
+#define safe_bcopy(x, y, z) memmove (y, x, z)
+
+#endif /* emacs. */
+
+#define NIL ((POINTER) 0)
+
+/* A flag to indicate whether we have initialized ralloc yet. For
+ Emacs's sake, please do not make this local to malloc_init; on some
+ machines, the dumping procedure makes all static variables
+ read-only. On these machines, the word static is #defined to be
+ the empty string, meaning that r_alloc_initialized becomes an
+ automatic variable, and loses its value each time Emacs is started up. */
+static int r_alloc_initialized = 0;
+
+static void r_alloc_init ();
+
+/* Declarations for working with the malloc, ralloc, and system breaks. */
+
+/* Function to set the real break value. */
+static POINTER (*real_morecore) ();
+
+/* The break value, as seen by malloc. */
+static POINTER virtual_break_value;
+
+/* The address of the end of the last data in use by ralloc,
+ including relocatable blocs as well as malloc data. */
+static POINTER break_value;
+
+/* This is the size of a page. We round memory requests to this boundary. */
+static int page_size;
+
+/* Whenever we get memory from the system, get this many extra bytes. This
+ must be a multiple of page_size. */
+static int extra_bytes;
+
+/* Macros for rounding. Note that rounding to any value is possible
+ by changing the definition of PAGE. */
+#define PAGE (getpagesize ())
+#define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
+#define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
+ & ~(page_size - 1))
+#define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
+
+#define MEM_ALIGN sizeof(double)
+#define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
+ & ~(MEM_ALIGN - 1))
+
+/* Data structures of heaps and blocs. */
+
+/* The relocatable objects, or blocs, and the malloc data
+ both reside within one or more heaps.
+ Each heap contains malloc data, running from `start' to `bloc_start',
+ and relocatable objects, running from `bloc_start' to `free'.
+
+ Relocatable objects may relocate within the same heap
+ or may move into another heap; the heaps themselves may grow
+ but they never move.
+
+ We try to make just one heap and make it larger as necessary.
+ But sometimes we can't do that, because we can't get continguous
+ space to add onto the heap. When that happens, we start a new heap. */
+
+typedef struct heap
+{
+ struct heap *next;
+ struct heap *prev;
+ /* Start of memory range of this heap. */
+ POINTER start;
+ /* End of memory range of this heap. */
+ POINTER end;
+ /* Start of relocatable data in this heap. */
+ POINTER bloc_start;
+ /* Start of unused space in this heap. */
+ POINTER free;
+ /* First bloc in this heap. */
+ struct bp *first_bloc;
+ /* Last bloc in this heap. */
+ struct bp *last_bloc;
+} *heap_ptr;
+
+#define NIL_HEAP ((heap_ptr) 0)
+#define HEAP_PTR_SIZE (sizeof (struct heap))
+
+/* This is the first heap object.
+ If we need additional heap objects, each one resides at the beginning of
+ the space it covers. */
+static struct heap heap_base;
+
+/* Head and tail of the list of heaps. */
+static heap_ptr first_heap, last_heap;
+
+/* These structures are allocated in the malloc arena.
+ The linked list is kept in order of increasing '.data' members.
+ The data blocks abut each other; if b->next is non-nil, then
+ b->data + b->size == b->next->data. */
+typedef struct bp
+{
+ struct bp *next;
+ struct bp *prev;
+ POINTER *variable;
+ POINTER data;
+ SIZE size;
+ POINTER new_data; /* tmporarily used for relocation */
+ /* Heap this bloc is in. */
+ struct heap *heap;
+} *bloc_ptr;
+
+#define NIL_BLOC ((bloc_ptr) 0)
+#define BLOC_PTR_SIZE (sizeof (struct bp))
+
+/* Head and tail of the list of relocatable blocs. */
+static bloc_ptr first_bloc, last_bloc;
+
+
+/* Functions to get and return memory from the system. */
+
+/* Find the heap that ADDRESS falls within. */
+
+static heap_ptr
+find_heap (address)
+ POINTER address;
+{
+ heap_ptr heap;
+
+ for (heap = last_heap; heap; heap = heap->prev)
+ {
+ if (heap->start <= address && address <= heap->end)
+ return heap;
+ }
+
+ return NIL_HEAP;
+}
+
+/* Find SIZE bytes of space in a heap.
+ Try to get them at ADDRESS (which must fall within some heap's range)
+ if we can get that many within one heap.
+
+ If enough space is not presently available in our reserve, this means
+ getting more page-aligned space from the system. If the retuned space
+ is not contiguos to the last heap, allocate a new heap, and append it
+
+ obtain does not try to keep track of whether space is in use
+ or not in use. It just returns the address of SIZE bytes that
+ fall within a single heap. If you call obtain twice in a row
+ with the same arguments, you typically get the same value.
+ to the heap list. It's the caller's responsibility to keep
+ track of what space is in use.
+
+ Return the address of the space if all went well, or zero if we couldn't
+ allocate the memory. */
+
+static POINTER
+obtain (address, size)
+ POINTER address;
+ SIZE size;
+{
+ heap_ptr heap;
+ SIZE already_available;
+
+ /* Find the heap that ADDRESS falls within. */
+ for (heap = last_heap; heap; heap = heap->prev)
+ {
+ if (heap->start <= address && address <= heap->end)
+ break;
+ }
+
+ if (! heap)
+ abort ();
+
+ /* If we can't fit SIZE bytes in that heap,
+ try successive later heaps. */
+ while (heap && address + size > heap->end)
+ {
+ heap = heap->next;
+ if (heap == NIL_HEAP)
+ break;
+ address = heap->bloc_start;
+ }
+
+ /* If we can't fit them within any existing heap,
+ get more space. */
+ if (heap == NIL_HEAP)
+ {
+ POINTER new = (*real_morecore)(0);
+ SIZE get;
+
+ already_available = (char *)last_heap->end - (char *)address;
+
+ if (new != last_heap->end)
+ {
+ /* Someone else called sbrk. Make a new heap. */
+
+ heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
+ POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
+
+ if ((*real_morecore) (bloc_start - new) != new)
+ return 0;
+
+ new_heap->start = new;
+ new_heap->end = bloc_start;
+ new_heap->bloc_start = bloc_start;
+ new_heap->free = bloc_start;
+ new_heap->next = NIL_HEAP;
+ new_heap->prev = last_heap;
+ new_heap->first_bloc = NIL_BLOC;
+ new_heap->last_bloc = NIL_BLOC;
+ last_heap->next = new_heap;
+ last_heap = new_heap;
+
+ address = bloc_start;
+ already_available = 0;
+ }
+
+ /* Add space to the last heap (which we may have just created).
+ Get some extra, so we can come here less often. */
+
+ get = size + extra_bytes - already_available;
+ get = (char *) ROUNDUP ((char *)last_heap->end + get)
+ - (char *) last_heap->end;
+
+ if ((*real_morecore) (get) != last_heap->end)
+ return 0;
+
+ last_heap->end += get;
+ }
+
+ return address;
+}
+
+/* Return unused heap space to the system
+ if there is a lot of unused space now.
+ This can make the last heap smaller;
+ it can also eliminate the last heap entirely. */
+
+static void
+relinquish ()
+{
+ register heap_ptr h;
+ int excess = 0;
+
+ /* Add the amount of space beyond break_value
+ in all heaps which have extend beyond break_value at all. */
+
+ for (h = last_heap; h && break_value < h->end; h = h->prev)
+ {
+ excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
+ ? h->bloc_start : break_value);
+ }
+
+ if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
+ {
+ /* Keep extra_bytes worth of empty space.
+ And don't free anything unless we can free at least extra_bytes. */
+ excess -= extra_bytes;
+
+ if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
+ {
+ /* This heap should have no blocs in it. */
+ if (last_heap->first_bloc != NIL_BLOC
+ || last_heap->last_bloc != NIL_BLOC)
+ abort ();
+
+ /* Return the last heap, with its header, to the system. */
+ excess = (char *)last_heap->end - (char *)last_heap->start;
+ last_heap = last_heap->prev;
+ last_heap->next = NIL_HEAP;
+ }
+ else
+ {
+ excess = (char *) last_heap->end
+ - (char *) ROUNDUP ((char *)last_heap->end - excess);
+ last_heap->end -= excess;
+ }
+
+ if ((*real_morecore) (- excess) == 0)
+ abort ();
+ }
+}
+
+/* The meat - allocating, freeing, and relocating blocs. */
+
+/* Find the bloc referenced by the address in PTR. Returns a pointer
+ to that block. */
+
+static bloc_ptr
+find_bloc (ptr)
+ POINTER *ptr;
+{
+ register bloc_ptr p = first_bloc;
+
+ while (p != NIL_BLOC)
+ {
+ if (p->variable == ptr && p->data == *ptr)
+ return p;
+
+ p = p->next;
+ }
+
+ return p;
+}
+
+/* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
+ Returns a pointer to the new bloc, or zero if we couldn't allocate
+ memory for the new block. */
+
+static bloc_ptr
+get_bloc (size)
+ SIZE size;
+{
+ register bloc_ptr new_bloc;
+ register heap_ptr heap;
+
+ if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
+ || ! (new_bloc->data = obtain (break_value, size)))
+ {
+ if (new_bloc)
+ free (new_bloc);
+
+ return 0;
+ }
+
+ break_value = new_bloc->data + size;
+
+ new_bloc->size = size;
+ new_bloc->next = NIL_BLOC;
+ new_bloc->variable = (POINTER *) NIL;
+ new_bloc->new_data = 0;
+
+ /* Record in the heap that this space is in use. */
+ heap = find_heap (new_bloc->data);
+ heap->free = break_value;
+
+ /* Maintain the correspondence between heaps and blocs. */
+ new_bloc->heap = heap;
+ heap->last_bloc = new_bloc;
+ if (heap->first_bloc == NIL_BLOC)
+ heap->first_bloc = new_bloc;
+
+ /* Put this bloc on the doubly-linked list of blocs. */
+ if (first_bloc)
+ {
+ new_bloc->prev = last_bloc;
+ last_bloc->next = new_bloc;
+ last_bloc = new_bloc;
+ }
+ else
+ {
+ first_bloc = last_bloc = new_bloc;
+ new_bloc->prev = NIL_BLOC;
+ }
+
+ return new_bloc;
+}
+
+/* Calculate new locations of blocs in the list beginning with BLOC,
+ relocating it to start at ADDRESS, in heap HEAP. If enough space is
+ not presently available in our reserve, call obtain for
+ more space.
+
+ Store the new location of each bloc in its new_data field.
+ Do not touch the contents of blocs or break_value. */
+
+static int
+relocate_blocs (bloc, heap, address)
+ bloc_ptr bloc;
+ heap_ptr heap;
+ POINTER address;
+{
+ register bloc_ptr b = bloc;
+
+ while (b)
+ {
+ /* If bloc B won't fit within HEAP,
+ move to the next heap and try again. */
+ while (heap && address + b->size > heap->end)
+ {
+ heap = heap->next;
+ if (heap == NIL_HEAP)
+ break;
+ address = heap->bloc_start;
+ }
+
+ /* If BLOC won't fit in any heap,
+ get enough new space to hold BLOC and all following blocs. */
+ if (heap == NIL_HEAP)
+ {
+ register bloc_ptr tb = b;
+ register SIZE s = 0;
+
+ /* Add up the size of all the following blocs. */
+ while (tb != NIL_BLOC)
+ {
+ s += tb->size;
+ tb = tb->next;
+ }
+
+ /* Get that space. */
+ address = obtain (address, s);
+ if (address == 0)
+ return 0;
+
+ heap = last_heap;
+ }
+
+ /* Record the new address of this bloc
+ and update where the next bloc can start. */
+ b->new_data = address;
+ address += b->size;
+ b = b->next;
+ }
+
+ return 1;
+}
+
+/* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
+ This is necessary if we put the memory of space of BLOC
+ before that of BEFORE. */
+
+static void
+reorder_bloc (bloc, before)
+ bloc_ptr bloc, before;
+{
+ bloc_ptr prev, next;
+
+ /* Splice BLOC out from where it is. */
+ prev = bloc->prev;
+ next = bloc->next;
+
+ if (prev)
+ prev->next = next;
+ if (next)
+ next->prev = prev;
+
+ /* Splice it in before BEFORE. */
+ prev = before->prev;
+
+ if (prev)
+ prev->next = bloc;
+ bloc->prev = prev;
+
+ before->prev = bloc;
+ bloc->next = before;
+}
+
+/* Update the records of which heaps contain which blocs, starting
+ with heap HEAP and bloc BLOC. */
+
+static void
+update_heap_bloc_correspondence (bloc, heap)
+ bloc_ptr bloc;
+ heap_ptr heap;
+{
+ register bloc_ptr b;
+
+ /* Initialize HEAP's status to reflect blocs before BLOC. */
+ if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
+ {
+ /* The previous bloc is in HEAP. */
+ heap->last_bloc = bloc->prev;
+ heap->free = bloc->prev->data + bloc->prev->size;
+ }
+ else
+ {
+ /* HEAP contains no blocs before BLOC. */
+ heap->first_bloc = NIL_BLOC;
+ heap->last_bloc = NIL_BLOC;
+ heap->free = heap->bloc_start;
+ }
+
+ /* Advance through blocs one by one. */
+ for (b = bloc; b != NIL_BLOC; b = b->next)
+ {
+ /* Advance through heaps, marking them empty,
+ till we get to the one that B is in. */
+ while (heap)
+ {
+ if (heap->bloc_start <= b->data && b->data <= heap->end)
+ break;
+ heap = heap->next;
+ /* We know HEAP is not null now,
+ because there has to be space for bloc B. */
+ heap->first_bloc = NIL_BLOC;
+ heap->last_bloc = NIL_BLOC;
+ heap->free = heap->bloc_start;
+ }
+
+ /* Update HEAP's status for bloc B. */
+ heap->free = b->data + b->size;
+ heap->last_bloc = b;
+ if (heap->first_bloc == NIL_BLOC)
+ heap->first_bloc = b;
+
+ /* Record that B is in HEAP. */
+ b->heap = heap;
+ }
+
+ /* If there are any remaining heaps and no blocs left,
+ mark those heaps as empty. */
+ heap = heap->next;
+ while (heap)
+ {
+ heap->first_bloc = NIL_BLOC;
+ heap->last_bloc = NIL_BLOC;
+ heap->free = heap->bloc_start;
+ heap = heap->next;
+ }
+}
+
+/* Resize BLOC to SIZE bytes. This relocates the blocs
+ that come after BLOC in memory. */
+
+static int
+resize_bloc (bloc, size)
+ bloc_ptr bloc;
+ SIZE size;
+{
+ register bloc_ptr b;
+ heap_ptr heap;
+ POINTER address;
+ SIZE old_size;
+
+ if (bloc == NIL_BLOC || size == bloc->size)
+ return 1;
+
+ for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
+ {
+ if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
+ break;
+ }
+
+ if (heap == NIL_HEAP)
+ abort ();
+
+ old_size = bloc->size;
+ bloc->size = size;
+
+ /* Note that bloc could be moved into the previous heap. */
+ address = (bloc->prev ? bloc->prev->data + bloc->prev->size
+ : first_heap->bloc_start);
+ while (heap)
+ {
+ if (heap->bloc_start <= address && address <= heap->end)
+ break;
+ heap = heap->prev;
+ }
+
+ if (! relocate_blocs (bloc, heap, address))
+ {
+ bloc->size = old_size;
+ return 0;
+ }
+
+ if (size > old_size)
+ {
+ for (b = last_bloc; b != bloc; b = b->prev)
+ {
+ safe_bcopy (b->data, b->new_data, b->size);
+ *b->variable = b->data = b->new_data;
+ }
+ safe_bcopy (bloc->data, bloc->new_data, old_size);
+ bzero (bloc->new_data + old_size, size - old_size);
+ *bloc->variable = bloc->data = bloc->new_data;
+ }
+ else
+ {
+ for (b = bloc; b != NIL_BLOC; b = b->next)
+ {
+ safe_bcopy (b->data, b->new_data, b->size);
+ *b->variable = b->data = b->new_data;
+ }
+ }
+
+ update_heap_bloc_correspondence (bloc, heap);
+
+ break_value = (last_bloc ? last_bloc->data + last_bloc->size
+ : first_heap->bloc_start);
+ return 1;
+}
+
+/* Free BLOC from the chain of blocs, relocating any blocs above it.
+ This may return space to the system. */
+
+static void
+free_bloc (bloc)
+ bloc_ptr bloc;
+{
+ heap_ptr heap = bloc->heap;
+
+ resize_bloc (bloc, 0);
+
+ if (bloc == first_bloc && bloc == last_bloc)
+ {
+ first_bloc = last_bloc = NIL_BLOC;
+ }
+ else if (bloc == last_bloc)
+ {
+ last_bloc = bloc->prev;
+ last_bloc->next = NIL_BLOC;
+ }
+ else if (bloc == first_bloc)
+ {
+ first_bloc = bloc->next;
+ first_bloc->prev = NIL_BLOC;
+ }
+ else
+ {
+ bloc->next->prev = bloc->prev;
+ bloc->prev->next = bloc->next;
+ }
+
+ /* Update the records of which blocs are in HEAP. */
+ if (heap->first_bloc == bloc)
+ {
+ if (bloc->next->heap == heap)
+ heap->first_bloc = bloc->next;
+ else
+ heap->first_bloc = heap->last_bloc = NIL_BLOC;
+ }
+ if (heap->last_bloc == bloc)
+ {
+ if (bloc->prev->heap == heap)
+ heap->last_bloc = bloc->prev;
+ else
+ heap->first_bloc = heap->last_bloc = NIL_BLOC;
+ }
+
+ relinquish ();
+ free (bloc);
+}
+
+/* Interface routines. */
+
+static int use_relocatable_buffers;
+static int r_alloc_freeze_level;
+
+/* Obtain SIZE bytes of storage from the free pool, or the system, as
+ necessary. If relocatable blocs are in use, this means relocating
+ them. This function gets plugged into the GNU malloc's __morecore
+ hook.
+
+ We provide hysteresis, never relocating by less than extra_bytes.
+
+ If we're out of memory, we should return zero, to imitate the other
+ __morecore hook values - in particular, __default_morecore in the
+ GNU malloc package. */
+
+POINTER
+r_alloc_sbrk (size)
+ long size;
+{
+ register bloc_ptr b;
+ POINTER address;
+
+ if (! use_relocatable_buffers)
+ return (*real_morecore) (size);
+
+ if (size == 0)
+ return virtual_break_value;
+
+ if (size > 0)
+ {
+ /* Allocate a page-aligned space. GNU malloc would reclaim an
+ extra space if we passed an unaligned one. But we could
+ not always find a space which is contiguos to the previous. */
+ POINTER new_bloc_start;
+ heap_ptr h = first_heap;
+ SIZE get = ROUNDUP (size);
+
+ address = (POINTER) ROUNDUP (virtual_break_value);
+
+ /* Search the list upward for a heap which is large enough. */
+ while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
+ {
+ h = h->next;
+ if (h == NIL_HEAP)
+ break;
+ address = (POINTER) ROUNDUP (h->start);
+ }
+
+ /* If not found, obtain more space. */
+ if (h == NIL_HEAP)
+ {
+ get += extra_bytes + page_size;
+
+ if (r_alloc_freeze_level > 0 || ! obtain (address, get))
+ return 0;
+
+ if (first_heap == last_heap)
+ address = (POINTER) ROUNDUP (virtual_break_value);
+ else
+ address = (POINTER) ROUNDUP (last_heap->start);
+ h = last_heap;
+ }
+
+ new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
+
+ if (first_heap->bloc_start < new_bloc_start)
+ {
+ /* Move all blocs upward. */
+ if (r_alloc_freeze_level > 0
+ || ! relocate_blocs (first_bloc, h, new_bloc_start))
+ return 0;
+
+ /* Note that (POINTER)(h+1) <= new_bloc_start since
+ get >= page_size, so the following does not destroy the heap
+ header. */
+ for (b = last_bloc; b != NIL_BLOC; b = b->prev)
+ {
+ safe_bcopy (b->data, b->new_data, b->size);
+ *b->variable = b->data = b->new_data;
+ }
+
+ h->bloc_start = new_bloc_start;
+
+ update_heap_bloc_correspondence (first_bloc, h);
+ }
+
+ if (h != first_heap)
+ {
+ /* Give up managing heaps below the one the new
+ virtual_break_value points to. */
+ first_heap->prev = NIL_HEAP;
+ first_heap->next = h->next;
+ first_heap->start = h->start;
+ first_heap->end = h->end;
+ first_heap->free = h->free;
+ first_heap->first_bloc = h->first_bloc;
+ first_heap->last_bloc = h->last_bloc;
+ first_heap->bloc_start = h->bloc_start;
+
+ if (first_heap->next)
+ first_heap->next->prev = first_heap;
+ else
+ last_heap = first_heap;
+ }
+
+ bzero (address, size);
+ }
+ else /* size < 0 */
+ {
+ SIZE excess = (char *)first_heap->bloc_start
+ - ((char *)virtual_break_value + size);
+
+ address = virtual_break_value;
+
+ if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
+ {
+ excess -= extra_bytes;
+ first_heap->bloc_start
+ = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
+
+ relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
+
+ for (b = first_bloc; b != NIL_BLOC; b = b->next)
+ {
+ safe_bcopy (b->data, b->new_data, b->size);
+ *b->variable = b->data = b->new_data;
+ }
+ }
+
+ if ((char *)virtual_break_value + size < (char *)first_heap->start)
+ {
+ /* We found an additional space below the first heap */
+ first_heap->start = (POINTER) ((char *)virtual_break_value + size);
+ }
+ }
+
+ virtual_break_value = (POINTER) ((char *)address + size);
+ break_value = (last_bloc
+ ? last_bloc->data + last_bloc->size
+ : first_heap->bloc_start);
+ if (size < 0)
+ relinquish ();
+
+ return address;
+}
+
+/* Allocate a relocatable bloc of storage of size SIZE. A pointer to
+ the data is returned in *PTR. PTR is thus the address of some variable
+ which will use the data area.
+
+ If we can't allocate the necessary memory, set *PTR to zero, and
+ return zero. */
+
+POINTER
+r_alloc (ptr, size)
+ POINTER *ptr;
+ SIZE size;
+{
+ register bloc_ptr new_bloc;
+
+ if (! r_alloc_initialized)
+ r_alloc_init ();
+
+ new_bloc = get_bloc (MEM_ROUNDUP (size));
+ if (new_bloc)
+ {
+ new_bloc->variable = ptr;
+ *ptr = new_bloc->data;
+ }
+ else
+ *ptr = 0;
+
+ return *ptr;
+}
+
+/* Free a bloc of relocatable storage whose data is pointed to by PTR.
+ Store 0 in *PTR to show there's no block allocated. */
+
+void
+r_alloc_free (ptr)
+ register POINTER *ptr;
+{
+ register bloc_ptr dead_bloc;
+
+ dead_bloc = find_bloc (ptr);
+ if (dead_bloc == NIL_BLOC)
+ abort ();
+
+ free_bloc (dead_bloc);
+ *ptr = 0;
+}
+
+/* Given a pointer at address PTR to relocatable data, resize it to SIZE.
+ Do this by shifting all blocks above this one up in memory, unless
+ SIZE is less than or equal to the current bloc size, in which case
+ do nothing.
+
+ Change *PTR to reflect the new bloc, and return this value.
+
+ If more memory cannot be allocated, then leave *PTR unchanged, and
+ return zero. */
+
+POINTER
+r_re_alloc (ptr, size)
+ POINTER *ptr;
+ SIZE size;
+{
+ register bloc_ptr bloc;
+
+ bloc = find_bloc (ptr);
+ if (bloc == NIL_BLOC)
+ abort ();
+
+ if (size <= bloc->size)
+ /* Wouldn't it be useful to actually resize the bloc here? */
+ return *ptr;
+
+ if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
+ return 0;
+
+ return *ptr;
+}
+
+/* Disable relocations, after making room for at least SIZE bytes
+ of non-relocatable heap if possible. The relocatable blocs are
+ guaranteed to hold still until thawed, even if this means that
+ malloc must return a null pointer. */
+
+void
+r_alloc_freeze (size)
+ long size;
+{
+ /* If already frozen, we can't make any more room, so don't try. */
+ if (r_alloc_freeze_level > 0)
+ size = 0;
+ /* If we can't get the amount requested, half is better than nothing. */
+ while (size > 0 && r_alloc_sbrk (size) == 0)
+ size /= 2;
+ ++r_alloc_freeze_level;
+ if (size > 0)
+ r_alloc_sbrk (-size);
+}
+
+void
+r_alloc_thaw ()
+{
+ if (--r_alloc_freeze_level < 0)
+ abort ();
+}
+
+/* The hook `malloc' uses for the function which gets more space
+ from the system. */
+extern POINTER (*__morecore) ();
+
+/* Initialize various things for memory allocation. */
+
+static void
+r_alloc_init ()
+{
+ if (r_alloc_initialized)
+ return;
+
+ r_alloc_initialized = 1;
+ real_morecore = __morecore;
+ __morecore = r_alloc_sbrk;
+
+ first_heap = last_heap = &heap_base;
+ first_heap->next = first_heap->prev = NIL_HEAP;
+ first_heap->start = first_heap->bloc_start
+ = virtual_break_value = break_value = (*real_morecore) (0);
+ if (break_value == NIL)
+ abort ();
+
+ page_size = PAGE;
+ extra_bytes = ROUNDUP (50000);
+
+ first_heap->end = (POINTER) ROUNDUP (first_heap->start);
+
+ /* The extra call to real_morecore guarantees that the end of the
+ address space is a multiple of page_size, even if page_size is
+ not really the page size of the system running the binary in
+ which page_size is stored. This allows a binary to be built on a
+ system with one page size and run on a system with a smaller page
+ size. */
+ (*real_morecore) (first_heap->end - first_heap->start);
+
+ /* Clear the rest of the last page; this memory is in our address space
+ even though it is after the sbrk value. */
+ /* Doubly true, with the additional call that explicitly adds the
+ rest of that page to the address space. */
+ bzero (first_heap->start, first_heap->end - first_heap->start);
+ virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
+ use_relocatable_buffers = 1;
+}
+#ifdef DEBUG
+#include <assert.h>
+
+int
+r_alloc_check ()
+{
+ int found = 0;
+ heap_ptr h, ph = 0;
+ bloc_ptr b, pb = 0;
+
+ if (!r_alloc_initialized)
+ return;
+
+ assert (first_heap);
+ assert (last_heap->end <= (POINTER) sbrk (0));
+ assert ((POINTER) first_heap < first_heap->start);
+ assert (first_heap->start <= virtual_break_value);
+ assert (virtual_break_value <= first_heap->end);
+
+ for (h = first_heap; h; h = h->next)
+ {
+ assert (h->prev == ph);
+ assert ((POINTER) ROUNDUP (h->end) == h->end);
+ assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
+ assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
+ assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
+
+ if (ph)
+ {
+ assert (ph->end < h->start);
+ assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
+ }
+
+ if (h->bloc_start <= break_value && break_value <= h->end)
+ found = 1;
+
+ ph = h;
+ }
+
+ assert (found);
+ assert (last_heap == ph);
+
+ for (b = first_bloc; b; b = b->next)
+ {
+ assert (b->prev == pb);
+ assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
+ assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
+
+ ph = 0;
+ for (h = first_heap; h; h = h->next)
+ {
+ if (h->bloc_start <= b->data && b->data + b->size <= h->end)
+ break;
+ ph = h;
+ }
+
+ assert (h);
+
+ if (pb && pb->data + pb->size != b->data)
+ {
+ assert (ph && b->data == h->bloc_start);
+ while (ph)
+ {
+ if (ph->bloc_start <= pb->data
+ && pb->data + pb->size <= ph->end)
+ {
+ assert (pb->data + pb->size + b->size > ph->end);
+ break;
+ }
+ else
+ {
+ assert (ph->bloc_start + b->size > ph->end);
+ }
+ ph = ph->prev;
+ }
+ }
+ pb = b;
+ }
+
+ assert (last_bloc == pb);
+
+ if (last_bloc)
+ assert (last_bloc->data + last_bloc->size == break_value);
+ else
+ assert (first_heap->bloc_start == break_value);
+}
+#endif /* DEBUG */
diff --git a/external/gpl2/libmalloc/dist/realloc.c b/external/gpl2/libmalloc/dist/realloc.c
new file mode 100644
index 00000000000..41bc344c921
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/realloc.c
@@ -0,0 +1,221 @@
+/* $NetBSD: realloc.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Change the size of a block allocated by `malloc'.
+ Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+ Written May 1989 by Mike Haertel.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+#if (defined (MEMMOVE_MISSING) || \
+ !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
+
+/* Snarfed directly from Emacs src/dispnew.c:
+ XXX Should use system bcopy if it handles overlap. */
+#ifndef emacs
+
+/* Like bcopy except never gets confused by overlap. */
+
+static void
+safe_bcopy (from, to, size)
+ char *from, *to;
+ int size;
+{
+ if (size <= 0 || from == to)
+ return;
+
+ /* If the source and destination don't overlap, then bcopy can
+ handle it. If they do overlap, but the destination is lower in
+ memory than the source, we'll assume bcopy can handle that. */
+ if (to < from || from + size <= to)
+ bcopy (from, to, size);
+
+ /* Otherwise, we'll copy from the end. */
+ else
+ {
+ register char *endf = from + size;
+ register char *endt = to + size;
+
+ /* If TO - FROM is large, then we should break the copy into
+ nonoverlapping chunks of TO - FROM bytes each. However, if
+ TO - FROM is small, then the bcopy function call overhead
+ makes this not worth it. The crossover point could be about
+ anywhere. Since I don't think the obvious copy loop is too
+ bad, I'm trying to err in its favor. */
+ if (to - from < 64)
+ {
+ do
+ *--endt = *--endf;
+ while (endf != from);
+ }
+ else
+ {
+ for (;;)
+ {
+ endt -= (to - from);
+ endf -= (to - from);
+
+ if (endt < to)
+ break;
+
+ bcopy (endf, endt, to - from);
+ }
+
+ /* If SIZE wasn't a multiple of TO - FROM, there will be a
+ little left over. The amount left over is
+ (endt + (to - from)) - to, which is endt - from. */
+ bcopy (from, to, endt - from);
+ }
+ }
+}
+#endif /* Not emacs. */
+
+#define memmove(to, from, size) safe_bcopy ((from), (to), (size))
+
+#endif
+
+
+#define min(A, B) ((A) < (B) ? (A) : (B))
+
+/* Debugging hook for realloc. */
+__ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
+
+/* Resize the given region to the new size, returning a pointer
+ to the (possibly moved) region. This is optimized for speed;
+ some benchmarks seem to indicate that greater compactness is
+ achieved by unconditionally allocating and copying to a
+ new region. This module has incestuous knowledge of the
+ internals of both free and malloc. */
+__ptr_t
+realloc (ptr, size)
+ __ptr_t ptr;
+ __malloc_size_t size;
+{
+ __ptr_t result;
+ int type;
+ __malloc_size_t block, blocks, oldlimit;
+
+ if (size == 0)
+ {
+ free (ptr);
+ return malloc (0);
+ }
+ else if (ptr == NULL)
+ return malloc (size);
+
+ if (__realloc_hook != NULL)
+ return (*__realloc_hook) (ptr, size);
+
+ block = BLOCK (ptr);
+
+ type = _heapinfo[block].busy.type;
+ switch (type)
+ {
+ case 0:
+ /* Maybe reallocate a large block to a small fragment. */
+ if (size <= BLOCKSIZE / 2)
+ {
+ result = malloc (size);
+ if (result != NULL)
+ {
+ memcpy (result, ptr, size);
+ _free_internal (ptr);
+ return result;
+ }
+ }
+
+ /* The new size is a large allocation as well;
+ see if we can hold it in place. */
+ blocks = BLOCKIFY (size);
+ if (blocks < _heapinfo[block].busy.info.size)
+ {
+ /* The new size is smaller; return
+ excess memory to the free list. */
+ _heapinfo[block + blocks].busy.type = 0;
+ _heapinfo[block + blocks].busy.info.size
+ = _heapinfo[block].busy.info.size - blocks;
+ _heapinfo[block].busy.info.size = blocks;
+ /* We have just created a new chunk by splitting a chunk in two.
+ Now we will free this chunk; increment the statistics counter
+ so it doesn't become wrong when _free_internal decrements it. */
+ ++_chunks_used;
+ _free_internal (ADDRESS (block + blocks));
+ result = ptr;
+ }
+ else if (blocks == _heapinfo[block].busy.info.size)
+ /* No size change necessary. */
+ result = ptr;
+ else
+ {
+ /* Won't fit, so allocate a new region that will.
+ Free the old region first in case there is sufficient
+ adjacent free space to grow without moving. */
+ blocks = _heapinfo[block].busy.info.size;
+ /* Prevent free from actually returning memory to the system. */
+ oldlimit = _heaplimit;
+ _heaplimit = 0;
+ _free_internal (ptr);
+ _heaplimit = oldlimit;
+ result = malloc (size);
+ if (result == NULL)
+ {
+ /* Now we're really in trouble. We have to unfree
+ the thing we just freed. Unfortunately it might
+ have been coalesced with its neighbors. */
+ if (_heapindex == block)
+ (void) malloc (blocks * BLOCKSIZE);
+ else
+ {
+ __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
+ (void) malloc (blocks * BLOCKSIZE);
+ _free_internal (previous);
+ }
+ return NULL;
+ }
+ if (ptr != result)
+ memmove (result, ptr, blocks * BLOCKSIZE);
+ }
+ break;
+
+ default:
+ /* Old size is a fragment; type is logarithm
+ to base two of the fragment size. */
+ if (size > (__malloc_size_t) (1 << (type - 1)) &&
+ size <= (__malloc_size_t) (1 << type))
+ /* The new size is the same kind of fragment. */
+ result = ptr;
+ else
+ {
+ /* The new size is different; allocate a new space,
+ and copy the lesser of the new size and the old. */
+ result = malloc (size);
+ if (result == NULL)
+ return NULL;
+ memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
+ free (ptr);
+ }
+ break;
+ }
+
+ return result;
+}
diff --git a/external/gpl2/libmalloc/dist/valloc.c b/external/gpl2/libmalloc/dist/valloc.c
new file mode 100644
index 00000000000..48f977c389c
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/valloc.c
@@ -0,0 +1,48 @@
+/* $NetBSD: valloc.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Allocate memory on a page boundary.
+ Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+This library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with this library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.
+
+ The author may be reached (Email) at the address mike@ai.mit.edu,
+ or (US mail) as Mike Haertel c/o Free Software Foundation. */
+
+#if defined (__GNU_LIBRARY__) || defined (_LIBC)
+#include <stddef.h>
+#include <sys/cdefs.h>
+extern size_t __getpagesize __P ((void));
+#else
+#include "getpagesize.h"
+#define __getpagesize() getpagesize()
+#endif
+
+#ifndef _MALLOC_INTERNAL
+#define _MALLOC_INTERNAL
+#include <malloc.h>
+#endif
+
+static __malloc_size_t pagesize;
+
+__ptr_t
+valloc (size)
+ __malloc_size_t size;
+{
+ if (pagesize == 0)
+ pagesize = __getpagesize ();
+
+ return memalign (pagesize, size);
+}
diff --git a/external/gpl2/libmalloc/dist/vm-limit.c b/external/gpl2/libmalloc/dist/vm-limit.c
new file mode 100644
index 00000000000..f00449a0bff
--- /dev/null
+++ b/external/gpl2/libmalloc/dist/vm-limit.c
@@ -0,0 +1,137 @@
+/* $NetBSD: vm-limit.c,v 1.1.1.1 2016/01/13 21:42:18 christos Exp $ */
+
+/* Functions for memory limit warnings.
+ Copyright (C) 1990, 1992 Free Software Foundation, Inc.
+
+
+This file is part of the GNU C Library. Its master source is NOT part of
+the C library, however. The master source lives in /gd/gnu/lib.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#ifdef emacs
+#include <config.h>
+#include "lisp.h"
+#endif
+
+#ifndef emacs
+#include <stddef.h>
+typedef size_t SIZE;
+typedef void *POINTER;
+#define EXCEEDS_LISP_PTR(x) 0
+#endif
+
+#include "mem-limits.h"
+
+/*
+ Level number of warnings already issued.
+ 0 -- no warnings issued.
+ 1 -- 75% warning already issued.
+ 2 -- 85% warning already issued.
+ 3 -- 95% warning issued; keep warning frequently.
+*/
+static int warnlevel;
+
+/* Function to call to issue a warning;
+ 0 means don't issue them. */
+static void (*warn_function) ();
+
+/* Get more memory space, complaining if we're near the end. */
+
+static void
+check_memory_limits ()
+{
+ extern POINTER (*__morecore) ();
+
+ register POINTER cp;
+ unsigned long five_percent;
+ unsigned long data_size;
+
+ if (lim_data == 0)
+ get_lim_data ();
+ five_percent = lim_data / 20;
+
+ /* Find current end of memory and issue warning if getting near max */
+ cp = (char *) (*__morecore) (0);
+ data_size = (char *) cp - (char *) data_space_start;
+
+ if (warn_function)
+ switch (warnlevel)
+ {
+ case 0:
+ if (data_size > five_percent * 15)
+ {
+ warnlevel++;
+ (*warn_function) ("Warning: past 75% of memory limit");
+ }
+ break;
+
+ case 1:
+ if (data_size > five_percent * 17)
+ {
+ warnlevel++;
+ (*warn_function) ("Warning: past 85% of memory limit");
+ }
+ break;
+
+ case 2:
+ if (data_size > five_percent * 19)
+ {
+ warnlevel++;
+ (*warn_function) ("Warning: past 95% of memory limit");
+ }
+ break;
+
+ default:
+ (*warn_function) ("Warning: past acceptable memory limits");
+ break;
+ }
+
+ /* If we go down below 70% full, issue another 75% warning
+ when we go up again. */
+ if (data_size < five_percent * 14)
+ warnlevel = 0;
+ /* If we go down below 80% full, issue another 85% warning
+ when we go up again. */
+ else if (warnlevel > 1 && data_size < five_percent * 16)
+ warnlevel = 1;
+ /* If we go down below 90% full, issue another 95% warning
+ when we go up again. */
+ else if (warnlevel > 2 && data_size < five_percent * 18)
+ warnlevel = 2;
+
+ if (EXCEEDS_LISP_PTR (cp))
+ (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
+}
+
+/* Cause reinitialization based on job parameters;
+ also declare where the end of pure storage is. */
+
+void
+memory_warnings (start, warnfun)
+ POINTER start;
+ void (*warnfun) ();
+{
+ extern void (* __after_morecore_hook) (); /* From gmalloc.c */
+
+ if (start)
+ data_space_start = start;
+ else
+ data_space_start = start_of_data ();
+
+ warn_function = warnfun;
+ __after_morecore_hook = check_memory_limits;
+}