1998-08-17 22:05:58 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1998-08-17 22:05:58 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-11-12 11:50:52 +01:00
|
|
|
#include <errno.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.h>
|
1998-08-17 22:05:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
#include <isc/align.h>
|
2013-04-10 13:49:57 -07:00
|
|
|
#include <isc/bind9.h>
|
2018-11-16 12:23:36 +11:00
|
|
|
#include <isc/hash.h>
|
2001-06-04 19:33:39 +00:00
|
|
|
#include <isc/magic.h>
|
1998-12-11 20:38:46 +00:00
|
|
|
#include <isc/mem.h>
|
2018-11-16 12:23:36 +11:00
|
|
|
#include <isc/mutex.h>
|
2006-01-04 03:16:47 +00:00
|
|
|
#include <isc/once.h>
|
2021-12-14 21:50:23 +01:00
|
|
|
#include <isc/os.h>
|
2018-11-16 12:23:36 +11:00
|
|
|
#include <isc/print.h>
|
2018-05-24 14:43:25 +02:00
|
|
|
#include <isc/refcount.h>
|
2018-11-16 12:23:36 +11:00
|
|
|
#include <isc/strerr.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2021-02-04 20:19:09 +01:00
|
|
|
#include <isc/types.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
2019-06-24 14:25:55 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
#include <libxml/xmlwriter.h>
|
|
|
|
#define ISC_XMLCHAR (const xmlChar *)
|
|
|
|
#endif /* HAVE_LIBXML2 */
|
1998-08-18 19:28:30 +00:00
|
|
|
|
2019-06-24 12:21:47 +02:00
|
|
|
#ifdef HAVE_JSON_C
|
|
|
|
#include <json_object.h>
|
|
|
|
#endif /* HAVE_JSON_C */
|
|
|
|
|
Use system allocator when jemalloc is unavailable
This commit adds support for systems where the jemalloc library is not
available as a package, here's the quick summary:
* On Linux - the jemalloc is usually available as a package, if
configured --without-jemalloc, the shim would be used around
malloc(), free(), realloc() and malloc_usable_size()
* On macOS - the jemalloc is available from homebrew or macports, if
configured --without-jemalloc, the shim would be used around
malloc(), free(), realloc() and malloc_size()
* On FreeBSD - the jemalloc is *the* system allocator, we just need
to check for <malloc_np.h> header to get access to non-standard API
* On NetBSD - the jemalloc is *the* system allocator, we just need to
check for <jemalloc/jemalloc.h> header to get access to non-standard
API
* On a system hostile to users and developers (read OpenBSD) - the
jemalloc API is emulated by using ((size_t *)ptr)[-1] field to hold
the size information. The OpenBSD developers care only for
themselves, so why should we care about speed on OpenBSD?
2021-05-25 12:46:00 +02:00
|
|
|
#if defined(HAVE_MALLOC_NP_H)
|
|
|
|
#include <malloc_np.h>
|
|
|
|
#elif defined(HAVE_JEMALLOC)
|
2021-05-11 12:33:31 +02:00
|
|
|
#include <jemalloc/jemalloc.h>
|
|
|
|
|
Use system allocator when jemalloc is unavailable
This commit adds support for systems where the jemalloc library is not
available as a package, here's the quick summary:
* On Linux - the jemalloc is usually available as a package, if
configured --without-jemalloc, the shim would be used around
malloc(), free(), realloc() and malloc_usable_size()
* On macOS - the jemalloc is available from homebrew or macports, if
configured --without-jemalloc, the shim would be used around
malloc(), free(), realloc() and malloc_size()
* On FreeBSD - the jemalloc is *the* system allocator, we just need
to check for <malloc_np.h> header to get access to non-standard API
* On NetBSD - the jemalloc is *the* system allocator, we just need to
check for <jemalloc/jemalloc.h> header to get access to non-standard
API
* On a system hostile to users and developers (read OpenBSD) - the
jemalloc API is emulated by using ((size_t *)ptr)[-1] field to hold
the size information. The OpenBSD developers care only for
themselves, so why should we care about speed on OpenBSD?
2021-05-25 12:46:00 +02:00
|
|
|
#if JEMALLOC_VERSION_MAJOR < 4
|
|
|
|
#define sdallocx(ptr, size, flags) dallocx(ptr, flags)
|
|
|
|
#endif /* JEMALLOC_VERSION_MAJOR < 4 */
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include "jemalloc_shim.h"
|
|
|
|
#endif
|
|
|
|
|
2018-10-03 19:04:46 -07:00
|
|
|
#include "mem_p.h"
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define MCTXLOCK(m) LOCK(&m->lock)
|
2019-09-06 12:46:57 +02:00
|
|
|
#define MCTXUNLOCK(m) UNLOCK(&m->lock)
|
2005-06-04 05:32:50 +00:00
|
|
|
|
2001-06-28 01:51:20 +00:00
|
|
|
#ifndef ISC_MEM_DEBUGGING
|
|
|
|
#define ISC_MEM_DEBUGGING 0
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef ISC_MEM_DEBUGGING */
|
2021-06-10 08:06:48 +02:00
|
|
|
unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
|
|
|
|
unsigned int isc_mem_defaultflags = ISC_MEMFLAG_DEFAULT;
|
1999-10-27 20:55:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Constants.
|
|
|
|
*/
|
|
|
|
|
2021-09-23 21:19:25 +02:00
|
|
|
#define ZERO_ALLOCATION_SIZE sizeof(void *)
|
|
|
|
#define ALIGNMENT 8U /*%< must be a power of 2 */
|
|
|
|
#define ALIGNMENT_SIZE sizeof(size_info)
|
|
|
|
#define DEBUG_TABLE_COUNT 512U
|
|
|
|
#define STATS_BUCKETS 512U
|
|
|
|
#define STATS_BUCKET_SIZE 32U
|
1999-10-27 20:55:21 +00:00
|
|
|
|
1998-08-17 22:05:58 +00:00
|
|
|
/*
|
|
|
|
* Types.
|
|
|
|
*/
|
2000-12-01 00:32:02 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2000-07-26 19:06:20 +00:00
|
|
|
typedef struct debuglink debuglink_t;
|
|
|
|
struct debuglink {
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_LINK(debuglink_t) link;
|
2020-02-13 14:44:37 -08:00
|
|
|
const void *ptr;
|
|
|
|
size_t size;
|
|
|
|
const char *file;
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int line;
|
2000-07-26 19:06:20 +00:00
|
|
|
};
|
1998-08-17 22:05:58 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
typedef ISC_LIST(debuglink_t) debuglist_t;
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define FLARG_PASS , file, line
|
2020-02-13 14:44:37 -08:00
|
|
|
#define FLARG , const char *file, unsigned int line
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if ISC_MEM_TRACKLINES */
|
2000-07-26 19:06:20 +00:00
|
|
|
#define FLARG_PASS
|
|
|
|
#define FLARG
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2000-02-01 00:18:35 +00:00
|
|
|
|
2000-07-26 19:06:20 +00:00
|
|
|
typedef struct element element;
|
2000-02-01 00:18:35 +00:00
|
|
|
struct element {
|
2020-02-12 13:59:18 +01:00
|
|
|
element *next;
|
2000-02-01 00:18:35 +00:00
|
|
|
};
|
1998-08-17 22:05:58 +00:00
|
|
|
|
|
|
|
struct stats {
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_size_t gets;
|
|
|
|
atomic_size_t totalgets;
|
1998-08-17 22:05:58 +00:00
|
|
|
};
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
|
1998-12-13 02:04:56 +00:00
|
|
|
|
2006-01-04 03:16:47 +00:00
|
|
|
/* List of all active memory contexts. */
|
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
static ISC_LIST(isc_mem_t) contexts;
|
2013-04-10 13:49:57 -07:00
|
|
|
|
2021-02-09 17:44:40 +01:00
|
|
|
static isc_once_t init_once = ISC_ONCE_INIT;
|
|
|
|
static isc_once_t shut_once = ISC_ONCE_INIT;
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_mutex_t contextslock;
|
2006-01-04 03:16:47 +00:00
|
|
|
|
2008-03-31 05:00:30 +00:00
|
|
|
/*%
|
|
|
|
* Total size of lost memory due to a bug of external library.
|
|
|
|
* Locked by the global lock.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
static uint64_t totallost;
|
2008-03-31 05:00:30 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
struct isc_mem {
|
|
|
|
unsigned int magic;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int flags;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
bool checkfree;
|
2021-02-04 21:56:49 +01:00
|
|
|
struct stats stats[STATS_BUCKETS + 1];
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_refcount_t references;
|
|
|
|
char name[16];
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_size_t total;
|
|
|
|
atomic_size_t inuse;
|
|
|
|
atomic_size_t maxinuse;
|
|
|
|
atomic_size_t malloced;
|
|
|
|
atomic_size_t maxmalloced;
|
|
|
|
atomic_bool hi_called;
|
|
|
|
atomic_bool is_overmem;
|
2021-07-22 06:14:32 +02:00
|
|
|
isc_mem_water_t water;
|
|
|
|
void *water_arg;
|
|
|
|
atomic_size_t hi_water;
|
|
|
|
atomic_size_t lo_water;
|
2021-02-04 20:19:09 +01:00
|
|
|
ISC_LIST(isc_mempool_t) pools;
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int poolcnt;
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2000-12-01 00:32:02 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2020-02-12 13:59:18 +01:00
|
|
|
debuglist_t *debuglist;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t debuglistcnt;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2001-06-11 20:27:16 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
ISC_LINK(isc_mem_t) link;
|
1999-06-08 02:38:30 +00:00
|
|
|
};
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
struct isc_mempool {
|
1999-09-15 17:47:08 +00:00
|
|
|
/* always unlocked */
|
2021-02-04 20:19:09 +01:00
|
|
|
unsigned int magic;
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mem_t *mctx; /*%< our memory context */
|
2021-02-04 20:19:09 +01:00
|
|
|
ISC_LINK(isc_mempool_t) link; /*%< next pool in this mem context */
|
2021-05-12 21:16:17 +02:00
|
|
|
element *items; /*%< low water item list */
|
|
|
|
size_t size; /*%< size of each item on this pool */
|
2021-07-09 14:01:22 +03:00
|
|
|
size_t allocated; /*%< # of items currently given out */
|
|
|
|
size_t freecount; /*%< # of items on reserved list */
|
|
|
|
size_t freemax; /*%< # of items allowed on free list */
|
|
|
|
size_t fillcount; /*%< # of items to fetch on each fill */
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%< Stats only. */
|
2021-07-09 14:01:22 +03:00
|
|
|
size_t gets; /*%< # of requests to this pool */
|
|
|
|
/*%< Debugging only. */
|
|
|
|
char name[16]; /*%< printed name in stats reports */
|
1998-08-17 22:05:58 +00:00
|
|
|
};
|
|
|
|
|
1999-10-27 20:55:21 +00:00
|
|
|
/*
|
|
|
|
* Private Inline-able.
|
|
|
|
*/
|
1998-08-17 22:05:58 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#if !ISC_MEM_TRACKLINES
|
2000-07-26 19:06:20 +00:00
|
|
|
#define ADD_TRACE(a, b, c, d, e)
|
|
|
|
#define DELETE_TRACE(a, b, c, d, e)
|
2012-10-25 18:56:47 -07:00
|
|
|
#define ISC_MEMFUNC_SCOPE
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if !ISC_MEM_TRACKLINES */
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
|
2021-02-04 21:56:49 +01:00
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
#define SHOULD_TRACE_OR_RECORD(ptr) \
|
|
|
|
((isc_mem_debugging & TRACE_OR_RECORD) != 0 && ptr != NULL)
|
2021-02-04 21:56:49 +01:00
|
|
|
|
|
|
|
#define ADD_TRACE(a, b, c, d, e) \
|
|
|
|
if (SHOULD_TRACE_OR_RECORD(b)) { \
|
|
|
|
add_trace_entry(a, b, c, d, e); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DELETE_TRACE(a, b, c, d, e) \
|
|
|
|
if (SHOULD_TRACE_OR_RECORD(b)) { \
|
|
|
|
delete_trace_entry(a, b, c, d, e); \
|
|
|
|
}
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
print_active(isc_mem_t *ctx, FILE *out);
|
2018-10-03 19:04:46 -07:00
|
|
|
#endif /* ISC_MEM_TRACKLINES */
|
|
|
|
|
2021-10-11 13:43:12 +02:00
|
|
|
static size_t
|
2021-02-04 21:56:49 +01:00
|
|
|
increment_malloced(isc_mem_t *ctx, size_t size) {
|
|
|
|
size_t malloced = atomic_fetch_add_relaxed(&ctx->malloced, size) + size;
|
2021-05-14 15:13:33 +02:00
|
|
|
size_t maxmalloced = atomic_load_relaxed(&ctx->maxmalloced);
|
2021-05-12 21:16:17 +02:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
if (malloced > maxmalloced) {
|
|
|
|
atomic_compare_exchange_strong(&ctx->maxmalloced, &maxmalloced,
|
|
|
|
malloced);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (malloced);
|
|
|
|
}
|
|
|
|
|
2021-10-11 13:43:12 +02:00
|
|
|
static size_t
|
2021-02-04 21:56:49 +01:00
|
|
|
decrement_malloced(isc_mem_t *ctx, size_t size) {
|
2021-05-14 15:13:33 +02:00
|
|
|
size_t malloced = atomic_fetch_sub_relaxed(&ctx->malloced, size) - size;
|
2021-02-04 21:56:49 +01:00
|
|
|
|
|
|
|
return (malloced);
|
|
|
|
}
|
|
|
|
|
2018-10-03 19:04:46 -07:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2021-02-04 21:56:49 +01:00
|
|
|
* mctx must not be locked.
|
2000-07-26 19:06:20 +00:00
|
|
|
*/
|
2017-08-24 10:58:20 +05:30
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
add_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
debuglink_t *dl = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t hash;
|
|
|
|
uint32_t idx;
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
MCTXLOCK(mctx);
|
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n",
|
2000-08-09 23:03:15 +00:00
|
|
|
ptr, size, file, line, mctx);
|
2018-11-23 21:35:01 +01:00
|
|
|
}
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (mctx->debuglist == NULL) {
|
2021-02-04 21:56:49 +01:00
|
|
|
goto unlock;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2020-02-18 09:40:21 +11:00
|
|
|
#ifdef __COVERITY__
|
|
|
|
/*
|
|
|
|
* Use simple conversion from pointer to hash to avoid
|
|
|
|
* tainting 'ptr' due to byte swap in isc_hash_function.
|
|
|
|
*/
|
|
|
|
hash = (uintptr_t)ptr >> 3;
|
|
|
|
#else
|
2019-04-04 13:51:09 +02:00
|
|
|
hash = isc_hash_function(&ptr, sizeof(ptr), true);
|
2020-02-18 09:40:21 +11:00
|
|
|
#endif
|
2017-08-24 10:58:20 +05:30
|
|
|
idx = hash % DEBUG_TABLE_COUNT;
|
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
dl = mallocx(sizeof(debuglink_t), 0);
|
2000-07-26 19:06:20 +00:00
|
|
|
INSIST(dl != NULL);
|
2021-02-04 21:56:49 +01:00
|
|
|
increment_malloced(mctx, sizeof(debuglink_t));
|
2000-07-26 19:06:20 +00:00
|
|
|
|
|
|
|
ISC_LINK_INIT(dl, link);
|
2017-08-24 10:58:20 +05:30
|
|
|
dl->ptr = ptr;
|
|
|
|
dl->size = size;
|
|
|
|
dl->file = file;
|
|
|
|
dl->line = line;
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2017-08-24 10:58:20 +05:30
|
|
|
ISC_LIST_PREPEND(mctx->debuglist[idx], dl, link);
|
2008-03-31 05:00:30 +00:00
|
|
|
mctx->debuglistcnt++;
|
2021-02-04 21:56:49 +01:00
|
|
|
unlock:
|
|
|
|
MCTXUNLOCK(mctx);
|
2000-07-26 19:06:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:58:20 +05:30
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size,
|
2020-02-13 14:44:37 -08:00
|
|
|
const char *file, unsigned int line) {
|
2021-05-12 21:16:17 +02:00
|
|
|
debuglink_t *dl = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t hash;
|
|
|
|
uint32_t idx;
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
MCTXLOCK(mctx);
|
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n",
|
2000-08-09 23:03:15 +00:00
|
|
|
ptr, size, file, line, mctx);
|
2018-11-23 21:35:01 +01:00
|
|
|
}
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (mctx->debuglist == NULL) {
|
2021-02-04 21:56:49 +01:00
|
|
|
goto unlock;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2020-02-18 09:40:21 +11:00
|
|
|
#ifdef __COVERITY__
|
|
|
|
/*
|
|
|
|
* Use simple conversion from pointer to hash to avoid
|
|
|
|
* tainting 'ptr' due to byte swap in isc_hash_function.
|
|
|
|
*/
|
|
|
|
hash = (uintptr_t)ptr >> 3;
|
|
|
|
#else
|
2019-04-04 13:51:09 +02:00
|
|
|
hash = isc_hash_function(&ptr, sizeof(ptr), true);
|
2020-02-18 09:40:21 +11:00
|
|
|
#endif
|
2017-08-24 10:58:20 +05:30
|
|
|
idx = hash % DEBUG_TABLE_COUNT;
|
|
|
|
|
|
|
|
dl = ISC_LIST_HEAD(mctx->debuglist[idx]);
|
2021-10-14 10:33:24 +02:00
|
|
|
while (dl != NULL) {
|
|
|
|
if (dl->ptr == ptr) {
|
2017-08-24 10:58:20 +05:30
|
|
|
ISC_LIST_UNLINK(mctx->debuglist[idx], dl, link);
|
2021-02-04 21:56:49 +01:00
|
|
|
decrement_malloced(mctx, sizeof(*dl));
|
2021-05-11 14:00:12 +02:00
|
|
|
sdallocx(dl, sizeof(*dl), 0);
|
2021-02-04 21:56:49 +01:00
|
|
|
goto unlock;
|
2000-07-26 19:06:20 +00:00
|
|
|
}
|
|
|
|
dl = ISC_LIST_NEXT(dl, link);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get here, we didn't find the item on the list. We're
|
|
|
|
* screwed.
|
|
|
|
*/
|
2021-10-11 12:50:17 +02:00
|
|
|
UNREACHABLE();
|
2021-02-04 21:56:49 +01:00
|
|
|
unlock:
|
|
|
|
MCTXUNLOCK(mctx);
|
2000-07-26 19:06:20 +00:00
|
|
|
}
|
|
|
|
#endif /* ISC_MEM_TRACKLINES */
|
|
|
|
|
2021-09-23 21:19:25 +02:00
|
|
|
#define ADJUST_ZERO_ALLOCATION_SIZE(s) \
|
2021-10-14 10:33:24 +02:00
|
|
|
if (s == 0) { \
|
2021-09-23 21:19:25 +02:00
|
|
|
s = ZERO_ALLOCATION_SIZE; \
|
|
|
|
}
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
#define MEM_ALIGN(a) ((a) ? MALLOCX_ALIGN(a) : 0)
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2001-01-25 01:18:00 +00:00
|
|
|
* Perform a malloc, doing memory filling and overrun detection as necessary.
|
|
|
|
*/
|
2021-10-11 13:43:12 +02:00
|
|
|
static void *
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_get(isc_mem_t *ctx, size_t size, int flags) {
|
2021-09-23 21:19:25 +02:00
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
ADJUST_ZERO_ALLOCATION_SIZE(size);
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
ret = mallocx(size, flags);
|
2021-09-29 10:58:58 +02:00
|
|
|
INSIST(ret != NULL);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((ctx->flags & ISC_MEMFLAG_FILL) != 0) {
|
2021-05-11 14:00:12 +02:00
|
|
|
memset(ret, 0xbe, size); /* Mnemonic for "beef". */
|
2018-01-05 12:13:17 +11:00
|
|
|
}
|
2001-01-25 01:18:00 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2001-01-25 01:18:00 +00:00
|
|
|
* Perform a free, doing memory filling and overrun detection as necessary.
|
|
|
|
*/
|
2012-11-02 11:16:32 +11:00
|
|
|
/* coverity[+free : arg-1] */
|
2021-10-11 13:43:12 +02:00
|
|
|
static void
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_put(isc_mem_t *ctx, void *mem, size_t size, int flags) {
|
2021-09-23 21:19:25 +02:00
|
|
|
ADJUST_ZERO_ALLOCATION_SIZE(size);
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((ctx->flags & ISC_MEMFLAG_FILL) != 0) {
|
2017-10-09 09:55:37 -07:00
|
|
|
memset(mem, 0xde, size); /* Mnemonic for "dead". */
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-12-14 21:50:23 +01:00
|
|
|
sdallocx(mem, size, flags);
|
2001-01-25 01:18:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 13:43:12 +02:00
|
|
|
static void *
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_realloc(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size,
|
|
|
|
int flags) {
|
2021-09-22 18:48:03 +02:00
|
|
|
void *new_ptr = NULL;
|
|
|
|
|
2021-09-23 21:19:25 +02:00
|
|
|
ADJUST_ZERO_ALLOCATION_SIZE(new_size);
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
new_ptr = rallocx(old_ptr, new_size, flags);
|
2021-09-29 10:58:58 +02:00
|
|
|
INSIST(new_ptr != NULL);
|
2021-09-22 18:48:03 +02:00
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((ctx->flags & ISC_MEMFLAG_FILL) != 0) {
|
2021-09-22 18:48:03 +02:00
|
|
|
ssize_t diff_size = new_size - old_size;
|
|
|
|
void *diff_ptr = (uint8_t *)new_ptr + old_size;
|
|
|
|
if (diff_size > 0) {
|
|
|
|
/* Mnemonic for "beef". */
|
|
|
|
memset(diff_ptr, 0xbe, diff_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new_ptr);
|
|
|
|
}
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
#define stats_bucket(ctx, size) \
|
|
|
|
((size / STATS_BUCKET_SIZE) >= STATS_BUCKETS \
|
|
|
|
? &ctx->stats[STATS_BUCKETS] \
|
|
|
|
: &ctx->stats[size / STATS_BUCKET_SIZE])
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2001-01-25 01:18:00 +00:00
|
|
|
* Update internal counters after a memory get.
|
|
|
|
*/
|
2021-10-11 13:43:12 +02:00
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
mem_getstats(isc_mem_t *ctx, size_t size) {
|
2021-02-04 21:56:49 +01:00
|
|
|
struct stats *stats = stats_bucket(ctx, size);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_fetch_add_relaxed(&ctx->total, size);
|
|
|
|
atomic_fetch_add_release(&ctx->inuse, size);
|
2017-04-24 11:33:30 +10:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_fetch_add_relaxed(&stats->gets, 1);
|
|
|
|
atomic_fetch_add_relaxed(&stats->totalgets, 1);
|
|
|
|
|
|
|
|
increment_malloced(ctx, size);
|
2001-01-25 01:18:00 +00:00
|
|
|
}
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
2001-01-25 01:18:00 +00:00
|
|
|
* Update internal counters after a memory put.
|
|
|
|
*/
|
2021-10-11 13:43:12 +02:00
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
mem_putstats(isc_mem_t *ctx, void *ptr, size_t size) {
|
2021-02-04 21:56:49 +01:00
|
|
|
struct stats *stats = stats_bucket(ctx, size);
|
2021-10-21 02:28:48 -07:00
|
|
|
uint_fast32_t s, g;
|
2021-02-04 21:56:49 +01:00
|
|
|
|
2001-01-25 01:18:00 +00:00
|
|
|
UNUSED(ptr);
|
|
|
|
|
2021-10-21 02:28:48 -07:00
|
|
|
s = atomic_fetch_sub_release(&ctx->inuse, size);
|
|
|
|
INSIST(s >= size);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-10-21 02:28:48 -07:00
|
|
|
g = atomic_fetch_sub_release(&stats->gets, 1);
|
|
|
|
INSIST(g >= 1);
|
2021-02-04 21:56:49 +01:00
|
|
|
|
|
|
|
decrement_malloced(ctx, size);
|
2001-01-25 01:18:00 +00:00
|
|
|
}
|
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
/*
|
|
|
|
* Private.
|
|
|
|
*/
|
|
|
|
|
2006-01-04 03:16:47 +00:00
|
|
|
static void
|
2021-02-09 17:44:40 +01:00
|
|
|
mem_initialize(void) {
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&contextslock);
|
2007-10-30 23:30:09 +00:00
|
|
|
ISC_LIST_INIT(contexts);
|
2008-03-31 05:00:30 +00:00
|
|
|
totallost = 0;
|
2006-01-04 03:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:44:40 +01:00
|
|
|
void
|
|
|
|
isc__mem_initialize(void) {
|
|
|
|
RUNTIME_CHECK(isc_once_do(&init_once, mem_initialize) == ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mem_shutdown(void) {
|
|
|
|
isc__mem_checkdestroyed();
|
|
|
|
|
|
|
|
isc_mutex_destroy(&contextslock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc__mem_shutdown(void) {
|
|
|
|
RUNTIME_CHECK(isc_once_do(&shut_once, mem_shutdown) == ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-09-06 11:31:15 +02:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
mem_create(isc_mem_t **ctxp, unsigned int flags) {
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mem_t *ctx = NULL;
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
REQUIRE(ctxp != NULL && *ctxp == NULL);
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2022-01-25 23:26:27 +01:00
|
|
|
ctx = mallocx(sizeof(*ctx), MALLOCX_ALIGN(isc_os_cacheline()));
|
2021-09-29 10:58:58 +02:00
|
|
|
INSIST(ctx != NULL);
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
*ctx = (isc_mem_t){
|
|
|
|
.magic = MEM_MAGIC,
|
|
|
|
.flags = flags,
|
|
|
|
.checkfree = true,
|
|
|
|
};
|
2005-03-15 01:11:01 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
isc_mutex_init(&ctx->lock);
|
2018-05-24 14:43:25 +02:00
|
|
|
isc_refcount_init(&ctx->references, 1);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_init(&ctx->total, 0);
|
|
|
|
atomic_init(&ctx->inuse, 0);
|
|
|
|
atomic_init(&ctx->maxinuse, 0);
|
|
|
|
atomic_init(&ctx->malloced, sizeof(*ctx));
|
|
|
|
atomic_init(&ctx->maxmalloced, sizeof(*ctx));
|
2021-07-22 06:14:32 +02:00
|
|
|
atomic_init(&ctx->hi_water, 0);
|
|
|
|
atomic_init(&ctx->lo_water, 0);
|
2021-05-26 17:55:43 +10:00
|
|
|
atomic_init(&ctx->hi_called, false);
|
|
|
|
atomic_init(&ctx->is_overmem, false);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < STATS_BUCKETS + 1; i++) {
|
|
|
|
atomic_init(&ctx->stats[i].gets, 0);
|
|
|
|
atomic_init(&ctx->stats[i].totalgets, 0);
|
|
|
|
}
|
2021-02-04 21:56:49 +01:00
|
|
|
ISC_LIST_INIT(ctx->pools);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2000-12-01 00:52:38 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
|
2001-08-30 05:40:04 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2021-05-11 12:40:42 +02:00
|
|
|
ctx->debuglist =
|
2021-05-11 14:00:12 +02:00
|
|
|
mallocx((DEBUG_TABLE_COUNT * sizeof(debuglist_t)), 0);
|
2021-09-29 10:58:58 +02:00
|
|
|
INSIST(ctx->debuglist != NULL);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
|
2001-08-30 05:40:04 +00:00
|
|
|
ISC_LIST_INIT(ctx->debuglist[i]);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-02-04 21:56:49 +01:00
|
|
|
increment_malloced(ctx,
|
|
|
|
DEBUG_TABLE_COUNT * sizeof(debuglist_t));
|
2001-08-30 05:40:04 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2015-01-20 13:29:18 -08:00
|
|
|
LOCK(&contextslock);
|
2006-01-04 03:16:47 +00:00
|
|
|
ISC_LIST_INITANDAPPEND(contexts, ctx, link);
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2006-01-04 03:16:47 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
*ctxp = ctx;
|
2000-06-01 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 11:31:15 +02:00
|
|
|
/*
|
|
|
|
* Public.
|
|
|
|
*/
|
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
destroy(isc_mem_t *ctx) {
|
2000-06-01 17:20:56 +00:00
|
|
|
unsigned int i;
|
2021-02-04 21:56:49 +01:00
|
|
|
size_t malloced;
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2015-01-20 13:29:18 -08:00
|
|
|
LOCK(&contextslock);
|
2006-01-04 03:16:47 +00:00
|
|
|
ISC_LIST_UNLINK(contexts, ctx, link);
|
2021-02-04 21:56:49 +01:00
|
|
|
totallost += isc_mem_inuse(ctx);
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2006-01-04 03:16:47 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
ctx->magic = 0;
|
2010-05-12 00:46:55 +00:00
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
INSIST(ISC_LIST_EMPTY(ctx->pools));
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2000-12-01 00:52:38 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2021-10-14 10:33:24 +02:00
|
|
|
if (ctx->debuglist != NULL) {
|
2017-08-24 10:58:20 +05:30
|
|
|
debuglink_t *dl;
|
2020-02-13 21:48:23 +01:00
|
|
|
for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
|
2020-02-12 13:59:18 +01:00
|
|
|
for (dl = ISC_LIST_HEAD(ctx->debuglist[i]); dl != NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
dl = ISC_LIST_HEAD(ctx->debuglist[i]))
|
|
|
|
{
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->checkfree && dl->ptr != NULL) {
|
2001-08-30 05:40:04 +00:00
|
|
|
print_active(ctx, stderr);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
INSIST(!ctx->checkfree || dl->ptr == NULL);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_LIST_UNLINK(ctx->debuglist[i], dl, link);
|
2021-05-11 14:00:12 +02:00
|
|
|
sdallocx(dl, sizeof(*dl), 0);
|
2021-02-04 21:56:49 +01:00
|
|
|
decrement_malloced(ctx, sizeof(*dl));
|
2001-08-30 05:40:04 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
sdallocx(ctx->debuglist,
|
|
|
|
(DEBUG_TABLE_COUNT * sizeof(debuglist_t)), 0);
|
2021-02-04 21:56:49 +01:00
|
|
|
decrement_malloced(ctx,
|
|
|
|
DEBUG_TABLE_COUNT * sizeof(debuglist_t));
|
2000-11-25 06:40:54 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2000-06-01 17:20:56 +00:00
|
|
|
|
|
|
|
if (ctx->checkfree) {
|
2021-02-04 21:56:49 +01:00
|
|
|
for (i = 0; i <= STATS_BUCKETS; i++) {
|
|
|
|
struct stats *stats = &ctx->stats[i];
|
|
|
|
size_t gets = atomic_load_acquire(&stats->gets);
|
|
|
|
if (gets != 0U) {
|
2015-03-27 23:09:28 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"Failing assertion due to probable "
|
|
|
|
"leaked memory in context %p (\"%s\") "
|
2021-02-04 21:56:49 +01:00
|
|
|
"(stats[%u].gets == %zu).\n",
|
|
|
|
ctx, ctx->name, i, gets);
|
2001-02-13 06:21:32 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
print_active(ctx, stderr);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2021-02-04 21:56:49 +01:00
|
|
|
INSIST(gets == 0U);
|
2015-03-27 23:09:28 +05:30
|
|
|
}
|
2001-02-13 06:21:32 +00:00
|
|
|
}
|
2000-06-01 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
isc_mutex_destroy(&ctx->lock);
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
malloced = decrement_malloced(ctx, sizeof(*ctx));
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->checkfree) {
|
2021-02-04 21:56:49 +01:00
|
|
|
INSIST(malloced == 0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-01-25 23:26:27 +01:00
|
|
|
sdallocx(ctx, sizeof(*ctx), MALLOCX_ALIGN(isc_os_cacheline()));
|
2000-06-01 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
|
|
|
|
REQUIRE(VALID_CONTEXT(source));
|
2000-06-01 17:20:56 +00:00
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
|
|
|
|
2018-08-17 15:16:59 +02:00
|
|
|
isc_refcount_increment(&source->references);
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
*targetp = source;
|
2000-06-01 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-05-11 12:18:56 +02:00
|
|
|
isc__mem_detach(isc_mem_t **ctxp FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mem_t *ctx = NULL;
|
|
|
|
|
2018-08-28 10:18:59 +02:00
|
|
|
REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
ctx = *ctxp;
|
2018-08-28 10:18:59 +02:00
|
|
|
*ctxp = NULL;
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2018-08-17 15:16:59 +02:00
|
|
|
if (isc_refcount_decrement(&ctx->references) == 1) {
|
2018-08-28 10:18:59 +02:00
|
|
|
isc_refcount_destroy(&ctx->references);
|
2021-05-11 12:18:56 +02:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "destroy mctx %p file %s line %u\n",
|
|
|
|
ctx, file, line);
|
|
|
|
}
|
|
|
|
#endif
|
2000-06-01 17:20:56 +00:00
|
|
|
destroy(ctx);
|
2018-05-24 14:43:25 +02:00
|
|
|
}
|
2000-06-01 17:20:56 +00:00
|
|
|
}
|
|
|
|
|
2000-09-05 03:30:19 +00:00
|
|
|
/*
|
|
|
|
* isc_mem_putanddetach() is the equivalent of:
|
|
|
|
*
|
|
|
|
* mctx = NULL;
|
|
|
|
* isc_mem_attach(ptr->mctx, &mctx);
|
|
|
|
* isc_mem_detach(&ptr->mctx);
|
|
|
|
* isc_mem_put(mctx, ptr, sizeof(*ptr);
|
|
|
|
* isc_mem_detach(&mctx);
|
|
|
|
*/
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-12-14 21:50:23 +01:00
|
|
|
isc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size,
|
|
|
|
size_t alignment FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mem_t *ctx = NULL;
|
|
|
|
|
2018-08-28 10:18:59 +02:00
|
|
|
REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
|
2000-09-05 03:30:19 +00:00
|
|
|
REQUIRE(ptr != NULL);
|
2021-09-23 21:19:25 +02:00
|
|
|
REQUIRE(size != 0);
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
ctx = *ctxp;
|
2000-09-05 03:30:19 +00:00
|
|
|
*ctxp = NULL;
|
|
|
|
|
2012-11-02 15:57:56 +11:00
|
|
|
DELETE_TRACE(ctx, ptr, size, file, line);
|
|
|
|
|
2021-02-04 20:11:20 +01:00
|
|
|
mem_putstats(ctx, ptr, size);
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_put(ctx, ptr, size, MEM_ALIGN(alignment));
|
2000-09-05 03:30:19 +00:00
|
|
|
|
2018-08-17 15:16:59 +02:00
|
|
|
if (isc_refcount_decrement(&ctx->references) == 1) {
|
2018-08-28 10:18:59 +02:00
|
|
|
isc_refcount_destroy(&ctx->references);
|
2000-09-05 03:30:19 +00:00
|
|
|
destroy(ctx);
|
2018-05-24 14:43:25 +02:00
|
|
|
}
|
2000-09-05 03:30:19 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-05-11 12:18:56 +02:00
|
|
|
isc__mem_destroy(isc_mem_t **ctxp FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mem_t *ctx = NULL;
|
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
/*
|
|
|
|
* This routine provides legacy support for callers who use mctxs
|
|
|
|
* without attaching/detaching.
|
|
|
|
*/
|
|
|
|
|
2020-02-01 17:59:19 +01:00
|
|
|
REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
|
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
ctx = *ctxp;
|
|
|
|
*ctxp = NULL;
|
2000-06-01 17:20:56 +00:00
|
|
|
|
2001-02-13 13:20:37 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2021-05-11 12:18:56 +02:00
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
|
2019-07-15 11:19:03 +02:00
|
|
|
if (isc_refcount_decrement(&ctx->references) > 1) {
|
2001-02-13 06:21:32 +00:00
|
|
|
print_active(ctx, stderr);
|
2018-05-24 14:43:25 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if ISC_MEM_TRACKLINES */
|
2019-12-05 13:29:45 +11:00
|
|
|
isc_refcount_decrementz(&ctx->references);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2018-05-24 14:43:25 +02:00
|
|
|
isc_refcount_destroy(&ctx->references);
|
2000-12-06 23:39:04 +00:00
|
|
|
destroy(ctx);
|
2000-06-01 17:20:56 +00:00
|
|
|
|
|
|
|
*ctxp = NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
#define CALL_HI_WATER(ctx) \
|
|
|
|
{ \
|
|
|
|
if (ctx->water != NULL && hi_water(ctx)) { \
|
|
|
|
(ctx->water)(ctx->water_arg, ISC_MEM_HIWATER); \
|
|
|
|
} \
|
2021-05-11 14:00:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
#define CALL_LO_WATER(ctx) \
|
|
|
|
{ \
|
|
|
|
if ((ctx->water != NULL) && lo_water(ctx)) { \
|
|
|
|
(ctx->water)(ctx->water_arg, ISC_MEM_LOWATER); \
|
|
|
|
} \
|
2021-05-11 14:00:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 13:43:12 +02:00
|
|
|
static bool
|
2021-07-22 06:14:32 +02:00
|
|
|
hi_water(isc_mem_t *ctx) {
|
2021-05-14 15:13:33 +02:00
|
|
|
size_t inuse;
|
|
|
|
size_t maxinuse;
|
2021-07-22 06:14:32 +02:00
|
|
|
size_t hiwater = atomic_load_relaxed(&ctx->hi_water);
|
2021-05-14 15:13:33 +02:00
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
if (hiwater == 0) {
|
2021-05-14 15:13:33 +02:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
inuse = atomic_load_acquire(&ctx->inuse);
|
2021-07-22 06:14:32 +02:00
|
|
|
if (inuse <= hiwater) {
|
2021-05-14 15:13:33 +02:00
|
|
|
return (false);
|
2021-02-04 21:56:49 +01:00
|
|
|
}
|
2021-05-14 15:13:33 +02:00
|
|
|
|
|
|
|
maxinuse = atomic_load_acquire(&ctx->maxinuse);
|
2021-02-04 21:56:49 +01:00
|
|
|
if (inuse > maxinuse) {
|
|
|
|
(void)atomic_compare_exchange_strong(&ctx->maxinuse, &maxinuse,
|
|
|
|
inuse);
|
|
|
|
|
2021-05-14 15:13:33 +02:00
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0) {
|
2021-02-04 21:56:49 +01:00
|
|
|
fprintf(stderr, "maxinuse = %lu\n",
|
|
|
|
(unsigned long)inuse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:13:33 +02:00
|
|
|
if (atomic_load_acquire(&ctx->hi_called)) {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are over water (for the first time) */
|
|
|
|
atomic_store_release(&ctx->is_overmem, true);
|
|
|
|
|
|
|
|
return (true);
|
2021-02-04 21:56:49 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 13:43:12 +02:00
|
|
|
static bool
|
2021-07-22 06:14:32 +02:00
|
|
|
lo_water(isc_mem_t *ctx) {
|
2021-05-14 15:13:33 +02:00
|
|
|
size_t inuse;
|
2021-07-22 06:14:32 +02:00
|
|
|
size_t lowater = atomic_load_relaxed(&ctx->lo_water);
|
2021-02-04 21:56:49 +01:00
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
if (lowater == 0) {
|
2021-05-14 15:13:33 +02:00
|
|
|
return (false);
|
2021-02-04 21:56:49 +01:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:13:33 +02:00
|
|
|
inuse = atomic_load_acquire(&ctx->inuse);
|
2021-07-22 06:14:32 +02:00
|
|
|
if (inuse >= lowater) {
|
2021-05-14 15:13:33 +02:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!atomic_load_acquire(&ctx->hi_called)) {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are no longer overmem */
|
2021-07-07 16:05:48 +02:00
|
|
|
atomic_store_release(&ctx->is_overmem, false);
|
2021-05-14 15:13:33 +02:00
|
|
|
|
|
|
|
return (true);
|
2021-02-04 21:56:49 +01:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void *
|
2021-12-14 21:50:23 +01:00
|
|
|
isc__mem_get(isc_mem_t *ctx, size_t size, size_t alignment FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
void *ptr = NULL;
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
ptr = mem_get(ctx, size, MEM_ALIGN(alignment));
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-06-10 10:18:24 +02:00
|
|
|
mem_getstats(ctx, size);
|
2000-07-26 19:06:20 +00:00
|
|
|
ADD_TRACE(ctx, ptr, size, file, line);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2021-07-07 16:05:48 +02:00
|
|
|
CALL_HI_WATER(ctx);
|
2000-08-31 12:15:17 +00:00
|
|
|
|
1998-08-17 22:05:58 +00:00
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-12-14 21:50:23 +01:00
|
|
|
isc__mem_put(isc_mem_t *ctx, void *ptr, size_t size, size_t alignment FLARG) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2012-11-02 15:57:56 +11:00
|
|
|
DELETE_TRACE(ctx, ptr, size, file, line);
|
|
|
|
|
2021-02-04 20:11:20 +01:00
|
|
|
mem_putstats(ctx, ptr, size);
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_put(ctx, ptr, size, MEM_ALIGN(alignment));
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
CALL_LO_WATER(ctx);
|
1998-08-17 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_waterack(isc_mem_t *ctx, int flag) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2008-02-07 02:41:26 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (flag == ISC_MEM_LOWATER) {
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_store(&ctx->hi_called, false);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (flag == ISC_MEM_HIWATER) {
|
2021-02-04 21:56:49 +01:00
|
|
|
atomic_store(&ctx->hi_called, true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-02-07 02:41:26 +00:00
|
|
|
}
|
|
|
|
|
2001-02-13 13:20:37 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2001-02-13 06:21:32 +00:00
|
|
|
static void
|
2021-02-04 20:19:09 +01:00
|
|
|
print_active(isc_mem_t *mctx, FILE *out) {
|
2001-08-30 05:40:04 +00:00
|
|
|
if (mctx->debuglist != NULL) {
|
2001-02-13 06:21:32 +00:00
|
|
|
debuglink_t *dl;
|
2017-08-24 10:58:20 +05:30
|
|
|
unsigned int i;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool found;
|
2001-02-13 06:21:32 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
fprintf(out, "Dump of all outstanding memory "
|
|
|
|
"allocations:\n");
|
2018-04-17 08:29:14 -07:00
|
|
|
found = false;
|
2017-08-24 10:58:20 +05:30
|
|
|
for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
|
2001-08-30 05:40:04 +00:00
|
|
|
dl = ISC_LIST_HEAD(mctx->debuglist[i]);
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
if (dl != NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
found = true;
|
2018-11-23 21:35:01 +01:00
|
|
|
}
|
2001-08-30 05:40:04 +00:00
|
|
|
|
|
|
|
while (dl != NULL) {
|
2018-11-23 21:35:01 +01:00
|
|
|
if (dl->ptr != NULL) {
|
|
|
|
fprintf(out,
|
2021-02-04 21:56:49 +01:00
|
|
|
"\tptr %p size %zu "
|
|
|
|
"file %s "
|
2020-02-12 13:59:18 +01:00
|
|
|
"line %u\n",
|
|
|
|
dl->ptr, dl->size, dl->file,
|
|
|
|
dl->line);
|
2018-11-23 21:35:01 +01:00
|
|
|
}
|
2001-08-30 05:40:04 +00:00
|
|
|
dl = ISC_LIST_NEXT(dl, link);
|
|
|
|
}
|
2001-02-13 06:21:32 +00:00
|
|
|
}
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
if (!found) {
|
2020-04-02 18:51:06 -07:00
|
|
|
fprintf(out, "\tNone.\n");
|
2018-11-23 21:35:01 +01:00
|
|
|
}
|
2001-02-13 06:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2001-02-13 06:21:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the stats[] on the stream "out" with suitable formatting.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_stats(isc_mem_t *ctx, FILE *out) {
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mempool_t *pool = NULL;
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
1998-08-17 22:05:58 +00:00
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXLOCK(ctx);
|
1998-08-17 22:05:58 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
for (size_t i = 0; i <= STATS_BUCKETS; i++) {
|
|
|
|
size_t totalgets;
|
|
|
|
size_t gets;
|
|
|
|
struct stats *stats = &ctx->stats[i];
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
totalgets = atomic_load_acquire(&stats->totalgets);
|
|
|
|
gets = atomic_load_acquire(&stats->gets);
|
|
|
|
|
|
|
|
if (totalgets != 0U && gets != 0U) {
|
|
|
|
fprintf(out, "%s%5zu: %11zu gets, %11zu rem",
|
|
|
|
(i == STATS_BUCKETS) ? ">=" : " ", i,
|
|
|
|
totalgets, gets);
|
|
|
|
fputc('\n', out);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
1999-09-15 17:47:08 +00:00
|
|
|
/*
|
2021-02-04 21:56:49 +01:00
|
|
|
* Note that since a pool can be locked now, these stats might
|
|
|
|
* be somewhat off if the pool is in active use at the time the
|
|
|
|
* stats are dumped. The link fields are protected by the
|
|
|
|
* isc_mem_t's lock, however, so walking this list and
|
|
|
|
* extracting integers from stats fields is always safe.
|
1999-09-15 17:47:08 +00:00
|
|
|
*/
|
1999-06-08 02:38:30 +00:00
|
|
|
pool = ISC_LIST_HEAD(ctx->pools);
|
|
|
|
if (pool != NULL) {
|
2020-04-02 18:51:06 -07:00
|
|
|
fprintf(out, "[Pool statistics]\n");
|
2021-05-13 00:29:11 +02:00
|
|
|
fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %1s\n", "name",
|
|
|
|
"size", "allocated", "freecount", "freemax",
|
|
|
|
"fillcount", "gets", "L");
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
while (pool != NULL) {
|
2021-02-04 21:56:49 +01:00
|
|
|
fprintf(out,
|
|
|
|
"%15s %10zu %10zu %10zu %10zu %10zu %10zu %10zu %s\n",
|
2021-07-09 14:01:22 +03:00
|
|
|
pool->name, pool->size, (size_t)0, pool->allocated,
|
|
|
|
pool->freecount, pool->freemax, pool->fillcount,
|
|
|
|
pool->gets, "N");
|
1999-06-08 02:38:30 +00:00
|
|
|
pool = ISC_LIST_NEXT(pool, link);
|
1998-08-17 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
2001-02-13 13:20:37 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2001-02-13 06:21:32 +00:00
|
|
|
print_active(ctx, out);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2000-07-26 19:06:20 +00:00
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXUNLOCK(ctx);
|
1998-08-17 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void *
|
2021-02-04 20:19:09 +01:00
|
|
|
isc__mem_allocate(isc_mem_t *ctx, size_t size FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
void *ptr = NULL;
|
2000-02-26 19:59:30 +00:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
ptr = mem_get(ctx, size, 0);
|
2021-05-11 14:00:12 +02:00
|
|
|
|
2021-09-23 21:19:25 +02:00
|
|
|
/* Recalculate the real allocated size */
|
|
|
|
size = sallocx(ptr, 0);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
mem_getstats(ctx, size);
|
|
|
|
ADD_TRACE(ctx, ptr, size, file, line);
|
2010-08-11 22:54:58 +00:00
|
|
|
|
2021-07-07 16:05:48 +02:00
|
|
|
CALL_HI_WATER(ctx);
|
2005-06-10 07:00:20 +00:00
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
return (ptr);
|
2000-02-26 19:59:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:03 +02:00
|
|
|
void *
|
2021-12-14 21:50:23 +01:00
|
|
|
isc__mem_reget(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size,
|
|
|
|
size_t alignment FLARG) {
|
2021-09-22 18:48:03 +02:00
|
|
|
void *new_ptr = NULL;
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if (old_ptr == NULL) {
|
2021-09-22 18:48:03 +02:00
|
|
|
REQUIRE(old_size == 0);
|
2021-12-14 21:50:23 +01:00
|
|
|
new_ptr = isc__mem_get(ctx, new_size, alignment FLARG_PASS);
|
2021-10-14 10:33:24 +02:00
|
|
|
} else if (new_size == 0) {
|
2021-12-14 21:50:23 +01:00
|
|
|
isc__mem_put(ctx, old_ptr, old_size, alignment FLARG_PASS);
|
2021-09-22 18:48:03 +02:00
|
|
|
} else {
|
|
|
|
DELETE_TRACE(ctx, old_ptr, old_size, file, line);
|
|
|
|
mem_putstats(ctx, old_ptr, old_size);
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
new_ptr = mem_realloc(ctx, old_ptr, old_size, new_size,
|
|
|
|
MEM_ALIGN(alignment));
|
2021-09-22 18:48:03 +02:00
|
|
|
|
|
|
|
mem_getstats(ctx, new_size);
|
|
|
|
ADD_TRACE(ctx, new_ptr, new_size, file, line);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to postpone the call to water in edge case
|
|
|
|
* where the realloc will exactly hit on the boundary of
|
|
|
|
* the water and we would call water twice.
|
|
|
|
*/
|
|
|
|
CALL_LO_WATER(ctx);
|
|
|
|
CALL_HI_WATER(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new_ptr);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void *
|
2021-05-11 14:00:12 +02:00
|
|
|
isc__mem_reallocate(isc_mem_t *ctx, void *old_ptr, size_t new_size FLARG) {
|
2020-02-13 14:44:37 -08:00
|
|
|
void *new_ptr = NULL;
|
2009-02-11 03:04:18 +00:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if (old_ptr == NULL) {
|
2021-05-12 21:16:17 +02:00
|
|
|
new_ptr = isc__mem_allocate(ctx, new_size FLARG_PASS);
|
2021-10-14 10:33:24 +02:00
|
|
|
} else if (new_size == 0) {
|
2021-05-11 14:00:12 +02:00
|
|
|
isc__mem_free(ctx, old_ptr FLARG_PASS);
|
|
|
|
} else {
|
|
|
|
size_t old_size = sallocx(old_ptr, 0);
|
|
|
|
|
|
|
|
DELETE_TRACE(ctx, old_ptr, old_size, file, line);
|
|
|
|
mem_putstats(ctx, old_ptr, old_size);
|
|
|
|
|
2021-12-14 21:50:23 +01:00
|
|
|
new_ptr = mem_realloc(ctx, old_ptr, old_size, new_size, 0);
|
2021-05-11 14:00:12 +02:00
|
|
|
|
|
|
|
/* Recalculate the real allocated size */
|
|
|
|
new_size = sallocx(new_ptr, 0);
|
|
|
|
|
|
|
|
mem_getstats(ctx, new_size);
|
|
|
|
ADD_TRACE(ctx, new_ptr, new_size, file, line);
|
|
|
|
|
|
|
|
/*
|
2021-06-10 10:18:24 +02:00
|
|
|
* We want to postpone the call to water in edge case
|
|
|
|
* where the realloc will exactly hit on the boundary of
|
|
|
|
* the water and we would call water twice.
|
2021-05-11 14:00:12 +02:00
|
|
|
*/
|
|
|
|
CALL_LO_WATER(ctx);
|
|
|
|
CALL_HI_WATER(ctx);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-02-11 03:04:18 +00:00
|
|
|
|
|
|
|
return (new_ptr);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc__mem_free(isc_mem_t *ctx, void *ptr FLARG) {
|
2021-06-10 10:18:24 +02:00
|
|
|
size_t size = 0;
|
2021-05-12 21:16:17 +02:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-09-23 21:19:25 +02:00
|
|
|
size = sallocx(ptr, 0);
|
2001-01-25 01:18:00 +00:00
|
|
|
|
2012-11-02 15:57:56 +11:00
|
|
|
DELETE_TRACE(ctx, ptr, size, file, line);
|
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
mem_putstats(ctx, ptr, size);
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_put(ctx, ptr, size, 0);
|
2005-06-10 07:00:20 +00:00
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
CALL_LO_WATER(ctx);
|
1998-08-17 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
1999-01-14 19:52:52 +00:00
|
|
|
/*
|
|
|
|
* Other useful things.
|
|
|
|
*/
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
char *
|
2021-02-04 20:19:09 +01:00
|
|
|
isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
size_t len;
|
|
|
|
char *ns = NULL;
|
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(mctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
REQUIRE(s != NULL);
|
|
|
|
|
2017-09-14 18:11:56 +10:00
|
|
|
len = strlen(s) + 1;
|
1998-12-30 20:17:02 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
ns = isc__mem_allocate(mctx, len FLARG_PASS);
|
2000-02-26 19:59:30 +00:00
|
|
|
|
2021-06-10 10:18:24 +02:00
|
|
|
strlcpy(ns, s, len);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-07-26 19:06:20 +00:00
|
|
|
return (ns);
|
2000-02-26 19:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:17:05 +01:00
|
|
|
char *
|
2021-02-04 20:19:09 +01:00
|
|
|
isc__mem_strndup(isc_mem_t *mctx, const char *s, size_t size FLARG) {
|
2021-05-12 21:16:17 +02:00
|
|
|
size_t len;
|
|
|
|
char *ns = NULL;
|
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(mctx));
|
2020-10-26 14:17:05 +01:00
|
|
|
REQUIRE(s != NULL);
|
2021-06-10 10:18:24 +02:00
|
|
|
REQUIRE(size != 0);
|
2020-10-26 14:17:05 +01:00
|
|
|
|
|
|
|
len = strlen(s) + 1;
|
|
|
|
if (len > size) {
|
|
|
|
len = size;
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
ns = isc__mem_allocate(mctx, len FLARG_PASS);
|
2020-10-26 14:17:05 +01:00
|
|
|
|
2021-06-10 10:18:24 +02:00
|
|
|
strlcpy(ns, s, len);
|
2020-10-26 14:17:05 +01:00
|
|
|
|
|
|
|
return (ns);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_setdestroycheck(isc_mem_t *ctx, bool flag) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2009-09-01 00:22:28 +00:00
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXLOCK(ctx);
|
1999-10-02 21:20:03 +00:00
|
|
|
|
2000-04-12 01:24:17 +00:00
|
|
|
ctx->checkfree = flag;
|
1999-10-02 21:20:03 +00:00
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXUNLOCK(ctx);
|
2000-04-12 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
size_t
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_inuse(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
return (atomic_load_acquire(&ctx->inuse));
|
2000-04-12 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
size_t
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_maxinuse(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
return (atomic_load_acquire(&ctx->maxinuse));
|
2012-05-14 10:06:05 -07:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
size_t
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_total(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
return (atomic_load_acquire(&ctx->total));
|
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
size_t
|
|
|
|
isc_mem_malloced(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2012-05-14 10:06:05 -07:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
return (atomic_load_acquire(&ctx->malloced));
|
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
size_t
|
|
|
|
isc_mem_maxmalloced(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2012-05-14 10:06:05 -07:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
return (atomic_load_acquire(&ctx->maxmalloced));
|
2012-05-14 10:06:05 -07:00
|
|
|
}
|
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
void
|
|
|
|
isc_mem_clearwater(isc_mem_t *mctx) {
|
|
|
|
isc_mem_setwater(mctx, NULL, NULL, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t hiwater, size_t lowater) {
|
2021-07-22 06:14:32 +02:00
|
|
|
isc_mem_water_t oldwater;
|
|
|
|
void *oldwater_arg;
|
2021-05-12 21:16:17 +02:00
|
|
|
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
|
|
|
REQUIRE(hiwater >= lowater);
|
2006-12-07 06:27:34 +00:00
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
oldwater = ctx->water;
|
|
|
|
oldwater_arg = ctx->water_arg;
|
2021-07-07 16:05:48 +02:00
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
/* No water was set and new water is also NULL */
|
|
|
|
if (oldwater == NULL && water == NULL) {
|
|
|
|
return;
|
2000-08-31 12:15:17 +00:00
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
/* The water function is being set for the first time */
|
2021-07-07 16:05:48 +02:00
|
|
|
if (oldwater == NULL) {
|
2021-07-22 06:14:32 +02:00
|
|
|
REQUIRE(water != NULL && lowater > 0);
|
|
|
|
|
|
|
|
INSIST(atomic_load(&ctx->hi_water) == 0);
|
|
|
|
INSIST(atomic_load(&ctx->lo_water) == 0);
|
|
|
|
|
|
|
|
ctx->water = water;
|
|
|
|
ctx->water_arg = water_arg;
|
|
|
|
atomic_store(&ctx->hi_water, hiwater);
|
|
|
|
atomic_store(&ctx->lo_water, lowater);
|
|
|
|
|
2021-07-07 16:05:48 +02:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-07-07 16:05:48 +02:00
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
REQUIRE((water == oldwater && water_arg == oldwater_arg) ||
|
|
|
|
(water == NULL && water_arg == NULL && hiwater == 0));
|
|
|
|
|
|
|
|
atomic_store(&ctx->hi_water, hiwater);
|
|
|
|
atomic_store(&ctx->lo_water, lowater);
|
2021-07-07 16:05:48 +02:00
|
|
|
|
|
|
|
if (atomic_load_acquire(&ctx->hi_called) &&
|
2021-07-22 06:14:32 +02:00
|
|
|
(atomic_load_acquire(&ctx->inuse) < lowater || lowater == 0U))
|
2021-07-07 16:05:48 +02:00
|
|
|
{
|
2021-07-22 06:14:32 +02:00
|
|
|
(oldwater)(oldwater_arg, ISC_MEM_LOWATER);
|
2021-07-07 16:05:48 +02:00
|
|
|
}
|
2000-08-31 12:15:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
bool
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_isovermem(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2010-08-11 23:11:45 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
return (atomic_load_relaxed(&ctx->is_overmem));
|
2010-08-11 22:54:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2020-07-14 14:24:10 +02:00
|
|
|
isc_mem_setname(isc_mem_t *ctx, const char *name) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2008-03-31 05:00:30 +00:00
|
|
|
|
2008-04-02 02:37:42 +00:00
|
|
|
LOCK(&ctx->lock);
|
2017-09-14 18:11:56 +10:00
|
|
|
strlcpy(ctx->name, name, sizeof(ctx->name));
|
2008-04-02 02:37:42 +00:00
|
|
|
UNLOCK(&ctx->lock);
|
2008-03-31 05:00:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
const char *
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_getname(isc_mem_t *ctx) {
|
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2008-03-31 05:00:30 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx->name[0] == 0) {
|
2013-04-10 13:49:57 -07:00
|
|
|
return ("");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-04-10 13:49:57 -07:00
|
|
|
|
2008-03-31 05:00:30 +00:00
|
|
|
return (ctx->name);
|
|
|
|
}
|
|
|
|
|
1999-06-08 02:38:30 +00:00
|
|
|
/*
|
|
|
|
* Memory pool stuff
|
|
|
|
*/
|
|
|
|
|
2020-02-02 08:39:45 +01:00
|
|
|
void
|
2021-07-09 14:24:33 +03:00
|
|
|
isc__mempool_create(isc_mem_t *restrict mctx, const size_t element_size,
|
|
|
|
isc_mempool_t **restrict mpctxp FLARG) {
|
|
|
|
isc_mempool_t *restrict mpctx = NULL;
|
|
|
|
size_t size = element_size;
|
2021-05-12 21:16:17 +02:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(mctx));
|
2003-07-25 00:01:16 +00:00
|
|
|
REQUIRE(size > 0U);
|
1999-06-08 02:38:30 +00:00
|
|
|
REQUIRE(mpctxp != NULL && *mpctxp == NULL);
|
|
|
|
|
2018-08-14 17:13:20 +10:00
|
|
|
/*
|
|
|
|
* Mempools are stored as a linked list of element.
|
|
|
|
*/
|
|
|
|
if (size < sizeof(element)) {
|
|
|
|
size = sizeof(element);
|
|
|
|
}
|
2021-02-04 21:56:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate space for this pool, initialize values, and if all
|
|
|
|
* works well, attach to the memory context.
|
|
|
|
*/
|
|
|
|
mpctx = isc_mem_get(mctx, sizeof(isc_mempool_t));
|
|
|
|
|
|
|
|
*mpctx = (isc_mempool_t){
|
|
|
|
.magic = MEMPOOL_MAGIC,
|
|
|
|
.mctx = mctx,
|
|
|
|
.size = size,
|
2021-07-09 14:01:22 +03:00
|
|
|
.freemax = 1,
|
|
|
|
.fillcount = 1,
|
2021-02-04 21:56:49 +01:00
|
|
|
};
|
|
|
|
|
2021-05-11 19:54:05 +02:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "create pool %p file %s line %u mctx %p\n",
|
|
|
|
mpctx, file, line, mctx);
|
|
|
|
}
|
|
|
|
#endif /* ISC_MEM_TRACKLINES */
|
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
*mpctxp = (isc_mempool_t *)mpctx;
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXLOCK(mctx);
|
2000-12-07 20:15:58 +00:00
|
|
|
ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
|
2008-03-31 05:00:30 +00:00
|
|
|
mctx->poolcnt++;
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXUNLOCK(mctx);
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_setname(isc_mempool_t *restrict mpctx, const char *name) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
1999-10-19 01:22:39 +00:00
|
|
|
REQUIRE(name != NULL);
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2017-09-14 18:11:56 +10:00
|
|
|
strlcpy(mpctx->name, name, sizeof(mpctx->name));
|
1999-10-19 01:22:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-07-09 14:24:33 +03:00
|
|
|
isc__mempool_destroy(isc_mempool_t **restrict mpctxp FLARG) {
|
|
|
|
isc_mempool_t *restrict mpctx = NULL;
|
2021-05-12 21:16:17 +02:00
|
|
|
isc_mem_t *mctx = NULL;
|
2021-07-09 14:24:33 +03:00
|
|
|
element *restrict item = NULL;
|
2021-05-12 21:16:17 +02:00
|
|
|
|
2020-02-01 17:59:19 +01:00
|
|
|
REQUIRE(mpctxp != NULL);
|
|
|
|
REQUIRE(VALID_MEMPOOL(*mpctxp));
|
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
mpctx = *mpctxp;
|
|
|
|
*mpctxp = NULL;
|
2021-05-11 19:54:05 +02:00
|
|
|
|
2021-05-12 21:16:17 +02:00
|
|
|
mctx = mpctx->mctx;
|
|
|
|
|
2021-05-11 19:54:05 +02:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "destroy pool %p file %s line %u mctx %p\n",
|
|
|
|
mpctx, file, line, mctx);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
if (mpctx->allocated > 0) {
|
2001-02-13 20:29:27 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2018-10-03 19:04:46 -07:00
|
|
|
"isc_mempool_destroy(): mempool %s "
|
2001-02-20 22:03:36 +00:00
|
|
|
"leaked memory",
|
|
|
|
mpctx->name);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-07-09 14:01:22 +03:00
|
|
|
REQUIRE(mpctx->allocated == 0);
|
1999-06-08 02:38:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return any items on the free list
|
|
|
|
*/
|
2001-01-25 01:18:00 +00:00
|
|
|
while (mpctx->items != NULL) {
|
2021-07-09 14:01:22 +03:00
|
|
|
INSIST(mpctx->freecount > 0);
|
|
|
|
mpctx->freecount--;
|
2021-02-04 21:56:49 +01:00
|
|
|
|
2001-01-25 01:18:00 +00:00
|
|
|
item = mpctx->items;
|
|
|
|
mpctx->items = item->next;
|
|
|
|
|
2021-02-04 20:11:20 +01:00
|
|
|
mem_putstats(mctx, item, mpctx->size);
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_put(mctx, item, mpctx->size, 0);
|
2001-01-25 01:18:00 +00:00
|
|
|
}
|
1999-06-08 02:38:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove our linked list entry from the memory context.
|
|
|
|
*/
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXLOCK(mctx);
|
1999-06-08 02:38:30 +00:00
|
|
|
ISC_LIST_UNLINK(mctx->pools, mpctx, link);
|
2008-03-31 05:00:30 +00:00
|
|
|
mctx->poolcnt--;
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXUNLOCK(mctx);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
mpctx->magic = 0;
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_put(mpctx->mctx, mpctx, sizeof(isc_mempool_t));
|
1999-09-15 17:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 11:08:34 +01:00
|
|
|
void *
|
2021-07-09 14:24:33 +03:00
|
|
|
isc__mempool_get(isc_mempool_t *restrict mpctx FLARG) {
|
|
|
|
element *restrict item = NULL;
|
2021-02-25 11:08:34 +01:00
|
|
|
|
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
mpctx->allocated++;
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if (mpctx->items == NULL) {
|
2021-02-04 21:56:49 +01:00
|
|
|
isc_mem_t *mctx = mpctx->mctx;
|
2021-12-15 12:07:22 +01:00
|
|
|
#if !__SANITIZE_ADDRESS__
|
2021-07-09 14:01:22 +03:00
|
|
|
const size_t fillcount = mpctx->fillcount;
|
2021-12-15 12:07:22 +01:00
|
|
|
#else
|
|
|
|
const size_t fillcount = 1;
|
|
|
|
#endif
|
2016-03-04 11:12:23 +05:30
|
|
|
/*
|
2021-12-15 12:07:22 +01:00
|
|
|
* We need to dip into the well. Fill up our free list.
|
2016-03-04 11:12:23 +05:30
|
|
|
*/
|
2021-05-11 12:18:56 +02:00
|
|
|
for (size_t i = 0; i < fillcount; i++) {
|
2021-12-14 21:50:23 +01:00
|
|
|
item = mem_get(mctx, mpctx->size, 0);
|
2021-02-04 20:11:20 +01:00
|
|
|
mem_getstats(mctx, mpctx->size);
|
2016-03-04 11:12:23 +05:30
|
|
|
item->next = mpctx->items;
|
|
|
|
mpctx->items = item;
|
2021-07-09 14:01:22 +03:00
|
|
|
mpctx->freecount++;
|
2005-06-17 02:22:45 +00:00
|
|
|
}
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item = mpctx->items;
|
2021-07-09 14:01:22 +03:00
|
|
|
INSIST(item != NULL);
|
|
|
|
|
1999-06-08 02:38:30 +00:00
|
|
|
mpctx->items = item->next;
|
2021-02-04 21:56:49 +01:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
INSIST(mpctx->freecount > 0);
|
|
|
|
mpctx->freecount--;
|
|
|
|
mpctx->gets++;
|
2021-02-22 08:44:31 +11:00
|
|
|
|
|
|
|
ADD_TRACE(mpctx->mctx, item, mpctx->size, file, line);
|
1999-09-15 17:47:08 +00:00
|
|
|
|
1999-06-08 02:38:30 +00:00
|
|
|
return (item);
|
|
|
|
}
|
|
|
|
|
2012-11-02 16:02:15 +11:00
|
|
|
/* coverity[+free : arg-1] */
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-07-09 14:24:33 +03:00
|
|
|
isc__mempool_put(isc_mempool_t *restrict mpctx, void *mem FLARG) {
|
|
|
|
element *restrict item = NULL;
|
2021-02-25 11:08:34 +01:00
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
1999-06-08 02:38:30 +00:00
|
|
|
REQUIRE(mem != NULL);
|
|
|
|
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_t *mctx = mpctx->mctx;
|
2021-07-09 14:01:22 +03:00
|
|
|
const size_t freecount = mpctx->freecount;
|
2021-12-15 12:07:22 +01:00
|
|
|
#if !__SANITIZE_ADDRESS__
|
2021-07-09 14:01:22 +03:00
|
|
|
const size_t freemax = mpctx->freemax;
|
2021-12-15 12:07:22 +01:00
|
|
|
#else
|
|
|
|
const size_t freemax = 0;
|
|
|
|
#endif
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
INSIST(mpctx->allocated > 0);
|
|
|
|
mpctx->allocated--;
|
1999-06-08 02:38:30 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
DELETE_TRACE(mctx, mem, mpctx->size, file, line);
|
2000-07-26 19:06:20 +00:00
|
|
|
|
1999-06-08 02:38:30 +00:00
|
|
|
/*
|
|
|
|
* If our free list is full, return this to the mctx directly.
|
|
|
|
*/
|
2021-02-04 21:56:49 +01:00
|
|
|
if (freecount >= freemax) {
|
2021-02-04 20:11:20 +01:00
|
|
|
mem_putstats(mctx, mem, mpctx->size);
|
2021-12-14 21:50:23 +01:00
|
|
|
mem_put(mctx, mem, mpctx->size, 0);
|
1999-06-08 02:38:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, attach it to our free list and bump the counter.
|
|
|
|
*/
|
|
|
|
item = (element *)mem;
|
|
|
|
item->next = mpctx->items;
|
|
|
|
mpctx->items = item;
|
2021-07-09 14:01:22 +03:00
|
|
|
mpctx->freecount++;
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Quotas
|
|
|
|
*/
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_setfreemax(isc_mempool_t *restrict mpctx,
|
|
|
|
const unsigned int limit) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
2021-07-09 14:01:22 +03:00
|
|
|
mpctx->freemax = limit;
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
unsigned int
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_getfreemax(isc_mempool_t *restrict mpctx) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
return (mpctx->freemax);
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
unsigned int
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_getfreecount(isc_mempool_t *restrict mpctx) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
return (mpctx->freecount);
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
unsigned int
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_getallocated(isc_mempool_t *restrict mpctx) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
return (mpctx->allocated);
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_setfillcount(isc_mempool_t *restrict mpctx,
|
|
|
|
unsigned int const limit) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
1999-06-08 02:38:30 +00:00
|
|
|
REQUIRE(limit > 0);
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
mpctx->fillcount = limit;
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
unsigned int
|
2021-07-09 14:24:33 +03:00
|
|
|
isc_mempool_getfillcount(isc_mempool_t *restrict mpctx) {
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_MEMPOOL(mpctx));
|
2009-09-01 00:22:28 +00:00
|
|
|
|
2021-07-09 14:01:22 +03:00
|
|
|
return (mpctx->fillcount);
|
1999-06-08 02:38:30 +00:00
|
|
|
}
|
2006-01-04 03:16:47 +00:00
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
/*
|
|
|
|
* Requires contextslock to be held by caller.
|
|
|
|
*/
|
2021-02-04 23:10:39 +01:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2017-09-08 13:39:09 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
print_contexts(FILE *file) {
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_t *ctx;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
|
|
|
|
ctx = ISC_LIST_NEXT(ctx, link)) {
|
2018-10-03 07:59:18 +10:00
|
|
|
fprintf(file, "context: %p (%s): %" PRIuFAST32 " references\n",
|
2020-02-12 13:59:18 +01:00
|
|
|
ctx, ctx->name[0] == 0 ? "<unknown>" : ctx->name,
|
2018-05-24 14:43:25 +02:00
|
|
|
isc_refcount_current(&ctx->references));
|
2017-09-08 13:39:09 -07:00
|
|
|
print_active(ctx, file);
|
|
|
|
}
|
|
|
|
fflush(file);
|
|
|
|
}
|
2021-02-04 23:10:39 +01:00
|
|
|
#endif
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2022-03-08 23:55:10 +01:00
|
|
|
static atomic_uintptr_t checkdestroyed = 0;
|
2021-02-09 17:44:40 +01:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_checkdestroyed(FILE *file) {
|
2021-02-09 17:44:40 +01:00
|
|
|
atomic_store_release(&checkdestroyed, (uintptr_t)file);
|
|
|
|
}
|
2006-01-04 03:16:47 +00:00
|
|
|
|
2021-02-09 17:44:40 +01:00
|
|
|
void
|
|
|
|
isc__mem_checkdestroyed(void) {
|
|
|
|
FILE *file = (FILE *)atomic_load_acquire(&checkdestroyed);
|
|
|
|
|
|
|
|
if (file == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2006-01-04 03:16:47 +00:00
|
|
|
|
2015-01-20 13:29:18 -08:00
|
|
|
LOCK(&contextslock);
|
2017-04-22 08:25:10 +05:30
|
|
|
if (!ISC_LIST_EMPTY(contexts)) {
|
2006-01-04 03:16:47 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((isc_mem_debugging & TRACE_OR_RECORD) != 0) {
|
2017-09-08 13:39:09 -07:00
|
|
|
print_contexts(file);
|
2006-01-04 03:16:47 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2021-10-11 12:50:17 +02:00
|
|
|
UNREACHABLE();
|
2006-01-04 03:16:47 +00:00
|
|
|
}
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2006-01-04 03:16:47 +00:00
|
|
|
}
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
unsigned int
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_references(isc_mem_t *ctx) {
|
2018-05-24 14:43:25 +02:00
|
|
|
return (isc_refcount_current(&ctx->references));
|
2008-01-02 05:03:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-31 05:00:30 +00:00
|
|
|
typedef struct summarystat {
|
2020-02-12 13:59:18 +01:00
|
|
|
uint64_t total;
|
|
|
|
uint64_t inuse;
|
|
|
|
uint64_t malloced;
|
|
|
|
uint64_t contextsize;
|
2008-03-31 05:00:30 +00:00
|
|
|
} summarystat_t;
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2013-03-13 14:24:50 -07:00
|
|
|
#ifdef HAVE_LIBXML2
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TRY0(a) \
|
|
|
|
do { \
|
|
|
|
xmlrc = (a); \
|
|
|
|
if (xmlrc < 0) \
|
|
|
|
goto error; \
|
|
|
|
} while (0)
|
2012-11-01 10:22:11 +11:00
|
|
|
static int
|
2021-02-04 20:19:09 +01:00
|
|
|
xml_renderctx(isc_mem_t *ctx, summarystat_t *summary, xmlTextWriterPtr writer) {
|
2007-02-13 02:49:08 +00:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
|
|
|
|
2020-02-01 17:59:19 +01:00
|
|
|
int xmlrc;
|
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXLOCK(ctx);
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "context"));
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id"));
|
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%p", ctx));
|
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* id */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2008-03-31 05:00:30 +00:00
|
|
|
if (ctx->name[0] != 0) {
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
|
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%s", ctx->name));
|
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* name */
|
2008-03-31 05:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
summary->contextsize += sizeof(*ctx);
|
2008-03-31 05:00:30 +00:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
if (ctx->debuglist != NULL) {
|
2020-02-13 14:44:37 -08:00
|
|
|
summary->contextsize += DEBUG_TABLE_COUNT *
|
|
|
|
sizeof(debuglist_t) +
|
|
|
|
ctx->debuglistcnt * sizeof(debuglink_t);
|
2008-03-31 05:00:30 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "references"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(
|
|
|
|
writer, "%" PRIuFAST32,
|
|
|
|
isc_refcount_current(&ctx->references)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* references */
|
2008-03-31 05:00:30 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
summary->total += isc_mem_total(ctx);
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "total"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2021-02-04 21:56:49 +01:00
|
|
|
(uint64_t)isc_mem_total(ctx)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* total */
|
2008-03-31 05:00:30 +00:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
summary->inuse += isc_mem_inuse(ctx);
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2021-02-04 21:56:49 +01:00
|
|
|
(uint64_t)isc_mem_inuse(ctx)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* inuse */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2021-02-04 21:56:49 +01:00
|
|
|
(uint64_t)isc_mem_maxinuse(ctx)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* maxinuse */
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
summary->malloced += isc_mem_malloced(ctx);
|
2017-02-02 15:36:38 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "malloced"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2021-02-04 21:56:49 +01:00
|
|
|
(uint64_t)isc_mem_malloced(ctx)));
|
2017-02-02 15:36:38 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* malloced */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxmalloced"));
|
2021-02-04 21:56:49 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(
|
|
|
|
writer, "%" PRIu64 "", (uint64_t)isc_mem_maxmalloced(ctx)));
|
2017-02-02 15:36:38 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* maxmalloced */
|
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools"));
|
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt));
|
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* pools */
|
2008-03-31 05:00:30 +00:00
|
|
|
summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater"));
|
2021-02-04 21:56:49 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(
|
|
|
|
writer, "%" PRIu64 "",
|
2021-07-22 06:14:32 +02:00
|
|
|
(uint64_t)atomic_load_relaxed(&ctx->hi_water)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* hiwater */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater"));
|
2021-02-04 21:56:49 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(
|
|
|
|
writer, "%" PRIu64 "",
|
2021-07-22 06:14:32 +02:00
|
|
|
(uint64_t)atomic_load_relaxed(&ctx->lo_water)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* lowater */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* context */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
error:
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXUNLOCK(ctx);
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
return (xmlrc);
|
2008-03-31 05:00:30 +00:00
|
|
|
}
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_renderxml(void *writer0) {
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_t *ctx;
|
2021-05-11 14:00:12 +02:00
|
|
|
summarystat_t summary = { 0 };
|
2020-02-13 14:44:37 -08:00
|
|
|
uint64_t lost;
|
|
|
|
int xmlrc;
|
2019-06-24 14:25:55 +02:00
|
|
|
xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts"));
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2015-01-20 13:29:18 -08:00
|
|
|
LOCK(&contextslock);
|
2008-03-31 05:00:30 +00:00
|
|
|
lost = totallost;
|
2020-02-12 13:59:18 +01:00
|
|
|
for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
|
2008-03-31 05:00:30 +00:00
|
|
|
ctx = ISC_LIST_NEXT(ctx, link)) {
|
2013-03-13 14:24:50 -07:00
|
|
|
xmlrc = xml_renderctx(ctx, &summary, writer);
|
2012-11-01 10:22:11 +11:00
|
|
|
if (xmlrc < 0) {
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2012-11-01 10:22:11 +11:00
|
|
|
goto error;
|
|
|
|
}
|
2008-03-31 05:00:30 +00:00
|
|
|
}
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* contexts */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary"));
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2012-10-31 23:45:49 +00:00
|
|
|
summary.total));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* TotalUse */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2012-10-31 23:45:49 +00:00
|
|
|
summary.inuse));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* InUse */
|
|
|
|
|
2017-02-02 15:36:38 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Malloced"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2017-02-02 15:36:38 +11:00
|
|
|
summary.malloced));
|
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* InUse */
|
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
|
2012-10-31 23:45:49 +00:00
|
|
|
summary.contextsize));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* ContextSize */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "", lost));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* Lost */
|
|
|
|
|
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* summary */
|
2020-02-12 13:59:18 +01:00
|
|
|
error:
|
2012-11-01 10:22:11 +11:00
|
|
|
return (xmlrc);
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LIBXML2 */
|
2013-03-13 14:24:50 -07:00
|
|
|
|
2019-02-06 11:56:42 +01:00
|
|
|
#ifdef HAVE_JSON_C
|
2018-11-18 09:15:46 +01:00
|
|
|
#define CHECKMEM(m) RUNTIME_CHECK(m != NULL)
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
static isc_result_t
|
2021-02-04 20:19:09 +01:00
|
|
|
json_renderctx(isc_mem_t *ctx, summarystat_t *summary, json_object *array) {
|
2013-03-13 14:24:50 -07:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
|
|
|
REQUIRE(summary != NULL);
|
|
|
|
REQUIRE(array != NULL);
|
|
|
|
|
2020-02-01 17:59:19 +01:00
|
|
|
json_object *ctxobj, *obj;
|
2020-02-13 14:44:37 -08:00
|
|
|
char buf[1024];
|
2020-02-01 17:59:19 +01:00
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXLOCK(ctx);
|
2013-03-13 14:24:50 -07:00
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
summary->contextsize += sizeof(*ctx);
|
|
|
|
summary->total += isc_mem_total(ctx);
|
|
|
|
summary->inuse += isc_mem_inuse(ctx);
|
|
|
|
summary->malloced += isc_mem_malloced(ctx);
|
2013-03-13 14:24:50 -07:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
if (ctx->debuglist != NULL) {
|
2020-02-13 14:44:37 -08:00
|
|
|
summary->contextsize += DEBUG_TABLE_COUNT *
|
|
|
|
sizeof(debuglist_t) +
|
|
|
|
ctx->debuglistcnt * sizeof(debuglink_t);
|
2013-03-13 14:24:50 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
ctxobj = json_object_new_object();
|
|
|
|
CHECKMEM(ctxobj);
|
|
|
|
|
2017-10-03 14:54:19 +11:00
|
|
|
snprintf(buf, sizeof(buf), "%p", ctx);
|
2013-03-13 14:24:50 -07:00
|
|
|
obj = json_object_new_string(buf);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "id", obj);
|
|
|
|
|
|
|
|
if (ctx->name[0] != 0) {
|
|
|
|
obj = json_object_new_string(ctx->name);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "name", obj);
|
|
|
|
}
|
|
|
|
|
2018-05-24 14:43:25 +02:00
|
|
|
obj = json_object_new_int64(isc_refcount_current(&ctx->references));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "references", obj);
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
obj = json_object_new_int64(isc_mem_total(ctx));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "total", obj);
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
obj = json_object_new_int64(isc_mem_inuse(ctx));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "inuse", obj);
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
obj = json_object_new_int64(isc_mem_maxinuse(ctx));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "maxinuse", obj);
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
obj = json_object_new_int64(isc_mem_malloced(ctx));
|
2017-02-02 15:36:38 +11:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "malloced", obj);
|
|
|
|
|
2021-02-04 21:56:49 +01:00
|
|
|
obj = json_object_new_int64(isc_mem_maxmalloced(ctx));
|
2017-02-02 15:36:38 +11:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "maxmalloced", obj);
|
|
|
|
|
2013-03-13 14:24:50 -07:00
|
|
|
obj = json_object_new_int64(ctx->poolcnt);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "pools", obj);
|
|
|
|
|
2015-03-09 10:29:03 +05:30
|
|
|
summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
|
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
obj = json_object_new_int64(atomic_load_relaxed(&ctx->hi_water));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "hiwater", obj);
|
|
|
|
|
2021-07-22 06:14:32 +02:00
|
|
|
obj = json_object_new_int64(atomic_load_relaxed(&ctx->lo_water));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(ctxobj, "lowater", obj);
|
|
|
|
|
2019-09-06 12:46:57 +02:00
|
|
|
MCTXUNLOCK(ctx);
|
2013-03-13 14:24:50 -07:00
|
|
|
json_object_array_add(array, ctxobj);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_renderjson(void *memobj0) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2021-02-04 20:19:09 +01:00
|
|
|
isc_mem_t *ctx;
|
2021-05-11 14:00:12 +02:00
|
|
|
summarystat_t summary = { 0 };
|
2020-02-13 14:44:37 -08:00
|
|
|
uint64_t lost;
|
|
|
|
json_object *ctxarray, *obj;
|
|
|
|
json_object *memobj = (json_object *)memobj0;
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
ctxarray = json_object_new_array();
|
|
|
|
CHECKMEM(ctxarray);
|
|
|
|
|
2015-01-20 13:29:18 -08:00
|
|
|
LOCK(&contextslock);
|
2013-03-13 14:24:50 -07:00
|
|
|
lost = totallost;
|
2020-02-12 13:59:18 +01:00
|
|
|
for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
|
2013-03-13 14:24:50 -07:00
|
|
|
ctx = ISC_LIST_NEXT(ctx, link)) {
|
|
|
|
result = json_renderctx(ctx, &summary, ctxarray);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2013-03-13 14:24:50 -07:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 13:29:18 -08:00
|
|
|
UNLOCK(&contextslock);
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
obj = json_object_new_int64(summary.total);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(memobj, "TotalUse", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(summary.inuse);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(memobj, "InUse", obj);
|
|
|
|
|
2017-02-02 15:36:38 +11:00
|
|
|
obj = json_object_new_int64(summary.malloced);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(memobj, "Malloced", obj);
|
|
|
|
|
2013-03-13 14:24:50 -07:00
|
|
|
obj = json_object_new_int64(summary.contextsize);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(memobj, "ContextSize", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(lost);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(memobj, "Lost", obj);
|
|
|
|
|
|
|
|
json_object_object_add(memobj, "contexts", ctxarray);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
error:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctxarray != NULL) {
|
2013-03-13 14:24:50 -07:00
|
|
|
json_object_put(ctxarray);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-03-13 14:24:50 -07:00
|
|
|
return (result);
|
|
|
|
}
|
2019-02-06 11:56:42 +01:00
|
|
|
#endif /* HAVE_JSON_C */
|
2013-04-10 13:49:57 -07:00
|
|
|
|
2019-09-05 18:40:57 +02:00
|
|
|
void
|
2021-05-11 12:18:56 +02:00
|
|
|
isc__mem_create(isc_mem_t **mctxp FLARG) {
|
2019-09-06 11:31:15 +02:00
|
|
|
mem_create(mctxp, isc_mem_defaultflags);
|
2021-05-11 12:18:56 +02:00
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
|
|
|
|
fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp,
|
|
|
|
file, line);
|
|
|
|
}
|
|
|
|
#endif /* ISC_MEM_TRACKLINES */
|
2013-04-10 13:49:57 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:04:46 -07:00
|
|
|
void
|
2021-02-04 20:19:09 +01:00
|
|
|
isc__mem_printactive(isc_mem_t *ctx, FILE *file) {
|
2018-10-03 19:04:46 -07:00
|
|
|
#if ISC_MEM_TRACKLINES
|
2021-02-04 20:19:09 +01:00
|
|
|
REQUIRE(VALID_CONTEXT(ctx));
|
2018-10-03 19:04:46 -07:00
|
|
|
REQUIRE(file != NULL);
|
|
|
|
|
|
|
|
print_active(ctx, file);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if ISC_MEM_TRACKLINES */
|
2021-02-04 20:19:09 +01:00
|
|
|
UNUSED(ctx);
|
2018-10-03 19:04:46 -07:00
|
|
|
UNUSED(file);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2013-04-10 13:49:57 -07:00
|
|
|
}
|