/*
* 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 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.
*/
if (freelists[new_size] == NULL) {
int i, frags;
size_t total_size;
void *new;
char *curr, *next;
#ifdef MEMCLUSTER_BIG_MALLOC
if (basic_blocks == NULL) {
new = malloc(NUM_BASIC_BLOCKS * mem_target);
if (new == NULL) {
MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
curr = new;
next = curr + mem_target;
for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
((memcluster_element *)curr)->next = next;
curr = next;
next += mem_target;
}
/*
* curr is now pointing at the last block in the
* array.
*/
((memcluster_element *)curr)->next = NULL;
basic_blocks = new;
}
total_size = mem_target;
new = basic_blocks;
basic_blocks = basic_blocks->next;
#else
if (new_size > mem_target_half)
total_size = mem_target_fudge;
else
total_size = mem_target;
new = malloc(total_size);
if (new == NULL) {
MEMUNLOCK;
errno = ENOMEM;
return (NULL);
}
#endif
frags = total_size / new_size;
stats[new_size].blocks++;
stats[new_size].freefrags += frags;
/* Set up a linked-list of blocks of size "new_size". */
curr = new;
next = curr + new_size;
for (i = 0; i < (frags - 1); i++) {
#if defined (DEBUGGING_MEMCLUSTER)
memset(curr, 0xa5, new_size);
#endif
((memcluster_element *)curr)->next = next;
curr = next;
next += new_size;
}
/* curr is now pointing at the last block in the array. */
#if defined (DEBUGGING_MEMCLUSTER)
memset(curr, 0xa5, new_size);
#endif
((memcluster_element *)curr)->next = freelists[new_size];
freelists[new_size] = new;
}
/* The free list uses the "rounded-up" size "new_size". */
#if defined (DEBUGGING_MEMCLUSTER)
e = freelists[new_size];
ret = (char *)e + sizeof *e;
/*
* Check to see if this buffer has been written to while on free list.
*/
check(ret, 0xa5, new_size - sizeof *e);
/*
* Mark memory we are returning.
*/
memset(ret, 0xe5, size);
#else
ret = freelists[new_size];
#endif
freelists[new_size] = freelists[new_size]->next;
#if defined(DEBUGGING_MEMCLUSTER)
e->next = NULL;
e->size = size;
e->fencepost = FRONT_FENCEPOST;
#ifdef MEMCLUSTER_RECORD
e->file = file;
e->line = line;
e->next = activelists[size];
activelists[size] = e;
#endif
p = (char *)e + sizeof *e + size;
memcpy(p, &fp, sizeof fp);
#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.
*/
stats[size].gets++;
stats[size].totalgets++;
stats[new_size].freefrags--;
MEMUNLOCK;
#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
__memput(void *mem, size_t size) {
__memput_record(mem, size, NULL, 0);
}
/*
* 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.
*/
INSIST(stats[size].gets != 0U);
stats[size].gets--;
stats[new_size].freefrags++;
MEMUNLOCK;
}
/*%
* Print the stats[] on the stream "out" with suitable formatting.
*/
void
memstats(FILE *out) {
size_t i;
#ifdef MEMCLUSTER_RECORD
memcluster_element *e;
#endif
MEMLOCK;
if (freelists == NULL) {
MEMUNLOCK;
return;
}
for (i = 1; i <= max_size; i++) {
const struct stats *s = &stats[i];
if (s->totalgets == 0U && s->gets == 0U)
continue;
fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
(i == max_size) ? ">=" : " ",
(unsigned long)i, s->totalgets, s->gets);
if (s->blocks != 0U)
fprintf(out, " (%lu bl, %lu ff)",
s->blocks, s->freefrags);
fputc('\n', out);
}
#ifdef MEMCLUSTER_RECORD
fprintf(out, "Active Memory:\n");
for (i = 1; i <= max_size; i++) {
if ((e = activelists[i]) != NULL)
while (e != NULL) {
fprintf(out, "%s:%d %p:%lu\n",
e->file != NULL ? e->file :
"<UNKNOWN>", e->line,
(char *)e + sizeof *e,
(u_long)e->size);
e = e->next;
}
}
#endif
MEMUNLOCK;
}
int
memactive(void) {
size_t i;
if (stats == NULL)
return (0);
for (i = 1; i <= max_size; i++)
if (stats[i].gets != 0U)
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
quantize(size_t size) {
int remainder;
/*
* If there is no remainder for the integer division of
*
* (rightsize/P_SIZE)
*
* 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.
*/
remainder = size % P_SIZE;
if (remainder != 0)
size += P_SIZE - remainder;
#if defined(DEBUGGING_MEMCLUSTER)
return (size + SMALL_SIZE_LIMIT + sizeof (int));
#else
return (size);
#endif
}
#if defined(DEBUGGING_MEMCLUSTER)
static void
check(unsigned char *a, int value, size_t len) {
size_t i;
for (i = 0; i < len; i++)
INSIST(a[i] == value);
}
#endif