summaryrefslogtreecommitdiffstats
path: root/contrib/bind9/lib/bind/isc/memcluster.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/bind9/lib/bind/isc/memcluster.c')
-rw-r--r--contrib/bind9/lib/bind/isc/memcluster.c50
1 files changed, 45 insertions, 5 deletions
diff --git a/contrib/bind9/lib/bind/isc/memcluster.c b/contrib/bind9/lib/bind/isc/memcluster.c
index 0632ec7..c5b7202 100644
--- a/contrib/bind9/lib/bind/isc/memcluster.c
+++ b/contrib/bind9/lib/bind/isc/memcluster.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * 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
@@ -24,7 +24,7 @@
#if !defined(LINT) && !defined(CODECENTER)
-static const char rcsid[] = "$Id: memcluster.c,v 1.3.206.4 2004/09/16 00:57:34 marka Exp $";
+static const char rcsid[] = "$Id: memcluster.c,v 1.3.206.7 2005/10/11 00:48:15 marka Exp $";
#endif /* not lint */
#include "port_before.h"
@@ -90,12 +90,28 @@ struct stats {
u_long freefrags;
};
+#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
+/*
+ * 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;
@@ -132,8 +148,10 @@ meminit(size_t init_max_size, size_t target_size) {
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) {
@@ -173,14 +191,20 @@ __memget_record(size_t size, const char *file, int line) {
#endif
void *ret;
+ MEMLOCK;
+
#if !defined(MEMCLUSTER_RECORD)
UNUSED(file);
UNUSED(line);
#endif
- if (freelists == NULL)
- if (meminit(0, 0) == -1)
+ if (freelists == NULL) {
+ if (meminit(0, 0) == -1) {
+ MEMUNLOCK;
return (NULL);
+ }
+ }
if (size == 0U) {
+ MEMUNLOCK;
errno = EINVAL;
return (NULL);
}
@@ -191,6 +215,7 @@ __memget_record(size_t size, const char *file, int line) {
#if defined(DEBUGGING_MEMCLUSTER)
e = malloc(new_size);
if (e == NULL) {
+ MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
@@ -202,11 +227,13 @@ __memget_record(size_t size, const char *file, int line) {
e->next = activelists[max_size];
activelists[max_size] = e;
#endif
+ MEMUNLOCK;
e->fencepost = FRONT_FENCEPOST;
p = (char *)e + sizeof *e + size;
memcpy(p, &fp, sizeof fp);
return ((char *)e + sizeof *e);
#else
+ MEMUNLOCK;
return (malloc(size));
#endif
}
@@ -226,6 +253,7 @@ __memget_record(size_t size, const char *file, int line) {
if (basic_blocks == NULL) {
new = malloc(NUM_BASIC_BLOCKS * mem_target);
if (new == NULL) {
+ MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
@@ -253,6 +281,7 @@ __memget_record(size_t size, const char *file, int line) {
total_size = mem_target;
new = malloc(total_size);
if (new == NULL) {
+ MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
@@ -318,6 +347,7 @@ __memget_record(size_t size, const char *file, int line) {
stats[size].gets++;
stats[size].totalgets++;
stats[new_size].freefrags--;
+ MEMUNLOCK;
#if defined(DEBUGGING_MEMCLUSTER)
return ((char *)e + sizeof *e);
#else
@@ -347,6 +377,8 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
char *p;
#endif
+ MEMLOCK;
+
#if !defined (MEMCLUSTER_RECORD)
UNUSED(file);
UNUSED(line);
@@ -355,6 +387,7 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
REQUIRE(freelists != NULL);
if (size == 0U) {
+ MEMUNLOCK;
errno = EINVAL;
return;
}
@@ -398,6 +431,7 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
INSIST(stats[max_size].gets != 0U);
stats[max_size].gets--;
+ MEMUNLOCK;
return;
}
@@ -436,6 +470,7 @@ __memput_record(void *mem, size_t size, const char *file, int line) {
INSIST(stats[size].gets != 0U);
stats[size].gets--;
stats[new_size].freefrags++;
+ MEMUNLOCK;
}
void *
@@ -464,8 +499,12 @@ memstats(FILE *out) {
memcluster_element *e;
#endif
- if (freelists == NULL)
+ MEMLOCK;
+
+ if (freelists == NULL) {
+ MEMUNLOCK;
return;
+ }
for (i = 1; i <= max_size; i++) {
const struct stats *s = &stats[i];
@@ -492,6 +531,7 @@ memstats(FILE *out) {
}
}
#endif
+ MEMUNLOCK;
}
int
OpenPOWER on IntegriCloud