xref: /freebsd/usr.bin/sort/mem.c (revision c859c6dd547718ff43f7cdec38cebbf56b6ff732)
1c66bbc91SGabor Kovesdan /*-
2c66bbc91SGabor Kovesdan  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
3*c859c6ddSGabor 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 <stdint.h>
33c66bbc91SGabor Kovesdan #include <stdlib.h>
34c66bbc91SGabor Kovesdan #include <string.h>
35c66bbc91SGabor Kovesdan 
36c66bbc91SGabor Kovesdan #include "mem.h"
37c66bbc91SGabor Kovesdan 
38c66bbc91SGabor Kovesdan /*
39c66bbc91SGabor Kovesdan  * malloc() wrapper.
40c66bbc91SGabor Kovesdan  */
41c66bbc91SGabor Kovesdan void *
42c66bbc91SGabor Kovesdan sort_malloc(size_t size)
43c66bbc91SGabor Kovesdan {
44c66bbc91SGabor Kovesdan 	void *ptr;
45c66bbc91SGabor Kovesdan 
46c66bbc91SGabor Kovesdan 	if ((ptr = malloc(size)) == NULL)
47c66bbc91SGabor Kovesdan 		err(2, NULL);
48c66bbc91SGabor Kovesdan 	return (ptr);
49c66bbc91SGabor Kovesdan }
50c66bbc91SGabor Kovesdan 
51c66bbc91SGabor Kovesdan /*
52c66bbc91SGabor Kovesdan  * free() wrapper.
53c66bbc91SGabor Kovesdan  */
54c66bbc91SGabor Kovesdan void
55c66bbc91SGabor Kovesdan sort_free(const void *ptr)
56c66bbc91SGabor Kovesdan {
57c66bbc91SGabor Kovesdan 
58c66bbc91SGabor Kovesdan 	if (ptr)
59c66bbc91SGabor Kovesdan 		free(__DECONST(void *, ptr));
60c66bbc91SGabor Kovesdan }
61c66bbc91SGabor Kovesdan 
62c66bbc91SGabor Kovesdan /*
63c66bbc91SGabor Kovesdan  * realloc() wrapper.
64c66bbc91SGabor Kovesdan  */
65c66bbc91SGabor Kovesdan void *
66c66bbc91SGabor Kovesdan sort_realloc(void *ptr, size_t size)
67c66bbc91SGabor Kovesdan {
68c66bbc91SGabor Kovesdan 
69c66bbc91SGabor Kovesdan 	if ((ptr = realloc(ptr, size)) == NULL)
70c66bbc91SGabor Kovesdan 		err(2, NULL);
71c66bbc91SGabor Kovesdan 	return (ptr);
72c66bbc91SGabor Kovesdan }
73c66bbc91SGabor Kovesdan 
74c66bbc91SGabor Kovesdan char *
75c66bbc91SGabor Kovesdan sort_strdup(const char *str)
76c66bbc91SGabor Kovesdan {
77c66bbc91SGabor Kovesdan 	char *dup;
78c66bbc91SGabor Kovesdan 
79c66bbc91SGabor Kovesdan 	if ((dup = strdup(str)) == NULL)
80c66bbc91SGabor Kovesdan 		err(2, NULL);
81c66bbc91SGabor Kovesdan 	return (dup);
82c66bbc91SGabor Kovesdan }
83