From 4289caa8e9c4802c307bfb69245d8cad956066db Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 27 Nov 2018 00:54:19 -0800 Subject: libix2: pivot from libc to libpad Note that libpad doesn't currently have the fixed size variant, so the pad_get() calls must include the size temporarily. Regardless, I've still utilized three distinct pads for the three object types in preparation for the fixed size variant. --- src/Makefile.am | 1 + src/ix2.c | 48 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index c2387f2..a967254 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,2 +1,3 @@ noinst_LIBRARIES = libix2.a libix2_a_SOURCES = list.h ix2.c ix2.h +libix2_a_CPPFLAGS = -I@top_srcdir@/libpad/src diff --git a/src/ix2.c b/src/ix2.c index d7c86b4..3569ae6 100644 --- a/src/ix2.c +++ b/src/ix2.c @@ -23,6 +23,8 @@ #include #include +#include + #include "ix2.h" #include "list.h" @@ -65,6 +67,9 @@ struct ix2_node_t { }; typedef struct ix2_t { + pad_t *node_pad; /* ix2_node_t allocator */ + pad_t *ref_pad; /* ix2_object_ref_t allocator */ + pad_t *object_pad; /* ix2_object_t allocator */ bb2f_t aabb; /* spatial bounds of ix2 root */ unsigned max_per_node; /* maximum number of objects to put in a node until max_depth reached */ unsigned max_depth; /* maximum depth of ix2 tree where nodes cease being divided further */ @@ -224,7 +229,7 @@ static void unlink_object(ix2_object_t *object, list_head_t *references_cache) continue; } - free(ref); + pad_put(ref); } } @@ -234,7 +239,7 @@ static void unlink_object(ix2_object_t *object, list_head_t *references_cache) * Returns the reference on success, NULL on failure * On falure the supplied object is left intact. */ -static ix2_object_ref_t * link_object(ix2_object_t *object, ix2_node_t *node, ix2_object_ref_t **cached_ref) +static ix2_object_ref_t * link_object(ix2_t *ix2, ix2_object_t *object, ix2_node_t *node, ix2_object_ref_t **cached_ref) { ix2_object_ref_t *ref = NULL; @@ -248,7 +253,7 @@ static ix2_object_ref_t * link_object(ix2_object_t *object, ix2_node_t *node, ix } if (!ref) - ref = calloc(1, sizeof(ix2_object_ref_t)); + ref = pad_get(ix2->ref_pad, sizeof(ix2_object_ref_t)); if (!ref) return NULL; @@ -284,7 +289,7 @@ static void remove_object(ix2_object_t *object) assert(object); unlink_object(object, NULL); - free(object); + pad_put(object); } @@ -304,7 +309,7 @@ static ix2_node_t * split_node(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb assert(node_aabb); /* allocate and setup the children nodes */ - node->children = calloc(4, sizeof(ix2_node_t)); + node->children = pad_get(ix2->node_pad, 4 * sizeof(ix2_node_t)); if (!node->children) goto _fail; @@ -378,7 +383,7 @@ static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, } /* Node is a leaf, optimistically link the object to the node */ - if (!link_object(object, node, reference_cache)) + if (!link_object(ix2, object, node, reference_cache)) goto _fail; /* If the node is overflowing, split it */ @@ -412,7 +417,7 @@ ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth) ix2 = calloc(1, sizeof(ix2_t)); if (!ix2) - return NULL; + goto fail_ix2; init_node(&ix2->root, aabb); @@ -420,7 +425,28 @@ ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth) ix2->max_per_node = max_per_node; ix2->max_depth = max_depth; + ix2->node_pad = pad_new(sizeof(ix2_node_t) * 8); + if (!ix2->node_pad) + goto fail_node_pad; + + ix2->ref_pad = pad_new(sizeof(ix2_object_ref_t) * 64); + if (!ix2->ref_pad) + goto fail_ref_pad; + + ix2->object_pad = pad_new(sizeof(ix2_object_t) * 32); + if (!ix2->object_pad) + goto fail_object_pad; + return ix2; + +fail_object_pad: + free(ix2->ref_pad); +fail_ref_pad: + free(ix2->node_pad); +fail_node_pad: + free(ix2); +fail_ix2: + return NULL; } @@ -428,7 +454,11 @@ ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth) /* Note the external objects which have been indexed are not freed */ void ix2_free(ix2_t *ix2) { - /* TODO: free everything */ + assert(ix2); + + pad_free(ix2->node_pad); + pad_free(ix2->ref_pad); + pad_free(ix2->object_pad); free(ix2); } @@ -444,7 +474,7 @@ ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *object_position, v2f_t *object_ assert(object_aabb->min.x <= object_aabb->max.x); assert(object_aabb->min.y <= object_aabb->max.y); - o = calloc(1, sizeof(ix2_object_t)); + o = pad_get(ix2->object_pad, sizeof(ix2_object_t)); if (!o) return NULL; -- cgit v1.2.1