zmalloc.c 4.24 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: zmalloc.c,v 1.14 1998/07/08 13:41:37 ceder Exp $
3
 * Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
/*
 *  zmalloc.c
 *		
 *
 *
30
 *  Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995	Lysator Computer Club,
Per Cederqvist's avatar
Per Cederqvist committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 *			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.
 */


51 52
static const char *
rcsid = "$Id: zmalloc.c,v 1.14 1998/07/08 13:41:37 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
53

Per Cederqvist's avatar
Per Cederqvist committed
54 55 56 57
#include <sys/types.h>
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
Linus Tolke Y's avatar
Linus Tolke Y committed
58
#ifdef HAVE_STDLIB_H
59
#  include <stdlib.h>
Linus Tolke Y's avatar
Linus Tolke Y committed
60
#endif
Linus Tolke Y's avatar
Linus Tolke Y committed
61 62

#if STDC_HEADERS || HAVE_STRING_H
63 64 65 66
#  include <string.h>
#  if !STDC_HEADERS && HAVE_MEMORY_H
#    include <memory.h>
#  endif
Linus Tolke Y's avatar
Linus Tolke Y committed
67
#else /* not STDC_HEADERS and not HAVE_STRING_H */
68 69
#  include <strings.h>
   /* memory.h and strings.h conflict on some systems.  */
Linus Tolke Y's avatar
Linus Tolke Y committed
70
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
Per Cederqvist's avatar
Per Cederqvist committed
71 72 73
#ifndef NULL
#  include <stdio.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
74

Per Cederqvist's avatar
Per Cederqvist committed
75
#include "compiler.h"
Per Cederqvist's avatar
Per Cederqvist committed
76
#include "zmalloc.h"
77 78
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
79 80 81 82 83 84 85 86 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


#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;
    }
}