summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
Commit message (Collapse)AuthorAgeFilesLines
* MFC r318514-r318515, r318517, r318917Xin LI2017-05-311-18/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | r318514: Use size_t. Inspired by: OpenBSD src/lib/libc/stdlib/qsort.c,v 1.11 r318515: The current qsort(3) implementation ignores the sizes of partitions, and always perform recursion on the left partition, then use a tail call to handle the right partition. In the worst case this could require O(N) levels of recursions. Reduce the possible recursion level to log2(N) by always recursing on the smaller partition instead. Obtained from: PostgreSQL 9d6077abf9d6efd992a59f05ef5aba981ea32096 r318517: Sync qsort.c with userland r318515. (Note that MIN macro is removed in favor of sys/param.h's version). PR: 213922 r318917: Disconnect heimdal version of qsort.c from build because we are already using libc's version of qsort. PR: bin/213922 Notes: svn path=/stable/11/; revision=319285
* MFC r318303:Konstantin Belousov2017-05-291-9/+7
| | | | | | | Style. Notes: svn path=/stable/11/; revision=319128
* MFC r318299:Konstantin Belousov2017-05-291-44/+31
| | | | | | | Simplify cleanup on failure in realpath(3). Notes: svn path=/stable/11/; revision=319127
* MFC r318298:Konstantin Belousov2017-05-291-19/+27
| | | | | | | | | Fix several buffer overflows in realpath(3), and other minor issues. PR: 219154 Notes: svn path=/stable/11/; revision=319126
* MFC r316213:Konstantin Belousov2017-04-233-2/+100
| | | | | | | | | | | Implement the memset_s(3) function as specified by the C11 ISO/IEC 9899:2011 Appendix K 3.7.4.1. MFC r316258: Only activate __EXT1_VISIBLE block when using sys/errno.h in userspace. Notes: svn path=/stable/11/; revision=317342
* MFC r315162:Pedro F. Giffuni2017-04-071-4/+4
| | | | | | | | | | | libc: provide some bounds-checking through reallocarray(3). reallocarray(3) is a non portable extension that originated in OpenBSD. Given that it is already in FreeBSD's libc it is useful for the cases where reallocation involves a multiplication. Notes: svn path=/stable/11/; revision=316613
* MFC r315095, r315096, r315097, r315187:Pedro F. Giffuni2017-03-151-1/+1
| | | | | | | | | | | | | | | | | | | libc: small cleanups. Rename nitems to numitems: it shares the anme with an existing macro in sys/params.h. Also initialize the value later which avoids asigning the value if we exit early. Unsign setlen: it is local and will never be negative. Having one more bit for growth is beneficial and it avoids a cast when it's going to be used for allocation. Remove unused initialization: "num" is properly defined before use. Let calloc(3) do the multiplication. Notes: svn path=/stable/11/; revision=315308
* MFC r313374:Enji Cooper2017-02-141-4/+4
| | | | | | | | | | | hcreate(3): fix the ERRORS section and bump .Dd - Add missing comma between functions that trigger ENOMEM error. - Fix the description for ESRCH. The action that triggers this error is FIND, not SEARCH (SEARCH does not exist). Notes: svn path=/stable/11/; revision=313724
* MFC r312332,r312446,r312451:Enji Cooper2017-02-041-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | r312332: Use SRCTOP where possible and use :H to manipulate .CURDIR to get rid of unnecessarily long relative path .PATH values with make r312446 (by emaste): libc: remove reference to nonexistent lib/locale directory As far as I can tell this was introduced in r72406 and updated in several subsequent revisions, but the lib/locale directory it referenced never existed. r312451: Replace dot-dot relative pathing with SRCTOP-relative paths where possible This reduces build output, need for recalculating paths, and makes it clearer which paths are relative to what areas in the source tree. The change in performance over a locally mounted UFS filesystem was negligible in my testing, but this may more positively impact other filesystems like NFS. LIBC_SRCTOP was left alone so Juniper (and other users) can continue to manipulate lib/libc/Makefile (and other Makefile.inc's under lib/libc) as include Makefiles with custom options. Discussed with: marcel, sjg Notes: svn path=/stable/11/; revision=313240
* MFC r311651:Konstantin Belousov2017-01-214-106/+162
| | | | | | | Export __cxa_thread_atexit_impl as an alias for __cxa_thread_atexit. Notes: svn path=/stable/11/; revision=312589
* MFC r305855, r306297, r306300, r306312-r306313Marcel Moolenaar2016-12-111-1/+8
| | | | | | | | | | | | | | | | | When MAKEOBJDIRPREFIX points to a case-insensitive file system, the build can break when different source files create the same object files (case-insensitivity speaking). This is the case for object files compiled with -fpic and shared libraries. The former uses an extension of ".So", and the latter an extension ".so". Rename shared object files from *.So to *.pico to match what NetBSD does. Also: o Compile _Exit.c as C99_Exit.c, as it conflicts with _exit.s o Add entry to UPDATING o Document .pico extension Notes: svn path=/stable/11/; revision=309843
* MFC r307227 and r307343:Ed Schouten2016-10-295-52/+54
| | | | | | | | | | | | | | | | | | | | | | | | | Improve typing of POSIX search tree functions. Back in 2015 when I reimplemented these functions to use an AVL tree, I was annoyed by the weakness of the typing of these functions. Both tree nodes and keys are represented by 'void *', meaning that things like the documentation for these functions are an absolute train wreck. To make things worse, users of these functions need to cast the return value of tfind()/tsearch() from 'void *' to 'type_of_key **' in order to access the key. Technically speaking such casts violate aliasing rules. I've observed actual breakages as a result of this by enabling features like LTO. I've filed a bug report at the Austin Group. Looking at the way the bug got resolved, they made a pretty good step in the right direction. A new type 'posix_tnode' has been added to correspond to tree nodes. It is still defined as 'void' for source-level compatibility, but in the very far future it could be replaced by a proper structure type containing a key pointer. Notes: svn path=/stable/11/; revision=308090
* MFC r303795:Konstantin Belousov2016-08-204-1/+152
| | | | | | | Add __cxa_thread_atexit(3) API implementation. Notes: svn path=/stable/11/; revision=304524
* Reflect error indication according to POSIX and what those functionsAndrey A. Chernov2016-06-051-2/+2
| | | | | | | currently do. Notes: svn path=/head/; revision=301448
* Don't use fixup for C99 and up, the compiler result is already correct.Andrey A. Chernov2016-06-014-0/+8
| | | | | | | | | Suggested by: bde MFC after: 1 week Notes: svn path=/head/; revision=301115
* Micro optimize: C standard guarantees that right shift for unsigned valueAndrey A. Chernov2016-05-291-1/+1
| | | | | | | | | | fills left bits with zero, and we have exact 32bit unsigned value (uint32_t), so there is no reason to add "& 0x7fffffff" here. MFC after: 1 week Notes: svn path=/head/; revision=300965
* 1) Unifdef USE_WEAK_SEEDING since it is too obsolete to support and makesAndrey A. Chernov2016-05-291-41/+11
| | | | | | | | | | | | | | | | reading hard. 2) Instead of doing range transformation in each and every function here, do it single time directly in do_rand(). One "mod" operation overhead is not a big deal, but the code looks nicer and possible future functions additions or PRNG change do not miss range transformations neither have unneeded ones. 3) Use POSIX argument types for visible functions (cosmetic). MFC after: 1 week Notes: svn path=/head/; revision=300956
* 1) Unifdef USE_WEAK_SEEDING it is too obsolete to support and makes readingAndrey A. Chernov2016-05-291-46/+14
| | | | | | | | | | | | | | | | harder. 2) ACM paper require seed to be in [1, 2^31-2] range, so use the same range shifting as already done for rand(3). Also protect srandomdev() + TYPE_0 case (non default) from negative seeds. 3) Don't check for valid "type" range in setstate(), it is always valid as calculated. Instead add a check that rear pointer not exceeed end pointer. MFC after: 1 week Notes: svn path=/head/; revision=300953
* Let l64a() properly null terminate its result.Ed Schouten2016-05-261-22/+14
| | | | | | | | | | | | | | | | | Though the buffer used by l64a() is initialized with null bytes, repetetive calls may end up having trailing garbage of previous invocations because we don't end up terminating the string. Instead of importing NetBSD's fix, use this opportunity to simplify this function dramatically, for example by just storing the Base64 character set in a string. There is also no need to do the bitmasking, as we can just use the proper integer type from <stdint.h>. MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D6511 Notes: svn path=/head/; revision=300775
* 1) POSIX prohibits printing errors to stderr here and requireAndrey A. Chernov2016-05-221-15/+10
| | | | | | | | | | | | | | | | | | returning NULL: "Upon successful completion, initstate() and setstate() shall return a pointer to the previous state array; otherwise, a null pointer shall be returned. Although some implementations of random() have written messages to standard error, such implementations do not conform to POSIX.1-2008." 2) Move error detections earlier to prevent state modifying. MFC after: 1 week Notes: svn path=/head/; revision=300397
* Update jemalloc to 4.2.0.Jason Evans2016-05-131-1/+1
| | | | Notes: svn path=/head/; revision=299587
* libc: spelling fixes.Pedro F. Giffuni2016-04-302-3/+3
| | | | | | | Mostly on comments. Notes: svn path=/head/; revision=298830
* libc: do not include <sys/types.h> where <sys/param.h> was already includedAndriy Voskoboinyk2016-04-181-1/+0
| | | | | | | | | According to style(9): > normally, include <sys/types.h> OR <sys/param.h>, but not both. (<sys/param.h> already includes <sys/types.h> when LOCORE is not defined). Notes: svn path=/head/; revision=298226
* Fixed indentation, minor style.Sergey Kandaurov2016-04-181-12/+12
| | | | Notes: svn path=/head/; revision=298201
* Fix markup on "\n" in printf so it renders correctly.Warren Block2016-04-171-4/+4
| | | | | | | | | PR: 208852 Submitted by: coder@tuxfamily.org MFC after: 1 week Notes: svn path=/head/; revision=298156
* Update jemalloc to 4.1.0.Jason Evans2016-02-292-2/+7
| | | | | | | | | | | | | Add missing Symbol.map entry for __aligned_alloc. Add weak-->strong symbol binding for {malloc_stats_print,mallctl,mallctlnametomib,mallctlbymib} --> {__malloc_stats_print,__mallctl,__mallctlnametomib,__mallctlbymib}. These bindings complete the set necessary to allow applications to replace all malloc-related symbols. Notes: svn path=/head/; revision=296221
* Add .NOMETA missed in r291320.Bryan Drewery2016-01-201-1/+1
| | | | | | | Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=294458
* Remove an unneeded assignment of the return value.Ed Schouten2016-01-141-1/+0
| | | | | | | | | | tdelete() is supposed to return the address of the parent node that has been deleted. We already keep track of this node in the loop between lines 94-107. The GO_LEFT()/GO_RIGHT() macros are used later on as well, so we must make sure not to change it to something else. Notes: svn path=/head/; revision=293879
* Replace implementation of hsearch() by one that scales.Ed Schouten2015-12-277-185/+335
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Traditionally the hcreate() function creates a hash table that uses chaining, using a fixed user-provided size. The problem with this approach is that this often either wastes memory (table too big) or yields bad performance (table too small). For applications it may not always be easy to estimate the right hash table size. A fixed number only increases performance compared to a linked list by a constant factor. This problem can be solved easily by dynamically resizing the hash table. If the size of the hash table is at least doubled, this has no negative on the running time complexity. If a dynamically sized hash table is used, we can also switch to using open addressing instead of chaining, which has the advantage of just using a single allocation for the entire table, instead of allocating many small objects. Finally, a problem with the existing implementation is that its deterministic algorithm for hashing makes it possible to come up with fixed patterns to trigger an excessive number of collisions. We can easily solve this by using FNV-1a as a hashing algorithm in combination with a randomly generated offset basis. Measurements have shown that this implementation is about 20-25% faster than the existing implementation (even if the existing implementation is given an excessive number of buckets). Though it allocates more memory through malloc() than the old implementation (between 4-8 pointers per used entry instead of 3), process memory use is similar to the old implementation as if the estimated size was underestimated by a factor 10. This is due to the fact that malloc() needs to perform less bookkeeping. Reviewed by: jilles, pfg Obtained from: https://github.com/NuxiNL/cloudlibc Differential Revision: https://reviews.freebsd.org/D4644 Notes: svn path=/head/; revision=292767
* Let tsearch()/tdelete() use an AVL tree.Ed Schouten2015-12-224-86/+473
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The existing implementations of POSIX tsearch() and tdelete() don't attempt to perform any balancing at all. Testing reveals that inserting 100k nodes into a tree sequentially takes approximately one minute on my system. Though most other BSDs also don't use any balanced tree internally, C libraries like glibc and musl do provide better implementations. glibc uses a red-black tree and musl uses an AVL tree. Red-black trees have the advantage over AVL trees that they only require O(1) rotations after insertion and deletion, but have the disadvantage that the tree has a maximum depth of 2*log2(n) instead of 1.44*log2(n). My take is that it's better to focus on having a lower maximum depth, for the reason that in the case of tsearch() the invocation of the comparator likely dominates the running time. This change replaces the tsearch() and tdelete() functions by versions that create an AVL tree. Compared to musl's implementation, this version is different in two different ways: - We don't keep track of heights; just balances. This is sufficient. This has the advantage that it reduces the number of nodes that are being accessed. Storing heights requires us to also access all of the siblings along the path. - Don't use any recursion at all. We know that the tree cannot 2^64 elements in size, so the height of the tree can never be larger than 96. Use a 128-bit bitmask to keep track of the path that is computed. This allows us to iterate over the same path twice, meaning we can apply rotations from top to bottom. Inserting 100k nodes into a tree now only takes 0.015 seconds. Insertion seems to be twice as fast as glibc, whereas deletion has about the same performance. Unlike glibc, it uses a fixed amount of memory. I also experimented with both recursive and iterative bottom-up implementations of the same algorithm. This iterative top-down version performs similar to the recursive bottom-up version in terms of speed and code size. For some reason, the iterative bottom-up algorithm was actually 30% faster for deletion, but has a quadratic memory complexity to keep track of all the parent pointers. Reviewed by: jilles Obtained from: https://github.com/NuxiNL/cloudlibc Differential Revision: https://reviews.freebsd.org/D4412 Notes: svn path=/head/; revision=292613
* META MODE: Don't create .meta files when symlinking sources into the obj ↵Bryan Drewery2015-11-251-4/+4
| | | | | | | | | | | | | | directory. Tracking these leads to situations where meta mode will consider the file to be out of date if /bin/sh or /bin/ln are newer than the source file. There's no reason for meta mode to do this as make is already handling the rebuild dependency fine. Sponsored by: EMC / Isilon Storage Division Notes: svn path=/head/; revision=291320
* Use ANSI C prototypes. Eliminates -Wold-style-definition warnings.Craig Rodrigues2015-09-2012-51/+28
| | | | Notes: svn path=/head/; revision=288030
* Remove names from prototypesCraig Rodrigues2015-09-203-7/+7
| | | | Notes: svn path=/head/; revision=288026
* Add declarations to eliminate -Wmissing-prototypes warnings.Craig Rodrigues2015-09-203-0/+15
| | | | Notes: svn path=/head/; revision=288005
* Use ANSI C prototypes.Craig Rodrigues2015-09-143-45/+14
| | | | | | | Eliminates gcc 4.9 warnings. Notes: svn path=/head/; revision=287793
* Switch libc from using _sig{procmask,action,suspend} symbols, whichKonstantin Belousov2015-08-292-12/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | are aliases for the syscall stubs and are plt-interposed, to the libc-private aliases of internally interposed sigprocmask() etc. Since e.g. _sigaction is not interposed by libthr, calling signal() removes thr_sighandler() from the handler slot etc. The result was breaking signal semantic and rtld locking. The added __libc_sigprocmask and other symbols are hidden, they are not exported and cannot be called through PLT. The setjmp/longjmp functions for x86 were changed to use direct calls, and since PIC_PROLOGUE only needed for functional PLT indirection on i386, it is removed as well. The PowerPC bug of calling the syscall directly in the setjmp/longjmp implementation is kept as is. Reported by: Pete French <petefrench@ingresso.co.uk> Tested by: Michiel Boland <boland37@xs4all.nl> Reviewed by: jilles (previous version) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=287292
* Add missing sdallocx updates and remove *allocm manpage links.Jason Evans2015-08-182-5/+6
| | | | | | | Submitted by: jbeich Notes: svn path=/head/; revision=286872
* Update jemalloc to version 4.0.0.Jason Evans2015-08-181-1/+2
| | | | Notes: svn path=/head/; revision=286866
* Document the fact that system(3) can easily be misused due to shell metaXin LI2015-07-251-2/+14
| | | | | | | | | | characters are honored. While I'm there also mention posix_spawn in the SEE ALSO section. MFC after: 2 weeks Notes: svn path=/head/; revision=285864
* Fix major copy/paste and other style errors.Sergey Kandaurov2015-05-051-5/+10
| | | | Notes: svn path=/head/; revision=282472
* Apply the copyright the the same owners as the original malloc(3) where most ofBaptiste Daroussin2015-05-011-2/+3
| | | | | | | | | | | the text here comes from Reported by: many Discussed with: miod@OpenBSD.org Pointyhat to: bapt Notes: svn path=/head/; revision=282321
* Import reallocarray(3) from OpenBSDBaptiste Daroussin2015-05-014-2/+182
| | | | | | | | | | | | Add a manpage for it, assign the copyright to the OpenBSD project on it since it is mostly copy/paste from OpenBSD manpage. style(9) fixes Differential Revision: https://reviews.freebsd.org/D2420 Reviewed by: kib Notes: svn path=/head/; revision=282314
* qsort(3): small style(9) cleanups.Pedro F. Giffuni2015-03-051-10/+10
| | | | | | | | Basically spaces vs. tabs. No functional change. Notes: svn path=/head/; revision=279666
* qsort(3): enhance to handle 32-bit aligned data on 64-bit systemsPedro F. Giffuni2015-03-051-12/+21
| | | | | | | | | | | | | | | | | Implement a small enhancement to the original qsort implementation: If the data is 32 bit aligned we can side-step the long type version and use int instead. The change brings a modest but significant improvement in 32 bit workloads. Relnotes: yes PR: 135718 Taken from: ache Notes: svn path=/head/; revision=279663
* Adjust wording slightly to emphasize that random(3) should not be used forColin Percival2015-02-251-3/+2
| | | | | | | | | | | any applications which need unpredictable random numbers, not merely those which are cryptographic in nature. If you work for a lottery and you're using random(3) to select the winning numbers, please let me know. Notes: svn path=/head/; revision=279269
* Update comment and NetBSD ID tag.Pedro F. Giffuni2015-02-061-3/+3
| | | | | | | | The NetBSD revisions correspond to changes we have already done like __P() removal and ANSI-fication of definitions. Notes: svn path=/head/; revision=278315
* tdelete(3): don't delete the node we are about to return.Pedro F. Giffuni2015-02-051-1/+2
| | | | | | | | | CID: 272528 Obtained from: NetBSD (CVS rev. 1.4) MFC after: 2 weeks Notes: svn path=/head/; revision=278301
* Fix known issues which blow up the process after dlopen("libthr.so")Konstantin Belousov2015-01-033-3/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (or loading a dso linked to libthr.so into process which was not linked against threading library). - Remove libthr interposers of the libc functions, including __error(). Instead, functions calls are indirected through the interposing table, similar to how pthread stubs in libc are already done. Libc by default points either to syscall trampolines or to existing libc implementations. On libthr load, libthr rewrites the pointers to the cancellable implementations already in libthr. The interposition table is separate from pthreads stubs indirection table to not pull pthreads stubs into static binaries. - Postpone the malloc(3) internal mutexes initialization until libthr is loaded. This avoids recursion between calloc(3) and static pthread_mutex_t initialization. - Reinstall signal handlers with wrapper on libthr load. The _rtld_is_dlopened(3) is used to avoid useless calls to sigaction(2) when libthr is statically referenced from the main binary. In the process, fix openat(2), swapcontext(2) and setcontext(2) interposing. The libc symbols were exported at different versions than libthr interposers. Export both libc and libthr versions from libc now, with default set to the higher version from libthr. Remove unused and disconnected swapcontext(3) userspace implementation from libc/gen. No objections from: deischen Tested by: pho, antoine (exp-run) (previous versions) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=276630
* Various mdoc fixes and a few EOL whitespace removals.Christian Brueffer2014-12-212-3/+3
| | | | | | | Found with: mandoc -Tlint Notes: svn path=/head/; revision=276006
* Fix quick_exit(3) manual page to match reality - the status was missing.Edward Tomasz Napierala2014-12-131-2/+2
| | | | | | | | MFC after: 1 month Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=275752