Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/valloc.c

raw
1 /*---------------------------------------------------------------------
2 *
3 *
4 * Copyright (c) 1988, Visual Edge Software Ltd.
5 *
6 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
7 * distribute this software and its documentation for any purpose
8 * and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Visual Edge Software not be
12 * used in advertising or publicity pertaining to distribution of
13 * the software without specific, written prior permission. The year
14 * included in the notice is the year of the creation of the work.
15 *-------------------------------------------------------------------*/
16
17
18 /* the general memory allocator for Visual Edge products;
19
20 This file consists of routines that should be used in place of
21 malloc calls. It eventually calls malloc to get memory, but does
22 some nifty error checking. If the system malloc call returns a NULL
23 pointer (out of memory), this will abort the program.
24
25 The memory subsystem requires catalog
26 subssytem to work. Please refer to the docs on the
27 catalog subsystem for info on how to link with them.
28
29 The include file "valloc.h" should be included in all your
30 application programs that uses UxMalloc et al.
31
32 .VERSION
33 090813 last modif KB
34
35 ----------------------------------------------------------------------------*/
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <valloc.h>
40
41 #include "uimx_cat.h"
42 #include "valloc_ds.h"
43 #define CGETS(x,y) UxCatGets(MC_VALLOC,(x),(y))
44
45 #define NO_MEMORY 1
46
47 #ifdef SAFE_MALLOC
48 #define malloc(S) calloc((S),(size_t)1)
49 #endif
50
51 extern void UxStandardError();
52
53
54 static char *fmt;
55
56 /*****************************************************************************
57 NAME: char *UxNoMemory(ptr, sz)
58 INPUT: char *ptr - previous pointer when called from
59 UxRealloc
60 unsigned sz - number of bytes requested
61 RETURN: char * - pointer to allocated space, or NULL
62 DESCRIPTION: Repeatedly attempts to allocate space, requesting commands
63 from the user at each step
64 CREATION: 4 July 1989
65 REVISIONS: --
66 -----------------------------------------------------------------------------*/
67
68 char *UxNoMemory(ptr, sz)
69 char *ptr;
70 unsigned sz;
71 {
72 UxStandardError(fmt, (int)sz);
73 abort();
74 return(NULL);
75 }
76
77
78 /*****************************************************************************
79 NAME: void *UxMalloc(sz)
80 DESCRIPTION: Replaces malloc(), checks return value automatically.
81 CREATION: 4 July 1989
82 REVISIONS: --
83 -----------------------------------------------------------------------------*/
84
85 void *UxMalloc(sz)
86 unsigned sz;
87 {
88 void *rtrn;
89
90 if (sz == 0)
91 return NULL;
92
93 rtrn = (void *) malloc((size_t)sz);
94 if (rtrn == NULL)
95 rtrn = UxNoMemory(NULL, sz);
96 return(rtrn);
97 }
98
99 /*****************************************************************************
100 NAME: void *UxCalloc(sz)
101 DESCRIPTION: Replaces calloc(), checks return value automatically.
102 CREATION: 4 July 1989
103 REVISIONS: --
104 -----------------------------------------------------------------------------*/
105
106 void *UxCalloc(n, sz)
107 unsigned n, sz;
108 {
109 void *rtrn;
110
111 if ((sz == 0) || (n == 0))
112 return NULL;
113
114 rtrn = (void *) calloc(n, sz);
115
116 if (rtrn == NULL)
117 rtrn = UxNoMemory(NULL, n * sz);
118
119 return(rtrn);
120 }
121
122 /*****************************************************************************
123 NAME: void *UxRealloc(sz)
124 DESCRIPTION: Replaces realloc(), checks return value automatically.
125 CREATION: 4 July 1989
126 REVISIONS: --
127 -----------------------------------------------------------------------------*/
128
129 void *UxRealloc(ptr, sz)
130 void *ptr;
131 unsigned sz;
132 {
133 void *rtrn= ptr;
134
135 if (sz == 0)
136 return ptr;
137
138 if (ptr)
139 rtrn = (void *) realloc(ptr, (size_t)sz);
140 else
141 rtrn = (void *) malloc((size_t)sz);
142
143 if (rtrn == NULL)
144 rtrn = UxNoMemory(ptr, sz);
145
146 return(rtrn);
147 }
148
149
150 /*---------------------------------------------------------------------------*/
151
152 void UxFree (p)
153 void *p;
154 {
155 if (p != (void *) 0) free (p);
156 }
157 /*****************************************************************************
158 NAME: void UxInitMallocMsg()
159 INPUT: None
160 CREATION: 22/08/90
161 REVISIONS: --
162 -----------------------------------------------------------------------------*/
163 void UxInitMallocMsg()
164 {
165 fmt = UxCopyString(CGETS(MS_VL_ATMPTOALLCBYTES, DS_MS_VL_ATMPTOALLCBYTES));
166 }