summaryrefslogtreecommitdiff
path: root/usr.sbin/nsd/region-allocator.h
diff options
context:
space:
mode:
authorJakob Schlyter <jakob@cvs.openbsd.org>2010-01-15 19:25:09 +0000
committerJakob Schlyter <jakob@cvs.openbsd.org>2010-01-15 19:25:09 +0000
commitd2081ac134d2d350ed92374b12613330132619c9 (patch)
tree511f9a412b24160516c7d3d29111801f9fa1bf35 /usr.sbin/nsd/region-allocator.h
parentefe15de128add506fb4055690c47221eb73d6346 (diff)
NSD v3.2.4
Diffstat (limited to 'usr.sbin/nsd/region-allocator.h')
-rw-r--r--usr.sbin/nsd/region-allocator.h114
1 files changed, 114 insertions, 0 deletions
diff --git a/usr.sbin/nsd/region-allocator.h b/usr.sbin/nsd/region-allocator.h
new file mode 100644
index 00000000000..41891651f45
--- /dev/null
+++ b/usr.sbin/nsd/region-allocator.h
@@ -0,0 +1,114 @@
+/*
+ * region-allocator.h -- region based memory allocator.
+ *
+ * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
+ *
+ * See LICENSE for the license.
+ *
+ */
+
+#ifndef _REGION_ALLOCATOR_H_
+#define _REGION_ALLOCATOR_H_
+
+#include <stdio.h>
+
+typedef struct region region_type;
+
+#define DEFAULT_CHUNK_SIZE 4096
+#define DEFAULT_LARGE_OBJECT_SIZE (DEFAULT_CHUNK_SIZE / 8)
+#define DEFAULT_INITIAL_CLEANUP_SIZE 16
+
+/*
+ * Create a new region.
+ */
+region_type *region_create(void *(*allocator)(size_t),
+ void (*deallocator)(void *));
+
+
+/*
+ * Create a new region, with chunk size and large object size.
+ * Note that large_object_size must be <= chunk_size.
+ * Anything larger than the large object size is individually alloced.
+ * large_object_size = chunk_size/8 is reasonable;
+ * initial_cleanup_size is the number of prealloced ptrs for cleanups.
+ * The cleanups are in a growing array, and it must start larger than zero.
+ * If recycle is true, environmentally friendly memory recycling is be enabled.
+ */
+region_type *region_create_custom(void *(*allocator)(size_t),
+ void (*deallocator)(void *),
+ size_t chunk_size,
+ size_t large_object_size,
+ size_t initial_cleanup_size,
+ int recycle);
+
+
+/*
+ * Destroy REGION. All memory associated with REGION is freed as if
+ * region_free_all was called.
+ */
+void region_destroy(region_type *region);
+
+
+/*
+ * Add a cleanup to REGION. ACTION will be called with DATA as
+ * parameter when the region is freed or destroyed.
+ *
+ * Returns 0 on failure.
+ */
+size_t region_add_cleanup(region_type *region,
+ void (*action)(void *),
+ void *data);
+
+
+/*
+ * Allocate SIZE bytes of memory inside REGION. The memory is
+ * deallocated when region_free_all is called for this region.
+ */
+void *region_alloc(region_type *region, size_t size);
+
+
+/*
+ * Allocate SIZE bytes of memory inside REGION and copy INIT into it.
+ * The memory is deallocated when region_free_all is called for this
+ * region.
+ */
+void *region_alloc_init(region_type *region, const void *init, size_t size);
+
+
+/*
+ * Allocate SIZE bytes of memory inside REGION that are initialized to
+ * 0. The memory is deallocated when region_free_all is called for
+ * this region.
+ */
+void *region_alloc_zero(region_type *region, size_t size);
+
+
+/*
+ * Run the cleanup actions and free all memory associated with REGION.
+ */
+void region_free_all(region_type *region);
+
+
+/*
+ * Duplicate STRING and allocate the result in REGION.
+ */
+char *region_strdup(region_type *region, const char *string);
+
+/*
+ * Recycle an allocated memory block. Pass size used to alloc it.
+ * Does nothing if recycling is not enabled for the region.
+ */
+void region_recycle(region_type *region, void *block, size_t size);
+
+/*
+ * Print some REGION statistics to OUT.
+ */
+void region_dump_stats(region_type *region, FILE *out);
+
+/* get size of recyclebin */
+size_t region_get_recycle_size(region_type* region);
+
+/* Debug print REGION statistics to LOG. */
+void region_log_stats(region_type *region);
+
+#endif /* _REGION_ALLOCATOR_H_ */