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

rename memcluster

parent c92ba071
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include <isc/result.h> #include <isc/result.h>
#include <isc/timer.h> #include <isc/timer.h>
mem_context_t mctx = NULL; isc_memctx_t mctx = NULL;
static isc_boolean_t static isc_boolean_t
my_callback(isc_task_t task, isc_event_t event) my_callback(isc_task_t task, isc_event_t event)
...@@ -61,7 +61,7 @@ main(int argc, char *argv[]) { ...@@ -61,7 +61,7 @@ main(int argc, char *argv[]) {
workers = 2; workers = 2;
printf("%d workers\n", workers); 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_taskmgr_create(mctx, workers, 0, &manager) == workers);
...@@ -149,5 +149,6 @@ main(int argc, char *argv[]) { ...@@ -149,5 +149,6 @@ main(int argc, char *argv[]) {
isc_taskmgr_destroy(&manager); isc_taskmgr_destroy(&manager);
printf("destroyed\n"); printf("destroyed\n");
mem_stats(mctx, stdout); isc_mem_stats(mctx, stdout);
isc_memctx_destroy(&mctx);
} }
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include <isc/result.h> #include <isc/result.h>
#include <isc/timer.h> #include <isc/timer.h>
mem_context_t mctx = NULL; isc_memctx_t mctx = NULL;
isc_task_t t1, t2, t3; isc_task_t t1, t2, t3;
isc_timer_t ti1, ti2, ti3; isc_timer_t ti1, ti2, ti3;
int tick_count = 0; int tick_count = 0;
...@@ -90,7 +90,7 @@ main(int argc, char *argv[]) { ...@@ -90,7 +90,7 @@ main(int argc, char *argv[]) {
workers = 2; workers = 2;
printf("%d workers\n", workers); 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_taskmgr_create(mctx, workers, 0, &manager) == workers);
INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1)); INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1));
INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2)); INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2));
...@@ -137,5 +137,6 @@ main(int argc, char *argv[]) { ...@@ -137,5 +137,6 @@ main(int argc, char *argv[]) {
isc_taskmgr_destroy(&manager); isc_taskmgr_destroy(&manager);
printf("destroyed\n"); printf("destroyed\n");
mem_stats(mctx, stdout); isc_mem_stats(mctx, stdout);
isc_memctx_destroy(&mctx);
} }
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
struct isc_heap { struct isc_heap {
unsigned int magic; unsigned int magic;
mem_context_t mctx; isc_memctx_t mctx;
unsigned int size; unsigned int size;
unsigned int size_increment; unsigned int size_increment;
unsigned int last; unsigned int last;
...@@ -56,16 +56,16 @@ struct isc_heap { ...@@ -56,16 +56,16 @@ struct isc_heap {
}; };
isc_result 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_heapindex_t index, unsigned int size_increment,
isc_heap_t *heapp) isc_heap_t *heapp)
{ {
isc_heap_t heap; isc_heap_t heap;
REQUIRE(heapp != NULL && *heapp == NULL); REQUIRE(heapp != NULL && *heapp == NULL);
REQUIRE(compare != NULL); REQUIRE(compare != NULL);
heap = mem_get(mctx, sizeof *heap); heap = isc_mem_get(mctx, sizeof *heap);
if (heap == NULL) if (heap == NULL)
return (ISC_R_NOMEMORY); return (ISC_R_NOMEMORY);
heap->magic = HEAP_MAGIC; heap->magic = HEAP_MAGIC;
...@@ -94,10 +94,10 @@ isc_heap_destroy(isc_heap_t *heapp) { ...@@ -94,10 +94,10 @@ isc_heap_destroy(isc_heap_t *heapp) {
REQUIRE(VALID_HEAP(heap)); REQUIRE(VALID_HEAP(heap));
if (heap->array != NULL) if (heap->array != NULL)
mem_put(heap->mctx, heap->array, isc_mem_put(heap->mctx, heap->array,
heap->size * sizeof (void *)); heap->size * sizeof (void *));
heap->magic = 0; heap->magic = 0;
mem_put(heap->mctx, heap, sizeof *heap); isc_mem_put(heap->mctx, heap, sizeof *heap);
*heapp = NULL; *heapp = NULL;
} }
...@@ -110,13 +110,13 @@ resize(isc_heap_t heap) { ...@@ -110,13 +110,13 @@ resize(isc_heap_t heap) {
REQUIRE(VALID_HEAP(heap)); REQUIRE(VALID_HEAP(heap));
new_size = heap->size + heap->size_increment; 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) if (new_array == NULL)
return (ISC_FALSE); return (ISC_FALSE);
if (heap->array != NULL) { if (heap->array != NULL) {
memcpy(new_array, heap->array, heap->size); 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 * sizeof (void *));
} }
heap->size = new_size; heap->size = new_size;
heap->array = new_array; heap->array = new_array;
......
...@@ -39,7 +39,7 @@ typedef struct isc_heap *isc_heap_t; ...@@ -39,7 +39,7 @@ typedef struct isc_heap *isc_heap_t;
#define isc_heap_element __isc_heap_element #define isc_heap_element __isc_heap_element
#define isc_heap_foreach __isc_heap_foreach #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 *); isc_heapindex_t, unsigned int, isc_heap_t *);
void isc_heap_destroy(isc_heap_t *); void isc_heap_destroy(isc_heap_t *);
isc_result isc_heap_insert(isc_heap_t, void *); isc_result isc_heap_insert(isc_heap_t, void *);
......
...@@ -15,41 +15,48 @@ ...@@ -15,41 +15,48 @@
* SOFTWARE. * SOFTWARE.
*/ */
#ifndef MEMCLUSTER_H #ifndef ISC_MEMCLUSTER_H
#define MEMCLUSTER_H 1 #define ISC_MEMCLUSTER_H 1
#include <stdio.h> #include <stdio.h>
#include <stddef.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 typedef struct isc_memctx * isc_memctx_t;
#define mem_context_destroy __mem_context_destroy
#ifdef MEMCLUSTER_DEBUG #define isc_memctx_create __isc_memctx_create
#define mem_get(c, s) __mem_get_debug(c, s, __FILE__, __LINE__) #define isc_memctx_destroy __isc_memctx_destroy
#define mem_put(c, p, s) __mem_put_debug(c, p, s, __FILE__, __LINE__) #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 #else
#define mem_get __mem_get #define isc_mem_get __isc_mem_get
#define mem_put __mem_put #define isc_mem_put __isc_mem_put
#endif #endif /* ISC_MEMCLUSTER_DEBUG */
#define mem_valid __mem_valid #define isc_mem_valid __isc_mem_valid
#define mem_stats __mem_stats #define isc_mem_stats __isc_mem_stats
#define mem_allocate __mem_allocate #define isc_mem_allocate __isc_mem_allocate
#define mem_free __mem_free #define isc_mem_free __isc_mem_free
int mem_context_create(size_t, size_t, isc_result_t isc_memctx_create(size_t, size_t,
mem_context_t *); isc_memctx_t *);
void mem_context_destroy(mem_context_t *); void isc_memctx_destroy(isc_memctx_t *);
void * __mem_get(mem_context_t, size_t); void * __isc_mem_get(isc_memctx_t, size_t);
void __mem_put(mem_context_t, void *, size_t); void __isc_mem_put(isc_memctx_t, void *, size_t);
void * __mem_get_debug(mem_context_t, size_t, void * __isc_mem_getdebug(isc_memctx_t, size_t,
const char *, int); const char *, int);
void __mem_put_debug(mem_context_t, void *, size_t, void __isc_mem_putdebug(isc_memctx_t, void *,
const char *, int); size_t, const char *, int);
int mem_valid(mem_context_t, void *); isc_boolean_t isc_mem_valid(isc_memctx_t, void *);
void mem_stats(mem_context_t, FILE *); void isc_mem_stats(isc_memctx_t, FILE *);
void * mem_allocate(mem_context_t, size_t); void * isc_mem_allocate(isc_memctx_t, size_t);
void mem_free(mem_context_t, void *); void isc_mem_free(isc_memctx_t, void *);
#ifdef ISC_MEMCLUSTER_LEGACY
/* /*
* Legacy. * Legacy.
...@@ -68,7 +75,7 @@ void mem_free(mem_context_t, void *); ...@@ -68,7 +75,7 @@ void mem_free(mem_context_t, void *);
#define memstats __memstats #define memstats __memstats
int meminit(size_t, size_t); 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 * __memget(size_t);
void __memput(void *, size_t); void __memput(void *, size_t);
void * __memget_debug(size_t, const char *, int); void * __memget_debug(size_t, const char *, int);
...@@ -77,4 +84,6 @@ void __memput_debug(void *, size_t, const char *, ...@@ -77,4 +84,6 @@ void __memput_debug(void *, size_t, const char *,
int memvalid(void *); int memvalid(void *);
void memstats(FILE *); void memstats(FILE *);
#endif /* ISC_MEMCLUSTER_LEGACY */
#endif /* MEMCLUSTER_H */ #endif /* MEMCLUSTER_H */
...@@ -37,7 +37,7 @@ typedef isc_boolean_t (*isc_taskaction_t)(isc_task_t, isc_event_t); ...@@ -37,7 +37,7 @@ typedef isc_boolean_t (*isc_taskaction_t)(isc_task_t, isc_event_t);
* defining new event types. * defining new event types.
*/ */
struct isc_event { struct isc_event {
mem_context_t mctx; isc_memctx_t mctx;
size_t size; size_t size;
void * sender; void * sender;
isc_eventtype_t type; isc_eventtype_t type;
...@@ -51,7 +51,7 @@ struct isc_event { ...@@ -51,7 +51,7 @@ struct isc_event {
typedef LIST(struct isc_event) isc_eventlist_t; 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 *, void *,
isc_eventtype_t, isc_eventtype_t,
isc_taskaction_t, isc_taskaction_t,
...@@ -83,7 +83,7 @@ void isc_task_destroy(isc_task_t *); ...@@ -83,7 +83,7 @@ void isc_task_destroy(isc_task_t *);
*** Task Manager. *** Task Manager.
***/ ***/
unsigned int isc_taskmgr_create(mem_context_t, unsigned int isc_taskmgr_create(isc_memctx_t,
unsigned int, unsigned int,
unsigned int, unsigned int,
isc_taskmgr_t *); isc_taskmgr_t *);
......
...@@ -235,7 +235,7 @@ isc_timer_detach(isc_timer_t *timerp); ...@@ -235,7 +235,7 @@ isc_timer_detach(isc_timer_t *timerp);
*/ */
isc_result_t 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. * Create a timer manager.
* *
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "attribute.h" #include "attribute.h"
#include <isc/assertions.h> #include <isc/assertions.h>
#include <isc/unexpect.h>
#include <isc/memcluster.h> #include <isc/memcluster.h>
...@@ -31,7 +32,7 @@ ...@@ -31,7 +32,7 @@
#endif #endif
#if !defined(LINT) && !defined(CODECENTER) #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 */ #endif /* not lint */
/* /*
...@@ -40,7 +41,7 @@ static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.5 1998/09/16 21:38 ...@@ -40,7 +41,7 @@ static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.5 1998/09/16 21:38
typedef struct { typedef struct {
void * next; void * next;
} memcluster_element; } element;
typedef struct { typedef struct {
size_t size; size_t size;
...@@ -56,11 +57,11 @@ struct stats { ...@@ -56,11 +57,11 @@ struct stats {
unsigned long freefrags; unsigned long freefrags;
}; };
struct mem_context { struct isc_memctx {
size_t max_size; size_t max_size;
size_t mem_target; size_t mem_target;
memcluster_element ** freelists; element ** freelists;
memcluster_element * basic_blocks; element * basic_blocks;
unsigned char ** basic_table; unsigned char ** basic_table;
unsigned int basic_table_count; unsigned int basic_table_count;
unsigned int basic_table_size; unsigned int basic_table_size;
...@@ -70,9 +71,6 @@ struct mem_context { ...@@ -70,9 +71,6 @@ struct mem_context {
os_mutex_t mutex; os_mutex_t mutex;
}; };
/* Private Data. */
static mem_context_t default_context = NULL;
/* Forward. */ /* Forward. */
static size_t quantize(size_t); static size_t quantize(size_t);
...@@ -117,10 +115,13 @@ quantize(size_t size) { ...@@ -117,10 +115,13 @@ quantize(size_t size) {
/* Public. */ /* Public. */
int isc_result_t
mem_context_create(size_t init_max_size, size_t target_size, isc_memctx_create(size_t init_max_size, size_t target_size,
mem_context_t *ctxp) { isc_memctx_t *ctxp)
mem_context_t ctx; {
isc_memctx_t ctx;
REQUIRE(ctxp != NULL && *ctxp == NULL);
ctx = malloc(sizeof *ctx); ctx = malloc(sizeof *ctx);
if (init_max_size == 0) if (init_max_size == 0)
...@@ -131,18 +132,18 @@ mem_context_create(size_t init_max_size, size_t target_size, ...@@ -131,18 +132,18 @@ mem_context_create(size_t init_max_size, size_t target_size,
ctx->mem_target = DEF_MEM_TARGET; ctx->mem_target = DEF_MEM_TARGET;
else else
ctx->mem_target = target_size; 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) { if (ctx->freelists == NULL) {
free(ctx); free(ctx);
return (-1); return (ISC_R_NOMEMORY);
} }
memset(ctx->freelists, 0, 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)); ctx->stats = malloc((ctx->max_size+1) * sizeof (struct stats));
if (ctx->stats == NULL) { if (ctx->stats == NULL) {
free(ctx->freelists); free(ctx->freelists);
free(ctx); free(ctx);
return (-1); return (ISC_R_NOMEMORY);
} }
memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof (struct stats)); memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof (struct stats));
ctx->basic_blocks = NULL; ctx->basic_blocks = NULL;
...@@ -155,18 +156,21 @@ mem_context_create(size_t init_max_size, size_t target_size, ...@@ -155,18 +156,21 @@ mem_context_create(size_t init_max_size, size_t target_size,
free(ctx->stats); free(ctx->stats);
free(ctx->freelists); free(ctx->freelists);
free(ctx); free(ctx);
return (-1); UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_mutex_init() failed");
return (ISC_R_UNEXPECTED);
} }
*ctxp = ctx; *ctxp = ctx;
return (0); return (ISC_R_SUCCESS);
} }
void void
mem_context_destroy(mem_context_t *ctxp) { isc_memctx_destroy(isc_memctx_t *ctxp) {
unsigned int i; unsigned int i;
mem_context_t ctx; isc_memctx_t ctx;
REQUIRE(ctxp != NULL); REQUIRE(ctxp != NULL);
ctx = *ctxp; ctx = *ctxp;
for (i = 0; i <= ctx->max_size; i++) for (i = 0; i <= ctx->max_size; i++)
...@@ -174,7 +178,6 @@ mem_context_destroy(mem_context_t *ctxp) { ...@@ -174,7 +178,6 @@ mem_context_destroy(mem_context_t *ctxp) {
for (i = 0; i < ctx->basic_table_count; i++) for (i = 0; i < ctx->basic_table_count; i++)
free(ctx->basic_table[i]); free(ctx->basic_table[i]);
free(ctx->freelists); free(ctx->freelists);
free(ctx->stats); free(ctx->stats);
free(ctx->basic_table); free(ctx->basic_table);
...@@ -185,7 +188,7 @@ mem_context_destroy(mem_context_t *ctxp) { ...@@ -185,7 +188,7 @@ mem_context_destroy(mem_context_t *ctxp) {
} }
static void static void
more_basic_blocks(mem_context_t ctx) { more_basic_blocks(isc_memctx_t ctx) {
void *new; void *new;
unsigned char *curr, *next; unsigned char *curr, *next;
unsigned char *first, *last; unsigned char *first, *last;
...@@ -219,7 +222,7 @@ more_basic_blocks(mem_context_t ctx) { ...@@ -219,7 +222,7 @@ more_basic_blocks(mem_context_t ctx) {
curr = new; curr = new;
next = curr + ctx->mem_target; next = curr + ctx->mem_target;
for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) { for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
((memcluster_element *)curr)->next = next; ((element *)curr)->next = next;
curr = next; curr = next;
next += ctx->mem_target; next += ctx->mem_target;
} }
...@@ -227,7 +230,7 @@ more_basic_blocks(mem_context_t ctx) { ...@@ -227,7 +230,7 @@ more_basic_blocks(mem_context_t ctx) {
* curr is now pointing at the last block in the * curr is now pointing at the last block in the
* array. * array.
*/ */
((memcluster_element *)curr)->next = NULL; ((element *)curr)->next = NULL;
first = new; first = new;
last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1; last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
if (first < ctx->lowest || ctx->lowest == NULL) if (first < ctx->lowest || ctx->lowest == NULL)
...@@ -238,7 +241,7 @@ more_basic_blocks(mem_context_t ctx) { ...@@ -238,7 +241,7 @@ more_basic_blocks(mem_context_t ctx) {
} }
void * 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); size_t new_size = quantize(size);
void *ret; void *ret;
...@@ -284,12 +287,12 @@ __mem_get(mem_context_t ctx, size_t size) { ...@@ -284,12 +287,12 @@ __mem_get(mem_context_t ctx, size_t size) {
curr = new; curr = new;
next = curr + new_size; next = curr + new_size;
for (i = 0; i < (frags - 1); i++) { for (i = 0; i < (frags - 1); i++) {
((memcluster_element *)curr)->next = next; ((element *)curr)->next = next;
curr = next; curr = next;
next += new_size; next += new_size;
} }
/* curr is now pointing at the last block in the array. */ /* 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; ctx->freelists[new_size] = new;
} }
...@@ -318,7 +321,7 @@ __mem_get(mem_context_t ctx, size_t size) { ...@@ -318,7 +321,7 @@ __mem_get(mem_context_t ctx, size_t size) {
* so we want to count this as a user "put". * so we want to count this as a user "put".
*/ */
void 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); size_t new_size = quantize(size);
REQUIRE(size > 0); REQUIRE(size > 0);
...@@ -334,8 +337,8 @@ __mem_put(mem_context_t ctx, void *mem, size_t size) { ...@@ -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": */ /* The free list uses the "rounded-up" size "new_size": */
((memcluster_element *)mem)->next = ctx->freelists[new_size]; ((element *)mem)->next = ctx->freelists[new_size];
ctx->freelists[new_size] = (memcluster_element *)mem; ctx->freelists[new_size] = (element *)mem;
/* /*
* The stats[] uses the _actual_ "size" requested by the * The stats[] uses the _actual_ "size" requested by the
...@@ -352,28 +355,29 @@ __mem_put(mem_context_t ctx, void *mem, size_t size) { ...@@ -352,28 +355,29 @@ __mem_put(mem_context_t ctx, void *mem, size_t size) {
} }
void * 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; void *ptr;