xref: /freebsd/usr.bin/sort/mem.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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 #include <err.h>
32c66bbc91SGabor Kovesdan #include <stdlib.h>
33c66bbc91SGabor Kovesdan #include <string.h>
34c66bbc91SGabor Kovesdan 
35c66bbc91SGabor Kovesdan #include "mem.h"
36c66bbc91SGabor Kovesdan 
37a312f3e7SBaptiste Daroussin void*
sort_calloc(size_t nb,size_t size)38a312f3e7SBaptiste Daroussin sort_calloc(size_t nb, size_t size)
39a312f3e7SBaptiste Daroussin {
40a312f3e7SBaptiste Daroussin 	void *ptr;
41a312f3e7SBaptiste Daroussin 
42a312f3e7SBaptiste Daroussin 	if ((ptr = calloc(nb, size)) == NULL)
43a312f3e7SBaptiste Daroussin 		err(2, NULL);
44a312f3e7SBaptiste Daroussin 	return (ptr);
45a312f3e7SBaptiste Daroussin }
46a312f3e7SBaptiste Daroussin 
47c66bbc91SGabor Kovesdan /*
48c66bbc91SGabor Kovesdan  * malloc() wrapper.
49c66bbc91SGabor Kovesdan  */
50c66bbc91SGabor Kovesdan void *
sort_malloc(size_t size)51c66bbc91SGabor Kovesdan sort_malloc(size_t size)
52c66bbc91SGabor Kovesdan {
53c66bbc91SGabor Kovesdan 	void *ptr;
54c66bbc91SGabor Kovesdan 
55c66bbc91SGabor Kovesdan 	if ((ptr = malloc(size)) == NULL)
56c66bbc91SGabor Kovesdan 		err(2, NULL);
57c66bbc91SGabor Kovesdan 	return (ptr);
58c66bbc91SGabor Kovesdan }
59c66bbc91SGabor Kovesdan 
60c66bbc91SGabor Kovesdan /*
61c66bbc91SGabor Kovesdan  * free() wrapper.
62c66bbc91SGabor Kovesdan  */
63c66bbc91SGabor Kovesdan void
sort_free(const void * ptr)64c66bbc91SGabor Kovesdan sort_free(const void *ptr)
65c66bbc91SGabor Kovesdan {
66e5f71a07SPedro F. Giffuni 
67c66bbc91SGabor Kovesdan 	if (ptr)
68c66bbc91SGabor Kovesdan 		free(__DECONST(void *, ptr));
69c66bbc91SGabor Kovesdan }
70c66bbc91SGabor Kovesdan 
71c66bbc91SGabor Kovesdan /*
72c66bbc91SGabor Kovesdan  * realloc() wrapper.
73c66bbc91SGabor Kovesdan  */
74c66bbc91SGabor Kovesdan void *
sort_realloc(void * ptr,size_t size)75c66bbc91SGabor Kovesdan sort_realloc(void *ptr, size_t size)
76c66bbc91SGabor Kovesdan {
77c66bbc91SGabor Kovesdan 
78c66bbc91SGabor Kovesdan 	if ((ptr = realloc(ptr, size)) == NULL)
79c66bbc91SGabor Kovesdan 		err(2, NULL);
80c66bbc91SGabor Kovesdan 	return (ptr);
81c66bbc91SGabor Kovesdan }
82c66bbc91SGabor Kovesdan 
83c66bbc91SGabor Kovesdan char *
sort_strdup(const char * str)84c66bbc91SGabor Kovesdan sort_strdup(const char *str)
85c66bbc91SGabor Kovesdan {
86c66bbc91SGabor Kovesdan 	char *dup;
87c66bbc91SGabor Kovesdan 
88c66bbc91SGabor Kovesdan 	if ((dup = strdup(str)) == NULL)
89c66bbc91SGabor Kovesdan 		err(2, NULL);
90c66bbc91SGabor Kovesdan 	return (dup);
91c66bbc91SGabor Kovesdan }
92