summaryrefslogtreecommitdiff
path: root/usr/src/lib/libresolv2/common/isc/memcluster.c
diff options
context:
space:
mode:
authorRao Shoaib <Rao.Shoaib@Sun.COM>2009-11-11 08:45:41 -0800
committerRao Shoaib <Rao.Shoaib@Sun.COM>2009-11-11 08:45:41 -0800
commit9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829 (patch)
treedf51891a276edf456c1481f49653a76cdfedee53 /usr/src/lib/libresolv2/common/isc/memcluster.c
parent0324f02a004039d6377111191fdd7134452d7817 (diff)
downloadillumos-gate-9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829.tar.gz
6289479 libresolv2 clean up and alignment with libbind.6.0
Diffstat (limited to 'usr/src/lib/libresolv2/common/isc/memcluster.c')
-rw-r--r--usr/src/lib/libresolv2/common/isc/memcluster.c146
1 files changed, 73 insertions, 73 deletions
diff --git a/usr/src/lib/libresolv2/common/isc/memcluster.c b/usr/src/lib/libresolv2/common/isc/memcluster.c
index 03fb6d3c8b..515793fd6a 100644
--- a/usr/src/lib/libresolv2/common/isc/memcluster.c
+++ b/usr/src/lib/libresolv2/common/isc/memcluster.c
@@ -1,26 +1,20 @@
/*
- * Copyright 1998-2002 Sun Microsystems, Inc. All rights reserved.
- * Use is subject to license terms.
- */
-
-/*
+ * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1997,1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
/* When this symbol is defined allocations via memget are made slightly
bigger and some debugging info stuck before and after the region given
@@ -30,7 +24,7 @@
#if !defined(LINT) && !defined(CODECENTER)
-static const char rcsid[] = "$Id: memcluster.c,v 8.23 2001/06/18 14:44:05 marka Exp $";
+static const char rcsid[] = "$Id: memcluster.c,v 1.11 2006/08/30 23:34:38 marka Exp $";
#endif /* not lint */
#include "port_before.h"
@@ -96,27 +90,28 @@ struct stats {
u_long freefrags;
};
-/* Private data. */
-
-#ifdef SUNW_MT_RESOLVER
-#include <thread.h>
-#include <synch.h>
-static mutex_t memlock = DEFAULTMUTEX;
-#define SUNW_MEMLOCK (void)mutex_lock(&memlock)
-#define SUNW_MEMUNLOCK (void)mutex_unlock(&memlock)
-#define SUNW_MEMLOCKBLOCK_BEGIN {
-#define SUNW_MEMLOCKBLOCK_END }
+#ifdef DO_PTHREADS
+#include <pthread.h>
+static pthread_mutex_t memlock = PTHREAD_MUTEX_INITIALIZER;
+#define MEMLOCK (void)pthread_mutex_lock(&memlock)
+#define MEMUNLOCK (void)pthread_mutex_unlock(&memlock)
#else
-#define SUNW_MEMLOCK
-#define SUNW_MEMUNLOCK
-#define SUNW_MEMLOCKBLOCK_BEGIN
-#define SUNW_MEMLOCKBLOCK_END
-#endif /* SUNW_MT_RESOLVER */
+/*
+ * Catch bad lock usage in non threaded build.
+ */
+static unsigned int memlock = 0;
+#define MEMLOCK do { INSIST(memlock == 0); memlock = 1; } while (0)
+#define MEMUNLOCK do { INSIST(memlock == 1); memlock = 0; } while (0)
+#endif /* DO_PTHEADS */
+
+/* Private data. */
static size_t max_size;
static size_t mem_target;
+#ifndef MEMCLUSTER_BIG_MALLOC
static size_t mem_target_half;
static size_t mem_target_fudge;
+#endif
static memcluster_element ** freelists;
#ifdef MEMCLUSTER_RECORD
static memcluster_element ** activelists;
@@ -145,16 +140,18 @@ meminit(size_t init_max_size, size_t target_size) {
errno = EEXIST;
return (-1);
}
- if (init_max_size == 0)
+ if (init_max_size == 0U)
max_size = DEF_MAX_SIZE;
else
max_size = init_max_size;
- if (target_size == 0)
+ if (target_size == 0U)
mem_target = DEF_MEM_TARGET;
else
mem_target = target_size;
+#ifndef MEMCLUSTER_BIG_MALLOC
mem_target_half = mem_target / 2;
mem_target_fudge = mem_target + mem_target / 4;
+#endif
freelists = malloc(max_size * sizeof (memcluster_element *));
stats = malloc((max_size+1) * sizeof (struct stats));
if (freelists == NULL || stats == NULL) {
@@ -194,20 +191,20 @@ __memget_record(size_t size, const char *file, int line) {
#endif
void *ret;
- SUNW_MEMLOCK;
+ MEMLOCK;
#if !defined(MEMCLUSTER_RECORD)
UNUSED(file);
UNUSED(line);
#endif
- if (freelists == NULL)
- if (meminit(0, 0) == -1)
-SUNW_MEMLOCKBLOCK_BEGIN
- SUNW_MEMUNLOCK;
+ if (freelists == NULL) {
+ if (meminit(0, 0) == -1) {
+ MEMUNLOCK;
return (NULL);
-SUNW_MEMLOCKBLOCK_END
- if (size == 0) {
- SUNW_MEMUNLOCK;
+ }
+ }
+ if (size == 0U) {
+ MEMUNLOCK;
errno = EINVAL;
return (NULL);
}
@@ -218,7 +215,7 @@ SUNW_MEMLOCKBLOCK_END
#if defined(DEBUGGING_MEMCLUSTER)
e = malloc(new_size);
if (e == NULL) {
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
@@ -230,13 +227,13 @@ SUNW_MEMLOCKBLOCK_END
e->next = activelists[max_size];
activelists[max_size] = e;
#endif
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
e->fencepost = FRONT_FENCEPOST;
p = (char *)e + sizeof *e + size;
memcpy(p, &fp, sizeof fp);
return ((char *)e + sizeof *e);
#else
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
return (malloc(size));
#endif
}
@@ -256,7 +253,7 @@ SUNW_MEMLOCKBLOCK_END
if (basic_blocks == NULL) {
new = malloc(NUM_BASIC_BLOCKS * mem_target);
if (new == NULL) {
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
@@ -284,7 +281,7 @@ SUNW_MEMLOCKBLOCK_END
total_size = mem_target;
new = malloc(total_size);
if (new == NULL) {
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
@@ -350,7 +347,7 @@ SUNW_MEMLOCKBLOCK_END
stats[size].gets++;
stats[size].totalgets++;
stats[new_size].freefrags--;
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
#if defined(DEBUGGING_MEMCLUSTER)
return ((char *)e + sizeof *e);
#else
@@ -358,7 +355,7 @@ SUNW_MEMLOCKBLOCK_END
#endif
}
-/*
+/*%
* This is a call from an external caller,
* so we want to count this as a user "put".
*/
@@ -380,7 +377,7 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
char *p;
#endif
- SUNW_MEMLOCK;
+ MEMLOCK;
#if !defined (MEMCLUSTER_RECORD)
UNUSED(file);
@@ -389,8 +386,8 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
REQUIRE(freelists != NULL);
- if (size == 0) {
- SUNW_MEMUNLOCK;
+ if (size == 0U) {
+ MEMUNLOCK;
errno = EINVAL;
return;
}
@@ -402,7 +399,7 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
p = (char *)e + sizeof *e + size;
memcpy(&fp, p, sizeof fp);
INSIST(fp == BACK_FENCEPOST);
- INSIST(((int)mem % 4) == 0);
+ INSIST(((u_long)mem % 4) == 0);
#ifdef MEMCLUSTER_RECORD
prev = NULL;
if (size == max_size || new_size >= max_size)
@@ -413,7 +410,7 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
prev = el;
el = el->next;
}
- INSIST(el != NULL); /* double free */
+ INSIST(el != NULL); /*%< double free */
if (prev == NULL) {
if (size == max_size || new_size >= max_size)
activelists[max_size] = el->next;
@@ -432,16 +429,16 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
free(mem);
#endif
- INSIST(stats[max_size].gets != 0);
+ INSIST(stats[max_size].gets != 0U);
stats[max_size].gets--;
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
return;
}
/* The free list uses the "rounded-up" size "new_size": */
#if defined(DEBUGGING_MEMCLUSTER)
- memset(mem, 0xa5, new_size - sizeof *e); /* catch write after free */
- e->size = 0; /* catch double memput() */
+ memset(mem, 0xa5, new_size - sizeof *e); /*%< catch write after free */
+ e->size = 0; /*%< catch double memput() */
#ifdef MEMCLUSTER_RECORD
e->file = file;
e->line = line;
@@ -470,10 +467,10 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
* max. size (max_size) ends up getting recorded as a call to
* max_size.
*/
- INSIST(stats[size].gets != 0);
+ INSIST(stats[size].gets != 0U);
stats[size].gets--;
stats[new_size].freefrags++;
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
}
void *
@@ -492,7 +489,7 @@ __memput_debug(void *ptr, size_t size, const char *file, int line) {
__memput_record(ptr, size, file, line);
}
-/*
+/*%
* Print the stats[] on the stream "out" with suitable formatting.
*/
void
@@ -502,21 +499,21 @@ memstats(FILE *out) {
memcluster_element *e;
#endif
- SUNW_MEMLOCK;
- if (freelists == NULL)
-SUNW_MEMLOCKBLOCK_BEGIN
- SUNW_MEMUNLOCK;
+ MEMLOCK;
+
+ if (freelists == NULL) {
+ MEMUNLOCK;
return;
-SUNW_MEMLOCKBLOCK_END
+ }
for (i = 1; i <= max_size; i++) {
const struct stats *s = &stats[i];
- if (s->totalgets == 0 && s->gets == 0)
+ if (s->totalgets == 0U && s->gets == 0U)
continue;
- fprintf(out, "%s%5d: %11lu gets, %11lu rem",
+ fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
(i == max_size) ? ">=" : " ",
- i, s->totalgets, s->gets);
- if (s->blocks != 0)
+ (unsigned long)i, s->totalgets, s->gets);
+ if (s->blocks != 0U)
fprintf(out, " (%lu bl, %lu ff)",
s->blocks, s->freefrags);
fputc('\n', out);
@@ -526,15 +523,16 @@ SUNW_MEMLOCKBLOCK_END
for (i = 1; i <= max_size; i++) {
if ((e = activelists[i]) != NULL)
while (e != NULL) {
- fprintf(out, "%s:%d %p:%d\n",
+ fprintf(out, "%s:%d %p:%lu\n",
e->file != NULL ? e->file :
"<UNKNOWN>", e->line,
- (char *)e + sizeof *e, e->size);
+ (char *)e + sizeof *e,
+ (u_long)e->size);
e = e->next;
}
}
#endif
- SUNW_MEMUNLOCK;
+ MEMUNLOCK;
}
int
@@ -544,14 +542,14 @@ memactive(void) {
if (stats == NULL)
return (0);
for (i = 1; i <= max_size; i++)
- if (stats[i].gets != 0)
+ if (stats[i].gets != 0U)
return (1);
return (0);
}
/* Private. */
-/*
+/*%
* Round up size to a multiple of sizeof(void *). This guarantees that a
* block is at least sizeof void *, and that we won't violate alignment
* restrictions, both of which are needed to make lists of blocks.
@@ -586,3 +584,5 @@ check(unsigned char *a, int value, size_t len) {
INSIST(a[i] == value);
}
#endif
+
+/*! \file */