mem.c revision 1cef22510e8065b4cbd7584d38b6b6f4a2082f53
/*
* Copyright (C) 1997, 1998 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM 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.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <isc/assertions.h>
#ifndef ISC_SINGLETHREADED
#include "util.h"
#else
#endif
/*
* Types.
*/
typedef struct {
void * next;
} element;
typedef struct {
/*
* This structure must be ALIGNMENT_SIZE bytes.
*/
} *size_info;
struct stats {
unsigned long gets;
unsigned long totalgets;
unsigned long blocks;
unsigned long freefrags;
};
#define VALID_CONTEXT(c) ((c) != NULL && \
struct isc_memctx {
unsigned int magic;
unsigned char ** basic_table;
unsigned int basic_table_count;
unsigned int basic_table_size;
unsigned char * lowest;
unsigned char * highest;
};
/* Forward. */
/* Constants. */
#define DEF_MAX_SIZE 1100
#define DEF_MEM_TARGET 4096
#define ALIGNMENT_SIZE sizeof (void *)
#define TABLE_INCREMENT 1024
/* Private Inline-able. */
static inline 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 be aligned on ALIGNMENT_SIZE
* byte boundaries.
*/
if (remainder != 0)
return (size);
}
/* Public. */
{
if (init_max_size == 0)
else
if (target_size == 0)
else
return (ISC_R_NOMEMORY);
}
return (ISC_R_NOMEMORY);
}
ctx->basic_table_count = 0;
ctx->basic_table_size = 0;
"isc_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
void
unsigned int i;
for (i = 0; i < ctx->basic_table_count; i++)
}
static void
void *new;
unsigned char **table;
unsigned int table_size;
int i;
/* Require: we hold the context lock. */
return;
ctx->basic_table_size * sizeof (unsigned char *));
} else
return;
}
ctx->basic_table_count++;
for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
}
/*
* curr is now pointing at the last block in the
* array.
*/
}
void *
void *ret;
/* memget() was called on something beyond our upper limit. */
}
goto done;
}
/*
* 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;
goto done;
}
}
/* Set up a linked-list of blocks of size "new_size". */
for (i = 0; i < (frags - 1); i++) {
}
/* curr is now pointing at the last block in the array. */
}
/* The free list uses the "rounded-up" size "new_size": */
/*
* 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.
*/
done:
return (ret);
}
/*
* This is a call from an external caller,
* so we want to count this as a user "put".
*/
void
/* memput() called on something beyond our upper limit */
goto done;
}
/* The free list uses the "rounded-up" size "new_size": */
/*
* 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.
*/
done:
}
void *
void *ptr;
return (ptr);
}
void
int line)
{
}
/*
* Print the stats[] on the stream "out" with suitable formatting.
*/
void
size_t i;
return;
continue;
if (s->blocks != 0)
}
}
return (result);
}
/*
* Replacements for malloc() and free().
*/
void *
size += ALIGNMENT_SIZE;
return (NULL);
return (&si[1]);
}
void
}
#ifdef ISC_MEMCLUSTER_LEGACY
/*
* Public Legacy.
*/
int
/* need default_context lock here */
if (default_context != NULL)
return (-1);
}
mem_default_context(void) {
/* need default_context lock here */
return (NULL);
return (default_context);
}
void *
/* need default_context lock here */
return (NULL);
}
void
/* need default_context lock here */
}
void *
void *ptr;
return (ptr);
}
void
}
int
/* need default_context lock here */
}
void
/* need default_context lock here */
}
#endif /* ISC_MEMCLUSTER_LEGACY */