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