1100411Speter/*	$NetBSD: sort.h,v 1.3 2021/12/19 11:20:41 riastradh Exp $	*/
2100411Speter
3100411Speter/*-
4100411Speter * Copyright (c) 2018 The NetBSD Foundation, Inc.
5100411Speter * All rights reserved.
6100411Speter *
7100411Speter * This code is derived from software contributed to The NetBSD Foundation
8100411Speter * by Taylor R. Campbell.
9100411Speter *
10100411Speter * Redistribution and use in source and binary forms, with or without
11100411Speter * modification, are permitted provided that the following conditions
12100411Speter * are met:
13100411Speter * 1. Redistributions of source code must retain the above copyright
14100411Speter *    notice, this list of conditions and the following disclaimer.
15100411Speter * 2. Redistributions in binary form must reproduce the above copyright
16100411Speter *    notice, this list of conditions and the following disclaimer in the
17100411Speter *    documentation and/or other materials provided with the distribution.
18100411Speter *
19100411Speter * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20100411Speter * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21100411Speter * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22100411Speter * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23100411Speter * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24100411Speter * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25100411Speter * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26100411Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27100411Speter * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28100411Speter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29100411Speter * POSSIBILITY OF SUCH DAMAGE.
30100411Speter */
31100411Speter
32100411Speter#ifndef	_LINUX_SORT_H_
33100411Speter#define	_LINUX_SORT_H_
34100411Speter
35100411Speter#include <sys/kmem.h>
36100411Speter
37100411Speter#include <lib/libkern/libkern.h>
38100411Speter
39100411Speterstatic inline void
40100411Spetersort(void *array, size_t nelem, size_t elemsize,
41100411Speter    int (*cmp)(const void *, const void *),
42100411Speter    void (*swap)(void *, void *, int))
43100411Speter{
44100411Speter	void *tmp;
45100411Speter
46100411Speter	KASSERT(swap == NULL);	/* XXX */
47105309Smarcel	KASSERT(elemsize != 0);
48100411Speter	KASSERT(nelem <= SIZE_MAX/elemsize);
49100411Speter
50100411Speter	tmp = kmem_alloc(elemsize, KM_SLEEP);
51100411Speter	kheapsort(array, nelem, elemsize, cmp, tmp);
52106284Smarcel	kmem_free(tmp, elemsize);
53100411Speter}
54100411Speter
55100411Speter#endif	/* _LINUX_SORT_H_ */
56206571Smarcel