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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
/*
* $Id: util.c,v 1.29.2.2 1997/06/06 04:39:18 wessels Exp $
*
* DEBUG:
* AUTHOR: Harvest Derived
*
* SQUID Internet Object Cache http://squid.nlanr.net/Squid/
* --------------------------------------------------------
*
* Squid is the result of efforts by numerous individuals from the
* Internet community. Development is led by Duane Wessels of the
* National Laboratory for Applied Network Research and funded by
* the National Science Foundation.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* Copyright (c) 1994, 1995. All rights reserved.
*
* The Harvest software was developed by the Internet Research Task
* Force Research Group on Resource Discovery (IRTF-RD):
*
* Mic Bowman of Transarc Corporation.
* Peter Danzig of the University of Southern California.
* Darren R. Hardy of the University of Colorado at Boulder.
* Udi Manber of the University of Arizona.
* Michael F. Schwartz of the University of Colorado at Boulder.
* Duane Wessels of the University of Colorado at Boulder.
*
* This copyright notice applies to software in the Harvest
* ``src/'' directory only. Users should consult the individual
* copyright notices in the ``components/'' subdirectories for
* copyright information about other software bundled with the
* Harvest source code distribution.
*
* TERMS OF USE
*
* The Harvest software may be used and re-distributed without
* charge, provided that the software origin and research team are
* cited in any use of the system. Most commonly this is
* accomplished by including a link to the Harvest Home Page
* (http://harvest.cs.colorado.edu/) from the query page of any
* Broker you deploy, as well as in the query result pages. These
* links are generated automatically by the standard Broker
* software distribution.
*
* The Harvest software is provided ``as is'', without express or
* implied warranty, and with no support nor obligation to assist
* in its use, correction, modification or enhancement. We assume
* no liability with respect to the infringement of copyrights,
* trade secrets, or any patents, and are not responsible for
* consequential damages. Proper use of the Harvest software is
* entirely the responsibility of the user.
*
* DERIVATIVE WORKS
*
* Users may make derivative works from the Harvest software, subject
* to the following constraints:
*
* - You must include the above copyright notice and these
* accompanying paragraphs in all forms of derivative works,
* and any documentation and other materials related to such
* distribution and use acknowledge that the software was
* developed at the above institutions.
*
* - You must notify IRTF-RD regarding your distribution of
* the derivative work.
*
* - You must clearly notify users that your are distributing
* a modified version and not the original Harvest software.
*
* - Any derivative product is also subject to these copyright
* and use restrictions.
*
* Note that the Harvest software is NOT in the public domain. We
* retain copyright, as specified above.
*
* HISTORY OF FREE SOFTWARE STATUS
*
* Originally we required sites to license the software in cases
* where they were going to build commercial products/services
* around Harvest. In June 1995 we changed this policy. We now
* allow people to use the core Harvest software (the code found in
* the Harvest ``src/'' directory) for free. We made this change
* in the interest of encouraging the widest possible deployment of
* the technology. The Harvest software is really a reference
* implementation of a set of protocols and formats, some of which
* we intend to standardize. We encourage commercial
* re-implementations of code complying to this set of standards.
*/
#include "config.h"
#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_GNUMALLLOC_H
#include <gnumalloc.h>
#elif HAVE_MALLOC_H && !defined(_SQUID_FREEBSD_) && !defined(_SQUID_NEXT_)
#include <malloc.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include "ansiproto.h"
#include "util.h"
void (*failure_notify) _PARAMS((const char *)) = NULL;
static char msg[128];
extern int sys_nerr;
#if XMALLOC_STATISTICS
#define DBG_MAXSIZE (1024*1024)
#define DBG_GRAIN (16)
#define DBG_MAXINDEX (DBG_MAXSIZE/DBG_GRAIN)
#define DBG_INDEX(sz) (sz<DBG_MAXSIZE?(sz+DBG_GRAIN-1)/DBG_GRAIN:DBG_MAXINDEX)
static int malloc_sizes[DBG_MAXINDEX + 1];
static int dbg_stat_init = 0;
static void
stat_init(void)
{
int i;
for (i = 0; i <= DBG_MAXINDEX; i++)
malloc_sizes[i] = 0;
dbg_stat_init = 1;
}
static int
malloc_stat(int sz)
{
if (!dbg_stat_init)
stat_init();
return malloc_sizes[DBG_INDEX(sz)] += 1;
}
void
malloc_statistics(void (*func) (int, int, void *), void *data)
{
int i;
for (i = 0; i <= DBG_MAXSIZE; i += DBG_GRAIN)
func(i, malloc_sizes[DBG_INDEX(i)], data);
}
#endif /* XMALLOC_STATISTICS */
#if XMALLOC_DEBUG
#define DBG_ARRY_SZ (1<<10)
#define DBG_ARRY_BKTS (1<<8)
static void *malloc_ptrs[DBG_ARRY_BKTS][DBG_ARRY_SZ];
static int malloc_size[DBG_ARRY_BKTS][DBG_ARRY_SZ];
static int dbg_initd = 0;
static int B = 0;
static int I = 0;
static void *P;
static void *Q;
static void
check_init(void)
{
for (B = 0; B < DBG_ARRY_BKTS; B++) {
for (I = 0; I < DBG_ARRY_SZ; I++) {
malloc_ptrs[B][I] = NULL;
malloc_size[B][I] = 0;
}
}
dbg_initd = 1;
}
static void
check_free(void *s)
{
B = (((int) s) >> 4) & 0xFF;
for (I = 0; I < DBG_ARRY_SZ; I++) {
if (malloc_ptrs[B][I] != s)
continue;
malloc_ptrs[B][I] = NULL;
malloc_size[B][I] = 0;
break;
}
if (I == DBG_ARRY_SZ) {
sprintf(msg, "xfree: ERROR: s=%p not found!", s);
(*failure_notify) (msg);
}
}
static void
check_malloc(void *p, size_t sz)
{
if (!dbg_initd)
check_init();
B = (((int) p) >> 4) & 0xFF;
for (I = 0; I < DBG_ARRY_SZ; I++) {
if ((P = malloc_ptrs[B][I]) == NULL)
continue;
Q = P + malloc_size[B][I];
if (P <= p && p < Q) {
sprintf(msg, "xmalloc: ERROR: p=%p falls in P=%p+%d",
p, P, malloc_size[B][I]);
(*failure_notify) (msg);
}
}
for (I = 0; I < DBG_ARRY_SZ; I++) {
if ((P = malloc_ptrs[B][I]))
continue;
malloc_ptrs[B][I] = p;
malloc_size[B][I] = (int) sz;
break;
}
if (I == DBG_ARRY_SZ)
(*failure_notify) ("xmalloc: debug out of array space!");
}
#endif
#if XMALLOC_COUNT && !HAVE_MALLOCBLKSIZE
int
mallocblksize(void *p)
{
B = (((int) p) >> 4) & 0xFF;
for (I = 0; I < DBG_ARRY_SZ; I++) {
if (malloc_ptrs[B][I] == p)
return malloc_size[B][I];
}
return 0;
}
#endif
#ifdef XMALLOC_COUNT
static void
xmalloc_count(void *p, int sign)
{
size_t sz;
static size_t total = 0;
int memoryAccounted();
int mallinfoTotal();
sz = mallocblksize(p) * sign;
total += sz;
fprintf(stderr, "xmalloc_count=%9d accounted=%9d mallinfo=%9d\n",
(int) total,
memoryAccounted(),
mallinfoTotal());
}
#endif /* XMALLOC_COUNT */
/*
* xmalloc() - same as malloc(3). Used for portability.
* Never returns NULL; fatal on error.
*/
void *
xmalloc(size_t sz)
{
static void *p;
if (sz < 1)
sz = 1;
if ((p = malloc(sz)) == NULL) {
if (failure_notify) {
sprintf(msg, "xmalloc: Unable to allocate %d bytes!\n",
(int) sz);
(*failure_notify) (msg);
} else {
perror("malloc");
}
exit(1);
}
#if XMALLOC_DEBUG
check_malloc(p, sz);
#endif
#if XMALLOC_STATISTICS
malloc_stat(sz);
#endif
#if XMALLOC_COUNT
xmalloc_count(p, 1);
#endif
return (p);
}
/*
* xfree() - same as free(3). Will not call free(3) if s == NULL.
*/
void
xfree(void *s)
{
#if XMALLOC_COUNT
xmalloc_count(s, -1);
#endif
#if XMALLOC_DEBUG
check_free(s);
#endif
if (s != NULL)
free(s);
}
/* xxfree() - like xfree(), but we already know s != NULL */
void
xxfree(void *s)
{
#if XMALLOC_COUNT
xmalloc_count(s, -1);
#endif
#if XMALLOC_DEBUG
check_free(s);
#endif
free(s);
}
/*
* xrealloc() - same as realloc(3). Used for portability.
* Never returns NULL; fatal on error.
*/
void *
xrealloc(void *s, size_t sz)
{
static void *p;
#if XMALLOC_COUNT
xmalloc_count(s, -1);
#endif
if (sz < 1)
sz = 1;
if ((p = realloc(s, sz)) == NULL) {
if (failure_notify) {
sprintf(msg, "xrealloc: Unable to reallocate %d bytes!\n",
(int) sz);
(*failure_notify) (msg);
} else {
perror("realloc");
}
exit(1);
}
#if XMALLOC_DEBUG
check_malloc(p, sz);
#endif
#if XMALLOC_STATISTICS
malloc_stat(sz);
#endif
#if XMALLOC_COUNT
xmalloc_count(p, 1);
#endif
return (p);
}
/*
* xcalloc() - same as calloc(3). Used for portability.
* Never returns NULL; fatal on error.
*/
void *
xcalloc(int n, size_t sz)
{
static void *p;
if (n < 1)
n = 1;
if (sz < 1)
sz = 1;
if ((p = calloc(n, sz)) == NULL) {
if (failure_notify) {
sprintf(msg, "xcalloc: Unable to allocate %d blocks of %d bytes!\n",
(int) n, (int) sz);
(*failure_notify) (msg);
} else {
perror("xcalloc");
}
exit(1);
}
#if XMALLOC_DEBUG
check_malloc(p, sz * n);
#endif
#if XMALLOC_STATISTICS
malloc_stat(sz);
#endif
#if XMALLOC_COUNT
xmalloc_count(p, 1);
#endif
return (p);
}
/*
* xstrdup() - same as strdup(3). Used for portability.
* Never returns NULL; fatal on error.
*/
char *
xstrdup(const char *s)
{
static char *p = NULL;
size_t sz;
if (s == NULL) {
if (failure_notify) {
(*failure_notify) ("xstrdup: tried to dup a NULL pointer!\n");
} else {
fprintf(stderr, "xstrdup: tried to dup a NULL pointer!\n");
}
exit(1);
}
sz = strlen(s);
p = xmalloc((size_t) sz + 1);
memcpy(p, s, sz); /* copy string */
p[sz] = '\0'; /* terminate string */
return (p);
}
/*
* xstrerror() - strerror() wrapper
*/
const char *
xstrerror(void)
{
static char xstrerror_buf[BUFSIZ];
if (errno < 0 || errno >= sys_nerr)
return ("Unknown");
sprintf(xstrerror_buf, "(%d) %s", errno, strerror(errno));
return xstrerror_buf;
}
void
Tolower(char *q)
{
char *s = q;
while (*s) {
*s = tolower((unsigned char) *s);
s++;
}
}
int
tvSubMsec(struct timeval t1, struct timeval t2)
{
return (t2.tv_sec - t1.tv_sec) * 1000 +
(t2.tv_usec - t1.tv_usec) / 1000;
}
int
tvSubUsec(struct timeval t1, struct timeval t2)
{
return (t2.tv_sec - t1.tv_sec) * 1000000 +
(t2.tv_usec - t1.tv_usec);
}
/*
* xstrncpy() - similar to strncpy(3) but terminates string
* always with '\0' if n != 0, and doesn't do padding
*/
char *
xstrncpy(char *dst, const char *src, size_t n)
{
if (n == 0)
return dst;
if (src == NULL)
return dst;
while (--n != 0 && *src != '\0')
*dst++ = *src++;
*dst = '\0';
return dst;
}
|