Debian Codegraph @

prim/tc3/libsrc/memory.c

raw
1 /*===========================================================================
2 Copyright (C) 1986-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .TYPE Module
30 .IDENTIFICATION MM_MEMORY
31 .AUTHOR Marc Wenger [CDS], Francois Ochsenbein [ESO-IPG]
32 .LANGUAGE C
33 .KEYWORDS Memory Management
34 .ENVIRONMENT
35 .COMMENTS
36 These routines allow the management of memory to allocate
37 pieces of memory, e.g. for temporary copy of
38 strings. It's especially useful for string communication
39 between FORTRAN and C.
40 \begin{TeX}
41 \\
42 Note that the allocated memory is never freed --- but may be
43 reused. The memory is decomposed into ``areas'', in principle multiple
44 of the page size (defined as MEM\_AMOUNT), and each area
45 may contain several ``zones''. Areas and zones are preceded
46 by a `memory header' made of :
47 \begin{itemize}
48 \item the length of the area / zone
49 \item chain pointer (to next for free zones, to preceding for occupied
50 zones)
51 \end{itemize}
52
53 The complete memory structure is described via a ``memory packet''
54 internal to this module.
55 \end{TeX}
56
57 .VERSION 1.0 28-Oct-1986
58 .VERSION 2.0 04-Mar-1988: Use OS routines
59 .VERSION 2.1 22-Jun-1989: Added mm_save mm_ssave
60
61 090831 last modif
62 ----------------------------------------------------------------------------*/
63
64 #define TRACING 0 /* No tracing, since these routines may
65 * be used by Program Monitoring Routines ... */
66
67 #include <midas_def.h>
68 #include <error.h>
69 #include <string.h>
70
71
72 extern int eh_put1();
73
74
75 /*==========================================================================*/
76 char *mm_alloc(len)
77 /*+++
78 .PURPOSE Allocate memory
79 .RETURNS Address of usable zone, or NULL (failed)
80 .REMARKS Error traced
81 ---*/
82 int len ; /* IN: Required length (bytes) */
83 {
84 register char *p;
85
86 p = osmmget(len);
87 if (!p) ERROR(osmsg());
88
89 return(p);
90 }
91
92 /*==========================================================================*/
93 int mm_free(a)
94 /*+++
95 .PURPOSE Allocate memory
96 .RETURNS OK / NOK
97 .REMARKS Error traced
98 ---*/
99 char *a; /* IN: Address to free */
100 {
101 osmmfree((void *) a);
102 return(1); /* that's O.K. in FO parlance... */
103 }
104
105 /*==========================================================================*/
106 char *mm_expand(addr, len)
107 /*+++
108 .PURPOSE Reallocates a piece of memory allocated via mm_alloc
109 .RETURNS Address of new zone, or NULL if failed.
110 .REMARKS Values are copied to new zone. Identical to mm_alloc if addr
111 is NULL.
112 ---*/
113 char *addr ; /* MOD: adress to realloc */
114 int len; /* IN: New length */
115 {
116 register char *p;
117
118 p = osmmexp(addr, len);
119 if (!p) ERROR(osmsg());
120
121 return(p);
122 }
123
124 /*==========================================================================*/
125 char *mm_save(addr, len)
126 /*+++
127 .PURPOSE Save a structure into a new piece of memory.
128 .RETURNS Address of copied staff.
129 .REMARKS Error traced
130 ---*/
131 char *addr; /* IN: addr of start of copy */
132 int len ; /* IN: Length (bytes) */
133 {
134 register char *p;
135
136 if ((p = osmmget(len))) oscopy(p, addr, len);
137 else ERROR(osmsg());
138
139 return(p);
140 }
141
142 /*==========================================================================*/
143 char *mm_ssave(addr)
144 /*+++
145 .PURPOSE Save a string into a new piece of memory.
146 .RETURNS Address of copied staff.
147 .REMARKS Error traced
148 ---*/
149 char *addr; /* IN: addr of string to copy */
150 {
151 register char *p;
152 int l;
153
154 l = (addr ? strlen(addr) : 0);
155
156 if ((p = osmmget(l+1))) *(p + oscopy(p, addr, l)) = '\0';
157 else ERROR(osmsg());
158
159 return(p);
160 }
161