summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
Commit message (Collapse)AuthorAgeFilesLines
* MFC r204636:Jaakko Heinonen2010-08-281-1/+8
| | | | | | | | | | | In reallocf(3), free the memory only when size != 0. Otherwise, when the System V compatibility option (malloc "V" flag) is in effect a zero sized reallocf() could cause a double free. PR: bin/141753 Notes: svn path=/stable/7/; revision=211919
* MFC: r207946Christian Brueffer2010-07-041-3/+2
| | | | | | | | The previous revision actually removed the bug being hinted at, so go ahead and remove the hint. Notes: svn path=/stable/7/; revision=209699
* MFC r185514 (by jasone):Konstantin Belousov2009-05-031-11/+37
| | | | | | | | | | Fix a lock order reversal bug that could cause deadlock during fork(2). Reported and tested by: makc Approved by: re (kensmith) Notes: svn path=/stable/7/; revision=191767
* Mostly synchronize lib/libthr and sys/kern/kern_umtx.c with the codeKonstantin Belousov2009-03-242-0/+12
| | | | | | | | | | | | | | | | | | | | | | from HEAD. Since libkse is still built on RELENG_7, pthread_cleanup_push/pop are left as the functions, but the support code in libthr is present for the macro versions. Malloc in RELENG_7 does not require thread exit hook, but I decided to add empty handler for it, instead of removing callback from thr_exit(). No mergeinfo since this change is prepared by patching libthr and then bringing in required missed bits. Requested by: bms Reviewed by: davidxu Tested by: bms, Mykola Dzham <i levsha org ua> Approved by: re (kensmith) Notes: svn path=/stable/7/; revision=190393
* MFC:Jason Evans2008-11-102-13/+18
| | | | | | | | | | | Revert to preferring mmap(2) over sbrk(2) when mapping memory, due to potential extreme contention in the kernel for multi-threaded applications on SMP systems. Approved by: re (kib) Notes: svn path=/stable/7/; revision=184819
* MFC r183565:Ed Schouten2008-10-262-9/+10
| | | | | | | | | | | | | | | | | | | | | | | | | Small cleanups to openpty(). - Pass O_NOCTTY to posix_openpt(2). This makes the implementation work consistently on implementations that make the PTY the controlling TTY by default. - Call unlockpt() before opening the slave device. POSIX mentions that de slave device should only be opened after grantpt() and unlockpt() have been called. - Replace some redundant code by a label. As a safety net, add a call to revoke() to unlockpt(). All applications out there use openpty(), explicitly call revoke() or implement their own PTY allocation routines. Adding the call to unlockpt() won't hurt, but will prevent foot-shooting. Reviewed by: jhb, kib Approved by: re Notes: svn path=/stable/7/; revision=184300
* MFC allocator improvements and fixes:Jason Evans2008-08-161-400/+341
| | | | | | | | | | | | | | * Enhance the chunk map to support run coalescing, and substantially reduce the number of red-black tree operations performed. * Remove unused code. * Fix arena_run_reg_dalloc() to use the entire precomputed division table. * Improve lock preemption performance for hyperthreaded CPUs. Notes: svn path=/stable/7/; revision=181788
* MFC: r181150, r181266Sean Farley2008-08-081-13/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | Detect if the application has cleared the environ variable by setting the first value (environ[0]) to NULL. This is in addition to the current detection of environ being replaced, which includes being set to NULL. Without this fix, the environment is not truly wiped, but appears to be by getenv() until an *env() call is made to alter the enviroment. This change is necessary to support those applications that use this method for clearing environ such as Dovecot and Postfix. Applications such as Sendmail and the base system's env replace environ (already detected). While neither of these methods are defined by SUSv3, it is best to support them due to historic reasons and in lieu of a clean, defined method. Add extra units tests for clearing environ using four different methods: 1. Set environ to NULL pointer. 2. Set environ[0] to NULL pointer. 3. Set environ to calloc()'d NULL-terminated array. 4. Set environ to static NULL-terminated array. Noticed by: Timo Sirainen Reviewed by: ache Notes: svn path=/stable/7/; revision=181407
* MFC r180323:Daniel Gerzo2008-07-111-3/+3
| | | | | | | | - remove superfluous word - remove contractions Notes: svn path=/stable/7/; revision=180444
* MFC r180325 and r180329:Daniel Gerzo2008-07-111-3/+35
| | | | | | | | | | | | - This code was intially obtained from NetBSD, but it's missing licence statement. Add the one from the current NetBSD version. - Add description about a missing return value [1] PR: docs/75995 Submitted by: Tarc <tarc@po.cs.msu.su> Notes: svn path=/stable/7/; revision=180441
* MFC rev. 180024:Daniel Gerzo2008-06-301-1/+1
| | | | | | | | | | - Mark the section describing return values with an appropriate section flag. PR: docs/122818 Notes: svn path=/stable/7/; revision=180117
* MFC allocator improvements and fixes:Jason Evans2008-06-162-161/+1155
| | | | | | | | | | | | | | | | | | | | * Implement more compact red-black trees, thus reducing memory usage by ~0.5-1%. * Add a separate tree to track dirty-page-containing chunks, thus improving worst case allocation performance. * Fix a deadlock in base_alloc() for the error (OOM) path. * Catch integer overflow for huge allocations when using sbrk(2). * Fix bit vector initialization for run headers. This fix has no practical impact for correct programs. Incorrect programs will potentially experience allocation failures rather than memory corruption, both of which are "undefined behavior". Notes: svn path=/stable/7/; revision=179836
* MFC: 1.16: Use calloc() instead of zeroing memory ourselves.Ruslan Ermilov2008-04-221-3/+13
| | | | | | | | 1.17: Better strfmon(3) conversion specifiers sanity checking. 1.18: Fix memory leaks. Notes: svn path=/stable/7/; revision=178415
* MFC: Merge malloc(3) improvements and fixes. The highlights are:Jason Evans2008-03-072-1260/+2261
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Avoid re-zeroing memory in calloc() when possible. * Use pthread mutexes where possible instead of libc "spinlocks", and actually spin some during contention before blocking. * Implement dynamic load balancing of thread-->arena mapping. * Avoid floating point math in order to avoid increased context switch overhead for applications that otherwise would not use floating point math. * Restructure how sbrk() and mmap() are used to acquire memory mappings. This provides a way to force malloc to only use sbrk(), which can be useful in the context of resource limits. * Reduce the number of mmap() calls typically necessary when allocating a chunk. * Track dirty unused pages so that they can be purged if they exceed a threshold. * Try to realloc() large objects in place. * Manage page runs with trees instead of chunk maps, which allows logarithmic-time run allocation. Notes: svn path=/stable/7/; revision=176922
* MFC:Sean Farley2008-03-061-12/+36
| | | | | | | | | | | | | | | | | | | | | Replace the use of warnx() with direct output to stderr using _write(). This reduces the size of a statically-linked binary by approximately 100KB in a trivial "return (0)" test application. readelf -S was used to verify that the .text section was reduced and that using strlen() saved a few more bytes over using sizeof(). Since the section of code is only called when environ is corrupt (program bug), I went with fewer bytes over fewer cycles. I made minor edits to the submitted patch to make the output resemble warnx(). src/lib/libc/stdlib/getenv.c: rev 1.12 -> 1.13 Submitted by: kib bz Approved by: wes (mentor) Notes: svn path=/stable/7/; revision=176876
* MFC qsort.c,v 1.14-1.15, heapsort.c,v 1.6David Schultz2008-01-262-6/+8
| | | | | | | | | Use size_t to avoid overflow when sorting arrays larger than 2 GB. PR: 111085 Notes: svn path=/stable/7/; revision=175674
* MFC: Update openpty(3) manpage for the recent pty fixes and remove someJohn Baldwin2008-01-231-12/+1
| | | | | | | now-unused macros from grantpt.c. Notes: svn path=/stable/7/; revision=175603
* Fix issues which allow snooping on ptys. [08:01]Colin Percival2008-01-141-37/+35
| | | | | | | | | | | Fix an off-by-one error in inet_network(3). [08:02] Security: FreeBSD-SA-08:01.pty Security: FreeBSD-SA-08:02.libc Approved by: re (kensmith) Notes: svn path=/stable/7/; revision=175335
* MFC: Clean up some of the pts(4) vs pty(4) stuff in grantpt(3) and friends.John Baldwin2007-12-311-22/+23
| | | | | | | Approved by: re (gnn) Notes: svn path=/stable/7/; revision=175016
* MFC: Update posix_openpt(3) to handle 512 ptys.John Baldwin2007-12-181-2/+2
| | | | | | | Approved by: re (gnn) Notes: svn path=/stable/7/; revision=174730
* Disable support for /dev/ptmx in FreeBSD 7.0. While it's a good idea inRobert Watson2007-12-061-0/+4
| | | | | | | | | | | | | | | | | | | principle, the current devfs-based cloning facilit makes the current user<->kernel protocol for allocating new pts nodes difficult or impossible to do without leading to resource leaks or race conditions. We tentatively plan to reintroduce ptmx/pts support in FreeBSD 7.1 once a fixed version has settled out in HEAD. People who really want to run with the current code can re-enable it but it will require tweaking conf files and unifdef'ing it. By removing it now before the release, we avoid having to support the current allocation protocol in future releases in order to provide backwards compatibility with older libc's. Discussed with: cognet, erwin Approved by: re (kensmith) Notes: svn path=/stable/7/; revision=174352
* Turn on MALLOC_PRODUCTION which turns off some stuff used for debuggingKen Smith2007-10-111-1/+1
| | | | | | | | | | support. Reminded by: kris Approved by: re (implicit) Notes: svn path=/stable/7/; revision=172538
* The precision for a string argument in a call to warnx() needs to be castSean Farley2007-09-221-1/+2
| | | | | | | | | | | | to an int to remove the warning from using a size_t variable on 64-bit platforms. Submitted by: Xin LI <delphij@FreeBSD.org> Approved by: wes Approved by: re (kensmith) Notes: svn path=/head/; revision=172294
* Skip rebuilding environ in setenv() only upon reuse of an active variable;Sean Farley2007-09-151-2/+2
| | | | | | | | | | | | | | inactive variables should cause a rebuild of environ, otherwise, exec()'d processes will be missing a variable in environ that has been unset then set. Submitted by: Taku Yamamoto <taku@tackymt.homeip.net> Reviewed by: ache Approved by: wes (mentor) Approved by: re (kensmith) Notes: svn path=/head/; revision=172191
* Added environ-replacement detection. For programs that "clean" (i.e., su)Sean Farley2007-07-201-78/+163
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | or replace (i.e., zdump) the environment after a call to setenv(), putenv() or unsetenv() has been made, a few changes were made. - getenv() will return the value from the new environ array. - setenv() was split into two functions: __setenv() which is most of the previous setenv() without checks on the name and setenv() which contains the checks before calling __setenv(). - setenv(), putenv() and unsetenv() will unset all previous values and call __setenv() on all entries in the new environ array which in turn adds them to the end of the envVars array. Calling __setenv() instead of setenv() is done to avoid the temporary replacement of the '=' in a string with a NUL byte. Some strings may be read-only data. Added more regression checks for clearing the environment array. Replaced gettimeofday() with getrusage() in timing regression check for better accuracy. Fixed an off-by-one bug in __remove_putenv() in the use of memmove(). This went unnoticed due to the allocation of double the number of environ entries when building envVars. Fixed a few spelling mistakes in the comments. Reviewed by: ache Approved by: wes Approved by: re (kensmith) Notes: svn path=/head/; revision=171525
* Significantly reduce the memory leak as noted in BUGS section forSean Farley2007-07-045-259/+622
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | setenv(3) by tracking the size of the memory allocated instead of using strlen() on the current value. Convert all calls to POSIX from historic BSD API: - unsetenv returns an int. - putenv takes a char * instead of const char *. - putenv no longer makes a copy of the input string. - errno is set appropriately for POSIX. Exceptions involve bad environ variable and internal initialization code. These both set errno to EFAULT. Several patches to base utilities to handle the POSIX changes from Andrey Chernov's previous commit. A few I re-wrote to use setenv() instead of putenv(). New regression module for tools/regression/environ to test these functions. It also can be used to test the performance. Bump __FreeBSD_version to 700050 due to API change. PR: kern/99826 Approved by: wes Approved by: re (kensmith) Notes: svn path=/head/; revision=171195
* Add information about the implications of using mmap(2) instead of sbrk(2).Jason Evans2007-06-151-1/+19
| | | | | | | Submitted by: bmah, jhb Notes: svn path=/head/; revision=170798
* Fix junk/zero filling for realloc(). Junk filling was missing in one case,Jason Evans2007-06-151-36/+48
| | | | | | | | | and zero filling was broken in a way that could cause memory corruption. Update comments. Notes: svn path=/head/; revision=170796
* Backout 1.5 as requested by deischenJonathan Chen2007-05-221-1/+0
| | | | Notes: svn path=/head/; revision=169850
* __cleanup() is needed for ports/devel/valgrind, export it.Jonathan Chen2007-05-221-0/+1
| | | | Notes: svn path=/head/; revision=169847
* Back out all POSIXified *env() changes.Andrey A. Chernov2007-05-014-96/+41
| | | | | | | | | | | | Not because I admit they are technically wrong and not because of bug reports (I receive nothing). But because I surprisingly meets so strong opposition and resistance so lost any desire to continue that. Anyone who interested in POSIX can dig out what changes and how through cvs diffs. Notes: svn path=/head/; revision=169177
* Bump .DdAndrey A. Chernov2007-04-301-1/+1
| | | | | | | Suggested by: Henrik Brix Andersen <henrik@brixandersen.dk> Notes: svn path=/head/; revision=169146
* Add phraseAndrey A. Chernov2007-04-301-1/+2
| | | | | | | | "so altering the argument shall change the environment." into putenv description. Notes: svn path=/head/; revision=169142
* Make putenv() fully conforms to Open Group specs Issue 6Andrey A. Chernov2007-04-304-26/+58
| | | | | | | | | | | | | | (also IEEE Std 1003.1-2001) The specs explicitly says that altering passed string should change the environment, i.e. putenv() directly puts its arg into environment (unlike setenv() which just copies it there). It means that putenv() can't be implemented via setenv() (like we have before) at all. Putenv() value lives (allows modifying) up to the next putenv() or setenv() call. Notes: svn path=/head/; revision=169138
* Remove special case skipping initial '=' of the setenv() value "forAndrey A. Chernov2007-04-302-7/+0
| | | | | | | | | | | | compatibility with the different environment conventions" (man page). With the standards, we don't have them different anymore and IEEE Std 1003.1-2001 says that "The values that the environment variables may be assigned are not restricted except that they are considered to end with a null byte" Notes: svn path=/head/; revision=169111
* Make setenv, putenv, getenv and unsetenv conforming to Open Group specsAndrey A. Chernov2007-04-304-19/+48
| | | | | | | | | | | | Issue 6 (also IEEE Std 1003.1-2001) in following areas: args, return, errors. Putenv still needs rewriting because specs explicitly says that altering passed string later should change the environment (currently we copy the string so can't provide that). Notes: svn path=/head/; revision=169109
* Use C comments since we now preprocess these files with CPP.Daniel Eischen2007-04-291-2/+4
| | | | Notes: svn path=/head/; revision=169092
* Swap "underflow"/"overflow" in the table header.Ruslan Ermilov2007-04-101-2/+2
| | | | | | | | Submitted by: Ricardo Nabinger Sanchez MFC after: 3 days Notes: svn path=/head/; revision=168578
* Use size_t instead of unsigned for pagesize-related values, in order toJason Evans2007-03-291-4/+8
| | | | | | | | | | | | avoid downcasting issues. In particular, this change fixes posix_memalign(3) for alignments greater than 2^31 on LP64 systems. Make sure that NDEBUG is always set to be compatible with MALLOC_DEBUG. [1] Reported by: [1] Lee Hyo geol <hyogeollee@gmail.com> Notes: svn path=/head/; revision=168029
* Remove the run promotion/demotion machinery. Replace it with red-blackJason Evans2007-03-281-430/+219
| | | | | | | | | | | | | | | | | | | | | | | | | | trees that track all non-full runs for each bin. Use the red-black trees to be able to guarantee that each new allocation is placed in the lowest address available in any non-full run. This change completes the transition to allocating from low addresses in order to reduce the retention of sparsely used chunks. If the run in current use by a bin becomes empty, deallocate the run rather than retaining it for later use. The previous behavior had the tendency to spread empty runs across multiple chunks, thus preventing the release of chunks that were completely unused. Generalize base_chunk_alloc() (and rename it to base_pages_alloc()) to handle allocation sizes larger than the chunk size, so that it is possible to support chunk sizes that are smaller than an arena object. Reduce the minimum chunk size from 64kB to 8kB. Optimize tracking of addresses for deleted chunks. Fix a statistics bug for huge allocations. Notes: svn path=/head/; revision=168003
* Update the IMPLEMENTATION NOTES section to reflect recent mallocJason Evans2007-03-281-13/+30
| | | | | | | enhancements. Notes: svn path=/head/; revision=167969
* Add a HISTORY section.Jason Evans2007-03-281-0/+5
| | | | Notes: svn path=/head/; revision=167967
* Fix some subtle bugs for posix_memalign() having to do with integerJason Evans2007-03-241-18/+43
| | | | | | | | | | | rounding and overflow. Carefully document what the various overflow tests actually detect. The bugs mostly canceled out, such that the worst possible failure cases resulted in non-fatal over-allocations. Notes: svn path=/head/; revision=167872
* Fix posix_memalign() for large objects. Now that runs are extents ratherJason Evans2007-03-231-151/+297
| | | | | | | | | | | than binary buddies, the alignment guarantees are weaker, which requires a more complex aligned allocation algorithm, similar to that used for alignment greater than the chunk size. Reported by: matteo Notes: svn path=/head/; revision=167853
* Use extents rather than binary buddies to track free pages withinJason Evans2007-03-231-323/+332
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | chunks. This allows runs to be any multiple of the page size. The primary advantage is that large objects are no longer constrained to be 2^n pages, which can dramatically decrease internal fragmentation for large objects. This also allows the sizes for runs that back small objects to be more finely tuned. Free runs are searched for linearly using the chunk page map (with the help of some heuristic optimizations). This changes the allocation policy from "first best fit" to "first fit". A prototype red-black tree implementation for tracking free runs that implemented "first best fit" did not cause a measurable speed or memory usage difference for realistic chunk sizes (though of course it is possible to construct benchmarks that favor one allocation policy over another). Refine the handling of fullness constraints for small runs to be more tunable. Restructure the per chunk page map to contain only two fields per entry, rather than four. Also, increase each entry from 4 to 8 bytes, since it allows for 32-bit integers, without increasing the number of chunk header pages. Relax the maximum chunk size constraint. This is of no practical interest; it is merely fallout from the chunk page map restructuring. Revamp statistics gathering and reporting to be faster, clearer and more informative. Statistics gathering is fast enough now to have little to no impact on application speed, but it still requires approximately two extra pages of memory per arena (per process). This memory overhead may be acceptable for most systems, but we still need to leave statistics gathering disabled by default in RELENG branches. Rename NO_MALLOC_EXTRAS to MALLOC_PRODUCTION in order to make its intent clearer (i.e. it should be defined in RELENG branches). Notes: svn path=/head/; revision=167828
* Avoid using vsnprintf(3) unless MALLOC_STATS is defined, in order toJason Evans2007-03-201-152/+233
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | avoid substantial potential bloat for static binaries that do not otherwise use any printf(3)-family functions. [1] Rearrange arena_run_t so that the region bitmask can be minimally sized according to constraints related to each bin's size class. Previously, the region bitmask was the same size for all run headers, which wasted a measurable amount of memory. Rather than making runs for small objects as large as possible, make runs as small as possible such that header overhead stays below a certain bound. There are two exceptions that override the header overhead bound: 1) If the bound is impossible to honor, it is relaxed on a per-size-class basis. Since there is one bit of header overhead per object (plus a constant), it is impossible to achieve a header overhead less than or equal to 1/(# of bits per object). For the current setting of maximum 0.5% header overhead, this relaxation comes into play for {2, 4, 8, 16}-byte objects, for which header overhead is (on 64-bit systems) {7.1, 4.3, 2.2, 1.2}%, respectively. 2) There is still a cap on small run size, still set to 64kB. This comes into play for {1024, 2048}-byte objects, for which header overhead is {1.6, 3.1}%, respectively. In practice, this reduces the run sizes, which makes worst case low-water memory usage due to fragmentation less bad. It also reduces worst case high-water run fragmentation due to non-full runs, but this is only a constant improvement (most important to small short-lived processes). Reduce the default chunk size from 2MB to 1MB. Benchmarks indicate that the external fragmentation reduction makes 1MB the new sweet spot (as small as possible without adversely affecting performance). Reported by: [1] kientzle Notes: svn path=/head/; revision=167733
* Modify chunk_alloc() to prefer mmap()ed memory over sbrk()ed memory.Jason Evans2007-02-221-36/+40
| | | | | | | | | | | | | | This has no impact unless USE_BRK is defined (32-bit platforms), in which case user allocations are allocated via mmap() if at all possible, in order to avoid the possibility of unreclaimable chunks in the data segment. Fix an obscure bug in base_alloc() that could have allowed undefined behavior if an application were to use sbrk() in conjunction with a USE_BRK-enabled malloc. Notes: svn path=/head/; revision=166890
* Fix a utrace(2)-related bug in calloc(3).Jason Evans2007-01-311-44/+56
| | | | | | | | | Integrate various pedantic cleanups. Submitted by: Andrew Doran <ad@netbsd.org> Notes: svn path=/head/; revision=166375
* Per Regents of the University of Calfornia letter, remove advertisingWarner Losh2007-01-0958-232/+0
| | | | | | | | | clause. # If I've done so improperly on a file, please let me know. Notes: svn path=/head/; revision=165903
* Implement chunk allocation/deallocation hysteresis by caching one spareJason Evans2006-12-231-51/+86
| | | | | | | | | | | chunk per arena, rather than immediately deallocating all unused chunks. This fixes a potential performance issue when allocating/deallocating an object of size (4kB..1MB] in a loop. Reported by: davidxu Notes: svn path=/head/; revision=165473