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
|
/*****************************************************************************\
*
* $Id: xmalloc.c,v 1.13 2002/08/11 02:47:42 garlick Exp $
* $Source: /chaos/cvs/pdsh/xmalloc.c,v $
*
* Copyright (C) 1998-2002 The Regents of the University of California.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* Written by Jim Garlick (garlick@llnl.gov>
* UCRL-CODE-980038
*
* This file is part of PDSH, a parallel remote shell program.
* For details, see <http://www.llnl.gov/linux/pdsh/>.
*
* PDSH 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.
*
* PDSH 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 PDSH; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\*****************************************************************************/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <limits.h> /* for INT_MAX */
#include <assert.h>
#include "xmalloc.h"
#if HAVE_UNSAFE_MALLOC
#include <pthread.h>
static pthread_mutex_t malloc_lock = PTHREAD_MUTEX_INITIALIZER;
#define MALLOC_LOCK() pthread_mutex_lock(&malloc_lock)
#define MALLOC_UNLOCK() pthread_mutex_unlock(&malloc_lock)
#else
#define MALLOC_LOCK()
#define MALLOC_UNLOCK()
#endif
/*
* "Safe" version of malloc().
* size (IN) number of bytes to malloc
* RETURN pointer to allocate heap space
*/
void *
Malloc(size_t size)
{
void *new;
int *p;
assert(size > 0 && size <= INT_MAX);
MALLOC_LOCK();
p = (int *)malloc(size + 2*sizeof(int));
MALLOC_UNLOCK();
if (!p) {
fprintf(stderr, "Malloc(%d) failed\n", size);
exit(1);
}
p[0] = XMALLOC_MAGIC; /* add "secret" magic cookie */
p[1] = size; /* store size in buffer */
new = &p[2];
memset(new, 0, size);
return new;
}
/*
* "Safe" version of realloc(). Args are different: pass in a pointer to
* the object to be realloced instead of the object itself.
* item (IN/OUT) double-pointer to allocated space
* newsize (IN) requested size
*/
void
Realloc(void **item, size_t newsize)
{
int *p = (int *)*item - 2;
assert(*item != NULL);
assert(newsize > 0 && newsize <= INT_MAX);
assert(p[0] == XMALLOC_MAGIC); /* magic cookie still there? */
MALLOC_LOCK();
p = (int *)realloc(p, newsize + 2*sizeof(int));
MALLOC_UNLOCK();
if (!p) {
fprintf(stderr, "Realloc(%d) failed\n", newsize);
exit(1);
}
assert(p[0] == XMALLOC_MAGIC);
p[1] = newsize;
*item = &p[2];
}
/*
* Duplicate a string.
* str (IN) string to duplicate
* RETURN copy of string
*/
char *
Strdup(char *str)
{
char *result = Malloc(strlen(str) + 1);
return strcpy(result, str);
}
/*
* Return the size of a buffer.
* item (IN) pointer to allocated space
*/
int
Size(void *item)
{
int *p = (int *)item - 2;
assert(item != NULL);
assert(p[0] == XMALLOC_MAGIC);
return p[1];
}
/*
* Free which takes a pointer to object to free, which it turns into a null
* object.
* item (IN/OUT) double-pointer to allocated space
*/
void
Free(void **item)
{
int *p = (int *)*item - 2;
if (*item != NULL) {
assert(p[0] == XMALLOC_MAGIC); /* magic cookie still there? */
p[0] = 0;
MALLOC_LOCK();
free(p);
MALLOC_UNLOCK();
*item = NULL;
}
}
|