zmalloc.c 4.4 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
Linus Tolke's avatar
Linus Tolke committed
2
 * $Id: zmalloc.c,v 1.8 1993/10/11 19:27:23 linus Exp $
Linus Tolke's avatar
Linus Tolke committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 *  zmalloc.c
 *		
 *
 *
 *  Copyright (C) 1990	Lysator Computer Club,
 *			Linkoping University,  Sweden
 *
 *  Everyone is granted permission to copy, modify and redistribute
 *  this code, provided the people they give it to can.
 *
 *
 *  Author:	Thomas Bellman
 *		Lysator Computer Club
 *		Linkoping University
 *		Sweden
 *
 *  email:	Bellman@Lysator.LiU.SE
 *
 *
 *  Any opinions expressed in this code are the author's PERSONAL opinions,
 *  and does NOT, repeat NOT, represent any official standpoint of Lysator,
 *  even if so stated.
 */


Linus Tolke's avatar
Linus Tolke committed
51
static char *rcsid = "$Id: zmalloc.c,v 1.8 1993/10/11 19:27:23 linus Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
52

Linus Tolke's avatar
Linus Tolke committed
53
#ifndef __vax__
Per Cederqvist's avatar
Per Cederqvist committed
54
#include <stddef.h>
Linus Tolke's avatar
Linus Tolke committed
55
56
#endif
#ifdef HAVE_MALLOC_H
Per Cederqvist's avatar
Per Cederqvist committed
57
#include <malloc.h>
Per Cederqvist's avatar
Per Cederqvist committed
58
#else
Linus Tolke's avatar
Linus Tolke committed
59
#ifdef HAVE_STDLIB_H
Per Cederqvist's avatar
Per Cederqvist committed
60
#include <stdlib.h>
Linus Tolke's avatar
Linus Tolke committed
61
#endif
Linus Tolke's avatar
Linus Tolke committed
62
#endif
Linus Tolke's avatar
Linus Tolke committed
63
64

#if STDC_HEADERS || HAVE_STRING_H
Per Cederqvist's avatar
Per Cederqvist committed
65
#include <string.h>
Linus Tolke's avatar
Linus Tolke committed
66
#if !STDC_HEADERS && HAVE_MEMORY_H
Per Cederqvist's avatar
Per Cederqvist committed
67
#include <memory.h>
Linus Tolke's avatar
Linus Tolke committed
68
#endif
69
70
71
72
73
/*#define index strchr*/
/*#define rindex strrchr*/
/*#define bcopy(s, d, n) memcpy ((d), (s), (n))*/
/*#define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))*/
/*#define bzero(s, n) memset ((s), 0, (n))*/
Linus Tolke's avatar
Linus Tolke committed
74
75
76
77
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
/* memory.h and strings.h conflict on some systems.  */
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
Per Cederqvist's avatar
Per Cederqvist committed
78

Per Cederqvist's avatar
Per Cederqvist committed
79
#include "compiler.h"
Per Cederqvist's avatar
Per Cederqvist committed
80

Linus Tolke's avatar
Linus Tolke committed
81
82
83
#ifdef __vax__
#define __SIZE_T is defined in string.h
#endif
Per Cederqvist's avatar
Per Cederqvist committed
84
#include "zmalloc.h"
85
86
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223


#define EXPORT


typedef	struct	{
	int		refcount;
	size_t		size;
}  z_info;



#define	REFCOUNT(pointer)	\
	(((z_info *) ((char *) pointer - sizeof (z_info)))->refcount)

#define	AREA_SIZE(pointer)	\
	(((z_info *) ((char *) pointer - sizeof (z_info)))->size)


#define MIN(n1, n2)		(((n1) < (n2)) ? (n1) : (n2))



EXPORT  void *
zmalloc (size_t	   size)
{
    void		* memory;

    memory = malloc (size + sizeof (z_info));
    if (memory == NULL)
	return  NULL;
    ((z_info *) memory)->size = size;
    ((z_info *) memory)->refcount = 1;
    return  (char *) memory + sizeof (z_info);
}



EXPORT  void *
zcalloc (size_t	   n,
	 size_t	   size)

{
    void		* memory;

    memory = malloc (n * size + sizeof (z_info));
    if (memory == NULL)
	return  NULL;
    ((z_info *) memory)->size = n * size;
    ((z_info *) memory)->refcount = 1;
    memset ((char *) memory + sizeof (z_info),
	    0, n * size);

    return  (char *) memory + sizeof (z_info);
}



EXPORT  void *
zrealloc (void	  * ptr,
	  size_t    size)

{
    void		* memory;

    if (ptr == NULL)
	return  zmalloc (size);
    if (REFCOUNT (ptr) > 1)
    {
	memory = zmalloc (size);
	if (memory == NULL)
	    return  NULL;
	memcpy (memory, ptr, MIN (size, AREA_SIZE (ptr)));
	--REFCOUNT (ptr);
	return  memory;
    }
    else
    {
	memory = realloc ((char *) ptr - sizeof (z_info),
			  size + sizeof (z_info));
	return  (char *) memory + sizeof (z_info);
    }
}



EXPORT  void
zfree (void	* ptr)
{
    if (ptr != NULL && --REFCOUNT (ptr) <= 0)
    {
	free ((char *) ptr - sizeof (z_info));
    }
}



EXPORT  void
zdestruct (void	  * ptr)
{
    if (ptr != NULL)
	free ((char *) ptr - sizeof (z_info));
}



EXPORT  void *
zuse (void	* ptr)
{
    if (ptr != NULL)
    {
	++REFCOUNT (ptr);
    }
    return  ptr;
}



EXPORT  void *
zown (void	* ptr)
{
    void	* memory;

    if (ptr == NULL)
	return  NULL;
    if (REFCOUNT (ptr) == 1)
	return  ptr;
    else
    {
	if ((memory = malloc (AREA_SIZE (ptr))) == NULL)
	    return  NULL;
	memcpy (memory, ptr, AREA_SIZE (ptr));
	zfree (ptr);

	return  memory;
    }
}