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