Commit de9282a1 authored by Bob Halley's avatar Bob Halley
Browse files

rename memcluster

parent c92ba071
......@@ -11,7 +11,7 @@
#include <isc/result.h>
#include <isc/timer.h>
mem_context_t mctx = NULL;
isc_memctx_t mctx = NULL;
static isc_boolean_t
my_callback(isc_task_t task, isc_event_t event)
......@@ -61,7 +61,7 @@ main(int argc, char *argv[]) {
workers = 2;
printf("%d workers\n", workers);
INSIST(mem_context_create(0, 0, &mctx) == 0);
INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS);
INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == workers);
......@@ -149,5 +149,6 @@ main(int argc, char *argv[]) {
isc_taskmgr_destroy(&manager);
printf("destroyed\n");
mem_stats(mctx, stdout);
isc_mem_stats(mctx, stdout);
isc_memctx_destroy(&mctx);
}
......@@ -11,7 +11,7 @@
#include <isc/result.h>
#include <isc/timer.h>
mem_context_t mctx = NULL;
isc_memctx_t mctx = NULL;
isc_task_t t1, t2, t3;
isc_timer_t ti1, ti2, ti3;
int tick_count = 0;
......@@ -90,7 +90,7 @@ main(int argc, char *argv[]) {
workers = 2;
printf("%d workers\n", workers);
INSIST(mem_context_create(0, 0, &mctx) == 0);
INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS);
INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == workers);
INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1));
INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2));
......@@ -137,5 +137,6 @@ main(int argc, char *argv[]) {
isc_taskmgr_destroy(&manager);
printf("destroyed\n");
mem_stats(mctx, stdout);
isc_mem_stats(mctx, stdout);
isc_memctx_destroy(&mctx);
}
......@@ -46,7 +46,7 @@
struct isc_heap {
unsigned int magic;
mem_context_t mctx;
isc_memctx_t mctx;
unsigned int size;
unsigned int size_increment;
unsigned int last;
......@@ -56,7 +56,7 @@ struct isc_heap {
};
isc_result
isc_heap_create(mem_context_t mctx, isc_heapcompare_t compare,
isc_heap_create(isc_memctx_t mctx, isc_heapcompare_t compare,
isc_heapindex_t index, unsigned int size_increment,
isc_heap_t *heapp)
{
......@@ -65,7 +65,7 @@ isc_heap_create(mem_context_t mctx, isc_heapcompare_t compare,
REQUIRE(heapp != NULL && *heapp == NULL);
REQUIRE(compare != NULL);
heap = mem_get(mctx, sizeof *heap);
heap = isc_mem_get(mctx, sizeof *heap);
if (heap == NULL)
return (ISC_R_NOMEMORY);
heap->magic = HEAP_MAGIC;
......@@ -94,10 +94,10 @@ isc_heap_destroy(isc_heap_t *heapp) {
REQUIRE(VALID_HEAP(heap));
if (heap->array != NULL)
mem_put(heap->mctx, heap->array,
isc_mem_put(heap->mctx, heap->array,
heap->size * sizeof (void *));
heap->magic = 0;
mem_put(heap->mctx, heap, sizeof *heap);
isc_mem_put(heap->mctx, heap, sizeof *heap);
*heapp = NULL;
}
......@@ -110,12 +110,12 @@ resize(isc_heap_t heap) {
REQUIRE(VALID_HEAP(heap));
new_size = heap->size + heap->size_increment;
new_array = mem_get(heap->mctx, new_size * sizeof (void *));
new_array = isc_mem_get(heap->mctx, new_size * sizeof (void *));
if (new_array == NULL)
return (ISC_FALSE);
if (heap->array != NULL) {
memcpy(new_array, heap->array, heap->size);
mem_put(heap->mctx, heap->array,
isc_mem_put(heap->mctx, heap->array,
heap->size * sizeof (void *));
}
heap->size = new_size;
......
......@@ -39,7 +39,7 @@ typedef struct isc_heap *isc_heap_t;
#define isc_heap_element __isc_heap_element
#define isc_heap_foreach __isc_heap_foreach
isc_result isc_heap_create(mem_context_t, isc_heapcompare_t,
isc_result isc_heap_create(isc_memctx_t, isc_heapcompare_t,
isc_heapindex_t, unsigned int, isc_heap_t *);
void isc_heap_destroy(isc_heap_t *);
isc_result isc_heap_insert(isc_heap_t, void *);
......
......@@ -15,41 +15,48 @@
* SOFTWARE.
*/
#ifndef MEMCLUSTER_H
#define MEMCLUSTER_H 1
#ifndef ISC_MEMCLUSTER_H
#define ISC_MEMCLUSTER_H 1
#include <stdio.h>
#include <stddef.h>
typedef struct mem_context * mem_context_t;
#include <isc/boolean.h>
#include <isc/result.h>
#define mem_context_create __mem_context_create
#define mem_context_destroy __mem_context_destroy
#ifdef MEMCLUSTER_DEBUG
#define mem_get(c, s) __mem_get_debug(c, s, __FILE__, __LINE__)
#define mem_put(c, p, s) __mem_put_debug(c, p, s, __FILE__, __LINE__)
typedef struct isc_memctx * isc_memctx_t;
#define isc_memctx_create __isc_memctx_create
#define isc_memctx_destroy __isc_memctx_destroy
#ifdef ISC_MEMCLUSTER_DEBUG
#define isc_mem_get(c, s) __isc_mem_getdebug(c, s, __FILE__, \
__LINE__)
#define isc_mem_put(c, p, s) __isc_mem_putdebug(c, p, s, __FILE__, \
__LINE__)
#else
#define mem_get __mem_get
#define mem_put __mem_put
#endif
#define mem_valid __mem_valid
#define mem_stats __mem_stats
#define mem_allocate __mem_allocate
#define mem_free __mem_free
#define isc_mem_get __isc_mem_get
#define isc_mem_put __isc_mem_put
#endif /* ISC_MEMCLUSTER_DEBUG */
#define isc_mem_valid __isc_mem_valid
#define isc_mem_stats __isc_mem_stats
#define isc_mem_allocate __isc_mem_allocate
#define isc_mem_free __isc_mem_free
int mem_context_create(size_t, size_t,
mem_context_t *);
void mem_context_destroy(mem_context_t *);
void * __mem_get(mem_context_t, size_t);
void __mem_put(mem_context_t, void *, size_t);
void * __mem_get_debug(mem_context_t, size_t,
isc_result_t isc_memctx_create(size_t, size_t,
isc_memctx_t *);
void isc_memctx_destroy(isc_memctx_t *);
void * __isc_mem_get(isc_memctx_t, size_t);
void __isc_mem_put(isc_memctx_t, void *, size_t);
void * __isc_mem_getdebug(isc_memctx_t, size_t,
const char *, int);
void __mem_put_debug(mem_context_t, void *, size_t,
const char *, int);
int mem_valid(mem_context_t, void *);
void mem_stats(mem_context_t, FILE *);
void * mem_allocate(mem_context_t, size_t);
void mem_free(mem_context_t, void *);
void __isc_mem_putdebug(isc_memctx_t, void *,
size_t, const char *, int);
isc_boolean_t isc_mem_valid(isc_memctx_t, void *);
void isc_mem_stats(isc_memctx_t, FILE *);
void * isc_mem_allocate(isc_memctx_t, size_t);
void isc_mem_free(isc_memctx_t, void *);
#ifdef ISC_MEMCLUSTER_LEGACY
/*
* Legacy.
......@@ -68,7 +75,7 @@ void mem_free(mem_context_t, void *);
#define memstats __memstats
int meminit(size_t, size_t);
mem_context_t mem_default_context(void);
isc_memctx_t mem_default_context(void);
void * __memget(size_t);
void __memput(void *, size_t);
void * __memget_debug(size_t, const char *, int);
......@@ -77,4 +84,6 @@ void __memput_debug(void *, size_t, const char *,
int memvalid(void *);
void memstats(FILE *);
#endif /* ISC_MEMCLUSTER_LEGACY */
#endif /* MEMCLUSTER_H */
......@@ -37,7 +37,7 @@ typedef isc_boolean_t (*isc_taskaction_t)(isc_task_t, isc_event_t);
* defining new event types.
*/
struct isc_event {
mem_context_t mctx;
isc_memctx_t mctx;
size_t size;
void * sender;
isc_eventtype_t type;
......@@ -51,7 +51,7 @@ struct isc_event {
typedef LIST(struct isc_event) isc_eventlist_t;
isc_event_t isc_event_allocate(mem_context_t,
isc_event_t isc_event_allocate(isc_memctx_t,
void *,
isc_eventtype_t,
isc_taskaction_t,
......@@ -83,7 +83,7 @@ void isc_task_destroy(isc_task_t *);
*** Task Manager.
***/
unsigned int isc_taskmgr_create(mem_context_t,
unsigned int isc_taskmgr_create(isc_memctx_t,
unsigned int,
unsigned int,
isc_taskmgr_t *);
......
......@@ -235,7 +235,7 @@ isc_timer_detach(isc_timer_t *timerp);
*/
isc_result_t
isc_timermgr_create(mem_context_t mctx, isc_timermgr_t *managerp);
isc_timermgr_create(isc_memctx_t mctx, isc_timermgr_t *managerp);
/*
* Create a timer manager.
*
......
......@@ -23,6 +23,7 @@
#include "attribute.h"
#include <isc/assertions.h>
#include <isc/unexpect.h>
#include <isc/memcluster.h>
......@@ -31,7 +32,7 @@
#endif
#if !defined(LINT) && !defined(CODECENTER)
static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.5 1998/09/16 21:38:08 halley Exp $";
static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.6 1998/10/21 22:00:56 halley Exp $";
#endif /* not lint */
/*
......@@ -40,7 +41,7 @@ static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.5 1998/09/16 21:38
typedef struct {
void * next;
} memcluster_element;
} element;
typedef struct {
size_t size;
......@@ -56,11 +57,11 @@ struct stats {
unsigned long freefrags;
};
struct mem_context {
struct isc_memctx {
size_t max_size;
size_t mem_target;
memcluster_element ** freelists;
memcluster_element * basic_blocks;
element ** freelists;
element * basic_blocks;
unsigned char ** basic_table;
unsigned int basic_table_count;
unsigned int basic_table_size;
......@@ -70,9 +71,6 @@ struct mem_context {
os_mutex_t mutex;
};
/* Private Data. */
static mem_context_t default_context = NULL;
/* Forward. */
static size_t quantize(size_t);
......@@ -117,10 +115,13 @@ quantize(size_t size) {
/* Public. */
int
mem_context_create(size_t init_max_size, size_t target_size,
mem_context_t *ctxp) {
mem_context_t ctx;
isc_result_t
isc_memctx_create(size_t init_max_size, size_t target_size,
isc_memctx_t *ctxp)
{
isc_memctx_t ctx;
REQUIRE(ctxp != NULL && *ctxp == NULL);
ctx = malloc(sizeof *ctx);
if (init_max_size == 0)
......@@ -131,18 +132,18 @@ mem_context_create(size_t init_max_size, size_t target_size,
ctx->mem_target = DEF_MEM_TARGET;
else
ctx->mem_target = target_size;
ctx->freelists = malloc(ctx->max_size * sizeof (memcluster_element *));
ctx->freelists = malloc(ctx->max_size * sizeof (element *));
if (ctx->freelists == NULL) {
free(ctx);
return (-1);
return (ISC_R_NOMEMORY);
}
memset(ctx->freelists, 0,
ctx->max_size * sizeof (memcluster_element *));
ctx->max_size * sizeof (element *));
ctx->stats = malloc((ctx->max_size+1) * sizeof (struct stats));
if (ctx->stats == NULL) {
free(ctx->freelists);
free(ctx);
return (-1);
return (ISC_R_NOMEMORY);
}
memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof (struct stats));
ctx->basic_blocks = NULL;
......@@ -155,18 +156,21 @@ mem_context_create(size_t init_max_size, size_t target_size,
free(ctx->stats);
free(ctx->freelists);
free(ctx);
return (-1);
UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
*ctxp = ctx;
return (0);
return (ISC_R_SUCCESS);
}
void
mem_context_destroy(mem_context_t *ctxp) {
isc_memctx_destroy(isc_memctx_t *ctxp) {
unsigned int i;
mem_context_t ctx;
isc_memctx_t ctx;
REQUIRE(ctxp != NULL);
ctx = *ctxp;
for (i = 0; i <= ctx->max_size; i++)
......@@ -174,7 +178,6 @@ mem_context_destroy(mem_context_t *ctxp) {
for (i = 0; i < ctx->basic_table_count; i++)
free(ctx->basic_table[i]);
free(ctx->freelists);
free(ctx->stats);
free(ctx->basic_table);
......@@ -185,7 +188,7 @@ mem_context_destroy(mem_context_t *ctxp) {
}
static void
more_basic_blocks(mem_context_t ctx) {
more_basic_blocks(isc_memctx_t ctx) {
void *new;
unsigned char *curr, *next;
unsigned char *first, *last;
......@@ -219,7 +222,7 @@ more_basic_blocks(mem_context_t ctx) {
curr = new;
next = curr + ctx->mem_target;
for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
((memcluster_element *)curr)->next = next;
((element *)curr)->next = next;
curr = next;
next += ctx->mem_target;
}
......@@ -227,7 +230,7 @@ more_basic_blocks(mem_context_t ctx) {
* curr is now pointing at the last block in the
* array.
*/
((memcluster_element *)curr)->next = NULL;
((element *)curr)->next = NULL;
first = new;
last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
if (first < ctx->lowest || ctx->lowest == NULL)
......@@ -238,7 +241,7 @@ more_basic_blocks(mem_context_t ctx) {
}
void *
__mem_get(mem_context_t ctx, size_t size) {
__isc_mem_get(isc_memctx_t ctx, size_t size) {
size_t new_size = quantize(size);
void *ret;
......@@ -284,12 +287,12 @@ __mem_get(mem_context_t ctx, size_t size) {
curr = new;
next = curr + new_size;
for (i = 0; i < (frags - 1); i++) {
((memcluster_element *)curr)->next = next;
((element *)curr)->next = next;
curr = next;
next += new_size;
}
/* curr is now pointing at the last block in the array. */
((memcluster_element *)curr)->next = NULL;
((element *)curr)->next = NULL;
ctx->freelists[new_size] = new;
}
......@@ -318,7 +321,7 @@ __mem_get(mem_context_t ctx, size_t size) {
* so we want to count this as a user "put".
*/
void
__mem_put(mem_context_t ctx, void *mem, size_t size) {
__isc_mem_put(isc_memctx_t ctx, void *mem, size_t size) {
size_t new_size = quantize(size);
REQUIRE(size > 0);
......@@ -334,8 +337,8 @@ __mem_put(mem_context_t ctx, void *mem, size_t size) {
}
/* The free list uses the "rounded-up" size "new_size": */
((memcluster_element *)mem)->next = ctx->freelists[new_size];
ctx->freelists[new_size] = (memcluster_element *)mem;
((element *)mem)->next = ctx->freelists[new_size];
ctx->freelists[new_size] = (element *)mem;
/*
* The stats[] uses the _actual_ "size" requested by the
......@@ -352,28 +355,29 @@ __mem_put(mem_context_t ctx, void *mem, size_t size) {
}
void *
__mem_get_debug(mem_context_t ctx, size_t size, const char *file, int line) {
__isc_mem_getdebug(isc_memctx_t ctx, size_t size, const char *file, int line) {
void *ptr;
ptr = __mem_get(ctx, size);
ptr = __isc_mem_get(ctx, size);
fprintf(stderr, "%s:%d: mem_get(%p, %lu) -> %p\n", file, line,
ctx, (unsigned long)size, ptr);
return (ptr);
}
void
__mem_put_debug(mem_context_t ctx, void *ptr, size_t size, const char *file,
__isc_mem_putdebug(isc_memctx_t ctx, void *ptr, size_t size, const char *file,
int line)
{
fprintf(stderr, "%s:%d: mem_put(%p, %p, %lu)\n", file, line,
ctx, ptr, (unsigned long)size);
__mem_put(ctx, ptr, size);
__isc_mem_put(ctx, ptr, size);
}
/*
* Print the stats[] on the stream "out" with suitable formatting.
*/
void
mem_stats(mem_context_t ctx, FILE *out) {
isc_mem_stats(isc_memctx_t ctx, FILE *out) {
size_t i;
LOCK_CONTEXT(ctx);
......@@ -397,20 +401,19 @@ mem_stats(mem_context_t ctx, FILE *out) {
UNLOCK_CONTEXT(ctx);
}
int
mem_valid(mem_context_t ctx, void *ptr) {
isc_boolean_t
isc_mem_valid(isc_memctx_t ctx, void *ptr) {
unsigned char *cp = ptr;
int ret;
isc_boolean_t result = ISC_FALSE;
LOCK_CONTEXT(ctx);
ret = 0;
if (ctx->lowest != NULL && cp >= ctx->lowest && cp <= ctx->highest)
ret = 1;
result = ISC_TRUE;
UNLOCK_CONTEXT(ctx);
return (ret);
return (result);
}
/*
......@@ -418,11 +421,11 @@ mem_valid(mem_context_t ctx, void *ptr) {
*/
void *
mem_allocate(mem_context_t ctx, size_t size) {
isc_mem_allocate(isc_memctx_t ctx, size_t size) {
size_info si;
size += ALIGNMENT_SIZE;
si = mem_get(ctx, size);
si = isc_mem_get(ctx, size);
if (si == NULL)
return (NULL);
si->size = size;
......@@ -430,27 +433,31 @@ mem_allocate(mem_context_t ctx, size_t size) {
}
void
mem_free(mem_context_t ctx, void *ptr) {
isc_mem_free(isc_memctx_t ctx, void *ptr) {
size_info si;
si = &(((size_info)ptr)[-1]);
mem_put(ctx, si, si->size);
isc_mem_put(ctx, si, si->size);
}
#ifdef ISC_MEMCLUSTER_LEGACY
/*
* Public Legacy.
*/
static isc_memctx_t default_context = NULL;
int
meminit(size_t init_max_size, size_t target_size) {
/* need default_context lock here */
if (default_context != NULL)
return (-1);
return (mem_context_create(init_max_size, target_size,
return (isc_mem_create(init_max_size, target_size,
&default_context));
}
mem_context_t
isc_memctx_t
mem_default_context(void) {
/* need default_context lock here */
if (default_context == NULL && meminit(0, 0) == -1)
......@@ -502,3 +509,5 @@ memstats(FILE *out) {
REQUIRE(default_context != NULL);
mem_stats(default_context, out);
}
#endif /* ISC_MEMCLUSTER_LEGACY */
......@@ -71,7 +71,7 @@ struct isc_task {
struct isc_taskmgr {
/* Not locked. */
unsigned int magic;
mem_context_t mctx;
isc_memctx_t mctx;
os_mutex_t lock;
/* Locked by task manager lock. */
unsigned int default_quantum;
......@@ -92,12 +92,12 @@ struct isc_taskmgr {
***/
static inline isc_event_t
event_allocate(mem_context_t mctx, void *sender, isc_eventtype_t type,
event_allocate(isc_memctx_t mctx, void *sender, isc_eventtype_t type,
isc_taskaction_t action, void *arg, size_t size)
{
isc_event_t event;
event = mem_get(mctx, size);
event = isc_mem_get(mctx, size);
if (event == NULL)
return (NULL);
event->mctx = mctx;
......@@ -111,7 +111,7 @@ event_allocate(mem_context_t mctx, void *sender, isc_eventtype_t type,
}
isc_event_t
isc_event_allocate(mem_context_t mctx, void *sender, isc_eventtype_t type,
isc_event_allocate(isc_memctx_t mctx, void *sender, isc_eventtype_t type,
isc_taskaction_t action, void *arg, size_t size)
{
if (size < sizeof (struct isc_event))
......@@ -132,7 +132,7 @@ isc_event_free(isc_event_t *eventp) {
event = *eventp;
REQUIRE(event != NULL);
mem_put(event->mctx, event, event->size);
isc_mem_put(event->mctx, event, event->size);
*eventp = NULL;
}
......@@ -164,7 +164,7 @@ task_free(isc_task_t task) {
if (task->shutdown_event != NULL)
isc_event_free(&task->shutdown_event);
task->magic = 0;
mem_put(manager->mctx, task, sizeof *task);
isc_mem_put(manager->mctx, task, sizeof *task);
}
isc_boolean_t
......@@ -176,14 +176,14 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action,
REQUIRE(VALID_MANAGER(manager));
REQUIRE(taskp != NULL && *taskp == NULL);
task = mem_get(manager->mctx, sizeof *task);
task = isc_mem_get(manager->mctx, sizeof *task);
if (task == NULL)
return (ISC_FALSE);
task->magic = TASK_MAGIC;
task->manager = manager;
if (!os_mutex_init(&task->lock)) {
mem_put(manager->mctx, task, sizeof *task);
isc_mem_put(manager->mctx, task, sizeof *task);
return (ISC_FALSE);
}
task->state = task_state_idle;
......@@ -199,7 +199,7 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action,
sizeof *task->shutdown_event);
if (task->shutdown_event == NULL) {
(void)os_mutex_destroy(&task->lock);
mem_put(manager->mctx, task, sizeof *task);
isc_mem_put(manager->mctx, task, sizeof *task);
return (ISC_FALSE);
}
INIT_LINK(task, link);
......@@ -689,11 +689,11 @@ manager_free(isc_taskmgr_t manager) {
(void)os_condition_destroy(&manager->no_workers);
(void)os_mutex_destroy(&manager->lock);
manager->magic = 0;
mem_put(manager->mctx, manager, sizeof *manager);
isc_mem_put(manager->mctx, manager, sizeof *manager);
}
unsigned int
isc_taskmgr_create(mem_context_t mctx, unsigned int workers,
isc_taskmgr_create(isc_memctx_t mctx, unsigned int workers,
unsigned int default_quantum, isc_taskmgr_t *managerp)