1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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
|
/*
* tardy - a tar post-processor
* Copyright (C) 1991-1995, 1998-1999, 2001 Peter Miller;
* All rights reserved.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate dynamic memory
*/
#include <ac/stddef.h>
#include <ac/string.h>
#include <ac/stdlib.h>
#include <ac/errno.h>
#include <mem.h>
#include <error.h>
#ifdef _AIX
/*
* Yet another AIX stupidity:
* malloc does not guarantee that the space is available in swap.
*/
#include <signal.h>
#include <setjmp.h>
static jmp_buf aix_bungy;
/*
* Catch SIGDANGER and longjmp to aix_touch.
*/
static void
aix_danger(int n)
{
longjmp(aix_bungy, 1);
}
/*
* Touch the pages that cover [p, p+nbytes-1].
*/
static int
aix_touch(void *vp, size_t nbytes)
{
char *p;
char *endp;
int pgsize;
volatile char c;
void (*oldsig)_((int));
oldsig = signal(SIGDANGER, aix_danger);
if (setjmp(aix_bungy))
{
signal(SIGDANGER, oldsig);
return -1;
}
/*
* A load is enough to cause the
* allocation of the paging space
*/
p = vp;
pgsize = getpagesize();
endp = p + nbytes;
while (p < endp)
{
c = *(volatile char *)p;
p += pgsize;
}
/*
* restore the signal handler
*/
signal(SIGDANGER, oldsig);
return 0;
}
#endif
/*
* NAME
* mem_alloc - allocate memory
*
* SYNOPSIS
* char *mem_alloc(size_t n);
*
* DESCRIPTION
* mem_alloc uses malloc to allocate the required sized chunk of memory.
* If any error is returned from malloc() an fatal diagnostic is issued.
*
* CAVEAT
* It is the responsibility of the caller to ensure that the space is
* freed when finished with, by a call to free().
*/
void *
mem_alloc(size_t n)
{
void *cp;
if (n < 1)
n = 1;
errno = ENOMEM;
cp = malloc(n);
if (!cp)
nfatal("malloc");
#ifdef _AIX
/*
* watch out for AIX stupidity
*/
if (aix_touch(cp, n))
{
errno = ENOMEM;
nfatal("malloc");
}
#endif
return cp;
}
/*
* NAME
* mem_alloc_clear - allocate and clear memory
*
* SYNOPSIS
* char *mem_alloc_clear(size_t n);
*
* DESCRIPTION
* mem_alloc_clear uses malloc to allocate the required sized chunk of memory.
* If any error is returned from malloc() an fatal diagnostic is issued.
* The memory is zeroed befor it is returned.
*
* CAVEAT
* It is the responsibility of the caller to ensure that the space is
* freed when finished with, by a call to free().
*/
void *
mem_alloc_clear(size_t n)
{
void *cp;
cp = mem_alloc(n);
memset(cp, 0, n);
return cp;
}
void *
mem_change_size(void *cp, size_t n)
{
if (n < 1)
n = 1;
errno = ENOMEM;
if (!cp)
cp = malloc(n);
else
cp = realloc(cp, n);
if (!cp)
nfatal("realloc");
return cp;
}
void
mem_free(void *cp)
{
free(cp);
}
char *
mem_copy_string(const char *s)
{
char *cp;
cp = (char *)mem_alloc(strlen(s) + 1);
strcpy(cp, s);
return cp;
}
|