mem.c revision 2983bfafb769d9c64d11bfdde7f5384ea236b4df
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/*
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater * Copyright (c) 1997, 1998 by Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * copyright notice and this permission notice appear in all copies.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews#include <sys/types.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdio.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <stdlib.h>
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff#include <string.h>
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley#include <isc/assertions.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/error.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews#include <isc/mem.h>
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
364a82f7c25b62967678027043425201a5e5171aBob Halley#ifndef ISC_SINGLETHREADED
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/mutex.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#endif
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley/*
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Types.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleytypedef struct {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley void * next;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley} element;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleytypedef struct {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley size_t size;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /*
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * This structure must be ALIGNMENT_SIZE bytes.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley} *size_info;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystruct stats {
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley unsigned long gets;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley unsigned long totalgets;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley unsigned long blocks;
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley unsigned long freefrags;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley};
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrewsstruct isc_memctx {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley size_t max_size;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley size_t mem_target;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley element ** freelists;
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews element * basic_blocks;
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley unsigned char ** basic_table;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews unsigned int basic_table_count;
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews unsigned int basic_table_size;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley unsigned char * lowest;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley unsigned char * highest;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley struct stats * stats;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley isc_mutex_t mutex;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley};
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/* Forward. */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic size_t quantize(size_t);
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/* Macros. */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DEF_MAX_SIZE 1100
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DEF_MEM_TARGET 4096
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley#define ALIGNMENT_SIZE sizeof (void *)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define NUM_BASIC_BLOCKS 64 /* must be > 1 */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define TABLE_INCREMENT 1024
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley#ifndef ISC_SINGLETHREADED
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff#define LOCK_CONTEXT(ctx) \
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews INSIST(isc_mutex_lock(&(ctx)->mutex) == ISC_R_SUCCESS)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define UNLOCK_CONTEXT(ctx) \
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley INSIST(isc_mutex_unlock(&(ctx)->mutex) == ISC_R_SUCCESS)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#else
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews#define LOCK_CONTEXT(ctx)
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley#define UNLOCK_CONTEXT(ctx)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#endif
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/* Private Inline-able. */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic inline size_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyquantize(size_t size) {
2cd0c38115b1efb043ed3104c0d08e51ceade0d7Bob Halley int remainder;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /*
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley * If there is no remainder for the integer division of
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * (rightsize/ALIGNMENT_SIZE)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * then we already have a good size; if not, then we need
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * to round up the result in order to get a size big
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * byte boundaries.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley remainder = size % ALIGNMENT_SIZE;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley if (remainder != 0)
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff size += ALIGNMENT_SIZE - remainder;
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews return (size);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/* Public. */
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halleyisc_result_t
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrewsisc_memctx_create(size_t init_max_size, size_t target_size,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley isc_memctx_t *ctxp)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley{
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley isc_memctx_t ctx;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley REQUIRE(ctxp != NULL && *ctxp == NULL);
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley ctx = malloc(sizeof *ctx);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (init_max_size == 0)
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley ctx->max_size = DEF_MAX_SIZE;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley else
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley ctx->max_size = init_max_size;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley if (target_size == 0)
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley ctx->mem_target = DEF_MEM_TARGET;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley else
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley ctx->mem_target = target_size;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley ctx->freelists = malloc(ctx->max_size * sizeof (element *));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (ctx->freelists == NULL) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley free(ctx);
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson return (ISC_R_NOMEMORY);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley memset(ctx->freelists, 0,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff ctx->max_size * sizeof (element *));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->stats = malloc((ctx->max_size+1) * sizeof (struct stats));
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson if (ctx->stats == NULL) {
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater free(ctx->freelists);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff free(ctx);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (ISC_R_NOMEMORY);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof (struct stats));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->basic_blocks = NULL;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->basic_table = NULL;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->basic_table_count = 0;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->basic_table_size = 0;
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson ctx->lowest = NULL;
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson ctx->highest = NULL;
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater if (isc_mutex_init(&ctx->mutex) != ISC_R_SUCCESS) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley free(ctx->stats);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley free(ctx->freelists);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley free(ctx);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley "isc_mutex_init() failed");
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (ISC_R_UNEXPECTED);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley }
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley *ctxp = ctx;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (ISC_R_SUCCESS);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley}
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleyvoid
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleyisc_memctx_destroy(isc_memctx_t *ctxp) {
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson unsigned int i;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley isc_memctx_t ctx;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley REQUIRE(ctxp != NULL);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ctx = *ctxp;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley for (i = 0; i <= ctx->max_size; i++)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley INSIST(ctx->stats[i].gets == 0);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley for (i = 0; i < ctx->basic_table_count; i++)
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley free(ctx->basic_table[i]);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews free(ctx->freelists);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews free(ctx->stats);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 free(ctx->basic_table);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 (void)isc_mutex_destroy(&ctx->mutex);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 free(ctx);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews *ctxp = NULL;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews}
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic void
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleymore_basic_blocks(isc_memctx_t ctx) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley void *new;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley unsigned char *curr, *next;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley unsigned char *first, *last;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley unsigned char **table;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley unsigned int table_size;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley int i;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley /* Require: we hold the context lock. */
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (ctx->basic_table_count <= ctx->basic_table_size) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley table_size = ctx->basic_table_size + TABLE_INCREMENT;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley table = malloc(table_size * sizeof (unsigned char *));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (table == NULL)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley memcpy(table, ctx->basic_table,
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->basic_table_size * sizeof (unsigned char *));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley free(ctx->basic_table);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ctx->basic_table = table;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ctx->basic_table_size = table_size;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley } else
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley table = NULL;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley new = malloc(NUM_BASIC_BLOCKS * ctx->mem_target);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley if (new == NULL) {
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley if (table != NULL)
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley free(table);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley }
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ctx->basic_table[ctx->basic_table_count] = new;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ctx->basic_table_count++;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley curr = new;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley next = curr + ctx->mem_target;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley ((element *)curr)->next = next;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley curr = next;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley next += ctx->mem_target;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley }
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley /*
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley * curr is now pointing at the last block in the
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley * array.
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley ((element *)curr)->next = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence first = new;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley if (first < ctx->lowest || ctx->lowest == NULL)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley ctx->lowest = first;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (last > ctx->highest)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ctx->highest = last;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ctx->basic_blocks = new;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyvoid *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley__isc_mem_get(isc_memctx_t ctx, size_t size) {
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley size_t new_size = quantize(size);
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley void *ret;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(size > 0);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley LOCK_CONTEXT(ctx);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (size >= ctx->max_size || new_size >= ctx->max_size) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /* memget() was called on something beyond our upper limit. */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ret = malloc(size);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (ret != NULL) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ctx->stats[ctx->max_size].gets++;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ctx->stats[ctx->max_size].totalgets++;
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley }
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley goto done;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley }
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /*
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * If there are no blocks in the free list for this size, get a chunk
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * of memory and then break it up into "new_size"-sized blocks, adding
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * them to the free list.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (ctx->freelists[new_size] == NULL) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley int i, frags;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley size_t total_size;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley void *new;
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley unsigned char *curr, *next;
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (ctx->basic_blocks == NULL) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley more_basic_blocks(ctx);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (ctx->basic_blocks == NULL) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ret = NULL;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley goto done;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence total_size = ctx->mem_target;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson new = ctx->basic_blocks;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ctx->basic_blocks = ctx->basic_blocks->next;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson frags = total_size / new_size;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ctx->stats[new_size].blocks++;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ctx->stats[new_size].freefrags += frags;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson /* Set up a linked-list of blocks of size "new_size". */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson curr = new;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson next = curr + new_size;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson for (i = 0; i < (frags - 1); i++) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ((element *)curr)->next = next;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson curr = next;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley next += new_size;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews /* curr is now pointing at the last block in the array. */
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews ((element *)curr)->next = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews ctx->freelists[new_size] = new;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington }
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /* The free list uses the "rounded-up" size "new_size": */
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews ret = ctx->freelists[new_size];
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff ctx->freelists[new_size] = ctx->freelists[new_size]->next;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews /*
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews * The stats[] uses the _actual_ "size" requested by the
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff * caller, with the caveat (in the code above) that "size" >= the
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * max. size (max_size) ends up getting recorded as a call to
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley * max_size.
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews */
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews ctx->stats[size].gets++;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ctx->stats[size].totalgets++;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews ctx->stats[new_size].freefrags--;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley done:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley UNLOCK_CONTEXT(ctx);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (ret);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley/*
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson * This is a call from an external caller,
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews * so we want to count this as a user "put".
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley */
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halleyvoid
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley__isc_mem_put(isc_memctx_t ctx, void *mem, size_t size) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley size_t new_size = quantize(size);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley REQUIRE(size > 0);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley LOCK_CONTEXT(ctx);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (size == ctx->max_size || new_size >= ctx->max_size) {
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley /* memput() called on something beyond our upper limit */
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington free(mem);
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington INSIST(ctx->stats[ctx->max_size].gets != 0);
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington ctx->stats[ctx->max_size].gets--;
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington goto done;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington }
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley /* The free list uses the "rounded-up" size "new_size": */
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ((element *)mem)->next = ctx->freelists[new_size];
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ctx->freelists[new_size] = (element *)mem;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff /*
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * The stats[] uses the _actual_ "size" requested by the
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * caller, with the caveat (in the code above) that "size" >= the
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * max. size (max_size) ends up getting recorded as a call to
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson * max_size.
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson INSIST(ctx->stats[size].gets != 0);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews ctx->stats[size].gets--;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ctx->stats[new_size].freefrags++;
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson done:
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews UNLOCK_CONTEXT(ctx);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews}
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrewsvoid *
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews__isc_mem_getdebug(isc_memctx_t ctx, size_t size, const char *file, int line) {
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews void *ptr;
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews ptr = __isc_mem_get(ctx, size);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews fprintf(stderr, "%s:%d: mem_get(%p, %lu) -> %p\n", file, line,
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews ctx, (unsigned long)size, ptr);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews return (ptr);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonvoid
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson__isc_mem_putdebug(isc_memctx_t ctx, void *ptr, size_t size, const char *file,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson int line)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson{
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson fprintf(stderr, "%s:%d: mem_put(%p, %p, %lu)\n", file, line,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ctx, ptr, (unsigned long)size);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson __isc_mem_put(ctx, ptr, size);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson/*
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson * Print the stats[] on the stream "out" with suitable formatting.
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonvoid
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrewsisc_mem_stats(isc_memctx_t ctx, FILE *out) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley size_t i;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley LOCK_CONTEXT(ctx);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley if (ctx->freelists == NULL)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews return;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley for (i = 1; i <= ctx->max_size; i++) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews const struct stats *s = &ctx->stats[i];
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (s->totalgets == 0 && s->gets == 0)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson continue;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson fprintf(out, "%s%5d: %11lu gets, %11lu rem",
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson (i == ctx->max_size) ? ">=" : " ",
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews i, s->totalgets, s->gets);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (s->blocks != 0)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews fprintf(out, " (%lu bl, %lu ff)",
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews s->blocks, s->freefrags);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews fputc('\n', out);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews UNLOCK_CONTEXT(ctx);
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews}
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrewsisc_boolean_t
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrewsisc_mem_valid(isc_memctx_t ctx, void *ptr) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews unsigned char *cp = ptr;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews isc_boolean_t result = ISC_FALSE;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews LOCK_CONTEXT(ctx);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (ctx->lowest != NULL && cp >= ctx->lowest && cp <= ctx->highest)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews result = ISC_TRUE;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson UNLOCK_CONTEXT(ctx);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (result);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley}
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews/*
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews * Replacements for malloc() and free().
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews */
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrewsvoid *
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrewsisc_mem_allocate(isc_memctx_t ctx, size_t size) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson size_info si;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
abc93ada82b37282122f8162f48761eb99736571Mark Andrews size += ALIGNMENT_SIZE;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews si = isc_mem_get(ctx, size);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (si == NULL)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews return (NULL);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson si->size = size;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (&si[1]);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halleyvoid
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halleyisc_mem_free(isc_memctx_t ctx, void *ptr) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews size_info si;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews si = &(((size_info)ptr)[-1]);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews isc_mem_put(ctx, si, si->size);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews}
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews#ifdef ISC_MEMCLUSTER_LEGACY
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley/*
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley * Public Legacy.
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson */
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleystatic isc_memctx_t default_context = NULL;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graffint
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencememinit(size_t init_max_size, size_t target_size) {
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff /* need default_context lock here */
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater if (default_context != NULL)
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews return (-1);
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson return (isc_mem_create(init_max_size, target_size, &default_context));
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleyisc_memctx_t
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graffmem_default_context(void) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley /* need default_context lock here */
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff if (default_context == NULL && meminit(0, 0) == -1)
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (NULL);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (default_context);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff}
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleyvoid *
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews__memget(size_t size) {
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff /* need default_context lock here */
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley if (default_context == NULL && meminit(0, 0) == -1)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (NULL);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (__mem_get(default_context, size));
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff}
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graffvoid
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff__memput(void *mem, size_t size) {
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff /* need default_context lock here */
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff REQUIRE(default_context != NULL);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff __mem_put(default_context, mem, size);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graffvoid *
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley__memget_debug(size_t size, const char *file, int line) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson void *ptr;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews ptr = __memget(size);
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews fprintf(stderr, "%s:%d: memget(%lu) -> %p\n", file, line,
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley (unsigned long)size, ptr);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (ptr);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleyvoid
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence__memput_debug(void *ptr, size_t size, const char *file, int line) {
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence fprintf(stderr, "%s:%d: memput(%p, %lu)\n", file, line,
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff ptr, (unsigned long)size);
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence __memput(ptr, size);
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence}
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewsint
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graffmemvalid(void *ptr) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley /* need default_context lock here */
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley REQUIRE(default_context != NULL);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (mem_valid(default_context, ptr));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halleyvoid
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonmemstats(FILE *out) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley /* need default_context lock here */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson REQUIRE(default_context != NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff mem_stats(default_context, out);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley#endif /* ISC_MEMCLUSTER_LEGACY */
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff