/*
* Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1997,1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* When this symbol is defined allocations via memget are made slightly
bigger and some debugging info stuck before and after the region given
back to the caller. */
/* #define DEBUGGING_MEMCLUSTER */
#define MEMCLUSTER_ATEND
#if !defined(LINT) && !defined(CODECENTER)
#endif /* not lint */
#include "port_before.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <isc/memcluster.h>
#include <isc/assertions.h>
#include "port_after.h"
#ifdef MEMCLUSTER_RECORD
#ifndef DEBUGGING_MEMCLUSTER
#define DEBUGGING_MEMCLUSTER
#endif
#endif
typedef struct {
void * next;
#if defined(DEBUGGING_MEMCLUSTER)
#if defined(MEMCLUSTER_RECORD)
const char * file;
int line;
#endif
#endif
#define P_SIZE sizeof(void *)
#ifndef MEMCLUSTER_LITTLE_MALLOC
#endif
struct stats {
};
#ifdef DO_PTHREADS
#include <pthread.h>
#else
/*
* Catch bad lock usage in non threaded build.
*/
static unsigned int memlock = 0;
#endif /* DO_PTHEADS */
/* Private data. */
#ifndef MEMCLUSTER_BIG_MALLOC
#endif
#ifdef MEMCLUSTER_RECORD
#endif
#ifdef MEMCLUSTER_BIG_MALLOC
#endif
/* Forward. */
#if defined(DEBUGGING_MEMCLUSTER)
#endif
/* Public. */
int
#if defined(DEBUGGING_MEMCLUSTER)
#endif
return (-1);
}
if (init_max_size == 0U)
else
if (target_size == 0U)
else
#ifndef MEMCLUSTER_BIG_MALLOC
#endif
return (-1);
}
max_size * sizeof (memcluster_element *));
#ifdef MEMCLUSTER_RECORD
if (activelists == NULL) {
return (-1);
}
memset(activelists, 0,
#endif
#ifdef MEMCLUSTER_BIG_MALLOC
basic_blocks = NULL;
#endif
return (0);
}
void *
}
void *
#if defined(DEBUGGING_MEMCLUSTER)
char *p;
#endif
void *ret;
#if !defined(MEMCLUSTER_RECORD)
#endif
if (meminit(0, 0) == -1) {
return (NULL);
}
}
if (size == 0U) {
return (NULL);
}
/* memget() was called on something beyond our upper limit. */
#if defined(DEBUGGING_MEMCLUSTER)
if (e == NULL) {
return (NULL);
}
#ifdef MEMCLUSTER_RECORD
activelists[max_size] = e;
#endif
e->fencepost = FRONT_FENCEPOST;
p = (char *)e + sizeof *e + size;
return ((char *)e + sizeof *e);
#else
#endif
}
/*
* If there are no blocks in the free list for this size, get a chunk
* of memory and then break it up into "new_size"-sized blocks, adding
* them to the free list.
*/
int i, frags;
void *new;
#ifdef MEMCLUSTER_BIG_MALLOC
if (basic_blocks == NULL) {
return (NULL);
}
for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
next += mem_target;
}
/*
* curr is now pointing at the last block in the
* array.
*/
basic_blocks = new;
}
new = basic_blocks;
#else
if (new_size > mem_target_half)
else
return (NULL);
}
#endif
/* Set up a linked-list of blocks of size "new_size". */
for (i = 0; i < (frags - 1); i++) {
#if defined (DEBUGGING_MEMCLUSTER)
#endif
}
/* curr is now pointing at the last block in the array. */
#if defined (DEBUGGING_MEMCLUSTER)
#endif
}
/* The free list uses the "rounded-up" size "new_size". */
#if defined (DEBUGGING_MEMCLUSTER)
ret = (char *)e + sizeof *e;
/*
* Check to see if this buffer has been written to while on free list.
*/
/*
* Mark memory we are returning.
*/
#else
#endif
#if defined(DEBUGGING_MEMCLUSTER)
e->fencepost = FRONT_FENCEPOST;
#ifdef MEMCLUSTER_RECORD
activelists[size] = e;
#endif
p = (char *)e + sizeof *e + size;
#endif
/*
* The stats[] uses the _actual_ "size" requested by the
* caller, with the caveat (in the code above) that "size" >= the
* max. size (max_size) ends up getting recorded as a call to
* max_size.
*/
#if defined(DEBUGGING_MEMCLUSTER)
return ((char *)e + sizeof *e);
#else
return (ret);
#endif
}
/*%
* This is a call from an external caller,
* so we want to count this as a user "put".
*/
void
}
void
#if defined (DEBUGGING_MEMCLUSTER)
#ifdef MEMCLUSTER_RECORD
#endif
char *p;
#endif
#if !defined (MEMCLUSTER_RECORD)
#endif
if (size == 0U) {
return;
}
#if defined (DEBUGGING_MEMCLUSTER)
e = (memcluster_element *) ((char *)mem - sizeof *e);
p = (char *)e + sizeof *e + size;
#ifdef MEMCLUSTER_RECORD
else
}
else
} else
#endif
#endif
/* memput() called on something beyond our upper limit */
#if defined(DEBUGGING_MEMCLUSTER)
free(e);
#else
#endif
return;
}
/* The free list uses the "rounded-up" size "new_size": */
#if defined(DEBUGGING_MEMCLUSTER)
e->size = 0; /*%< catch double memput() */
#ifdef MEMCLUSTER_RECORD
#endif
#ifdef MEMCLUSTER_ATEND
if (el)
else
#else
#endif
#else
#endif
/*
* The stats[] uses the _actual_ "size" requested by the
* caller, with the caveat (in the code above) that "size" >= the
* max. size (max_size) ends up getting recorded as a call to
* max_size.
*/
}
void *
void *ptr;
return (ptr);
}
void
}
/*%
* Print the stats[] on the stream "out" with suitable formatting.
*/
void
size_t i;
#ifdef MEMCLUSTER_RECORD
#endif
return;
}
for (i = 1; i <= max_size; i++) {
continue;
if (s->blocks != 0U)
}
#ifdef MEMCLUSTER_RECORD
for (i = 1; i <= max_size; i++) {
if ((e = activelists[i]) != NULL)
while (e != NULL) {
"<UNKNOWN>", e->line,
(char *)e + sizeof *e,
e = e->next;
}
}
#endif
}
int
memactive(void) {
size_t i;
return (0);
for (i = 1; i <= max_size; i++)
return (1);
return (0);
}
/* Private. */
/*%
* Round up size to a multiple of sizeof(void *). This guarantees that a
* block is at least sizeof void *, and that we won't violate alignment
* restrictions, both of which are needed to make lists of blocks.
*/
static size_t
int remainder;
/*
* If there is no remainder for the integer division of
*
*
* then we already have a good size; if not, then we need
* to round up the result in order to get a size big
* enough to satisfy the request _and_ aligned on P_SIZE boundaries.
*/
if (remainder != 0)
#if defined(DEBUGGING_MEMCLUSTER)
return (size + SMALL_SIZE_LIMIT + sizeof (int));
#else
return (size);
#endif
}
#if defined(DEBUGGING_MEMCLUSTER)
static void
size_t i;
for (i = 0; i < len; i++)
}
#endif
/*! \file */