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