1235267Sgabor/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4235267Sgabor * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5251245Sgabor * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6235267Sgabor * All rights reserved.
7235267Sgabor *
8235267Sgabor * Redistribution and use in source and binary forms, with or without
9235267Sgabor * modification, are permitted provided that the following conditions
10235267Sgabor * are met:
11235267Sgabor * 1. Redistributions of source code must retain the above copyright
12235267Sgabor *    notice, this list of conditions and the following disclaimer.
13235267Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14235267Sgabor *    notice, this list of conditions and the following disclaimer in the
15235267Sgabor *    documentation and/or other materials provided with the distribution.
16235267Sgabor *
17235267Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18235267Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19235267Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20235267Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21235267Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22235267Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23235267Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24235267Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25235267Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26235267Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27235267Sgabor * SUCH DAMAGE.
28235267Sgabor */
29235267Sgabor
30235267Sgabor#include <sys/cdefs.h>
31235267Sgabor__FBSDID("$FreeBSD: stable/11/usr.bin/sort/mem.c 330449 2018-03-05 07:26:05Z eadler $");
32235267Sgabor
33235267Sgabor#include <err.h>
34235267Sgabor#include <stdlib.h>
35235267Sgabor#include <string.h>
36235267Sgabor
37235267Sgabor#include "mem.h"
38235267Sgabor
39235267Sgabor/*
40235267Sgabor * malloc() wrapper.
41235267Sgabor */
42235267Sgaborvoid *
43235267Sgaborsort_malloc(size_t size)
44235267Sgabor{
45235267Sgabor	void *ptr;
46235267Sgabor
47235267Sgabor	if ((ptr = malloc(size)) == NULL)
48235267Sgabor		err(2, NULL);
49235267Sgabor	return (ptr);
50235267Sgabor}
51235267Sgabor
52235267Sgabor/*
53235267Sgabor * free() wrapper.
54235267Sgabor */
55235267Sgaborvoid
56235267Sgaborsort_free(const void *ptr)
57235267Sgabor{
58281132Spfg
59235267Sgabor	if (ptr)
60235267Sgabor		free(__DECONST(void *, ptr));
61235267Sgabor}
62235267Sgabor
63235267Sgabor/*
64235267Sgabor * realloc() wrapper.
65235267Sgabor */
66235267Sgaborvoid *
67235267Sgaborsort_realloc(void *ptr, size_t size)
68235267Sgabor{
69235267Sgabor
70235267Sgabor	if ((ptr = realloc(ptr, size)) == NULL)
71235267Sgabor		err(2, NULL);
72235267Sgabor	return (ptr);
73235267Sgabor}
74235267Sgabor
75235267Sgaborchar *
76235267Sgaborsort_strdup(const char *str)
77235267Sgabor{
78235267Sgabor	char *dup;
79235267Sgabor
80235267Sgabor	if ((dup = strdup(str)) == NULL)
81235267Sgabor		err(2, NULL);
82235267Sgabor	return (dup);
83235267Sgabor}
84