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