/* sort.c -- Sort without allocating memory1Copyright (C) 2012-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#include "config.h"3334#include <stddef.h>35#include <sys/types.h>3637#include "backtrace.h"38#include "internal.h"3940/* The GNU glibc version of qsort allocates memory, which we must not41do if we are invoked by a signal handler. So provide our own42sort. */4344static void45swap (char *a, char *b, size_t size)46{47size_t i;4849for (i = 0; i < size; i++, a++, b++)50{51char t;5253t = *a;54*a = *b;55*b = t;56}57}5859void60backtrace_qsort (void *basearg, size_t count, size_t size,61int (*compar) (const void *, const void *))62{63char *base = (char *) basearg;64size_t i;65size_t mid;6667tail_recurse:68if (count < 2)69return;7071/* The symbol table and DWARF tables, which is all we use this72routine for, tend to be roughly sorted. Pick the middle element73in the array as our pivot point, so that we are more likely to74cut the array in half for each recursion step. */75swap (base, base + (count / 2) * size, size);7677mid = 0;78for (i = 1; i < count; i++)79{80if ((*compar) (base, base + i * size) > 0)81{82++mid;83if (i != mid)84swap (base + mid * size, base + i * size, size);85}86}8788if (mid > 0)89swap (base, base + mid * size, size);9091/* Recurse with the smaller array, loop with the larger one. That92ensures that our maximum stack depth is log count. */93if (2 * mid < count)94{95backtrace_qsort (base, mid, size, compar);96base += (mid + 1) * size;97count -= mid + 1;98goto tail_recurse;99}100else101{102backtrace_qsort (base + (mid + 1) * size, count - (mid + 1),103size, compar);104count = mid;105goto tail_recurse;106}107}108109110