xref: /freebsd/usr.bin/sort/mem.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1c66bbc91SGabor Kovesdan /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4c66bbc91SGabor Kovesdan  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5c859c6ddSGabor Kovesdan  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6c66bbc91SGabor Kovesdan  * All rights reserved.
7c66bbc91SGabor Kovesdan  *
8c66bbc91SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
9c66bbc91SGabor Kovesdan  * modification, are permitted provided that the following conditions
10c66bbc91SGabor Kovesdan  * are met:
11c66bbc91SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
12c66bbc91SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
13c66bbc91SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
14c66bbc91SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
15c66bbc91SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
16c66bbc91SGabor Kovesdan  *
17c66bbc91SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c66bbc91SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c66bbc91SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c66bbc91SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c66bbc91SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c66bbc91SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c66bbc91SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c66bbc91SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c66bbc91SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c66bbc91SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c66bbc91SGabor Kovesdan  * SUCH DAMAGE.
28c66bbc91SGabor Kovesdan  */
29c66bbc91SGabor Kovesdan 
30c66bbc91SGabor Kovesdan #include <sys/cdefs.h>
31c66bbc91SGabor Kovesdan __FBSDID("$FreeBSD$");
32c66bbc91SGabor Kovesdan 
33c66bbc91SGabor Kovesdan #include <err.h>
34c66bbc91SGabor Kovesdan #include <stdlib.h>
35c66bbc91SGabor Kovesdan #include <string.h>
36c66bbc91SGabor Kovesdan 
37c66bbc91SGabor Kovesdan #include "mem.h"
38c66bbc91SGabor Kovesdan 
39a312f3e7SBaptiste Daroussin void*
40a312f3e7SBaptiste Daroussin sort_calloc(size_t nb, size_t size)
41a312f3e7SBaptiste Daroussin {
42a312f3e7SBaptiste Daroussin 	void *ptr;
43a312f3e7SBaptiste Daroussin 
44a312f3e7SBaptiste Daroussin 	if ((ptr = calloc(nb, size)) == NULL)
45a312f3e7SBaptiste Daroussin 		err(2, NULL);
46a312f3e7SBaptiste Daroussin 	return (ptr);
47a312f3e7SBaptiste Daroussin }
48a312f3e7SBaptiste Daroussin 
49c66bbc91SGabor Kovesdan /*
50c66bbc91SGabor Kovesdan  * malloc() wrapper.
51c66bbc91SGabor Kovesdan  */
52c66bbc91SGabor Kovesdan void *
53c66bbc91SGabor Kovesdan sort_malloc(size_t size)
54c66bbc91SGabor Kovesdan {
55c66bbc91SGabor Kovesdan 	void *ptr;
56c66bbc91SGabor Kovesdan 
57c66bbc91SGabor Kovesdan 	if ((ptr = malloc(size)) == NULL)
58c66bbc91SGabor Kovesdan 		err(2, NULL);
59c66bbc91SGabor Kovesdan 	return (ptr);
60c66bbc91SGabor Kovesdan }
61c66bbc91SGabor Kovesdan 
62c66bbc91SGabor Kovesdan /*
63c66bbc91SGabor Kovesdan  * free() wrapper.
64c66bbc91SGabor Kovesdan  */
65c66bbc91SGabor Kovesdan void
66c66bbc91SGabor Kovesdan sort_free(const void *ptr)
67c66bbc91SGabor Kovesdan {
68e5f71a07SPedro F. Giffuni 
69c66bbc91SGabor Kovesdan 	if (ptr)
70c66bbc91SGabor Kovesdan 		free(__DECONST(void *, ptr));
71c66bbc91SGabor Kovesdan }
72c66bbc91SGabor Kovesdan 
73c66bbc91SGabor Kovesdan /*
74c66bbc91SGabor Kovesdan  * realloc() wrapper.
75c66bbc91SGabor Kovesdan  */
76c66bbc91SGabor Kovesdan void *
77c66bbc91SGabor Kovesdan sort_realloc(void *ptr, size_t size)
78c66bbc91SGabor Kovesdan {
79c66bbc91SGabor Kovesdan 
80c66bbc91SGabor Kovesdan 	if ((ptr = realloc(ptr, size)) == NULL)
81c66bbc91SGabor Kovesdan 		err(2, NULL);
82c66bbc91SGabor Kovesdan 	return (ptr);
83c66bbc91SGabor Kovesdan }
84c66bbc91SGabor Kovesdan 
85c66bbc91SGabor Kovesdan char *
86c66bbc91SGabor Kovesdan sort_strdup(const char *str)
87c66bbc91SGabor Kovesdan {
88c66bbc91SGabor Kovesdan 	char *dup;
89c66bbc91SGabor Kovesdan 
90c66bbc91SGabor Kovesdan 	if ((dup = strdup(str)) == NULL)
91c66bbc91SGabor Kovesdan 		err(2, NULL);
92c66bbc91SGabor Kovesdan 	return (dup);
93c66bbc91SGabor Kovesdan }
94