1 /* @(#)mem.c	1.11 15/05/10 Copyright 1998-2015 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)mem.c	1.11 15/05/10 Copyright 1998-2015 J. Schilling";
6 #endif
7 /*
8  *	Memory handling with error checking
9  *
10  *	Copyright (c) 1998-2015 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include <schily/stdio.h>
27 #include <schily/stdlib.h>
28 #include <schily/unistd.h>
29 #include <schily/string.h>
30 #include <schily/standard.h>
31 #include <schily/schily.h>
32 #include <schily/nlsdefs.h>
33 
34 EXPORT	int	___mexval	__PR((int exval));
35 EXPORT	void	*___malloc	__PR((size_t size, char *msg));
36 EXPORT	void	*___realloc	__PR((void *ptr, size_t size, char *msg));
37 EXPORT	char	*___savestr	__PR((const char *s));
38 
39 LOCAL	int	mexval;
40 
41 EXPORT	int
42 ___mexval(exval)
43 	int	exval;
44 {
45 	int	ret = mexval;
46 
47 	mexval = exval;
48 
49 	return (ret);
50 }
51 
52 EXPORT void *
53 ___malloc(size, msg)
54 	size_t	size;
55 	char	*msg;
56 {
57 	void	*ret;
58 
59 	ret = malloc(size);
60 	if (ret == NULL) {
61 		int	err = geterrno();
62 
63 		errmsg(gettext("Cannot allocate memory for %s.\n"), msg);
64 		if (mexval)
65 			err = mexval;
66 		comexit(err);
67 		/* NOTREACHED */
68 	}
69 	return (ret);
70 }
71 
72 EXPORT void *
73 ___realloc(ptr, size, msg)
74 	void	*ptr;
75 	size_t	size;
76 	char	*msg;
77 {
78 	void	*ret;
79 
80 	if (ptr == NULL)
81 		ret = malloc(size);
82 	else
83 		ret = realloc(ptr, size);
84 	if (ret == NULL) {
85 		int	err = geterrno();
86 
87 		errmsg(gettext("Cannot realloc memory for %s.\n"), msg);
88 		if (mexval)
89 			err = mexval;
90 		comexit(err);
91 		/* NOTREACHED */
92 	}
93 	return (ret);
94 }
95 
96 EXPORT char *
97 ___savestr(s)
98 	const char	*s;
99 {
100 	char	*ret = ___malloc(strlen(s)+1, "saved string");
101 
102 	strcpy(ret, s);
103 	return (ret);
104 }
105