From 16baa70f9d74508137ced5c62c117808ceb875bf Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Tue, 27 Apr 1999 23:20:10 +0000 Subject: [PATCH] basic quota support; fix bugs in more_basic_blocks() --- lib/isc/mem.c | 62 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 96e97b0c98..53b65b9ebb 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -72,6 +72,7 @@ struct isc_mem { unsigned char * lowest; unsigned char * highest; struct stats * stats; + size_t quota; }; /* Forward. */ @@ -155,6 +156,7 @@ isc_mem_create(size_t init_max_size, size_t target_size, "isc_mutex_init() failed"); return (ISC_R_UNEXPECTED); } + ctx->quota = 0; ctx->magic = MEM_MAGIC; *ctxp = ctx; return (ISC_R_SUCCESS); @@ -192,31 +194,43 @@ more_basic_blocks(isc_mem_t *ctx) { unsigned char *first, *last; unsigned char **table; unsigned int table_size; + size_t total; int i; /* Require: we hold the context lock. */ - if (ctx->basic_table_count <= ctx->basic_table_size) { + /* + * Did we hit the quota for this context? + */ + if (ctx->quota != 0) { + total = (size_t)(ctx->basic_table_count + 1) * + NUM_BASIC_BLOCKS * ctx->mem_target; + if (total > ctx->quota) + return; + } + + INSIST(ctx->basic_table_count <= ctx->basic_table_size); + if (ctx->basic_table_count == ctx->basic_table_size) { table_size = ctx->basic_table_size + TABLE_INCREMENT; table = malloc(table_size * sizeof (unsigned char *)); if (table == NULL) return; - memcpy(table, ctx->basic_table, - ctx->basic_table_size * sizeof (unsigned char *)); - free(ctx->basic_table); + if (ctx->basic_table_size != 0) { + memcpy(table, ctx->basic_table, + ctx->basic_table_size * + sizeof (unsigned char *)); + free(ctx->basic_table); + } ctx->basic_table = table; ctx->basic_table_size = table_size; - } else - table = NULL; + } new = malloc(NUM_BASIC_BLOCKS * ctx->mem_target); - if (new == NULL) { - if (table != NULL) - free(table); + if (new == NULL) return; - } ctx->basic_table[ctx->basic_table_count] = new; ctx->basic_table_count++; + curr = new; next = curr + ctx->mem_target; for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) { @@ -454,6 +468,34 @@ isc_mem_strdup(isc_mem_t *mctx, const char *s) { return (ns); } +/* + * Quotas + */ + +void +isc_mem_setquota(isc_mem_t *ctx, size_t quota) { + REQUIRE(VALID_CONTEXT(ctx)); + LOCK(&ctx->lock); + + ctx->quota = quota; + + UNLOCK(&ctx->lock); +} + +size_t +isc_mem_getquota(isc_mem_t *ctx) { + size_t quota; + + REQUIRE(VALID_CONTEXT(ctx)); + LOCK(&ctx->lock); + + quota = ctx->quota; + + UNLOCK(&ctx->lock); + + return (quota); +} + #ifdef ISC_MEMCLUSTER_LEGACY /*