Browse Source

Stylistic changes

git-svn-id: https://klish.googlecode.com/svn/trunk@391 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
15be23e998
100 changed files with 4435 additions and 5018 deletions
  1. 2 3
      bin/konfd.c
  2. 51 63
      clish/shell/shell_tinyxml_read.cpp
  3. 19 38
      lub/argv.h
  4. 7 9
      lub/argv/argv__get_arg.c
  5. 15 16
      lub/argv/argv__get_argv.c
  6. 3 3
      lub/argv/argv__get_count.c
  7. 7 9
      lub/argv/argv__get_offset.c
  8. 7 9
      lub/argv/argv__get_quoted.c
  9. 14 17
      lub/argv/argv_delete.c
  10. 43 51
      lub/argv/argv_new.c
  11. 50 59
      lub/argv/argv_nextword.c
  12. 14 15
      lub/argv/argv_wordcount.c
  13. 10 17
      lub/argv/private.h
  14. 55 87
      lub/bintree.h
  15. 16 20
      lub/bintree/bintree_dump.c
  16. 9 11
      lub/bintree/bintree_find.c
  17. 28 29
      lub/bintree/bintree_findfirst.c
  18. 28 31
      lub/bintree/bintree_findlast.c
  19. 26 31
      lub/bintree/bintree_findnext.c
  20. 26 31
      lub/bintree/bintree_findprevious.c
  21. 9 8
      lub/bintree/bintree_init.c
  22. 50 61
      lub/bintree/bintree_insert.c
  23. 14 15
      lub/bintree/bintree_iterator_init.c
  24. 8 8
      lub/bintree/bintree_iterator_next.c
  25. 8 8
      lub/bintree/bintree_iterator_previous.c
  26. 7 8
      lub/bintree/bintree_node_init.c
  27. 24 29
      lub/bintree/bintree_remove.c
  28. 54 63
      lub/bintree/bintree_splay.c
  29. 4 5
      lub/bintree/private.h
  30. 47 58
      lub/blockpool.h
  31. 19 15
      lub/blockpool/blockpool__get_stats.c
  32. 21 28
      lub/blockpool/blockpool_alloc.c
  33. 15 19
      lub/blockpool/blockpool_free.c
  34. 23 27
      lub/blockpool/blockpool_init.c
  35. 2 3
      lub/blockpool/private.h
  36. 6 6
      lub/c_decl.h
  37. 13 24
      lub/ctype.h
  38. 4 4
      lub/ctype/ctype_isdigit.c
  39. 4 4
      lub/ctype/ctype_isspace.c
  40. 4 4
      lub/ctype/ctype_tolower.c
  41. 4 4
      lub/ctype/ctype_toupper.c
  42. 26 37
      lub/dblockpool.h
  43. 40 46
      lub/dblockpool/dblockpool_alloc.c
  44. 9 10
      lub/dblockpool/dblockpool_fini.c
  45. 26 30
      lub/dblockpool/dblockpool_free.c
  46. 7 8
      lub/dblockpool/dblockpool_init.c
  47. 4 5
      lub/dblockpool/private.h
  48. 10 13
      lub/dump.h
  49. 28 31
      lub/dump/dump.c
  50. 185 240
      lub/heap.h
  51. 350 363
      lub/heap/cache.c
  52. 35 44
      lub/heap/cache.h
  53. 53 53
      lub/heap/cache_bucket.c
  54. 614 680
      lub/heap/context.c
  55. 99 129
      lub/heap/context.h
  56. 13 12
      lub/heap/heap__get_max_free.c
  57. 24 26
      lub/heap/heap__get_stacktrace.c
  58. 3 4
      lub/heap/heap__get_stats.c
  59. 91 97
      lub/heap/heap_add_segment.c
  60. 110 126
      lub/heap/heap_align_block.c
  61. 4 4
      lub/heap/heap_block__get_tail.c
  62. 4 4
      lub/heap/heap_block_check.c
  63. 3 3
      lub/heap/heap_block_from_ptr.c
  64. 3 3
      lub/heap/heap_block_getfirst.c
  65. 9 10
      lub/heap/heap_block_getkey.c
  66. 10 11
      lub/heap/heap_block_getnext.c
  67. 17 18
      lub/heap/heap_block_getprevious.c
  68. 70 79
      lub/heap/heap_check.c
  69. 10 11
      lub/heap/heap_context_delete.c
  70. 22 23
      lub/heap/heap_context_find_or_create.c
  71. 68 72
      lub/heap/heap_create.c
  72. 37 45
      lub/heap/heap_destroy.c
  73. 25 22
      lub/heap/heap_extend_both_ways.c
  74. 69 68
      lub/heap/heap_extend_downwards.c
  75. 57 53
      lub/heap/heap_extend_upwards.c
  76. 15 17
      lub/heap/heap_foreach_free_block.c
  77. 9 16
      lub/heap/heap_foreach_segment.c
  78. 52 52
      lub/heap/heap_graft_to_bottom.c
  79. 44 47
      lub/heap/heap_graft_to_top.c
  80. 35 36
      lub/heap/heap_init_free_block.c
  81. 27 29
      lub/heap/heap_merge_with_next.c
  82. 39 39
      lub/heap/heap_merge_with_previous.c
  83. 60 61
      lub/heap/heap_new_alloc_block.c
  84. 18 21
      lub/heap/heap_new_free_block.c
  85. 27 32
      lub/heap/heap_post_realloc.c
  86. 24 31
      lub/heap/heap_pre_realloc.c
  87. 228 241
      lub/heap/heap_raw_realloc.c
  88. 46 48
      lub/heap/heap_realloc.c
  89. 43 43
      lub/heap/heap_scan_stack.c
  90. 116 127
      lub/heap/heap_show.c
  91. 63 64
      lub/heap/heap_slice_from_bottom.c
  92. 57 58
      lub/heap/heap_slice_from_top.c
  93. 85 98
      lub/heap/heap_static_alloc.c
  94. 44 46
      lub/heap/heap_stop_here.c
  95. 39 44
      lub/heap/heap_tainted_memory.c
  96. 392 416
      lub/heap/node.c
  97. 36 88
      lub/heap/node.h
  98. 2 2
      lub/heap/posix/heap_clean_stacks.c
  99. 25 28
      lub/heap/posix/heap_leak_mutex.c
  100. 4 4
      lub/heap/posix/heap_scan_bss.c

+ 2 - 3
bin/konfd.c

@@ -347,9 +347,8 @@ static char * process_query(int sock, konf_tree_t * conf, char *str)
 	/* Go through the pwd */
 	iconf = conf;
 	for (i = 0; i < konf_query__get_pwdc(query); i++) {
-		if (!
-		    (iconf =
-		     konf_tree_find_conf(iconf, konf_query__get_pwd(query, i), 0, 0))) {
+		if (!(iconf = konf_tree_find_conf(iconf,
+			konf_query__get_pwd(query, i), 0, 0))) {
 			iconf = NULL;
 			break;
 		}

+ 51 - 63
clish/shell/shell_tinyxml_read.cpp

@@ -89,7 +89,7 @@ process_children(clish_shell_t * shell,
 	TiXmlElement * element, void *parent = NULL)
 {
 	for (TiXmlNode * node = element->FirstChild();
-		NULL != node; node = element->IterateChildren(node)) {
+		node; node = element->IterateChildren(node)) {
 		// Now deal with all the contained elements
 		process_node(shell, node, parent);
 	}
@@ -101,8 +101,8 @@ process_clish_module(clish_shell_t * shell, TiXmlElement * element, void *)
 {
 	// create the global view
 	if (!shell->global)
-		shell->global =
-			clish_shell_find_create_view(shell, "global", "");
+		shell->global = clish_shell_find_create_view(shell,
+			"global", "");
 	process_children(shell, element, shell->global);
 }
 
@@ -118,7 +118,7 @@ static void process_view(clish_shell_t * shell, TiXmlElement * element, void *)
 	// re-use a view if it already exists
 	view = clish_shell_find_create_view(shell, name, prompt);
 
-	if ((depth != NULL) && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
+	if (depth && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
 		unsigned res = atoi(depth);
 		clish_view__set_depth(view, res);
 	}
@@ -164,7 +164,7 @@ process_overview(clish_shell_t * shell, TiXmlElement * element, void *)
 	// read the following text element
 	TiXmlNode *text = element->FirstChild();
 
-	if (NULL != text) {
+	if (text) {
 		assert(TiXmlNode::TEXT == text->Type());
 		// set the overview text for this view
 		assert(NULL == shell->overview);
@@ -196,14 +196,12 @@ process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	const char *interrupt = element->Attribute("interrupt");
 	const char *ref = element->Attribute("ref");
 
-	if (NULL != access) {
-		allowed = false;	// err on the side of caution
+	if (access) {
+		allowed = false; // err on the side of caution
 		if (shell->client_hooks->access_fn) {
 			// get the client to authenticate
-			allowed =
-			    shell->client_hooks->access_fn(shell,
-							   access) ? true :
-			    false;
+			allowed = shell->client_hooks->access_fn(shell,
+				access) ? true : false;
 		}
 	}
 	if (!allowed)
@@ -249,41 +247,35 @@ process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	cmd = clish_view_new_command(v, name, help);
 	assert(cmd);
 	clish_command__set_pview(cmd, v);
-	if (NULL != escape_chars) {
-		/* define some specialist escape characters */
-		clish_command__set_escape_chars(cmd,
-						escape_chars);
-	}
-	if (NULL != args_name) {
+	/* define some specialist escape characters */
+	if (escape_chars)
+		clish_command__set_escape_chars(cmd, escape_chars);
+	if (args_name) {
 		/* define a "rest of line" argument */
 		clish_param_t *param;
 		clish_ptype_t *tmp = NULL;
 
-		assert(NULL != args_help);
+		assert(args_help);
 		tmp = clish_shell_find_create_ptype(shell,
-					  "internal_ARGS",
-					  "Arguments", "[^\\]+",
-					  CLISH_PTYPE_REGEXP,
-					  CLISH_PTYPE_NONE);
+			"internal_ARGS",
+			"Arguments", "[^\\]+",
+			CLISH_PTYPE_REGEXP,
+			CLISH_PTYPE_NONE);
 		assert(tmp);
-		param =
-		    clish_param_new(args_name, args_help, tmp);
-
+		param = clish_param_new(args_name, args_help, tmp);
 		clish_command__set_args(cmd, param);
 	}
 	// define the view which this command changes to
-	if (NULL != view) {
-		clish_view_t *next =
-		    clish_shell_find_create_view(shell, view,
-						 NULL);
+	if (view) {
+		clish_view_t *next = clish_shell_find_create_view(shell, view,
+			NULL);
 
 		// reference the next view
 		clish_command__set_view(cmd, next);
 	}
 	// define the view id which this command changes to
-	if (NULL != viewid) {
+	if (viewid)
 		clish_command__set_viewid(cmd, viewid);
-	}
 	/* lock field */
 	if (lock && (lub_string_nocasecmp(lock, "false") == 0))
 		clish_command__set_lock(cmd, BOOL_FALSE);
@@ -316,7 +308,7 @@ process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	const char *viewid = element->Attribute("viewid");
 	const char *default_shebang = element->Attribute("default_shebang");
 
-	assert(NULL == shell->startup);
+	assert(!shell->startup);
 	assert(view);
 
 	/* create a command with NULL help */
@@ -329,11 +321,10 @@ process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	clish_command__set_view(cmd, next);
 
 	// define the view id which this command changes to
-	if (NULL != viewid) {
+	if (viewid)
 		clish_command__set_viewid(cmd, viewid);
-	}
 
-	if (NULL != default_shebang)
+	if (default_shebang)
 		clish_shell__set_default_shebang(shell, default_shebang);
 
 	// remember this command
@@ -353,7 +344,7 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	else
 		cmd = (clish_command_t *) parent;
 
-	if ((NULL != cmd) || (NULL != p_param)) {
+	if (cmd || p_param) {
 		assert((!cmd) || (cmd != shell->startup));
 		const char *name = element->Attribute("name");
 		const char *help = element->Attribute("help");
@@ -374,9 +365,9 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		assert(ptype);
 		if (ptype && *ptype) {
 			tmp = clish_shell_find_create_ptype(shell, ptype,
-							    NULL, NULL,
-							    CLISH_PTYPE_REGEXP,
-							    CLISH_PTYPE_NONE);
+				NULL, NULL,
+				CLISH_PTYPE_REGEXP,
+				CLISH_PTYPE_NONE);
 			assert(tmp);
 		}
 		param = clish_param_new(name, help, tmp);
@@ -385,7 +376,7 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		 * command syntax over newer optional command mechanism.
 		 * It will create nested PARAM.
 		 */
-		if (NULL != prefix) {
+		if (prefix) {
 			const char *ptype_name = "__SUBCOMMAND";
 			clish_param_t *opt_param = NULL;
 
@@ -406,7 +397,7 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 					      CLISH_PARAM_SUBCOMMAND);
 			clish_param__set_optional(opt_param, BOOL_TRUE);
 
-			if (NULL != test)
+			if (test)
 				clish_param__set_test(opt_param, test);
 
 			if (cmd)
@@ -420,16 +411,15 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 			p_param = opt_param;
 		}
 
-		if (NULL != defval) {
+		if (defval)
 			clish_param__set_default(param, defval);
-		}
 
 		if (hidden && (lub_string_nocasecmp(hidden, "true") == 0))
 			clish_param__set_hidden(param, BOOL_TRUE);
 		else
 			clish_param__set_hidden(param, BOOL_FALSE);
 
-		if (NULL != mode) {
+		if (mode) {
 			if (!lub_string_nocasecmp(mode, "switch")) {
 				clish_param__set_mode(param,
 					CLISH_PARAM_SWITCH);
@@ -448,14 +438,14 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		else
 			clish_param__set_optional(param, BOOL_FALSE);
 
-		if (NULL != value) {
+		if (value) {
 			clish_param__set_value(param, value);
 			/* Force mode to subcommand */
 			clish_param__set_mode(param,
 				CLISH_PARAM_SUBCOMMAND);
 		}
 
-		if ((NULL != test) && (NULL == prefix))
+		if (test && !prefix)
 			clish_param__set_test(param, test);
 
 		if (cmd)
@@ -476,22 +466,21 @@ process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
 {
 	clish_command_t *cmd = (clish_command_t *) parent;
 
-	if (NULL != cmd) {
+	if (cmd) {
 		// read the following text element
 		TiXmlNode *text = element->FirstChild();
 		const char *builtin = element->Attribute("builtin");
 		const char *shebang = element->Attribute("shebang");
 
-		if (NULL != text) {
+		if (text) {
 			assert(TiXmlNode::TEXT == text->Type());
 			// store the action
 			clish_command__set_action(cmd, text->Value());
 		}
-		if (NULL != builtin) {
-			// store the action
+		// store the action
+		if (builtin)
 			clish_command__set_builtin(cmd, builtin);
-		}
-		if (NULL != shebang)
+		if (shebang)
 			clish_command__set_shebang(cmd, shebang);
 	}
 }
@@ -505,7 +494,7 @@ process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	// read the following text element
 	TiXmlNode *text = element->FirstChild();
 
-	if ((NULL != cmd) && (NULL != text)) {
+	if (cmd && text) {
 		assert(TiXmlNode::TEXT == text->Type());
 		// store the action
 		clish_command__set_detail(cmd, text->Value());
@@ -528,14 +517,14 @@ process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	const char *inherit = element->Attribute("inherit");
 
 	assert(view);
-	clish_view_t *ref_view =
-	    clish_shell_find_create_view(shell, view, NULL);
+	clish_view_t *ref_view = clish_shell_find_create_view(shell,
+		view, NULL);
 	assert(ref_view);
 	nspace = clish_nspace_new(ref_view);
 	assert(nspace);
 	clish_view_insert_nspace(v, nspace);
 
-	if (NULL != prefix) {
+	if (prefix) {
 		clish_nspace__set_prefix(nspace, prefix);
 		if (prefix_help)
 			clish_nspace_create_prefix_cmd(nspace,
@@ -566,7 +555,6 @@ process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		clish_nspace__set_inherit(nspace, BOOL_FALSE);
 	else
 		clish_nspace__set_inherit(nspace, BOOL_TRUE);
-
 }
 
 ////////////////////////////////////////
@@ -575,7 +563,7 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 {
 	clish_command_t *cmd = (clish_command_t *) parent;
 
-	if (NULL == cmd)
+	if (!cmd)
 		return;
 
 	// read the following text element
@@ -600,7 +588,7 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		clish_command__set_priority(cmd, 0x7f00);
 	}
 
-	if ((priority != NULL) && (*priority != '\0')) {
+	if (priority && (*priority != '\0')) {
 		long val = 0;
 		char *endptr;
 		unsigned short pri;
@@ -617,12 +605,12 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		clish_command__set_priority(cmd, pri);
 	}
 
-	if (pattern != NULL)
+	if (pattern)
 		clish_command__set_pattern(cmd, pattern);
 	else
 		clish_command__set_pattern(cmd, "^${__cmd}");
 
-	if (file != NULL)
+	if (file)
 		clish_command__set_file(cmd, file);
 
 	if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
@@ -635,13 +623,13 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	else
 		clish_command__set_unique(cmd, BOOL_TRUE);
 
-	if (seq != NULL)
+	if (seq)
 		clish_command__set_seq(cmd, seq);
 	else
 		/* The entries without sequence cannot be non-unique */
 		clish_command__set_unique(cmd, BOOL_TRUE);
 
-	if (cfg_depth != NULL)
+	if (cfg_depth)
 		clish_command__set_cfg_depth(cmd, cfg_depth);
 
 }

+ 19 - 38
lub/argv.h

@@ -28,7 +28,6 @@ contains four "words" the third of which is a string.
 #include "types.h"
 
 _BEGIN_C_DECL
-
 /**
  * This type is used to reference an instance of an argument vector
  */
@@ -47,13 +46,11 @@ typedef struct lub_argv_s lub_argv_t;
  * \post
  * - none
  */
-unsigned
-    lub_argv_wordcount(
-        /** 
+unsigned lub_argv_wordcount(
+	/** 
          * The string to analyse
          */
-        const char *line
-    );
+				   const char *line);
 /**
  *  This operation is used to construct an instance of this class. The client
  * species a string and an offset within that string, from which to start 
@@ -71,41 +68,25 @@ unsigned
  * - The client becomes resposible for releasing the instance when they are
  *   finished with it, by calling lub_argv_delete()
  */
-lub_argv_t *
-    lub_argv_new(
-        /** 
+lub_argv_t *lub_argv_new(
+	/** 
          * The string to analyse
-         */            
-        const char *line,
-        /** 
+         */
+				const char *line,
+	/** 
          * The offset in the string to start from
          */
-        size_t offset
-    );
-            
-void
-    lub_argv_delete(lub_argv_t *instance);
-unsigned
-    lub_argv__get_count(const lub_argv_t *instance);
-const char *
-    lub_argv__get_arg(const lub_argv_t *instance,
-                      unsigned          index);
-size_t
-    lub_argv__get_offset(const lub_argv_t *instance,
-                         unsigned          index);
-bool_t
-    lub_argv__get_quoted(const lub_argv_t *instance,
-                         unsigned          index);
-void
-    lub_argv__set_arg(lub_argv_t *instance,
-                      unsigned    index,
-                      const char *arg);
-char **
-    lub_argv__get_argv(const lub_argv_t *instance, char *argv0);
-const char *
-    lub_argv__get_line(const lub_argv_t *instance);
+				size_t offset);
 
-_END_C_DECL
+void lub_argv_delete(lub_argv_t * instance);
+unsigned lub_argv__get_count(const lub_argv_t * instance);
+const char *lub_argv__get_arg(const lub_argv_t * instance, unsigned index);
+size_t lub_argv__get_offset(const lub_argv_t * instance, unsigned index);
+bool_t lub_argv__get_quoted(const lub_argv_t * instance, unsigned index);
+void lub_argv__set_arg(lub_argv_t * instance, unsigned index, const char *arg);
+char **lub_argv__get_argv(const lub_argv_t * instance, char *argv0);
+const char *lub_argv__get_line(const lub_argv_t * instance);
 
-#endif /* _lub_argv_h */
+_END_C_DECL
+#endif				/* _lub_argv_h */
 /** @} lub_argv */

+ 7 - 9
lub/argv/argv__get_arg.c

@@ -4,16 +4,14 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-const char *
-lub_argv__get_arg(const lub_argv_t *this,
-                  unsigned          index)
+const char *lub_argv__get_arg(const lub_argv_t * this, unsigned index)
 {
-    const char *result = NULL;
+	const char *result = NULL;
 
-    if(this->argc > index)
-    {
-        result = this->argv[index].arg;
-    }
-    return result;
+	if (this->argc > index) {
+		result = this->argv[index].arg;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 15 - 16
lub/argv/argv__get_argv.c

@@ -7,31 +7,30 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-const char *
-lub_argv__get_line(const lub_argv_t *this)
+const char *lub_argv__get_line(const lub_argv_t * this)
 {
 	return this->line;
 }
 
 /*--------------------------------------------------------- */
-char **
-lub_argv__get_argv(const lub_argv_t *this, char *argv0)
+char **lub_argv__get_argv(const lub_argv_t * this, char *argv0)
 {
-    char **result = NULL;
-    unsigned i;
-    unsigned a = 0;
+	char **result = NULL;
+	unsigned i;
+	unsigned a = 0;
 
-    if(argv0)
-        a = 1;
+	if (argv0)
+		a = 1;
 
-    result = malloc(sizeof(char *) * (this->argc + 1 + a));
+	result = malloc(sizeof(char *) * (this->argc + 1 + a));
 
-    if(argv0)
-        result[0] = argv0;
-    for(i = 0; i < this->argc; i++)
-        result[i + a] = this->argv[i].arg;
-    result[i + a] = NULL;
+	if (argv0)
+		result[0] = argv0;
+	for (i = 0; i < this->argc; i++)
+		result[i + a] = this->argv[i].arg;
+	result[i + a] = NULL;
 
-    return result;
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 3 - 3
lub/argv/argv__get_count.c

@@ -3,9 +3,9 @@
  */
 #include "private.h"
 /*--------------------------------------------------------- */
-unsigned
-lub_argv__get_count(const lub_argv_t *this)
+unsigned lub_argv__get_count(const lub_argv_t * this)
 {
-    return this->argc;
+	return this->argc;
 }
+
 /*--------------------------------------------------------- */

+ 7 - 9
lub/argv/argv__get_offset.c

@@ -4,16 +4,14 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-size_t
-lub_argv__get_offset(const lub_argv_t *this,
-                     unsigned          index)
+size_t lub_argv__get_offset(const lub_argv_t * this, unsigned index)
 {
-    size_t result = 0;
+	size_t result = 0;
 
-    if(this->argc > index)
-    {
-        result = this->argv[index].offset;
-    }
-    return result;
+	if (this->argc > index) {
+		result = this->argv[index].offset;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 7 - 9
lub/argv/argv__get_quoted.c

@@ -4,16 +4,14 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-bool_t
-lub_argv__get_quoted(const lub_argv_t *this,
-                     unsigned          index)
+bool_t lub_argv__get_quoted(const lub_argv_t * this, unsigned index)
 {
-    bool_t result = BOOL_FALSE;
+	bool_t result = BOOL_FALSE;
 
-    if(this->argc > index)
-    {
-        result = this->argv[index].quoted;
-    }
-    return result;
+	if (this->argc > index) {
+		result = this->argv[index].quoted;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 14 - 17
lub/argv/argv_delete.c

@@ -6,27 +6,24 @@
 
 #include <stdlib.h>
 /*--------------------------------------------------------- */
-static void
-lub_argv_fini(lub_argv_t *this)
+static void lub_argv_fini(lub_argv_t * this)
 {
-    unsigned i;
+	unsigned i;
 
-    lub_string_free(this->line);
-    this->line = NULL;
-    for(i = 0;
-        i < this->argc;
-        i++)
-    {
-        lub_string_free(this->argv[i].arg);
-    }
-    free(this->argv);
-    this->argv = NULL;
+	lub_string_free(this->line);
+	this->line = NULL;
+	for (i = 0; i < this->argc; i++) {
+		lub_string_free(this->argv[i].arg);
+	}
+	free(this->argv);
+	this->argv = NULL;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_argv_delete(lub_argv_t *this)
+void lub_argv_delete(lub_argv_t * this)
 {
-    lub_argv_fini(this);
-    free(this);
+	lub_argv_fini(this);
+	free(this);
 }
+
 /*--------------------------------------------------------- */

+ 43 - 51
lub/argv/argv_new.c

@@ -8,66 +8,58 @@
 #include <string.h>
 
 /*--------------------------------------------------------- */
-static void
-lub_argv_init(lub_argv_t *this,
-              const char *line,
-              size_t      offset)
+static void lub_argv_init(lub_argv_t * this, const char *line, size_t offset)
 {
-    size_t      len;
-    const char *word;
-    lub_arg_t  *arg;
-    bool_t      quoted;
+	size_t len;
+	const char *word;
+	lub_arg_t *arg;
+	bool_t quoted;
 
-    /* Save the whole line */
-    this->line = lub_string_dup(line);
+	/* Save the whole line */
+	this->line = lub_string_dup(line);
 
-    /* first of all count the words in the line */
-    this->argc = lub_argv_wordcount(line);
+	/* first of all count the words in the line */
+	this->argc = lub_argv_wordcount(line);
 
-    /* allocate space to hold the vector */
-    arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
+	/* allocate space to hold the vector */
+	arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
 
-    if(arg)
-    {
-        /* then fill out the array with the words */
-        for(word = lub_argv_nextword(line,&len,&offset,&quoted);
-            *word;
-            word = lub_argv_nextword(word+len,&len,&offset,&quoted))
-        {
-            char * tmp = lub_string_dupn(word,len);
-            (*arg).arg = lub_string_decode(tmp);
-            lub_string_free(tmp);
-            (*arg).offset = offset;
-            (*arg).quoted = quoted;
+	if (arg) {
+		/* then fill out the array with the words */
+		for (word = lub_argv_nextword(line, &len, &offset, &quoted);
+		     *word;
+		     word =
+		     lub_argv_nextword(word + len, &len, &offset, &quoted)) {
+			char *tmp = lub_string_dupn(word, len);
+			(*arg).arg = lub_string_decode(tmp);
+			lub_string_free(tmp);
+			(*arg).offset = offset;
+			(*arg).quoted = quoted;
 
-            offset += len;
+			offset += len;
 
-            if(BOOL_TRUE == quoted)
-            {
-                len    += 1; /* account for terminating quotation mark */
-                offset += 2; /* account for quotation marks */
-            }
-            arg++;
-        }
-    }
-    else
-    {
-        /* failed to get memory so don't pretend otherwise */
-        this->argc = 0;
-    }
+			if (BOOL_TRUE == quoted) {
+				len += 1;	/* account for terminating quotation mark */
+				offset += 2;	/* account for quotation marks */
+			}
+			arg++;
+		}
+	} else {
+		/* failed to get memory so don't pretend otherwise */
+		this->argc = 0;
+	}
 }
+
 /*--------------------------------------------------------- */
-lub_argv_t *
-lub_argv_new(const char *line,
-             size_t      offset)
+lub_argv_t *lub_argv_new(const char *line, size_t offset)
 {
-    lub_argv_t *this;
-    
-    this = malloc(sizeof(lub_argv_t));
-    if(NULL != this)
-    {
-        lub_argv_init(this,line,offset);
-    }
-    return this;
+	lub_argv_t *this;
+
+	this = malloc(sizeof(lub_argv_t));
+	if (NULL != this) {
+		lub_argv_init(this, line, offset);
+	}
+	return this;
 }
+
 /*--------------------------------------------------------- */

+ 50 - 59
lub/argv/argv_nextword.c

@@ -7,65 +7,56 @@
 
 #include <stddef.h>
 /*--------------------------------------------------------- */
-const char *
-lub_argv_nextword(const char *string,
-                  size_t     *len,
-                  size_t     *offset,
-                  bool_t     *quoted)
+const char *lub_argv_nextword(const char *string,
+			      size_t * len, size_t * offset, bool_t * quoted)
 {
-    const char *word;
-    bool_t      quote = BOOL_FALSE;
-    
-    *quoted = BOOL_FALSE;
-    
-    /* find the start of a word (not including an opening quote) */
-    while(*string && lub_ctype_isspace(*string))
-    {
-        string++;
-        (*offset)++;
-    }
-    if(*string == '\\')
-    {
-        string++;
-        if (*string)
-            string++;
-    }
-    /* is this the start of a quoted string ? */
-    if(*string == '"')
-    {
-        quote = BOOL_TRUE;
-        string++;
-    }
-    word = string;
-    *len = 0;
-    
-    /* find the end of the word */
-    while(*string)
-    {
-        if(*string == '\\')
-        {
-            string++;
-            (*len)++;
-            if (*string) {
-                (*len)++;
-                string++;
-            }
-            continue;
-        }
-        if((BOOL_FALSE == quote) && lub_ctype_isspace(*string))
-        {
-            /* end of word */
-            break;
-        }
-        if(*string == '"')
-        {
-            /* end of a quoted string */
-            *quoted = BOOL_TRUE;
-            break;
-        }
-        (*len)++;
-        string++;
-    }
-    return word;
+	const char *word;
+	bool_t quote = BOOL_FALSE;
+
+	*quoted = BOOL_FALSE;
+
+	/* find the start of a word (not including an opening quote) */
+	while (*string && lub_ctype_isspace(*string)) {
+		string++;
+		(*offset)++;
+	}
+	if (*string == '\\') {
+		string++;
+		if (*string)
+			string++;
+	}
+	/* is this the start of a quoted string ? */
+	if (*string == '"') {
+		quote = BOOL_TRUE;
+		string++;
+	}
+	word = string;
+	*len = 0;
+
+	/* find the end of the word */
+	while (*string) {
+		if (*string == '\\') {
+			string++;
+			(*len)++;
+			if (*string) {
+				(*len)++;
+				string++;
+			}
+			continue;
+		}
+		if ((BOOL_FALSE == quote) && lub_ctype_isspace(*string)) {
+			/* end of word */
+			break;
+		}
+		if (*string == '"') {
+			/* end of a quoted string */
+			*quoted = BOOL_TRUE;
+			break;
+		}
+		(*len)++;
+		string++;
+	}
+	return word;
 }
+
 /*--------------------------------------------------------- */

+ 14 - 15
lub/argv/argv_wordcount.c

@@ -5,22 +5,21 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-unsigned
-lub_argv_wordcount(const char *line)
+unsigned lub_argv_wordcount(const char *line)
 {
-    const char *word;
-    unsigned    result=0;
-    size_t      len=0,offset=0;
-    bool_t      quoted;
+	const char *word;
+	unsigned result = 0;
+	size_t len = 0, offset = 0;
+	bool_t quoted;
 
-    for(word = lub_argv_nextword(line,&len,&offset,&quoted);
-        *word;
-        word = lub_argv_nextword(word+len,&len,&offset,&quoted))
-    {
-        /* account for the terminating quotation mark */
-        len += (BOOL_TRUE == quoted) ? 1 : 0;
-        result++;
-    }
-    return result;
+	for (word = lub_argv_nextword(line, &len, &offset, &quoted);
+	     *word;
+	     word = lub_argv_nextword(word + len, &len, &offset, &quoted)) {
+		/* account for the terminating quotation mark */
+		len += (BOOL_TRUE == quoted) ? 1 : 0;
+		result++;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 10 - 17
lub/argv/private.h

@@ -7,26 +7,19 @@
 #include "lub/argv.h"
 
 typedef struct lub_arg_s lub_arg_t;
-struct lub_arg_s
-{
-    char   *arg;
-    size_t  offset;
-    bool_t  quoted;
+struct lub_arg_s {
+	char *arg;
+	size_t offset;
+	bool_t quoted;
 };
 
-struct lub_argv_s
-{
-    char *line;
-    unsigned   argc;
-    lub_arg_t *argv;
+struct lub_argv_s {
+	char *line;
+	unsigned argc;
+	lub_arg_t *argv;
 };
 /*-------------------------------------
  * PRIVATE META FUNCTIONS
  *------------------------------------- */
-const char *
-    lub_argv_nextword(
-        const char *string,
-        size_t     *len,
-        size_t     *offset,
-        bool_t     *quoted
-    );
+const char *lub_argv_nextword(const char *string,
+			      size_t * len, size_t * offset, bool_t * quoted);

+ 55 - 87
lub/bintree.h

@@ -119,10 +119,9 @@ typedef struct lub_bintree_node_s lub_bintree_node_t;
 /** 
  * CLIENTS MUSTN'T TOUCH THE CONTENTS OF THIS STRUCTURE
  */
-struct lub_bintree_node_s
-{
-	/** internal */lub_bintree_node_t *left; 
-	/** internal */lub_bintree_node_t *right;
+struct lub_bintree_node_s {
+	/** internal */ lub_bintree_node_t *left;
+	/** internal */ lub_bintree_node_t *right;
 };
 
 /**
@@ -138,8 +137,7 @@ struct lub_bintree_node_s
  *     >0 if clientnode  > clientkey
  */
 typedef int
-		lub_bintree_compare_fn(const void *clientnode,
-				       const void *clientkey);
+lub_bintree_compare_fn(const void *clientnode, const void *clientkey);
 /**
  * This is used to size the key storage area for an opaque key.
  * If any client requires a greater storage size then this will need to
@@ -155,10 +153,9 @@ typedef struct lub_bintree_key_s lub_bintree_key_t;
 /** 
  * CLIENTS MUSTN'T TOUCH THE CONTENTS OF THIS STRUCTURE
  */
-struct lub_bintree_key_s
-{
-    /** internal */char storage[lub_bintree_MAX_KEY_STORAGE];
-    /** internal */int  magic;
+struct lub_bintree_key_s {
+    /** internal */ char storage[lub_bintree_MAX_KEY_STORAGE];
+    /** internal */ int magic;
 };
 
 /**
@@ -172,8 +169,7 @@ struct lub_bintree_key_s
  * A "key" which corresponds the "node" in this view
  */
 typedef void
-		lub_bintree_getkey_fn(const void        *clientnode,
-                		      lub_bintree_key_t *key);
+lub_bintree_getkey_fn(const void *clientnode, lub_bintree_key_t * key);
 
 /**
  * This type represents an binary tree instance
@@ -182,12 +178,11 @@ typedef struct lub_bintree_s lub_bintree_t;
 /** 
  * CLIENTS MUSTN'T TOUCH THE CONTENTS OF THIS STRUCTURE
  */
-struct lub_bintree_s
-{
-	/** internal */lub_bintree_node_t     *root;
-	/** internal */size_t                  node_offset;
-	/** internal */lub_bintree_compare_fn *compareFn;
-	/** internal */lub_bintree_getkey_fn  *getkeyFn;
+struct lub_bintree_s {
+	/** internal */ lub_bintree_node_t *root;
+	/** internal */ size_t node_offset;
+	/** internal */ lub_bintree_compare_fn *compareFn;
+	/** internal */ lub_bintree_getkey_fn *getkeyFn;
 };
 
 /**
@@ -197,10 +192,9 @@ typedef struct lub_bintree_iterator_s lub_bintree_iterator_t;
 /** 
  * CLIENTS MUSTN'T TOUCH THE CONTENTS OF THIS STRUCTURE
  */
-struct lub_bintree_iterator_s
-{
-	/** internal */lub_bintree_t    *tree;
-    /** internal */lub_bintree_key_t key;
+struct lub_bintree_iterator_s {
+	/** internal */ lub_bintree_t *tree;
+    /** internal */ lub_bintree_key_t key;
 };
 
 /****************************************************************
@@ -213,28 +207,26 @@ struct lub_bintree_iterator_s
  *
  * \post The tree is ready to have client nodes inserted.
  */
-extern void
-	lub_bintree_init(
+extern void lub_bintree_init(
 		/** 
 		* the "tree" instance to initialise 
 		*/
-		lub_bintree_t         *tree,
+				    lub_bintree_t * tree,
 		/** 
 		* the offset of the bintree "node" structure within the
 		* "clientnode" structure. This is typically passed 
 		* using the offsetof() macro.
 		*/
-		size_t                 node_offset,
+				    size_t node_offset,
 		/**
 		* a comparison function for comparing a "clientnode"
 		* with a "clientkey"
 		*/
-		lub_bintree_compare_fn compareFn,
+				    lub_bintree_compare_fn compareFn,
 		/**
 		* a function which will fill out a "key" from a clientnode
 		*/
-		lub_bintree_getkey_fn  getkeyFn
-	);
+				    lub_bintree_getkey_fn getkeyFn);
 
 /**
  * This operation is called to initialise a "clientnode" ready for
@@ -245,13 +237,11 @@ extern void
  *
  * \post The node is ready to be inserted into a tree.
  */
-extern void
-		lub_bintree_node_init(
+extern void lub_bintree_node_init(
 			/** 
 			 * the bintree node to initialise
 			  */
-			lub_bintree_node_t *node
-		);
+					 lub_bintree_node_t * node);
 
 /*****************************************
  * NODE MANIPULATION OPERATIONS
@@ -270,18 +260,16 @@ extern void
  * \post If the bintree "node" is already part of a tree, then an
  * assert will fire.
  */
-extern int
-		lub_bintree_insert(
+extern int lub_bintree_insert(
 			/**
 			 * the "tree" instance to invoke this operation upon
-			 */ 
-			lub_bintree_t *tree,
+			 */
+				     lub_bintree_t * tree,
 			/** 
 			 * a pointer to a client node. NB the tree can find the
 			 * necessary lub_BintreeNodeT from it's stored offset.
 			 */
-			void          *clientnode
-		);
+				     void *clientnode);
 
 /**
  * This operation removes a "clientnode" from the specified "tree"
@@ -294,17 +282,15 @@ extern int
  * \post If the clientnode is not present in the specified tree, then an
  *  assert will fire.
  */
-extern void
-		lub_bintree_remove(
+extern void lub_bintree_remove(
 			/** 
 			* the "tree" instance to invoke this operation upon
 			*/
-			lub_bintree_t *tree,
+				      lub_bintree_t * tree,
 			/**
 			* the node to remove
 			*/
-			void          *clientnode
-		);
+				      void *clientnode);
 
 /*****************************************
  * NODE RETRIEVAL OPERATIONS
@@ -317,13 +303,11 @@ extern void
  * \return
  * "clientnode" instance or NULL if no nodes are present in this tree.
  */
-extern void *
-	lub_bintree_findfirst(
+extern void *lub_bintree_findfirst(
 		/** 
 		 * the "tree" instance to invoke this operation upon
 		 */
-		lub_bintree_t *tree
-	);
+					  lub_bintree_t * tree);
 
 /**
  * This operation returns the last "clientnode" present in the specified "tree"
@@ -333,13 +317,11 @@ extern void *
  * \return
  * "clientnode" instance or NULL if no nodes are present in this tree.
  */
-extern void *
-	lub_bintree_findlast(
+extern void *lub_bintree_findlast(
 		/** 
 		* the "tree" instance to invoke this operation upon
 		*/
-		lub_bintree_t *tree
-	);
+					 lub_bintree_t * tree);
 
 /**
  * This operation searches the specified "tree" for a "clientnode" which matches the
@@ -350,17 +332,15 @@ extern void *
  * \return
  * "clientnode" instance or NULL if no node is found.
  */
-extern void *
-	lub_bintree_find(
+extern void *lub_bintree_find(
 		/** 
 		 * the "tree" instance to invoke this operation upon
 		 */
-		lub_bintree_t *tree,
+				     lub_bintree_t * tree,
 		/** 
 		  * the "key" to search with
 		  */
-        	const void    *key
-	);
+				     const void *key);
 
 /**
  * This operation searches the specified "tree" for a "clientnode" which is
@@ -373,17 +353,15 @@ extern void *
  * \return
  * "clientnode" instance or NULL if no node is found.
  */
-extern void *
-	lub_bintree_findnext(
+extern void *lub_bintree_findnext(
 		/** 
 		 * the "tree" instance to invoke this operation upon
 		 */
-		lub_bintree_t *tree,
+					 lub_bintree_t * tree,
 		/** 
 		  * the "key" to search with
 		  */
-        	const void    *key
-	);
+					 const void *key);
 
 /**
  * This operation searches the specified "tree" for a "clientnode" which is
@@ -396,17 +374,15 @@ extern void *
  * \return
  * "clientnode" instance or NULL if no node is found.
  */
-extern void *
-	lub_bintree_findprevious(
+extern void *lub_bintree_findprevious(
 		/** 
 		 * the "tree" instance to invoke this operation upon
 		 */
-		lub_bintree_t *tree,
+					     lub_bintree_t * tree,
 		/** 
 		  * the "key" to search with
 		  */
-                const void    *key
-	);
+					     const void *key);
 
 /*****************************************
  * ITERATION OPERATIONS
@@ -422,21 +398,19 @@ extern void *
  *
  * \post The interator instance will be updated to reference the position in the tree for the clientnode.
  */
-extern void
-		lub_bintree_iterator_init(
+extern void lub_bintree_iterator_init(
 			/** 
 			* the iterator instance to initialise
 			*/
-			lub_bintree_iterator_t *iter,
+					     lub_bintree_iterator_t * iter,
 			/** 
 			* the tree to associate with this iterator
 			*/
-			lub_bintree_t          *tree,
+					     lub_bintree_t * tree,
 			/** 
 			 * the starting point for the iteration
 			 */
-			const void             *clientnode
-			);
+					     const void *clientnode);
 
 /**
  * This operation returns the next "clientnode" in an iteration.
@@ -449,13 +423,11 @@ extern void
  *
  * \post The interator instance will be updated to reference the position in the tree for the returned value.
  */
-extern void *
-	lub_bintree_iterator_next(
+extern void *lub_bintree_iterator_next(
 		/** 
 		 * the iterator instance to invoke this operation upon.
 		 */
-		lub_bintree_iterator_t *iter
-	);
+					      lub_bintree_iterator_t * iter);
 
 /**
  * This operation returns the previous "clientnode" in an iteration.
@@ -468,13 +440,12 @@ extern void *
  *
  * \post The interator instance will be updated to reference the position in the tree for the returned value.
  */
-extern void *
-	lub_bintree_iterator_previous(
+extern void *lub_bintree_iterator_previous(
 		/** 
 		 * the iterator instance to invoke this operation upon.
 		 */
-		lub_bintree_iterator_t *iter
-	);
+						  lub_bintree_iterator_t *
+						  iter);
 /**
  * This operation dumps the node list of the specified tree to stdout
  *
@@ -482,14 +453,11 @@ extern void *
  *
  * \post The structure of the tree will be unaltered.
  */
-extern void
-	lub_bintree_dump(
+extern void lub_bintree_dump(
 	/** 
 	 * the "tree" instance to invoke this operation upon
 	 */
-	lub_bintree_t *tree
-	);
+				    lub_bintree_t * tree);
 
-#endif /* _lub_bintree_h */
+#endif				/* _lub_bintree_h */
 /** @} */
-

+ 16 - 20
lub/bintree/bintree_dump.c

@@ -5,29 +5,25 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-_lub_bintree_dump(lub_bintree_t      *this,
-                  lub_bintree_node_t *node)
+void _lub_bintree_dump(lub_bintree_t * this, lub_bintree_node_t * node)
 {
-    if(node->left)
-    {
-        _lub_bintree_dump(this,node->left);
-    }
-    printf(" %s%p",
-           (this->root == node) ? "(R)" : "",
-           lub_bintree_getclientnode(this,node));
-    if(node->right)
-    {
-        _lub_bintree_dump(this,node->right);
-    }
+	if (node->left) {
+		_lub_bintree_dump(this, node->left);
+	}
+	printf(" %s%p",
+	       (this->root == node) ? "(R)" : "",
+	       lub_bintree_getclientnode(this, node));
+	if (node->right) {
+		_lub_bintree_dump(this, node->right);
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_bintree_dump(lub_bintree_t *this)
+void lub_bintree_dump(lub_bintree_t * this)
 {
-    if(this->root)
-    {
-        _lub_bintree_dump(this, this->root);
-    }
+	if (this->root) {
+		_lub_bintree_dump(this, this->root);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 9 - 11
lub/bintree/bintree_find.c

@@ -33,17 +33,15 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_find(lub_bintree_t *this,
-                 const void    *clientkey)
+void *lub_bintree_find(lub_bintree_t * this, const void *clientkey)
 {
-        this->root = lub_bintree_splay(this,this->root,clientkey);
-        
-        if(NULL != this->root)
-        {
-	        if(lub_bintree_compare(this,this->root,clientkey) == 0)
-	                return lub_bintree_getclientnode(this,this->root);
-        }
-        return NULL;
+	this->root = lub_bintree_splay(this, this->root, clientkey);
+
+	if (NULL != this->root) {
+		if (lub_bintree_compare(this, this->root, clientkey) == 0)
+			return lub_bintree_getclientnode(this, this->root);
+	}
+	return NULL;
 }
+
 /*--------------------------------------------------------- */

+ 28 - 29
lub/bintree/bintree_findfirst.c

@@ -36,31 +36,31 @@
 static lub_bintree_compare_fn compareleft;
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_findfirst(lub_bintree_t *this)
+void *lub_bintree_findfirst(lub_bintree_t * this)
 {
-        lub_bintree_compare_fn *client_compare = this->compareFn;
-        
-        /*
-         * put dummy functions in place
-         * This will make the search faster and direct it to the left most
-         * node
-         */
-        this->compareFn = compareleft;
-        
-        /*
-         * the key doesn't matter here cos we've cobbled the compare function
-         */
-        this->root = lub_bintree_splay(this,this->root,NULL);
-        
-        /* restore the client functions */
-        this->compareFn = client_compare;
-        
-        if(NULL == this->root)
-                return NULL;
-        else
-                return lub_bintree_getclientnode(this,this->root);
+	lub_bintree_compare_fn *client_compare = this->compareFn;
+
+	/*
+	 * put dummy functions in place
+	 * This will make the search faster and direct it to the left most
+	 * node
+	 */
+	this->compareFn = compareleft;
+
+	/*
+	 * the key doesn't matter here cos we've cobbled the compare function
+	 */
+	this->root = lub_bintree_splay(this, this->root, NULL);
+
+	/* restore the client functions */
+	this->compareFn = client_compare;
+
+	if (NULL == this->root)
+		return NULL;
+	else
+		return lub_bintree_getclientnode(this, this->root);
 }
+
 /*--------------------------------------------------------- */
 /*
  * This comparison operation always returns 1 hence will force a
@@ -69,12 +69,11 @@ lub_bintree_findfirst(lub_bintree_t *this)
  * clientnode - node to compare (not used)
  * clientkey  - key to compare (not used)
  */
-static int
-compareleft(const void *clientnode,
-            const void *clientkey)
+static int compareleft(const void *clientnode, const void *clientkey)
 {
-        clientnode = clientnode;
-        clientkey  = clientkey;
-        return 1;
+	clientnode = clientnode;
+	clientkey = clientkey;
+	return 1;
 }
+
 /*--------------------------------------------------------- */

+ 28 - 31
lub/bintree/bintree_findlast.c

@@ -37,31 +37,31 @@
 static lub_bintree_compare_fn compareright;
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_findlast(lub_bintree_t *this)
+void *lub_bintree_findlast(lub_bintree_t * this)
 {
-        lub_bintree_compare_fn *client_compare = this->compareFn;
-        
-        /*
-         * put dummy functions in place
-         * This will make the search faster and direct it to the right most
-         * node
-         */
-        this->compareFn = compareright;
-        
-        /*
-         * the key doesn't matter here cos we've cobbled the compare function
-         */
-        this->root = lub_bintree_splay(this,this->root,NULL);
-        
-        /* restore the client functions */
-        this->compareFn = client_compare;
-        
-        if(NULL == this->root)
-                return NULL;
-        else
-                return lub_bintree_getclientnode(this,this->root);
+	lub_bintree_compare_fn *client_compare = this->compareFn;
+
+	/*
+	 * put dummy functions in place
+	 * This will make the search faster and direct it to the right most
+	 * node
+	 */
+	this->compareFn = compareright;
+
+	/*
+	 * the key doesn't matter here cos we've cobbled the compare function
+	 */
+	this->root = lub_bintree_splay(this, this->root, NULL);
+
+	/* restore the client functions */
+	this->compareFn = client_compare;
+
+	if (NULL == this->root)
+		return NULL;
+	else
+		return lub_bintree_getclientnode(this, this->root);
 }
+
 /*--------------------------------------------------------- */
 /*
  * This comparison operation always returns -1 hence will force a
@@ -70,14 +70,11 @@ lub_bintree_findlast(lub_bintree_t *this)
  * key1 - first node to compare (not used)
  * key2 - second node to compare (not used)
  */
-static int
-compareright(const void *clientnode,
-             const void *clientkey)
+static int compareright(const void *clientnode, const void *clientkey)
 {
-        clientnode = clientnode;
-        clientkey = clientkey;
-        return -1;
+	clientnode = clientnode;
+	clientkey = clientkey;
+	return -1;
 }
-/*--------------------------------------------------------- */
-
 
+/*--------------------------------------------------------- */

+ 26 - 31
lub/bintree/bintree_findnext.c

@@ -35,38 +35,33 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_findnext(lub_bintree_t *this,
-                     const void    *clientkey)
+void *lub_bintree_findnext(lub_bintree_t * this, const void *clientkey)
 {
-    lub_bintree_node_t *t = this->root;
-    int                 comp;
-    
-    /*
-     * have a look for a direct match
-     */
-    t = this->root = lub_bintree_splay(this,t,clientkey);
+	lub_bintree_node_t *t = this->root;
+	int comp;
 
-    if(NULL != t)
-    {
-        /* now look at what we have got */
-        comp = lub_bintree_compare(this,t,clientkey);
-        if(comp <= 0)
-        {
-            /*
-             * time to fiddle with the right hand side of the tree
-             * we need the closest node from the right hand side
-             */
-            t = t->right = lub_bintree_splay(this,t->right,clientkey);
-        }
-    }
-    if(NULL == t)
-    {
-        return NULL;
-    }
-    else
-    {
-        return lub_bintree_getclientnode(this,t);
-    }
+	/*
+	 * have a look for a direct match
+	 */
+	t = this->root = lub_bintree_splay(this, t, clientkey);
+
+	if (NULL != t) {
+		/* now look at what we have got */
+		comp = lub_bintree_compare(this, t, clientkey);
+		if (comp <= 0) {
+			/*
+			 * time to fiddle with the right hand side of the tree
+			 * we need the closest node from the right hand side
+			 */
+			t = t->right =
+			    lub_bintree_splay(this, t->right, clientkey);
+		}
+	}
+	if (NULL == t) {
+		return NULL;
+	} else {
+		return lub_bintree_getclientnode(this, t);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 26 - 31
lub/bintree/bintree_findprevious.c

@@ -35,38 +35,33 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_findprevious(lub_bintree_t *this,
-                         const void    *clientkey)
+void *lub_bintree_findprevious(lub_bintree_t * this, const void *clientkey)
 {
-    lub_bintree_node_t *t = this->root;
-    int                 comp;
+	lub_bintree_node_t *t = this->root;
+	int comp;
 
-    /*
-     * have a look for a direct match
-     */
-    t = this->root = lub_bintree_splay(this,t,clientkey);
-        
-    if(NULL != t)
-    {
-        /* now look at what we have got */
-        comp = lub_bintree_compare(this,t,clientkey);
-        if(comp >= 0)
-        {
-            /*
-             * time to fiddle with the left hand side of the tree
-             * we need the closest node from the left hand side
-             */
-            t = t->left = lub_bintree_splay(this,t->left,clientkey);
-        }
-    }
-    if(NULL == t)
-    {
-        return NULL;
-    }
-    else
-    {
-        return lub_bintree_getclientnode(this,t);
-    }
+	/*
+	 * have a look for a direct match
+	 */
+	t = this->root = lub_bintree_splay(this, t, clientkey);
+
+	if (NULL != t) {
+		/* now look at what we have got */
+		comp = lub_bintree_compare(this, t, clientkey);
+		if (comp >= 0) {
+			/*
+			 * time to fiddle with the left hand side of the tree
+			 * we need the closest node from the left hand side
+			 */
+			t = t->left =
+			    lub_bintree_splay(this, t->left, clientkey);
+		}
+	}
+	if (NULL == t) {
+		return NULL;
+	} else {
+		return lub_bintree_getclientnode(this, t);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 9 - 8
lub/bintree/bintree_init.c

@@ -38,14 +38,15 @@
 
 /*--------------------------------------------------------- */
 void
-lub_bintree_init(lub_bintree_t         *this,
-                 size_t                 node_offset,
-                 lub_bintree_compare_fn compareFn,
-                 lub_bintree_getkey_fn  getkeyFn)
+lub_bintree_init(lub_bintree_t * this,
+		 size_t node_offset,
+		 lub_bintree_compare_fn compareFn,
+		 lub_bintree_getkey_fn getkeyFn)
 {
-        this->root          = NULL;
-        this->node_offset   = node_offset;
-        this->compareFn     = compareFn;
-        this->getkeyFn      = getkeyFn;
+	this->root = NULL;
+	this->node_offset = node_offset;
+	this->compareFn = compareFn;
+	this->getkeyFn = getkeyFn;
 }
+
 /*--------------------------------------------------------- */

+ 50 - 61
lub/bintree/bintree_insert.c

@@ -42,73 +42,62 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-int
-lub_bintree_insert(lub_bintree_t *this,
-                   void          *clientnode)
+int lub_bintree_insert(lub_bintree_t * this, void *clientnode)
 {
-    int                 result = -1;
-    lub_bintree_node_t *new;
-    lub_bintree_key_t   key;
+	int result = -1;
+	lub_bintree_node_t *new;
+	lub_bintree_key_t key;
 
-    assert(clientnode);
-    if( NULL != clientnode)
-    {
-        /* obtain the control block from the clientnode */
-        new = lub_bintree_getnode(this,clientnode);
+	assert(clientnode);
+	if (NULL != clientnode) {
+		/* obtain the control block from the clientnode */
+		new = lub_bintree_getnode(this, clientnode);
 
-        /* check this node is not currently in another tree */
-        assert(new->left == NULL);
-        assert(new->right == NULL);
+		/* check this node is not currently in another tree */
+		assert(new->left == NULL);
+		assert(new->right == NULL);
 
-        /* add this node to the splay tree */
+		/* add this node to the splay tree */
 
-        /* Insert "node" into the tree , unless it's already there. */
-        if (NULL == this->root) 
-        {
-            this->root = new;
-            this->root->left = this->root->right = NULL;
-            result = 0;
-        }
-        else
-        {
-            int comp;
-            /* get a key from the node */
-            this->getkeyFn(clientnode,&key);
+		/* Insert "node" into the tree , unless it's already there. */
+		if (NULL == this->root) {
+			this->root = new;
+			this->root->left = this->root->right = NULL;
+			result = 0;
+		} else {
+			int comp;
+			/* get a key from the node */
+			this->getkeyFn(clientnode, &key);
 
-            /* do the biz */
-            this->root = lub_bintree_splay(this,this->root,&key);
+			/* do the biz */
+			this->root = lub_bintree_splay(this, this->root, &key);
 
-            /*
-             * compare the new key with the detail found
-             * in the tree
-             */
-            comp = lub_bintree_compare(this,this->root,&key);
-            if (comp > 0)
-            {
-                new->left        = this->root->left;
-                new->right       = this->root;
-                this->root->left = NULL;
-                result           = 0;
-            }
-            else if (comp < 0)
-            {
-                new->right        = this->root->right;
-                new->left         = this->root;
-                this->root->right = NULL;
-                result            = 0;
-            }
-            else
-            {
-                /* We get here if it's already in the tree */
-            }
-        }
-        if(0 == result)
-        {
-            /* update the tree root */
-            this->root = new;
-        }
-    }
-    /* added OK */
-    return result;
+			/*
+			 * compare the new key with the detail found
+			 * in the tree
+			 */
+			comp = lub_bintree_compare(this, this->root, &key);
+			if (comp > 0) {
+				new->left = this->root->left;
+				new->right = this->root;
+				this->root->left = NULL;
+				result = 0;
+			} else if (comp < 0) {
+				new->right = this->root->right;
+				new->left = this->root;
+				this->root->right = NULL;
+				result = 0;
+			} else {
+				/* We get here if it's already in the tree */
+			}
+		}
+		if (0 == result) {
+			/* update the tree root */
+			this->root = new;
+		}
+	}
+	/* added OK */
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 14 - 15
lub/bintree/bintree_iterator_init.c

@@ -37,21 +37,20 @@
 #define MAGIC_NUMBER 0x12345678
 /*--------------------------------------------------------- */
 void
-lub_bintree_iterator_init(lub_bintree_iterator_t *this,
-                          lub_bintree_t          *tree,
-                          const void             *clientnode)
+lub_bintree_iterator_init(lub_bintree_iterator_t * this,
+			  lub_bintree_t * tree, const void *clientnode)
 {
-    if(clientnode != NULL)
-    {
-        this->tree = tree;
-        this->key.magic = MAGIC_NUMBER;
-        /* fill out the iterator's key */
-        this->tree->getkeyFn(clientnode,&this->key);
-        /*
-         * this assert will fire if the client tries to store more than 
-         * the current storage permits
-         */
-        assert(this->key.magic == MAGIC_NUMBER);
-    }
+	if (clientnode != NULL) {
+		this->tree = tree;
+		this->key.magic = MAGIC_NUMBER;
+		/* fill out the iterator's key */
+		this->tree->getkeyFn(clientnode, &this->key);
+		/*
+		 * this assert will fire if the client tries to store more than 
+		 * the current storage permits
+		 */
+		assert(this->key.magic == MAGIC_NUMBER);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 8 - 8
lub/bintree/bintree_iterator_next.c

@@ -30,14 +30,14 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_iterator_next(lub_bintree_iterator_t *this)
+void *lub_bintree_iterator_next(lub_bintree_iterator_t * this)
 {
-        void *clientnode = lub_bintree_findnext(this->tree,&this->key);
-        
-        /* make sure that next time we've move onward */
-        lub_bintree_iterator_init(this,this->tree,clientnode);
-        
-        return clientnode;
+	void *clientnode = lub_bintree_findnext(this->tree, &this->key);
+
+	/* make sure that next time we've move onward */
+	lub_bintree_iterator_init(this, this->tree, clientnode);
+
+	return clientnode;
 }
+
 /*--------------------------------------------------------- */

+ 8 - 8
lub/bintree/bintree_iterator_previous.c

@@ -30,14 +30,14 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_bintree_iterator_previous(lub_bintree_iterator_t *this)
+void *lub_bintree_iterator_previous(lub_bintree_iterator_t * this)
 {
-        void *clientnode = lub_bintree_findprevious(this->tree,&this->key);
-        
-        /* make sure that next time we've move onward */
-        lub_bintree_iterator_init(this,this->tree,clientnode);
-        
-        return clientnode;
+	void *clientnode = lub_bintree_findprevious(this->tree, &this->key);
+
+	/* make sure that next time we've move onward */
+	lub_bintree_iterator_init(this, this->tree, clientnode);
+
+	return clientnode;
 }
+
 /*--------------------------------------------------------- */

+ 7 - 8
lub/bintree/bintree_node_init.c

@@ -32,14 +32,13 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-lub_bintree_node_init(lub_bintree_node_t *node)
+void lub_bintree_node_init(lub_bintree_node_t * node)
 {
-    assert(node);
-    if(node)
-    {
-        node->left  = NULL;
-        node->right = NULL;
-    }
+	assert(node);
+	if (node) {
+		node->left = NULL;
+		node->right = NULL;
+	}
 }
+
 /*--------------------------------------------------------- */

+ 24 - 29
lub/bintree/bintree_remove.c

@@ -47,40 +47,35 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-lub_bintree_remove(lub_bintree_t *this,
-                   void          *clientnode)
+void lub_bintree_remove(lub_bintree_t * this, void *clientnode)
 {
-    lub_bintree_node_t *x,*t;
-    lub_bintree_key_t   key;
-    int                 comp;
+	lub_bintree_node_t *x, *t;
+	lub_bintree_key_t key;
+	int comp;
 
-    /* get the key from the node */
-    this->getkeyFn(clientnode,&key);
+	/* get the key from the node */
+	this->getkeyFn(clientnode, &key);
 
-    /* bring the node in question to the root of the tree */
-    t = lub_bintree_splay(this,this->root,&key);
+	/* bring the node in question to the root of the tree */
+	t = lub_bintree_splay(this, this->root, &key);
 
-    /* check that the node was there to remove */
-    comp = lub_bintree_compare(this,t,&key);
+	/* check that the node was there to remove */
+	comp = lub_bintree_compare(this, t, &key);
 
-    assert(0 == comp);
-    if(0 == comp)
-    {
-        if (t->left == NULL)
-        {
-            x = t->right;
-        }
-        else
-        {
-            x        = lub_bintree_splay(this,t->left,&key);
-            x->right = t->right;
-        }
-        /* set the new root */
-        this->root  = x;
+	assert(0 == comp);
+	if (0 == comp) {
+		if (t->left == NULL) {
+			x = t->right;
+		} else {
+			x = lub_bintree_splay(this, t->left, &key);
+			x->right = t->right;
+		}
+		/* set the new root */
+		this->root = x;
 
-        /* re-initialise the node control block for re-use */
-        lub_bintree_node_init(lub_bintree_getnode(this,clientnode));
-    }
+		/* re-initialise the node control block for re-use */
+		lub_bintree_node_init(lub_bintree_getnode(this, clientnode));
+	}
 }
+
 /*--------------------------------------------------------- */

+ 54 - 63
lub/bintree/bintree_splay.c

@@ -71,69 +71,60 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_bintree_node_t *
-lub_bintree_splay (const lub_bintree_t *this,
-                   lub_bintree_node_t  *t,
-                   const void          *key)
+lub_bintree_node_t *lub_bintree_splay(const lub_bintree_t * this,
+				      lub_bintree_node_t * t, const void *key)
 {
-        /* Simple top down splay, not requiring "key" to be in the tree t.  */
-        /* What it does is described above.                                 */
-        lub_bintree_node_t N, *leftTreeMax, *rightTreeMin, *y;
-        int                comp;
-        
-        if (t == NULL)
-                return t;
-        N.left = N.right = NULL;
-        leftTreeMax = rightTreeMin = &N;
-        
-        for (;;)
-        {
-                comp = lub_bintree_compare(this,t,key);
-                if (comp > 0)
-                {
-                        if (t->left == NULL)
-                                break;
-                        if (lub_bintree_compare(this,t->left,key) > 0)
-                        {
-                                y        = t->left; /* rotate right */
-                                t->left  = y->right;
-                                y->right = t;
-                                t        = y;
-                                if (t->left == NULL)
-                                        break;
-                        }
-                        rightTreeMin->left = t; /* link right */
-                        rightTreeMin       = t;
-                        t                  = t->left;
-                }
-                else if (comp < 0)
-                {
-                        if (t->right == NULL)
-                                break;
-                        if (lub_bintree_compare(this,t->right,key) < 0)
-                        {
-                                y        = t->right; /* rotate left */
-                                t->right = y->left;
-                                y->left  = t;
-                                t        = y;
-                                if (t->right == NULL)
-                                        break;
-                        }
-                        leftTreeMax->right = t; /* link left */
-                        leftTreeMax        = t;
-                        t                  = t->right;
-                }
-                else
-                {
-                        break;
-                }
-        }
-        leftTreeMax->right = t->left; /* assemble */
-        rightTreeMin->left = t->right;
-        t->left            = N.right;
-        t->right           = N.left;
-        
-        /* return the new root */
-        return t;
+	/* Simple top down splay, not requiring "key" to be in the tree t.  */
+	/* What it does is described above.                                 */
+	lub_bintree_node_t N, *leftTreeMax, *rightTreeMin, *y;
+	int comp;
+
+	if (t == NULL)
+		return t;
+	N.left = N.right = NULL;
+	leftTreeMax = rightTreeMin = &N;
+
+	for (;;) {
+		comp = lub_bintree_compare(this, t, key);
+		if (comp > 0) {
+			if (t->left == NULL)
+				break;
+			if (lub_bintree_compare(this, t->left, key) > 0) {
+				y = t->left;	/* rotate right */
+				t->left = y->right;
+				y->right = t;
+				t = y;
+				if (t->left == NULL)
+					break;
+			}
+			rightTreeMin->left = t;	/* link right */
+			rightTreeMin = t;
+			t = t->left;
+		} else if (comp < 0) {
+			if (t->right == NULL)
+				break;
+			if (lub_bintree_compare(this, t->right, key) < 0) {
+				y = t->right;	/* rotate left */
+				t->right = y->left;
+				y->left = t;
+				t = y;
+				if (t->right == NULL)
+					break;
+			}
+			leftTreeMax->right = t;	/* link left */
+			leftTreeMax = t;
+			t = t->right;
+		} else {
+			break;
+		}
+	}
+	leftTreeMax->right = t->left;	/* assemble */
+	rightTreeMin->left = t->right;
+	t->left = N.right;
+	t->right = N.left;
+
+	/* return the new root */
+	return t;
 }
+
 /*--------------------------------------------------------- */

+ 4 - 5
lub/bintree/private.h

@@ -34,10 +34,9 @@
  * t    - the root node to splay to.
  * key  - the value with which to splay
  */
-extern lub_bintree_node_t *
-		lub_bintree_splay(const lub_bintree_t *tree,
-                		  lub_bintree_node_t  *t,
-                  		  const void          *key);
+extern lub_bintree_node_t *lub_bintree_splay(const lub_bintree_t * tree,
+					     lub_bintree_node_t * t,
+					     const void *key);
 /*------------------------------------------------------------ */
 /* This operation converts a "node" into a "clientnode"
  * subtracting the offset gives the base pointer to the node
@@ -55,7 +54,7 @@ extern lub_bintree_node_t *
  * clientnode - the clientnode to convert
  */
 #define lub_bintree_getnode(this,clientnode)\
-(lub_bintree_node_t *)(((char*)clientnode) + this->node_offset) /*lint -e826 */
+(lub_bintree_node_t *)(((char*)clientnode) + this->node_offset)	/*lint -e826 */
 /*------------------------------------------------------------ */
 /* This operation compares a key with a "node"
  * it returns

+ 47 - 58
lub/blockpool.h

@@ -45,89 +45,87 @@ typedef struct _lub_blockpool_block lub_blockpool_block_t;
  * This type represents a "blockpool" instance.
  */
 typedef struct _lub_blockpool lub_blockpool_t;
-struct _lub_blockpool
-{
-    /* CLIENTS MUSTN'T TOUCH THESE DETAILS */
-    lub_blockpool_block_t *m_head;
-    lub_blockpool_block_t *m_tail;
-    size_t                 m_block_size;
-    size_t                 m_num_blocks;
-    unsigned               m_alloc_blocks;
-    unsigned               m_alloc_total_blocks;
-    unsigned               m_alloc_hightide_blocks;
-    unsigned               m_alloc_failures;
+struct _lub_blockpool {
+	/* CLIENTS MUSTN'T TOUCH THESE DETAILS */
+	lub_blockpool_block_t *m_head;
+	lub_blockpool_block_t *m_tail;
+	size_t m_block_size;
+	size_t m_num_blocks;
+	unsigned m_alloc_blocks;
+	unsigned m_alloc_total_blocks;
+	unsigned m_alloc_hightide_blocks;
+	unsigned m_alloc_failures;
 };
 /**
  * This type defines the statistics available for each blockpool.
  */
 typedef struct _lub_blockpool_stats lub_blockpool_stats_t;
-struct _lub_blockpool_stats
-{
+struct _lub_blockpool_stats {
     /*----------------------------------------------------- */
     /**
      * NUmber of bytes in each block.
      */
-    size_t block_size;
+	size_t block_size;
     /**
      * Total number of blocks in this pool.
      */
-    size_t num_blocks;
+	size_t num_blocks;
     /*----------------------------------------------------- */
     /**
      * Number of dynamically allocated blocks currently
      * held by clients of a blockpool.
      */
-    size_t alloc_blocks;
+	size_t alloc_blocks;
     /**
      * Number of dynamically allocated bytes currently
      * held by clients of a blockpool.
      */
-    size_t alloc_bytes;
+	size_t alloc_bytes;
     /**
      * Number of free blocks.
      */
-    size_t free_blocks;
+	size_t free_blocks;
     /**
      * Number of bytes available in a blockpool.
      */
-    size_t free_bytes;
+	size_t free_bytes;
     /*----------------------------------------------------- */
     /**
      * Cumulative number of dynamically allocated blocks
      * given to clients of a blockpool.
      */
-    size_t alloc_total_blocks;
+	size_t alloc_total_blocks;
     /**
      * Cumulative number of dynamically allocated bytes
      * given to clients of a blockpool.
      */
-    size_t alloc_total_bytes;
+	size_t alloc_total_bytes;
     /*----------------------------------------------------- */
     /**
      * Number of dynamically allocated blocks
      * given to clients when the memory usage was at it's highest.
      */
-    size_t alloc_hightide_blocks;
+	size_t alloc_hightide_blocks;
     /**
      * Number of dynamically allocated bytes
      * given to clients of a blockpool when the memory usage was at it's
      * highest
      */
-    size_t alloc_hightide_bytes;
+	size_t alloc_hightide_bytes;
     /**
      * Number of free blocks when the memory usage was at it's
      * highest
      */
-    size_t free_hightide_blocks;
+	size_t free_hightide_blocks;
     /**
      * Number of free bytes when the memory usage was at it's highest.
      */
-    size_t free_hightide_bytes;
+	size_t free_hightide_bytes;
     /*----------------------------------------------------- */
     /**
      * Number of time an allocation has failed from this block
      */
-    size_t alloc_failures;
+	size_t alloc_failures;
     /*----------------------------------------------------- */
 };
 /****************************************************************
@@ -144,26 +142,24 @@ struct _lub_blockpool_stats
  * \post If the size constraint is not met an assert will fire.
  * \post Following initialisation the allocation of memory can be performed.
  */
-extern void
-    lub_blockpool_init(
-        /** 
+extern void lub_blockpool_init(
+	/** 
          * the "blockpool" instance to initialise.
          */
-        lub_blockpool_t *blockpool,
-        /** 
+				      lub_blockpool_t * blockpool,
+	/** 
          * the memory to be managed.
          */
-        void *memory,
-        /**
+				      void *memory,
+	/**
          * The size in bytes of each block.
          */
-        size_t blocksize,
-        /** The number of blocks to be managed. NB the client is
+				      size_t blocksize,
+	/** The number of blocks to be managed. NB the client is
          * responsible for ensuring that (blocksize x blockcount)
          * bytes of memory are available for use.
          */
-        unsigned blockcount
-    );
+				      unsigned blockcount);
 
 /**
  * This operation allocates a "block" of memory from a "blockpool"
@@ -179,13 +175,11 @@ extern void
  * \post
  * The behaviour is undefined if the "blockpool" is uninitialised.
  */
-extern void *
-    lub_blockpool_alloc(
-        /** 
+extern void *lub_blockpool_alloc(
+	/** 
          * the "blockpool" instance to invoke this operation upon
          */
-        lub_blockpool_t *blockpool
-    );
+					lub_blockpool_t * blockpool);
 
 /**
  * This operation de-allocates a "block" of memory back into a "blockpool"
@@ -198,17 +192,15 @@ extern void *
  * The de-allocated block become available for subsequent
  * lub_blockpool_alloc() requests.
  */
-extern void
-    lub_blockpool_free(
-        /** 
+extern void lub_blockpool_free(
+	/** 
          * the "blockpool" instance to invoke this operation upon
          */
-        lub_blockpool_t *blockpool,
-        /** 
+				      lub_blockpool_t * blockpool,
+	/** 
          * the "block" to release back to the pool
          */
-        void *block
-    );
+				      void *block);
 /**
  * This operation fills out a statistics structure with the details for the 
  * specified blockpool.
@@ -220,18 +212,15 @@ extern void
  * - the results filled out are a snapshot of the statistics as the time
  *   of the call.
  */
-void
-    lub_blockpool__get_stats(
-        /**
+void lub_blockpool__get_stats(
+	/**
          * The instance on which to operate
          */
-        lub_blockpool_t *instance,
-        /**
+				     lub_blockpool_t * instance,
+	/**
          * A client provided structure to fill out with the blockpool details
          */
-        lub_blockpool_stats_t *stats
-    );
+				     lub_blockpool_stats_t * stats);
 
-#endif /* _lub_blockpool_h */
+#endif				/* _lub_blockpool_h */
 /** @} blockpool */
-

+ 19 - 15
lub/blockpool/blockpool__get_stats.c

@@ -5,21 +5,25 @@
 
 /*--------------------------------------------------------- */
 void
-lub_blockpool__get_stats(lub_blockpool_t *this,
-                         lub_blockpool_stats_t *stats)
+lub_blockpool__get_stats(lub_blockpool_t * this, lub_blockpool_stats_t * stats)
 {
-    stats->block_size            = this->m_block_size;
-    stats->num_blocks            = this->m_num_blocks;
-    stats->alloc_blocks          = this->m_alloc_blocks;
-    stats->alloc_bytes           = stats->alloc_blocks * stats->block_size;
-    stats->free_blocks           = stats->num_blocks - stats->alloc_blocks;
-    stats->free_bytes            = stats->free_blocks * stats->block_size;
-    stats->alloc_total_blocks    = this->m_alloc_total_blocks;
-    stats->alloc_total_bytes     = stats->alloc_total_blocks * stats->block_size;
-    stats->alloc_hightide_blocks = this->m_alloc_hightide_blocks;
-    stats->alloc_hightide_bytes  = stats->alloc_hightide_blocks * stats->block_size;
-    stats->free_hightide_blocks  = stats->num_blocks - stats->alloc_hightide_blocks;
-    stats->free_hightide_bytes   = stats->free_hightide_blocks * stats->block_size;
-    stats->alloc_failures        = this->m_alloc_failures;
+	stats->block_size = this->m_block_size;
+	stats->num_blocks = this->m_num_blocks;
+	stats->alloc_blocks = this->m_alloc_blocks;
+	stats->alloc_bytes = stats->alloc_blocks * stats->block_size;
+	stats->free_blocks = stats->num_blocks - stats->alloc_blocks;
+	stats->free_bytes = stats->free_blocks * stats->block_size;
+	stats->alloc_total_blocks = this->m_alloc_total_blocks;
+	stats->alloc_total_bytes =
+	    stats->alloc_total_blocks * stats->block_size;
+	stats->alloc_hightide_blocks = this->m_alloc_hightide_blocks;
+	stats->alloc_hightide_bytes =
+	    stats->alloc_hightide_blocks * stats->block_size;
+	stats->free_hightide_blocks =
+	    stats->num_blocks - stats->alloc_hightide_blocks;
+	stats->free_hightide_bytes =
+	    stats->free_hightide_blocks * stats->block_size;
+	stats->alloc_failures = this->m_alloc_failures;
 }
+
 /*--------------------------------------------------------- */

+ 21 - 28
lub/blockpool/blockpool_alloc.c

@@ -44,34 +44,27 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_blockpool_alloc(lub_blockpool_t *this)
+void *lub_blockpool_alloc(lub_blockpool_t * this)
 {
-    lub_blockpool_block_t *newblock = this->m_head;
-    if(newblock)
-    {
-        if(newblock == this->m_tail)
-        {
-            /* remove the last item from the pool */
-            this->m_head = NULL;
-            this->m_tail = NULL;
-        }
-        else
-        {
-            this->m_head = newblock->next;
-        }
-        /* updated the stats */
-        ++this->m_alloc_blocks;
-        ++this->m_alloc_total_blocks;
-        if(this->m_alloc_blocks > this->m_alloc_hightide_blocks)
-        {
-            this->m_alloc_hightide_blocks = this->m_alloc_blocks;
-        }
-    }
-    else
-    {
-        ++this->m_alloc_failures;
-    }
-    return newblock;
+	lub_blockpool_block_t *newblock = this->m_head;
+	if (newblock) {
+		if (newblock == this->m_tail) {
+			/* remove the last item from the pool */
+			this->m_head = NULL;
+			this->m_tail = NULL;
+		} else {
+			this->m_head = newblock->next;
+		}
+		/* updated the stats */
+		++this->m_alloc_blocks;
+		++this->m_alloc_total_blocks;
+		if (this->m_alloc_blocks > this->m_alloc_hightide_blocks) {
+			this->m_alloc_hightide_blocks = this->m_alloc_blocks;
+		}
+	} else {
+		++this->m_alloc_failures;
+	}
+	return newblock;
 }
+
 /*--------------------------------------------------------- */

+ 15 - 19
lub/blockpool/blockpool_free.c

@@ -36,27 +36,23 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-lub_blockpool_free(lub_blockpool_t *this,
-                   void            *block)
+void lub_blockpool_free(lub_blockpool_t * this, void *block)
 {
-    lub_blockpool_block_t *newfree = block;
+	lub_blockpool_block_t *newfree = block;
 
-    /* simply add this block to the end of the free list */
-    newfree->next = NULL;
-    if(NULL != this->m_tail)
-    {
-        this->m_tail->next = newfree;
-    }
-    else
-    {
-        /* first entry in the list */
-        this->m_head = newfree;
-    }
-    /* add to end of list */
-    this->m_tail = newfree;
+	/* simply add this block to the end of the free list */
+	newfree->next = NULL;
+	if (NULL != this->m_tail) {
+		this->m_tail->next = newfree;
+	} else {
+		/* first entry in the list */
+		this->m_head = newfree;
+	}
+	/* add to end of list */
+	this->m_tail = newfree;
 
-    /* updated the stats */
-    --this->m_alloc_blocks;
+	/* updated the stats */
+	--this->m_alloc_blocks;
 }
+
 /*--------------------------------------------------------- */

+ 23 - 27
lub/blockpool/blockpool_init.c

@@ -44,34 +44,30 @@
 
 /*--------------------------------------------------------- */
 void
-lub_blockpool_init(lub_blockpool_t *this,
-                   void            *memory,
-                   size_t           blocksize,
-                   unsigned         blockcount)
+lub_blockpool_init(lub_blockpool_t * this,
+		   void *memory, size_t blocksize, unsigned blockcount)
 {
-        unsigned i;
-        char    *ptr = memory;
-        
-        /* check that this is a multiple of sizeof(void*) */
-        assert((blocksize & (sizeof(void*)-1)) == 0);
+	unsigned i;
+	char *ptr = memory;
 
-        /* start off with nothing in the list */
-        this->m_head = this->m_tail         = NULL;
-    
-        /* run through all the blocks placing them into the free list */
-        for(i = 0;
-            i < blockcount;
-            ++i)
-        {
-                lub_blockpool_free(this,ptr);
-                ptr += blocksize;
-        }
-        /* intialise the stats */
-        this->m_block_size            = blocksize;
-        this->m_num_blocks            = blockcount;
-        this->m_alloc_blocks          = 0;
-        this->m_alloc_total_blocks    = 0;
-        this->m_alloc_hightide_blocks = 0;
-        this->m_alloc_failures        = 0;
+	/* check that this is a multiple of sizeof(void*) */
+	assert((blocksize & (sizeof(void *) - 1)) == 0);
+
+	/* start off with nothing in the list */
+	this->m_head = this->m_tail = NULL;
+
+	/* run through all the blocks placing them into the free list */
+	for (i = 0; i < blockcount; ++i) {
+		lub_blockpool_free(this, ptr);
+		ptr += blocksize;
+	}
+	/* intialise the stats */
+	this->m_block_size = blocksize;
+	this->m_num_blocks = blockcount;
+	this->m_alloc_blocks = 0;
+	this->m_alloc_total_blocks = 0;
+	this->m_alloc_hightide_blocks = 0;
+	this->m_alloc_failures = 0;
 }
+
 /*--------------------------------------------------------- */

+ 2 - 3
lub/blockpool/private.h

@@ -21,9 +21,8 @@
  **************************************************************** */
 #include "lub/blockpool.h"
 
-struct _lub_blockpool_block
-{
-    lub_blockpool_block_t *next;
+struct _lub_blockpool_block {
+	lub_blockpool_block_t *next;
 };
 /*************************************************************
  * PRIVATE OPERATIONS

+ 6 - 6
lub/c_decl.h

@@ -34,14 +34,14 @@ _END_C_DECL
 
 #ifndef _lub_c_decl_h
 #define _lub_c_decl_h
- 
+
 #ifdef __cplusplus
 #define _BEGIN_C_DECL extern "C" {
 #define _END_C_DECL   }
-#else  /* not __cplusplus */
-#define _BEGIN_C_DECL 
-#define _END_C_DECL   
-#endif /* not __cplusplus */
+#else				/* not __cplusplus */
+#define _BEGIN_C_DECL
+#define _END_C_DECL
+#endif				/* not __cplusplus */
 
 /** @} */
-#endif /* _lub_c_decl_h */
+#endif				/* _lub_c_decl_h */

+ 13 - 24
lub/ctype.h

@@ -40,7 +40,6 @@ basis.
 
 #include "lub/c_decl.h"
 _BEGIN_C_DECL
-
 /**
  * This operation identifies whether a character is a decimal digit
  * or not.
@@ -55,13 +54,11 @@ _BEGIN_C_DECL
  * \post 
  * - none
  */
-bool_t
-    lub_ctype_isdigit(
-        /** 
+bool_t lub_ctype_isdigit(
+	/** 
          * The character to check
          */
-        char c
-    );
+				char c);
 /**
  * This operation identifies whether a character is a standard white space
  * character. (space, tab, carriage-return, vertical tab, form-feed)
@@ -76,13 +73,11 @@ bool_t
  * \post 
  * - none
  */
-bool_t
-    lub_ctype_isspace(
-        /** 
+bool_t lub_ctype_isspace(
+	/** 
          * The character to check
          */
-        char c
-    );
+				char c);
 /**
  * This operation converts an uppercase letter to the corresponding 
  * lowercase letter.
@@ -99,13 +94,11 @@ bool_t
  * \post 
  * - none
  */
-char
-    lub_ctype_tolower(
-        /** 
+char lub_ctype_tolower(
+	/** 
          * The character to convert
          */
-        char c
-    );
+			      char c);
 /**
  * This operation converts a lowercase letter to the corresponding 
  * uppercase letter.
@@ -122,16 +115,12 @@ char
  * \post 
  * - none
  */
-char
-    lub_ctype_toupper(
-        /** 
+char lub_ctype_toupper(
+	/** 
          * The character to convert
          */
-        char c
-    );
+			      char c);
 
 _END_C_DECL
-
-#endif /* _lub_ctype_h */
+#endif				/* _lub_ctype_h */
 /** @} */
-

+ 4 - 4
lub/ctype/ctype_isdigit.c

@@ -5,10 +5,10 @@
 #include <ctype.h>
 
 /*--------------------------------------------------------- */
-bool_t
-lub_ctype_isdigit(char c)
+bool_t lub_ctype_isdigit(char c)
 {
-    unsigned char tmp = (unsigned char)c;
-    return isdigit(tmp) ? BOOL_TRUE : BOOL_FALSE; 
+	unsigned char tmp = (unsigned char)c;
+	return isdigit(tmp) ? BOOL_TRUE : BOOL_FALSE;
 }
+
 /*--------------------------------------------------------- */

+ 4 - 4
lub/ctype/ctype_isspace.c

@@ -5,10 +5,10 @@
 #include <ctype.h>
 
 /*--------------------------------------------------------- */
-bool_t
-lub_ctype_isspace(char c)
+bool_t lub_ctype_isspace(char c)
 {
-    unsigned char tmp = (unsigned char)c;
-    return isspace(tmp) ? BOOL_TRUE : BOOL_FALSE; 
+	unsigned char tmp = (unsigned char)c;
+	return isspace(tmp) ? BOOL_TRUE : BOOL_FALSE;
 }
+
 /*--------------------------------------------------------- */

+ 4 - 4
lub/ctype/ctype_tolower.c

@@ -5,10 +5,10 @@
 #include <ctype.h>
 
 /*--------------------------------------------------------- */
-char
-lub_ctype_tolower(char c)
+char lub_ctype_tolower(char c)
 {
-    unsigned char tmp = (unsigned char)c;
-    return tolower(tmp); 
+	unsigned char tmp = (unsigned char)c;
+	return tolower(tmp);
 }
+
 /*--------------------------------------------------------- */

+ 4 - 4
lub/ctype/ctype_toupper.c

@@ -5,10 +5,10 @@
 #include <ctype.h>
 
 /*--------------------------------------------------------- */
-char
-lub_ctype_toupper(char c)
+char lub_ctype_toupper(char c)
 {
-    unsigned char tmp = (unsigned char)c;
-    return toupper(tmp); 
+	unsigned char tmp = (unsigned char)c;
+	return toupper(tmp);
 }
+
 /*--------------------------------------------------------- */

+ 26 - 37
lub/dblockpool.h

@@ -35,7 +35,6 @@ limits on re-use.
 #include "c_decl.h"
 
 _BEGIN_C_DECL
-
 /****************************************************************
  * TYPE DEFINITIONS
  **************************************************************** */
@@ -46,12 +45,11 @@ typedef struct _lub_dblockpool lub_dblockpool_t;
 /**
  * CLIENTS MUST NOT USE THESE FIELDS DIRECTLY
  */
-struct _lub_dblockpool
-{
-    struct _lub_dblockpool_chunk *first_chunk;
-    size_t                        block_size;
-    unsigned                      chunk_size;
-    unsigned                      max_chunks;
+struct _lub_dblockpool {
+	struct _lub_dblockpool_chunk *first_chunk;
+	size_t block_size;
+	unsigned chunk_size;
+	unsigned max_chunks;
 };
 
 /****************************************************************
@@ -67,26 +65,24 @@ struct _lub_dblockpool
  * \post If the size constraint is not met an assert will fire.
  * \post Following initialisation the allocation of memory can be performed.
  */
-extern void 
-    lub_dblockpool_init(
-        /** 
+extern void lub_dblockpool_init(
+	/** 
          * the "dblockpool" instance to initialise.
          */
-        lub_dblockpool_t *instance,
-        /**
+				       lub_dblockpool_t * instance,
+	/**
          * The size in bytes of each block.
          */
-        size_t blocksize,
-        /** 
+				       size_t blocksize,
+	/** 
          * The number of blocks to be managed in a chunk.
          */
-        unsigned chunksize,
-        /** 
+				       unsigned chunksize,
+	/** 
          * The maximum number of chunks to be allocated.
          * (a value of zero means unlimited...)
          */
-        unsigned max_chunks
-    );
+				       unsigned max_chunks);
 /**
  * This operation finalises an instance of a dblockpool.
  *
@@ -94,13 +90,11 @@ extern void
  *
  * \post All the dynamic memory allocated will be released.
  */
-extern void 
-    lub_dblockpool_fini(
-        /** 
+extern void lub_dblockpool_fini(
+	/** 
          * the "dblockpool" instance to finalise.
          */
-        lub_dblockpool_t *instance
-    );
+				       lub_dblockpool_t * instance);
 /**
  * This operation allocates a "block" of memory from a "dblockpool"
  *
@@ -115,13 +109,11 @@ extern void
  * \post
  * The behaviour is undefined if the "dblockpool" is uninitialised.
  */
-extern void *
-    lub_dblockpool_alloc(
-        /** 
+extern void *lub_dblockpool_alloc(
+	/** 
          * the "dblockpool" instance to operate on
          */
-        lub_dblockpool_t *instance
-    );
+					 lub_dblockpool_t * instance);
 /**
  * This operation de-allocates a "block" of memory back into a "blockpool"
  *
@@ -133,19 +125,16 @@ extern void *
  * The de-allocated block become available for subsequent
  * lub_blockpool_alloc() requests.
  */
-extern void
-    lub_dblockpool_free(
-        /** 
+extern void lub_dblockpool_free(
+	/** 
          * the "blockpool" instance to invoke this operation upon
          */
-        lub_dblockpool_t *instance,
-        /** 
+				       lub_dblockpool_t * instance,
+	/** 
          * the "block" to release back to the pool
          */
-        void *block
-    );
+				       void *block);
 
 _END_C_DECL
-
-#endif /* _lub_dblockpool_h */
+#endif				/* _lub_dblockpool_h */
 /** @} lub_dblockpool */

+ 40 - 46
lub/dblockpool/dblockpool_alloc.c

@@ -3,52 +3,46 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_dblockpool_alloc(lub_dblockpool_t *this)
+void *lub_dblockpool_alloc(lub_dblockpool_t * this)
 {
-    void                    *result = NULL;
-    lub_dblockpool_chunk_t *chunk;
-    unsigned                chunk_count = 0;
-    
-    /* first find a chunk which can service this request */
-    for(chunk = this->first_chunk;
-        chunk;
-        chunk = chunk->next)
-    {
-        chunk_count++;
-        /* try and get a block from this chunk */
-        result = lub_blockpool_alloc(&chunk->pool);
-        if(NULL != result)
-        {
-            /* got some memory */
-            break;
-        }
-    }
-    if( (NULL == result) 
-        && (!this->max_chunks || (chunk_count < this->max_chunks)) )
-    {
-        /* dynamically allocate a new chunk */
-        chunk = malloc(sizeof(lub_dblockpool_chunk_t) + (this->block_size * this->chunk_size));
-        if(NULL != chunk)
-        {
-            /* configure the new chunk */
-            chunk->next = this->first_chunk;
-            lub_blockpool_init(&chunk->pool,
-                               &chunk[1],
-                               this->block_size,
-                               this->chunk_size);
-            this->first_chunk = chunk;
-            chunk->count      = 0;
-            
-            /* now allocate the memory */
-            result = lub_blockpool_alloc(&chunk->pool);
-        }
-    }
-    if((NULL != result) && (NULL != chunk))
-    {
-        /* track the number of allocations */
-        chunk->count++;
-    }
-    return result;
+	void *result = NULL;
+	lub_dblockpool_chunk_t *chunk;
+	unsigned chunk_count = 0;
+
+	/* first find a chunk which can service this request */
+	for (chunk = this->first_chunk; chunk; chunk = chunk->next) {
+		chunk_count++;
+		/* try and get a block from this chunk */
+		result = lub_blockpool_alloc(&chunk->pool);
+		if (NULL != result) {
+			/* got some memory */
+			break;
+		}
+	}
+	if ((NULL == result)
+	    && (!this->max_chunks || (chunk_count < this->max_chunks))) {
+		/* dynamically allocate a new chunk */
+		chunk =
+		    malloc(sizeof(lub_dblockpool_chunk_t) +
+			   (this->block_size * this->chunk_size));
+		if (NULL != chunk) {
+			/* configure the new chunk */
+			chunk->next = this->first_chunk;
+			lub_blockpool_init(&chunk->pool,
+					   &chunk[1],
+					   this->block_size, this->chunk_size);
+			this->first_chunk = chunk;
+			chunk->count = 0;
+
+			/* now allocate the memory */
+			result = lub_blockpool_alloc(&chunk->pool);
+		}
+	}
+	if ((NULL != result) && (NULL != chunk)) {
+		/* track the number of allocations */
+		chunk->count++;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 9 - 10
lub/dblockpool/dblockpool_fini.c

@@ -3,16 +3,15 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-lub_dblockpool_fini(lub_dblockpool_t *this)
+void lub_dblockpool_fini(lub_dblockpool_t * this)
 {
-    lub_dblockpool_chunk_t *chunk;
-    
-    /* release all the memory used for contexts */
-    while((chunk = this->first_chunk))
-    {
-        this->first_chunk = chunk->next;
-        free(chunk);
-    }
+	lub_dblockpool_chunk_t *chunk;
+
+	/* release all the memory used for contexts */
+	while ((chunk = this->first_chunk)) {
+		this->first_chunk = chunk->next;
+		free(chunk);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 26 - 30
lub/dblockpool/dblockpool_free.c

@@ -3,36 +3,32 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-lub_dblockpool_free(lub_dblockpool_t *this,
-                    void             *block)
+void lub_dblockpool_free(lub_dblockpool_t * this, void *block)
 {
-    lub_dblockpool_chunk_t **chunk_ptr;
-    
-    /* find the chunk from which this derived. */
-    for(chunk_ptr = &this->first_chunk;
-        *chunk_ptr;
-        chunk_ptr = &(*chunk_ptr)->next)
-    {
-        const char *pool_start = (char *)&(*chunk_ptr)[1];
-        const char *pool_end   = pool_start + (this->block_size * this->chunk_size);
-        const char *ptr = block;
-        
-        if((ptr >= pool_start) && (ptr < pool_end))
-        {
-            /* found the right pool */
-            lub_blockpool_free(&(*chunk_ptr)->pool,block);
-            (*chunk_ptr)->count--;
-            /* track the number of allocations */
-            if(0 == (*chunk_ptr)->count)
-            {
-                lub_dblockpool_chunk_t *tmp = *chunk_ptr;
-                /* removing last block from this chunk */
-                *chunk_ptr = (*chunk_ptr)->next;
-                free(tmp);
-            }
-            break;
-        }
-    }
+	lub_dblockpool_chunk_t **chunk_ptr;
+
+	/* find the chunk from which this derived. */
+	for (chunk_ptr = &this->first_chunk;
+	     *chunk_ptr; chunk_ptr = &(*chunk_ptr)->next) {
+		const char *pool_start = (char *)&(*chunk_ptr)[1];
+		const char *pool_end =
+		    pool_start + (this->block_size * this->chunk_size);
+		const char *ptr = block;
+
+		if ((ptr >= pool_start) && (ptr < pool_end)) {
+			/* found the right pool */
+			lub_blockpool_free(&(*chunk_ptr)->pool, block);
+			(*chunk_ptr)->count--;
+			/* track the number of allocations */
+			if (0 == (*chunk_ptr)->count) {
+				lub_dblockpool_chunk_t *tmp = *chunk_ptr;
+				/* removing last block from this chunk */
+				*chunk_ptr = (*chunk_ptr)->next;
+				free(tmp);
+			}
+			break;
+		}
+	}
 }
+
 /*--------------------------------------------------------- */

+ 7 - 8
lub/dblockpool/dblockpool_init.c

@@ -2,14 +2,13 @@
 
 /*--------------------------------------------------------- */
 void
-lub_dblockpool_init(lub_dblockpool_t *this,
-                    size_t            block_size,
-                    unsigned          chunk_size,
-                    unsigned          max_chunks)
+lub_dblockpool_init(lub_dblockpool_t * this,
+		    size_t block_size, unsigned chunk_size, unsigned max_chunks)
 {
-    this->first_chunk = NULL;
-    this->block_size  = block_size;
-    this->chunk_size  = chunk_size;
-    this->max_chunks  = max_chunks;
+	this->first_chunk = NULL;
+	this->block_size = block_size;
+	this->chunk_size = chunk_size;
+	this->max_chunks = max_chunks;
 }
+
 /*--------------------------------------------------------- */

+ 4 - 5
lub/dblockpool/private.h

@@ -2,9 +2,8 @@
 #include "lub/blockpool.h"
 
 typedef struct _lub_dblockpool_chunk lub_dblockpool_chunk_t;
-struct _lub_dblockpool_chunk
-{
-    lub_dblockpool_chunk_t *next;
-    lub_blockpool_t         pool;
-    unsigned                count;
+struct _lub_dblockpool_chunk {
+	lub_dblockpool_chunk_t *next;
+	lub_blockpool_t pool;
+	unsigned count;
 };

+ 10 - 13
lub/dump.h

@@ -30,15 +30,14 @@ easy.
  *
  * \post
  * - The formatted message will be sent to stdout.
- */ 
+ */
  /*lint -esym(534,lub_dump_printf) Ignoring return value of function */
-int
-    lub_dump_printf(
-            /**
+int lub_dump_printf(
+	    /**
              * printf-like format string
              */
-            const char *fmt, ...
-        );
+			   const char *fmt, ...
+    );
 /**
  * This operation indicates that the offset for messages should be increased by
  * one level.
@@ -51,9 +50,8 @@ int
  *   in offset.
  * - Subsequent calls to lub_dump_printf() will output at this new offset.
  * - Client may call lub_undent() to restore offset.
- */ 
-void 
-    lub_dump_indent(void);
+ */
+void lub_dump_indent(void);
 /**
  * This operation indicates that the offset for messages should be decreased by
  * one level.
@@ -66,9 +64,8 @@ void
  * - An indentation divider will be sent to stdout to emphasise the change
  *   in offset.
  * - Subsequent calls to lub_dump_printf() will output at this new offset.
- */ 
-void 
-		lub_dump_undent(void);
+ */
+void lub_dump_undent(void);
 
-#endif /* _lub_dump_h */
+#endif				/* _lub_dump_h */
 /** @} lub_dump */

+ 28 - 31
lub/dump/dump.c

@@ -2,55 +2,52 @@
  * dump.c
  * Provides indented printf functionality
  */
- #include "private.h"
- 
+#include "private.h"
+
 #include <stdio.h>
 #include <stdarg.h>
 
 static int indent = 0;
 
 /*--------------------------------------------------------- */
-int
-lub_dump_printf(const char *fmt,...)
+int lub_dump_printf(const char *fmt, ...)
 {
-    va_list args;
-    int len;
+	va_list args;
+	int len;
 
-    va_start(args, fmt);
-    fprintf(stderr, "%*s", indent, "");
-    len = vfprintf(stderr, fmt, args);
-    va_end(args);
+	va_start(args, fmt);
+	fprintf(stderr, "%*s", indent, "");
+	len = vfprintf(stderr, fmt, args);
+	va_end(args);
 
-    return len;
+	return len;
 }
+
 /*--------------------------------------------------------- */
-static void 
-lub_dump_divider(char c)
+static void lub_dump_divider(char c)
 {
-    int i;
-
-    lub_dump_printf("");
-    for(i = 0;
-        i < (80 - indent);
-        i++)
-    {
-        fputc(c, stderr);
-    }
-    fputc('\n', stderr);
+	int i;
+
+	lub_dump_printf("");
+	for (i = 0; i < (80 - indent); i++) {
+		fputc(c, stderr);
+	}
+	fputc('\n', stderr);
 }
+
 /*--------------------------------------------------------- */
-void 
-lub_dump_indent(void)
+void lub_dump_indent(void)
 {
-    indent += 2;
-    lub_dump_divider('_');	
+	indent += 2;
+	lub_dump_divider('_');
 }
+
 /*--------------------------------------------------------- */
-void 
-lub_dump_undent(void)
+void lub_dump_undent(void)
 {
-    lub_dump_divider('^');	
+	lub_dump_divider('^');
 
-    indent -= 2;
+	indent -= 2;
 }
+
 /*--------------------------------------------------------- */

+ 185 - 240
lub/heap.h

@@ -112,7 +112,6 @@ fast searching for the appropriate block.
 #include "lub/types.h"
 #include "lub/c_decl.h"
 _BEGIN_C_DECL
-
 /**
  * This type is used to reference an instance of a heap.
  */
@@ -127,302 +126,300 @@ typedef struct lub_heap_free_block_s lub_heap_free_block_t;
  * This type defines the statistics available for each heap.
  */
 typedef struct lub_heap_stats_s lub_heap_stats_t;
-struct lub_heap_stats_s
-{
+struct lub_heap_stats_s {
     /*----------------------------------------------------- */
     /**
      * Number of segments comprising this heap.
      */
-    size_t segs;
+	size_t segs;
     /**
      * Number of bytes available in all the segments.
      */
-    size_t segs_bytes;
+	size_t segs_bytes;
     /**
      * Number of bytes used in housekeeping overheads
      */
-    size_t segs_overhead;
+	size_t segs_overhead;
     /*----------------------------------------------------- */
     /**
      * Number of free blocks. This is indication 
      * the fragmentation state of a heap.
      */
-    size_t free_blocks;
+	size_t free_blocks;
     /**
      * Number of bytes available in a heap.
      */
-    size_t free_bytes;
+	size_t free_bytes;
     /**
      * Number of bytes used in housekeeping overheads
      */
-    size_t free_overhead;
+	size_t free_overhead;
     /*----------------------------------------------------- */
     /**
      * Number of dynamically allocated blocks currently
      * held by clients of a heap.
      */
-    size_t alloc_blocks;
+	size_t alloc_blocks;
     /**
      * Number of dynamically allocated bytes currently
      * held by clients of a heap.
      */
-    size_t alloc_bytes;
+	size_t alloc_bytes;
     /**
      * Number of bytes used in housekeeping overheads
      */
-    size_t alloc_overhead;
+	size_t alloc_overhead;
     /*----------------------------------------------------- */
     /**
      * Cumulative number of dynamically allocated blocks
      * given to clients of a heap.
      */
-    size_t alloc_total_blocks;
+	size_t alloc_total_blocks;
     /**
      * Cumulative number of dynamically allocated bytes
      * given to clients of a heap.
      */
-    size_t alloc_total_bytes;
+	size_t alloc_total_bytes;
     /*----------------------------------------------------- */
     /**
      * Number of dynamically allocated blocks
      * given to clients when the memory usage was at it's highest.
      */
-    size_t alloc_hightide_blocks;
+	size_t alloc_hightide_blocks;
     /**
      * Number of dynamically allocated bytes
      * given to clients of a heap when the memory usage was at it's
      * highest
      */
-    size_t alloc_hightide_bytes;
+	size_t alloc_hightide_bytes;
     /**
      * Number of bytes of overhead in use when the memory usage 
      * was at it's highest
      */
-    size_t alloc_hightide_overhead;
+	size_t alloc_hightide_overhead;
     /**
      * Number of free blocks when the memory usage was at it's
      * highest
      */
-    size_t free_hightide_blocks;
+	size_t free_hightide_blocks;
     /**
      * Number of free bytes when the memory usage was at it's highest.
      */
-    size_t free_hightide_bytes;
+	size_t free_hightide_bytes;
    /**
      * Number of housekeeping overhead bytes when the memory 
      * usage was at it's highest.
      */
-    size_t free_hightide_overhead;
+	size_t free_hightide_overhead;
     /*----------------------------------------------------- */
     /**
      * Number of statically allocated blocks currently
      * held by clients of a heap.
      */
-    size_t static_blocks;
+	size_t static_blocks;
     /**
      * Number of statically allocated bytes currently
      * held by clients of a heap.
      */
-    size_t static_bytes;    
+	size_t static_bytes;
     /**
      * Number of dynamically allocated bytes currently
      * held by clients of a heap.
      */
-    size_t static_overhead;
+	size_t static_overhead;
     /*----------------------------------------------------- */
 };
 /**
  * This type is used to indicate the result of a dynamic
  * memory allocation
  */
-typedef enum
-{
+typedef enum {
     /**
      * The allocation was successful
      */
-    LUB_HEAP_OK,
+	LUB_HEAP_OK,
     /**
      * There was insufficient resource to satisfy the request
      */
-    LUB_HEAP_FAILED,
+	LUB_HEAP_FAILED,
     /**
      * An attempt has been made to release an already freed block
      * of memory.
      */
-    LUB_HEAP_DOUBLE_FREE,
+	LUB_HEAP_DOUBLE_FREE,
     /**
      * A memory corruption has been detected. e.g. someone writing
      * beyond the bounds of an allocated block of memory.
      */
-    LUB_HEAP_CORRUPTED,
+	LUB_HEAP_CORRUPTED,
     /**
      * The client has passed in an invalid pointer
      * i.e. one which lies outside the bounds of the current heap.
      */
-    LUB_HEAP_INVALID_POINTER
+	LUB_HEAP_INVALID_POINTER
 } lub_heap_status_t;
 
-typedef struct {void *ptr;} struct_void_ptr;
+typedef struct {
+	void *ptr;
+} struct_void_ptr;
 /**
  * This type is used to indicate the alignment required 
  * for a memory allocation.
  */
-typedef enum
-{
+typedef enum {
     /**
      * This is the "native" alignment required for the current
      * CPU architecture.
      */
-    LUB_HEAP_ALIGN_NATIVE = sizeof(struct_void_ptr),
+	LUB_HEAP_ALIGN_NATIVE = sizeof(struct_void_ptr),
     /**
      * 4 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_2 = 0x00000004,
+	LUB_HEAP_ALIGN_2_POWER_2 = 0x00000004,
     /**
      * 8 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_3 = 0x00000008,
+	LUB_HEAP_ALIGN_2_POWER_3 = 0x00000008,
     /**
      * 16 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_4 = 0x00000010,
+	LUB_HEAP_ALIGN_2_POWER_4 = 0x00000010,
     /**
      * 32 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_5 = 0x00000020,
+	LUB_HEAP_ALIGN_2_POWER_5 = 0x00000020,
     /**
      * 64 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_6 = 0x00000040,
+	LUB_HEAP_ALIGN_2_POWER_6 = 0x00000040,
     /**
      * 128 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_7 = 0x00000080,
+	LUB_HEAP_ALIGN_2_POWER_7 = 0x00000080,
     /**
      * 256 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_8 = 0x00000100,
+	LUB_HEAP_ALIGN_2_POWER_8 = 0x00000100,
     /**
      * 512 byte alignment
      */
-    LUB_HEAP_ALIGN_2_POWER_9 = 0x00000200,
+	LUB_HEAP_ALIGN_2_POWER_9 = 0x00000200,
     /**
      * 1024 byte alignment (1KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_10 = 0x00000400,
+	LUB_HEAP_ALIGN_2_POWER_10 = 0x00000400,
     /**
      * 2048 byte alignment (2KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_11 = 0x00000800,
+	LUB_HEAP_ALIGN_2_POWER_11 = 0x00000800,
     /**
      * 4096 byte alignment (4KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_12 = 0x00001000,
+	LUB_HEAP_ALIGN_2_POWER_12 = 0x00001000,
     /**
      * 8192 byte alignment (8KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_13 = 0x00002000,
+	LUB_HEAP_ALIGN_2_POWER_13 = 0x00002000,
     /**
      * 16384 byte alignment (16KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_14 = 0x00004000,
+	LUB_HEAP_ALIGN_2_POWER_14 = 0x00004000,
     /**
      * 32768 byte alignment (32KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_15 = 0x00008000,
+	LUB_HEAP_ALIGN_2_POWER_15 = 0x00008000,
     /**
      * 65536 byte alignment (64KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_16 = 0x00010000,
+	LUB_HEAP_ALIGN_2_POWER_16 = 0x00010000,
     /**
      * 131072 byte alignment (128KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_17 = 0x00020000,
+	LUB_HEAP_ALIGN_2_POWER_17 = 0x00020000,
     /**
      * 262144 byte alignment (256KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_18 = 0x00040000,
+	LUB_HEAP_ALIGN_2_POWER_18 = 0x00040000,
     /**
      * 524288 byte alignment (512KB)
      */
-    LUB_HEAP_ALIGN_2_POWER_19 = 0x00080000,
+	LUB_HEAP_ALIGN_2_POWER_19 = 0x00080000,
     /**
      * 1048576 byte alignment (1MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_20 = 0x00100000,
+	LUB_HEAP_ALIGN_2_POWER_20 = 0x00100000,
     /**
      * 2097152 byte alignment (2MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_21 = 0x00200000,
+	LUB_HEAP_ALIGN_2_POWER_21 = 0x00200000,
     /**
      * 4194304 byte alignment (4MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_22 = 0x00400000,
+	LUB_HEAP_ALIGN_2_POWER_22 = 0x00400000,
     /**
      * 8388608 byte alignment (8MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_23 = 0x00800000,
+	LUB_HEAP_ALIGN_2_POWER_23 = 0x00800000,
     /**
      *  16777216 byte alignment (16MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_24 = 0x01000000,
+	LUB_HEAP_ALIGN_2_POWER_24 = 0x01000000,
     /**
      *  33554432 byte alignment (32MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_25 = 0x02000000,
+	LUB_HEAP_ALIGN_2_POWER_25 = 0x02000000,
     /**
      * 67108864 byte alignment (64MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_26 = 0x04000000,
+	LUB_HEAP_ALIGN_2_POWER_26 = 0x04000000,
     /**
      * 134217728 byte alignment (128MB)
      */
-    LUB_HEAP_ALIGN_2_POWER_27 = 0x08000000
-} lub_heap_align_t;
+	LUB_HEAP_ALIGN_2_POWER_27 = 0x08000000
+}
+lub_heap_align_t;
 
 /**
  * This type defines how leak details should be displayed
  */
-typedef enum
-{
+typedef enum {
     /**
      * Only show allocations which have no reference elsewhere
      * in the system
      */
-    LUB_HEAP_SHOW_LEAKS,
+	LUB_HEAP_SHOW_LEAKS,
     /**
      * Only show allocations which have no direct reference elsewhere
      * in the system, but do have their contents referenced.
      */
-    LUB_HEAP_SHOW_PARTIALS,
+	LUB_HEAP_SHOW_PARTIALS,
     /**
      * Show all the current allocations in the system.
      */
-    LUB_HEAP_SHOW_ALL
+	LUB_HEAP_SHOW_ALL
 } lub_heap_show_e;
 
 /**
  * This type defines a function prototype to be used to 
  * iterate around each of a number of things in the system.
  */
-typedef void 
-    lub_heap_foreach_fn(
-        /**
+typedef void lub_heap_foreach_fn(
+	/**
          * The current entity
          */
-        void *block,
-        /**
+					void *block,
+	/**
          * A simple 1-based identifier for this entity
          */
-        unsigned index,
-        /**
+					unsigned index,
+	/**
          * The number of bytes available in this entity
          */
-        size_t size,
-        /**
+					size_t size,
+	/**
          * Client specific argument
          */
-        void *arg);
+					void *arg);
 
 /**
  * This operation is a diagnostic which can be used to 
@@ -440,21 +437,20 @@ typedef void
  * -The specified function will have been called once for every segment
  *  in the specified heap
  */
-void
-    lub_heap_foreach_segment(
-        /**
+void lub_heap_foreach_segment(
+	/**
          * The heap instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+				     lub_heap_t * instance,
+	/**
          * The client provided function to call for each free block
          */
-        lub_heap_foreach_fn *fn,
-        /**
+				     lub_heap_foreach_fn * fn,
+	/**
          * Some client specific data to pass through to the callback
          * function.
          */
-        void *arg);
+				     void *arg);
 /**
  * This operation is a diagnostic which can be used to 
  * iterate around all the free blocks in the specified heap.
@@ -471,21 +467,20 @@ void
  * -The specified function will have been called once for every free
  *  block in the specified heap
  */
-void
-    lub_heap_foreach_free_block(
-        /**
+void lub_heap_foreach_free_block(
+	/**
          * The heap instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+					lub_heap_t * instance,
+	/**
          * The client provided function to call for each free block
          */
-        lub_heap_foreach_fn *fn,
-        /**
+					lub_heap_foreach_fn * fn,
+	/**
          * Some client specific data to pass through to the callback
          * function.
          */
-        void *arg);
+					void *arg);
 
 /**
   * This operation creates a dynamic heap from the provided
@@ -503,18 +498,16 @@ void
   * - further memory segements can be augmented to the heap using
   *   the lub_heap_add_segment() operation.
   */
-lub_heap_t *
-    lub_heap_create(
-        /**
+lub_heap_t *lub_heap_create(
+	/**
          * The begining of the first memory segment to associate with 
          * this heap
          */
-        void *start,
-        /**
+				   void *start,
+	/**
          * The number of bytes available for use in the first segment.
          */
-        size_t size
-    );
+				   size_t size);
 /**
   * This operation creates a dynamic heap from the provided
   * memory segment.
@@ -531,13 +524,11 @@ lub_heap_t *
   *   may now be reused.
   * - Any extra resources used for leak detection will have been released.
   */
-void
-    lub_heap_destroy(
-        /**
+void lub_heap_destroy(
+	/**
          * The heap instance on which to operate
          */
-        lub_heap_t *instance
-    );
+			     lub_heap_t * instance);
 /**
  * This operation augments an existing heap with 
  * some more memory to manage.
@@ -555,21 +546,19 @@ void
  * \post
  * - The new segment of memory becomes available for use by this heap.
  */
-void 
-    lub_heap_add_segment(
-        /**
+void lub_heap_add_segment(
+	/**
          * The heap instance on which to operate
          */
-        lub_heap_t *instance,
-        /** 
+				 lub_heap_t * instance,
+	/** 
          * The beginning of the memory segment to be managed
          */
-        void *start,
-        /**
+				 void *start,
+	/**
          * The number of bytes available for use in this segment
          */
-        size_t size
-    );
+				 size_t size);
 /**
  * This operation allocates some "static" memory from a heap. This is 
  * memory which will remain allocted for the lifetime of the heap instance.
@@ -589,17 +578,15 @@ void
  *   managing memory which itself has been dynamically allocated, then 
  *   the memory will be recovered when the heap is released.
  */
-void *
-    lub_heap_static_alloc(
-        /**
+void *lub_heap_static_alloc(
+	/**
          * The heap instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+				   lub_heap_t * instance,
+	/**
          * The number of bytes to allocate
          */
-        size_t size
-    );
+				   size_t size);
 /**
  * This operation changes the size of the object referenced by a passed in 
  * pointer to "size". The contents will be unchanged up to the minimum of the old 
@@ -624,26 +611,24 @@ void *
  * - (size == 0) No new memory will be allocated, *ptr will be set to NULL,
  *   and any original memory referenced by it will have been released.
  */
-lub_heap_status_t
-    lub_heap_realloc(
-        /**
+lub_heap_status_t lub_heap_realloc(
+	/**
          * The heap instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+					  lub_heap_t * instance,
+	/**
          * Reference to a pointer containing previously allocated memory 
          * or NULL.
          */
-        char **ptr,
-        /**
+					  char **ptr,
+	/**
          * The number of bytes required for the object 
          */
-        size_t size,
-        /**
+					  size_t size,
+	/**
          * The alignement required for a new allocations.
          */
-        lub_heap_align_t alignment
-    );
+					  lub_heap_align_t alignment);
 /**
  * This operation controls the tainted memory facility.
  * This means that during certain heap operations memory can
@@ -668,13 +653,11 @@ lub_heap_status_t
  *   be set to 0xAA as the "uninitialised" value.
  * - (disabled) no memory tainting will occur.
  */
-bool_t
-    lub_heap_taint(
-        /**
+bool_t lub_heap_taint(
+	/**
          * BOOL_TRUE to enable tainting or BOOL_FALSE to disable
          */
-        bool_t enable
-    );
+			     bool_t enable);
 /**
  * This operation indicates the current status of the memory tainting
  * facility
@@ -688,9 +671,8 @@ bool_t
  *
  * \post 
  * none
- */ 
-bool_t
-    lub_heap_is_tainting(void);
+ */
+bool_t lub_heap_is_tainting(void);
 /**
  * This operation controls runtime heap corruption detection.
  * This means that during every heap operation a full check is 
@@ -714,13 +696,11 @@ bool_t
  * return status of the invoking heap operation will be LUB_HEAP_CORRUPTED.
  * - (disabled) no entire heap memory checking will occur.
  */
-bool_t
-    lub_heap_check(
-        /**
+bool_t lub_heap_check(
+	/**
          * BOOL_TRUE to enable checking or BOOL_FALSE to disable
          */
-        bool_t enable
-    );
+			     bool_t enable);
 
 /**
  * This operation indicates the current status of the full memory checking
@@ -735,9 +715,8 @@ bool_t
  *
  * \post 
  * none
- */ 
-bool_t
-    lub_heap_is_checking(void);
+ */
+bool_t lub_heap_is_checking(void);
 
 /**
  * This operation checks the integrety of the memory in the specified
@@ -754,29 +733,26 @@ bool_t
  * \post
  * - none
  */
-extern bool_t 
-    lub_heap_check_memory(lub_heap_t *instance);
+extern bool_t lub_heap_check_memory(lub_heap_t * instance);
 
 /**
  * This function is invoked whenever a call to lub_heap_realloc() fails.
  * It is provided as a debugging aid; simple set a breakpoint to
  * stop execution of the program and any failures will be caught in context.
  */
-void 
-    lub_heap_stop_here(
-        /**
+void lub_heap_stop_here(
+	/**
          * The failure status of the the call to realloc
          */
-        lub_heap_status_t status,
-        /**
+			       lub_heap_status_t status,
+	/**
          * The old value of the pointer passed in
          */
-        char *old_ptr,
-        /**
+			       char *old_ptr,
+	/**
          * The requested number of bytes
          */
-        size_t new_size
-    );
+			       size_t new_size);
 
 /**
  * This operation fills out a statistics structure with the details for the 
@@ -789,32 +765,28 @@ void
  * - the results filled out are a snapshot of the statistics as the time
  *   of the call.
  */
-void
-    lub_heap__get_stats(
-        /**
+void lub_heap__get_stats(
+	/**
          * The instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+				lub_heap_t * instance,
+	/**
          * A client provided structure to fill out with the heap details
          */
-        lub_heap_stats_t *stats
-    );
+				lub_heap_stats_t * stats);
 
 /**
  * This operation dumps the salient details of the specified heap to stdout
  */
-void
-    lub_heap_show(
-        /**
+void lub_heap_show(
+	/**
          * The instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+			  lub_heap_t * instance,
+	/**
          * Whether to be verbose or not
          */
-        bool_t verbose
-    );
+			  bool_t verbose);
 /**
  * This method provides the size, in bytes, of the largest allocation
  * which can be performed.
@@ -827,21 +799,16 @@ void
  * \post
  * - none
  */
-size_t    
-lub_heap__get_max_free(
-        /**
+size_t lub_heap__get_max_free(
+	/**
          * The instance on which to operate
          */
-        lub_heap_t *instance
-    );
+				     lub_heap_t * instance);
 
 extern size_t
-    lub_heap__get_block_overhead(lub_heap_t *instance,
-                                 const void *ptr);
+lub_heap__get_block_overhead(lub_heap_t * instance, const void *ptr);
 
-extern size_t
-    lub_heap__get_block_size(lub_heap_t *instance,
-                             const void *ptr);
+extern size_t lub_heap__get_block_size(lub_heap_t * instance, const void *ptr);
 /**
  * This function scans memory to identify memory leaks
  * 
@@ -849,8 +816,7 @@ extern size_t
  * references may remain in freed memory.
  *
  */
-extern void
-    lub_heap_leak_scan(void);
+extern void lub_heap_leak_scan(void);
 /**
  * This function dumps all the context details for the heap
  * to stdout.
@@ -865,33 +831,26 @@ extern void
  * \return 
  * - a boolean indicating whether any leaks were displayed or not.
  */
-extern bool_t
-    lub_heap_leak_report(
-        /**
+extern bool_t lub_heap_leak_report(
+	/**
          * how to display the details
          */
-        lub_heap_show_e how,
-        /**
+					  lub_heap_show_e how,
+	/**
          * an optional substring to use to filter contexts.
          * Only contexts which contain the substring will be displayed
          */
-        const char *substring
-    );
+					  const char *substring);
 
-void
-    lub_heap__set_framecount(
-        /**
+void lub_heap__set_framecount(
+	/**
          * The new framecount to use
          */
-        unsigned framecount
-    );
+				     unsigned framecount);
 
-unsigned
-    lub_heap__get_framecount(void);
-    
-extern bool_t 
-    lub_heap_validate_pointer(lub_heap_t *instance,
-                              char       *ptr);
+unsigned lub_heap__get_framecount(void);
+
+extern bool_t lub_heap_validate_pointer(lub_heap_t * instance, char *ptr);
 /**
  * This 'magic' pointer is returned when a client requests zero bytes
  * The client can see that the allocation has succeeded, but cannot use 
@@ -915,15 +874,12 @@ extern bool_t
  *   support (disabled by default) then only the address of
  *   each stack frame will be shown.
  */
-extern void
-    lub_heap_init(
-        /** 
+extern void lub_heap_init(
+	/** 
          * The full pathname of the current executable
          * This is typically obtained from argv[0] in main()
          */
-        const char *program_name
-    );
-
+				 const char *program_name);
 
 #if defined(__CYGWIN__)
 
@@ -931,12 +887,11 @@ extern void
  * CYGWIN requires a specialised initialisation to account for 
  * argv[0] not containing the trailing ".exe" of the executable.
  */
-extern void 
-    cygwin_lub_heap_init(const char *file_name);
+extern void cygwin_lub_heap_init(const char *file_name);
 
 #define lub_heap_init(arg0) cygwin_lub_heap_init(arg0)
 
-#endif /* __CYGWIN__ */
+#endif				/* __CYGWIN__ */
 /**
   * This operation adds a cache to the current heap, which speeds up
   * the allocation and releasing of smaller block sizes.
@@ -953,21 +908,19 @@ extern void
   * - memory allocations for smaller block sizes may come from the
   *   cache.
   */
-lub_heap_status_t
-    lub_heap_cache_init(
-        /**
+lub_heap_status_t lub_heap_cache_init(
+	/**
          * The instance on which to operate
          */
-        lub_heap_t *instance,
-        /**
+					     lub_heap_t * instance,
+	/**
          * The maximum block size for the cache
          */
-        lub_heap_align_t max_block_size,
-        /**
+					     lub_heap_align_t max_block_size,
+	/**
          * The number of maximum sized blocks to make available.
          */
-        size_t num_max_blocks
-    );
+					     size_t num_max_blocks);
 /**
   * This operation signals the start of a section of code which 
   * should not have any of it's heap usage monitored by the leak
@@ -983,13 +936,11 @@ lub_heap_status_t
   * - If leak detection is enabled then no subsequent allocations will
   *   be monitored until the lub_heap_leak_restore_detection() is called.
   */
-void
-    lub_heap_leak_suppress_detection(
-        /**
+void lub_heap_leak_suppress_detection(
+	/**
          * The instance on which to operate
          */
-        lub_heap_t *instance
-    );
+					     lub_heap_t * instance);
 /**
   * This operation signals the end of a section of code which 
   * should not have any of it's heap usage monitored by the leak
@@ -1010,13 +961,11 @@ void
   * - If leak detection is enabled then no subsequent allocations will
   *   be monitored until the lub_heap_end_unmonitored_section() is called.
   */
-void
-    lub_heap_leak_restore_detection(
-        /**
+void lub_heap_leak_restore_detection(
+	/**
          * The instance on which to operate
          */
-        lub_heap_t *instance
-    );
+					    lub_heap_t * instance);
 
 /**
   * This operation returns the overhead, in bytes, which is required
@@ -1033,20 +982,16 @@ void
   * \post
   * - none
   */
-size_t
-    lub_heap_overhead_size(
-        /**
+size_t lub_heap_overhead_size(
+	/**
          * The maximum block size for the cache
          */
-        lub_heap_align_t max_block_size,
-        /**
+				     lub_heap_align_t max_block_size,
+	/**
          * The number of maximum sized blocks to make available.
          */
-        size_t num_max_blocks
-    );
-
+				     size_t num_max_blocks);
 
 _END_C_DECL
-
-#endif /* _lub_heap_h */
+#endif				/* _lub_heap_h */
 /** @} */

+ 350 - 363
lub/heap/cache.c

@@ -3,413 +3,400 @@
 #include "cache.h"
 
 /*--------------------------------------------------------- */
-size_t
-lub_heap_cache__get_max_free(lub_heap_cache_t *this)
+size_t lub_heap_cache__get_max_free(lub_heap_cache_t * this)
 {
-    size_t size = 0;
-    /* get the size from the cache */
-    lub_heap_cache_bucket_t **ptr;
-
-    for(ptr = this->m_bucket_start;
-        ptr < this->m_bucket_end;
-        ++ptr)
-    {
-        lub_blockpool_stats_t stats;
-        lub_blockpool__get_stats(&(*ptr)->m_blockpool,&stats);
-        if(stats.free_blocks && ((stats.block_size - sizeof(lub_heap_cache_bucket_t*)) > size))
-        {
-            size = stats.block_size - sizeof(lub_heap_cache_bucket_t*);
-        }
-    }
-    return size;
+	size_t size = 0;
+	/* get the size from the cache */
+	lub_heap_cache_bucket_t **ptr;
+
+	for (ptr = this->m_bucket_start; ptr < this->m_bucket_end; ++ptr) {
+		lub_blockpool_stats_t stats;
+		lub_blockpool__get_stats(&(*ptr)->m_blockpool, &stats);
+		if (stats.free_blocks
+		    && ((stats.block_size - sizeof(lub_heap_cache_bucket_t *)) >
+			size)) {
+			size =
+			    stats.block_size -
+			    sizeof(lub_heap_cache_bucket_t *);
+		}
+	}
+	return size;
 }
+
 /*--------------------------------------------------------- */
-static lub_heap_cache_bucket_t *
-_lub_heap_cache_find_bucket_from_size(lub_heap_cache_t *this,
-                                      size_t            size)
+static lub_heap_cache_bucket_t
+    *_lub_heap_cache_find_bucket_from_size(lub_heap_cache_t * this, size_t size)
 {
-    lub_heap_cache_bucket_t **ptr = this->m_bucket_start;
-    /* leave space to put the bucket reference into place */
-    size += sizeof(lub_heap_cache_bucket_t*);
-    /* place powers of two into the correct bucket */
-    --size; 
-    /* ignore sizes of 1,2 and 4 bytes */
-    size >>= 3; 
-    for(;
-        (ptr < this->m_bucket_end) && size;
-        ++ptr)
-    {
-        size >>= 1;
-    }
-    return (ptr == this->m_bucket_end) ? 0 : *ptr;
+	lub_heap_cache_bucket_t **ptr = this->m_bucket_start;
+	/* leave space to put the bucket reference into place */
+	size += sizeof(lub_heap_cache_bucket_t *);
+	/* place powers of two into the correct bucket */
+	--size;
+	/* ignore sizes of 1,2 and 4 bytes */
+	size >>= 3;
+	for (; (ptr < this->m_bucket_end) && size; ++ptr) {
+		size >>= 1;
+	}
+	return (ptr == this->m_bucket_end) ? 0 : *ptr;
 }
+
 /*--------------------------------------------------------- */
-static size_t
-lub_heap_cache_num_buckets(lub_heap_align_t max_block_size)
+static size_t lub_heap_cache_num_buckets(lub_heap_align_t max_block_size)
 {
-    unsigned num_buckets = 0;
-
-    /* count the number of buckets to be created */
-    max_block_size >>= 3; /* ignore buckets of size 1,2 and 4 bytes */
-    while(max_block_size)
-    {
-        ++num_buckets;
-        max_block_size >>= 1;
-    }
-    return num_buckets;
+	unsigned num_buckets = 0;
+
+	/* count the number of buckets to be created */
+	max_block_size >>= 3;	/* ignore buckets of size 1,2 and 4 bytes */
+	while (max_block_size) {
+		++num_buckets;
+		max_block_size >>= 1;
+	}
+	return num_buckets;
 }
+
 /*--------------------------------------------------------- */
 size_t
 lub_heap_cache_overhead_size(lub_heap_align_t max_block_size,
-                             size_t           num_max_blocks)
+			     size_t num_max_blocks)
 {
-    size_t overhead = 0;
-    if(num_max_blocks && max_block_size)
-    {
-        /* 
-         * account for the cache overheads
-         */
-        size_t num_buckets = lub_heap_cache_num_buckets(max_block_size);
-        
-        /* cache control block */
-        overhead += sizeof(lub_heap_cache_t);
-        
-        /* array of bucket pointers (-1 because one is contained in control block) */
-        overhead += sizeof(lub_heap_cache_bucket_t*) * (num_buckets-1);
-        
-        /* fast size lookup array */
-        overhead += sizeof(lub_heap_cache_bucket_t*) * max_block_size;
-        
-        /* buckets themselves */
-        overhead += num_buckets * offsetof(lub_heap_cache_bucket_t,m_memory_start);
-    }
-    return overhead;
+	size_t overhead = 0;
+	if (num_max_blocks && max_block_size) {
+		/* 
+		 * account for the cache overheads
+		 */
+		size_t num_buckets = lub_heap_cache_num_buckets(max_block_size);
+
+		/* cache control block */
+		overhead += sizeof(lub_heap_cache_t);
+
+		/* array of bucket pointers (-1 because one is contained in control block) */
+		overhead +=
+		    sizeof(lub_heap_cache_bucket_t *) * (num_buckets - 1);
+
+		/* fast size lookup array */
+		overhead += sizeof(lub_heap_cache_bucket_t *) * max_block_size;
+
+		/* buckets themselves */
+		overhead +=
+		    num_buckets * offsetof(lub_heap_cache_bucket_t,
+					   m_memory_start);
+	}
+	return overhead;
 }
+
 /*--------------------------------------------------------- */
 size_t
-lub_heap_overhead_size(lub_heap_align_t max_block_size,
-                       size_t           num_max_blocks)
+lub_heap_overhead_size(lub_heap_align_t max_block_size, size_t num_max_blocks)
 {
-    /* heap control block */
-    size_t overhead = sizeof(lub_heap_t);
-    
-    /* need at least one free blocks worth */
-    overhead += sizeof(lub_heap_free_block_t);
-
-    if(max_block_size && num_max_blocks)
-    {
-        size_t num_buckets = lub_heap_cache_num_buckets(max_block_size);
-        size_t bucket_size = max_block_size * num_max_blocks;
-
-        /* now add any cache overhead contribution */
-        overhead += lub_heap_cache_overhead_size(max_block_size,num_max_blocks);
-
-        /* add the bucket contents */
-        overhead += (num_buckets * bucket_size);
-    }
-    return overhead;
+	/* heap control block */
+	size_t overhead = sizeof(lub_heap_t);
+
+	/* need at least one free blocks worth */
+	overhead += sizeof(lub_heap_free_block_t);
+
+	if (max_block_size && num_max_blocks) {
+		size_t num_buckets = lub_heap_cache_num_buckets(max_block_size);
+		size_t bucket_size = max_block_size * num_max_blocks;
+
+		/* now add any cache overhead contribution */
+		overhead +=
+		    lub_heap_cache_overhead_size(max_block_size,
+						 num_max_blocks);
+
+		/* add the bucket contents */
+		overhead += (num_buckets * bucket_size);
+	}
+	return overhead;
 }
+
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_cache_init(lub_heap_t      *this,
-                    lub_heap_align_t max_block_size,
-                    size_t           num_max_blocks)
+lub_heap_cache_init(lub_heap_t * this,
+		    lub_heap_align_t max_block_size, size_t num_max_blocks)
 {
-    lub_heap_status_t status      = LUB_HEAP_FAILED;
-    do
-    {
-        size_t                    bucket_size = (max_block_size * num_max_blocks);
-        unsigned                  num_buckets = lub_heap_cache_num_buckets(max_block_size);
-        size_t                    block_size  = 8;
-        lub_heap_cache_t         *cache;
-        lub_heap_cache_bucket_t **ptr;
-        int                       i;
-        /* cannot call this multiple times */
-        if(this->cache) break;
-
-        
-        /* allocate a cache control block */
-        cache = this->cache = lub_heap_static_alloc(this,
-                                                    sizeof(lub_heap_cache_t) + sizeof(lub_heap_cache_bucket_t*)*(num_buckets-1));
-        if(!cache) break;
-        
-        cache->m_max_block_size = max_block_size;
-        cache->m_num_max_blocks = num_max_blocks;
-        cache->m_num_buckets    = num_buckets;
-        cache->m_bucket_size    = bucket_size;
-        cache->m_misses         = 0;
-        cache->m_bucket_end     = &cache->m_bucket_start[num_buckets];
-    
-        /* allocate each bucket for the cache */
-        for(ptr = cache->m_bucket_start;
-            ptr < cache->m_bucket_end;
-            ++ptr)
-        {
-            *ptr = lub_heap_static_alloc(this,
-                                         bucket_size + offsetof(lub_heap_cache_bucket_t,m_memory_start));
-            if(!*ptr) break;
-            /* set up each bucket */
-            lub_heap_cache_bucket_init(*ptr,
-                                       cache,
-                                       block_size,
-                                       bucket_size);
-            block_size <<= 1;
-        }
-        if(ptr != cache->m_bucket_end) break;
-
-        /* now create a fast lookup for resolving size to bucket */
-        cache->m_bucket_lookup = lub_heap_static_alloc(this,sizeof(lub_heap_cache_bucket_t*)*max_block_size);
-        if(!cache->m_bucket_lookup) break;
-
-        for(i = 0;
-            i < cache->m_max_block_size;
-            ++i)
-        {
-            cache->m_bucket_lookup[i] = _lub_heap_cache_find_bucket_from_size(cache,i+1);
-        }
-        status = LUB_HEAP_OK;
-    } while(0);
-    
-    return status;
+	lub_heap_status_t status = LUB_HEAP_FAILED;
+	do {
+		size_t bucket_size = (max_block_size * num_max_blocks);
+		unsigned num_buckets =
+		    lub_heap_cache_num_buckets(max_block_size);
+		size_t block_size = 8;
+		lub_heap_cache_t *cache;
+		lub_heap_cache_bucket_t **ptr;
+		int i;
+		/* cannot call this multiple times */
+		if (this->cache)
+			break;
+
+		/* allocate a cache control block */
+		cache = this->cache = lub_heap_static_alloc(this,
+							    sizeof
+							    (lub_heap_cache_t) +
+							    sizeof
+							    (lub_heap_cache_bucket_t
+							     *) * (num_buckets -
+								   1));
+		if (!cache)
+			break;
+
+		cache->m_max_block_size = max_block_size;
+		cache->m_num_max_blocks = num_max_blocks;
+		cache->m_num_buckets = num_buckets;
+		cache->m_bucket_size = bucket_size;
+		cache->m_misses = 0;
+		cache->m_bucket_end = &cache->m_bucket_start[num_buckets];
+
+		/* allocate each bucket for the cache */
+		for (ptr = cache->m_bucket_start;
+		     ptr < cache->m_bucket_end; ++ptr) {
+			*ptr = lub_heap_static_alloc(this,
+						     bucket_size +
+						     offsetof
+						     (lub_heap_cache_bucket_t,
+						      m_memory_start));
+			if (!*ptr)
+				break;
+			/* set up each bucket */
+			lub_heap_cache_bucket_init(*ptr,
+						   cache,
+						   block_size, bucket_size);
+			block_size <<= 1;
+		}
+		if (ptr != cache->m_bucket_end)
+			break;
+
+		/* now create a fast lookup for resolving size to bucket */
+		cache->m_bucket_lookup =
+		    lub_heap_static_alloc(this,
+					  sizeof(lub_heap_cache_bucket_t *) *
+					  max_block_size);
+		if (!cache->m_bucket_lookup)
+			break;
+
+		for (i = 0; i < cache->m_max_block_size; ++i) {
+			cache->m_bucket_lookup[i] =
+			    _lub_heap_cache_find_bucket_from_size(cache, i + 1);
+		}
+		status = LUB_HEAP_OK;
+	} while (0);
+
+	return status;
 }
+
 /*--------------------------------------------------------- */
-lub_heap_cache_bucket_t *
-lub_heap_cache_find_bucket_from_address(lub_heap_cache_t *this,
-                                        const void       *address)
+lub_heap_cache_bucket_t
+    *lub_heap_cache_find_bucket_from_address(lub_heap_cache_t * this,
+					     const void *address)
 {
-    lub_heap_cache_bucket_t **bucket = (void*)address;
-    
-    /* get the bucket reference */
-    --bucket;
-    
-    if(    (*bucket > this->m_bucket_start[0]) 
-        || (*bucket < this->m_bucket_end[-1]) )
-    {
-        bucket = 0;
-    }
-    return bucket ? *bucket : 0;
+	lub_heap_cache_bucket_t **bucket = (void *)address;
+
+	/* get the bucket reference */
+	--bucket;
+
+	if ((*bucket > this->m_bucket_start[0])
+	    || (*bucket < this->m_bucket_end[-1])) {
+		bucket = 0;
+	}
+	return bucket ? *bucket : 0;
 }
+
 /*--------------------------------------------------------- */
-lub_heap_cache_bucket_t *
-lub_heap_cache_find_bucket_from_size(lub_heap_cache_t *this,
-                                     size_t            size)
+lub_heap_cache_bucket_t *lub_heap_cache_find_bucket_from_size(lub_heap_cache_t *
+							      this, size_t size)
 {
-    lub_heap_cache_bucket_t *bucket = 0;
-    
-    /* simply get the result from the fast lookup table */
-    if(size > 0)
-    {        
-        if(size <= this->m_max_block_size)
-        {
-            bucket = this->m_bucket_lookup[size-1];
-        }
-        else
-        {
-            ++this->m_misses;
-        }
-    }
-    return bucket;
+	lub_heap_cache_bucket_t *bucket = 0;
+
+	/* simply get the result from the fast lookup table */
+	if (size > 0) {
+		if (size <= this->m_max_block_size) {
+			bucket = this->m_bucket_lookup[size - 1];
+		} else {
+			++this->m_misses;
+		}
+	}
+	return bucket;
 }
+
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_cache_realloc(lub_heap_t *this,
-                       char      **ptr,
-                       size_t      size)
+lub_heap_cache_realloc(lub_heap_t * this, char **ptr, size_t size)
 {
-    lub_heap_status_t        status     = LUB_HEAP_FAILED;
-    lub_heap_cache_bucket_t *old_bucket = 0;
-    lub_heap_cache_bucket_t *new_bucket = size ? lub_heap_cache_find_bucket_from_size(this->cache,size) : 0;
-    char                    *new_ptr = 0;
-    
-    do
-    {
-        char *old_ptr = *ptr;
-        if(old_ptr)
-        {
-            old_bucket = lub_heap_cache_find_bucket_from_address(this->cache,old_ptr);
-        }
-        else if (size == 0)
-        {
-            /* nothing to do */
-            status = LUB_HEAP_OK;
-            break;
-        }
-        if(old_bucket)
-        {
-            /********************************
+	lub_heap_status_t status = LUB_HEAP_FAILED;
+	lub_heap_cache_bucket_t *old_bucket = 0;
+	lub_heap_cache_bucket_t *new_bucket =
+	    size ? lub_heap_cache_find_bucket_from_size(this->cache, size) : 0;
+	char *new_ptr = 0;
+
+	do {
+		char *old_ptr = *ptr;
+		if (old_ptr) {
+			old_bucket =
+			    lub_heap_cache_find_bucket_from_address(this->cache,
+								    old_ptr);
+		} else if (size == 0) {
+			/* nothing to do */
+			status = LUB_HEAP_OK;
+			break;
+		}
+		if (old_bucket) {
+	    /********************************
              * old from CACHE, new from CACHE
              ******************************** */
-            if(new_bucket)
-            {
-                if(old_bucket == new_bucket)
-                {
-                    /* keep the existing cache block which is big enough */
-                    new_ptr = old_ptr;
-                    status = LUB_HEAP_OK;
-                    break;
-                }
-                /* try and get memory from the cache */
-                new_ptr = lub_heap_cache_bucket_alloc(new_bucket);
-                if(new_ptr)
-                {
-                    if(old_ptr)
-                    {
-                        /* copy the old details across */
-                        memcpy(new_ptr,old_ptr,size);
-
-                        /* release the old cache block */
-                        status = lub_heap_cache_bucket_free(old_bucket,old_ptr);
-                        if(LUB_HEAP_OK != status)
-                        {
-                            break;
-                        }
-                    }
-                    /* all done */
-                    status = LUB_HEAP_OK;
-                    break;
-                }
-            }
-            /********************************
+			if (new_bucket) {
+				if (old_bucket == new_bucket) {
+					/* keep the existing cache block which is big enough */
+					new_ptr = old_ptr;
+					status = LUB_HEAP_OK;
+					break;
+				}
+				/* try and get memory from the cache */
+				new_ptr =
+				    lub_heap_cache_bucket_alloc(new_bucket);
+				if (new_ptr) {
+					if (old_ptr) {
+						/* copy the old details across */
+						memcpy(new_ptr, old_ptr, size);
+
+						/* release the old cache block */
+						status =
+						    lub_heap_cache_bucket_free
+						    (old_bucket, old_ptr);
+						if (LUB_HEAP_OK != status) {
+							break;
+						}
+					}
+					/* all done */
+					status = LUB_HEAP_OK;
+					break;
+				}
+			}
+	    /********************************
              * old from CACHE, new from HEAP
              ******************************** */
-            if(size)
-            {
-                /* get new memory from the dynamic heap */
-                status = lub_heap_raw_realloc(this,&new_ptr,size,LUB_HEAP_ALIGN_NATIVE);
-                if(LUB_HEAP_OK != status)
-                {
-                    break;
-                }
-                /* copy the old details across */
-                memcpy(new_ptr,old_ptr,size);
-            }
-            /* release the old cache block */
-            status = lub_heap_cache_bucket_free(old_bucket,old_ptr);
-        }
-        else
-        {
-            /********************************
+			if (size) {
+				/* get new memory from the dynamic heap */
+				status =
+				    lub_heap_raw_realloc(this, &new_ptr, size,
+							 LUB_HEAP_ALIGN_NATIVE);
+				if (LUB_HEAP_OK != status) {
+					break;
+				}
+				/* copy the old details across */
+				memcpy(new_ptr, old_ptr, size);
+			}
+			/* release the old cache block */
+			status =
+			    lub_heap_cache_bucket_free(old_bucket, old_ptr);
+		} else {
+	    /********************************
              * old from HEAP, new from CACHE
              ******************************** */
-            if(size)
-            {
-                if(new_bucket)
-                {
-                    /* try and get memory from the cache */
-                    new_ptr = lub_heap_cache_bucket_alloc(new_bucket);
-                    if(new_ptr)
-                    {
-                        if(old_ptr)
-                        {
-                            /* copy the old details across */
-                            memcpy(new_ptr,old_ptr,size);
-                            size = 0;
-                        }
-                        else
-                        {
-                            /* all done */
-                            status = LUB_HEAP_OK;
-                            break;
-                        }
-                    }
-                }
-            }
-            else if(!old_ptr)
-            {
-                /* nothing to do */
-                status = LUB_HEAP_OK;
-                break;
-            }
-            /********************************
+			if (size) {
+				if (new_bucket) {
+					/* try and get memory from the cache */
+					new_ptr =
+					    lub_heap_cache_bucket_alloc
+					    (new_bucket);
+					if (new_ptr) {
+						if (old_ptr) {
+							/* copy the old details across */
+							memcpy(new_ptr, old_ptr,
+							       size);
+							size = 0;
+						} else {
+							/* all done */
+							status = LUB_HEAP_OK;
+							break;
+						}
+					}
+				}
+			} else if (!old_ptr) {
+				/* nothing to do */
+				status = LUB_HEAP_OK;
+				break;
+			}
+	    /********************************
              * old from HEAP, new from HEAP
              ******************************** */
-            /* release old memory and potentially get new memory */
-            status = lub_heap_raw_realloc(this,&old_ptr,size,LUB_HEAP_ALIGN_NATIVE);
-            if(LUB_HEAP_OK != status)
-            {
-                break;
-            }
-            else if(size)
-            {
-                new_ptr = old_ptr;
-            }
-        }
-    } while(0);
-
-    if(LUB_HEAP_OK == status)
-    {
-        /* set up the new pointer value */
-        *ptr = new_ptr;
-    }
-    return status;
+			/* release old memory and potentially get new memory */
+			status =
+			    lub_heap_raw_realloc(this, &old_ptr, size,
+						 LUB_HEAP_ALIGN_NATIVE);
+			if (LUB_HEAP_OK != status) {
+				break;
+			} else if (size) {
+				new_ptr = old_ptr;
+			}
+		}
+	} while (0);
+
+	if (LUB_HEAP_OK == status) {
+		/* set up the new pointer value */
+		*ptr = new_ptr;
+	}
+	return status;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_cache_show(lub_heap_cache_t *this)
+void lub_heap_cache_show(lub_heap_cache_t * this)
 {
-    lub_heap_cache_bucket_t **ptr;
-        
-    printf("       size num blocks    current  high-tide cumulative     misses\n");
-    printf(" ---------- ---------- ---------- ---------- ---------- ----------\n");
-    for(ptr = this->m_bucket_start;
-        ptr < this->m_bucket_end;
-        ++ptr)
-    {
-        lub_blockpool_t      *blockpool = &(*ptr)->m_blockpool;
-        lub_blockpool_stats_t stats;
-        
-        lub_blockpool__get_stats(blockpool,&stats);
-
-        printf(" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT"\n",
-               stats.block_size,
-               stats.num_blocks,
-               stats.alloc_blocks,
-               stats.alloc_hightide_blocks,
-               stats.alloc_total_blocks,
-               stats.alloc_failures);
-    }
-
-    printf(" %10u %10s %10s %10s %10s %10"SIZE_FMT"\n",
-           this->m_max_block_size,
-           "-",
-           "-",
-           "-",
-           "-",
-          this->m_misses);
+	lub_heap_cache_bucket_t **ptr;
+
+	printf
+	    ("       size num blocks    current  high-tide cumulative     misses\n");
+	printf
+	    (" ---------- ---------- ---------- ---------- ---------- ----------\n");
+	for (ptr = this->m_bucket_start; ptr < this->m_bucket_end; ++ptr) {
+		lub_blockpool_t *blockpool = &(*ptr)->m_blockpool;
+		lub_blockpool_stats_t stats;
+
+		lub_blockpool__get_stats(blockpool, &stats);
+
+		printf(" %10" SIZE_FMT " %10" SIZE_FMT " %10" SIZE_FMT " %10"
+		       SIZE_FMT " %10" SIZE_FMT " %10" SIZE_FMT "\n",
+		       stats.block_size, stats.num_blocks, stats.alloc_blocks,
+		       stats.alloc_hightide_blocks, stats.alloc_total_blocks,
+		       stats.alloc_failures);
+	}
+
+	printf(" %10u %10s %10s %10s %10s %10" SIZE_FMT "\n",
+	       this->m_max_block_size, "-", "-", "-", "-", this->m_misses);
 }
+
 /*--------------------------------------------------------- */
 void
-lub_heap_cache__get_stats(lub_heap_cache_t *this,
-                          lub_heap_stats_t *stats)
+lub_heap_cache__get_stats(lub_heap_cache_t * this, lub_heap_stats_t * stats)
 {
-    lub_heap_cache_bucket_t **ptr;
-
-    memset(stats,0,sizeof(lub_heap_stats_t));
-    stats->free_overhead = lub_heap_cache_overhead_size(this->m_max_block_size,this->m_num_max_blocks);
-
-    for(ptr = this->m_bucket_start;
-        ptr < this->m_bucket_end;
-        ++ptr)
-    {
-        lub_blockpool_t      *blockpool = &(*ptr)->m_blockpool;
-        lub_blockpool_stats_t bucket_stats;
-        size_t                block_size;
-        size_t                block_overhead = sizeof(lub_heap_cache_bucket_t*); /* used for fast lookup from address */
-        
-        lub_blockpool__get_stats(blockpool,&bucket_stats);
-        block_size = (bucket_stats.block_size - block_overhead);
-
-        stats->free_blocks   += bucket_stats.free_blocks;
-        stats->free_bytes    += block_size * bucket_stats.free_blocks;
-        stats->free_overhead += block_overhead * bucket_stats.free_blocks;
-
-        stats->alloc_blocks   += bucket_stats.alloc_blocks;
-        stats->alloc_bytes    += block_size * bucket_stats.alloc_blocks;
-        stats->alloc_overhead += block_overhead * bucket_stats.alloc_blocks;
-
-        stats->alloc_total_blocks += bucket_stats.alloc_total_blocks;
-        stats->alloc_total_bytes  += block_size * bucket_stats.alloc_total_blocks;
-    }
-    
+	lub_heap_cache_bucket_t **ptr;
+
+	memset(stats, 0, sizeof(lub_heap_stats_t));
+	stats->free_overhead =
+	    lub_heap_cache_overhead_size(this->m_max_block_size,
+					 this->m_num_max_blocks);
+
+	for (ptr = this->m_bucket_start; ptr < this->m_bucket_end; ++ptr) {
+		lub_blockpool_t *blockpool = &(*ptr)->m_blockpool;
+		lub_blockpool_stats_t bucket_stats;
+		size_t block_size;
+		size_t block_overhead = sizeof(lub_heap_cache_bucket_t *);	/* used for fast lookup from address */
+
+		lub_blockpool__get_stats(blockpool, &bucket_stats);
+		block_size = (bucket_stats.block_size - block_overhead);
+
+		stats->free_blocks += bucket_stats.free_blocks;
+		stats->free_bytes += block_size * bucket_stats.free_blocks;
+		stats->free_overhead +=
+		    block_overhead * bucket_stats.free_blocks;
+
+		stats->alloc_blocks += bucket_stats.alloc_blocks;
+		stats->alloc_bytes += block_size * bucket_stats.alloc_blocks;
+		stats->alloc_overhead +=
+		    block_overhead * bucket_stats.alloc_blocks;
+
+		stats->alloc_total_blocks += bucket_stats.alloc_total_blocks;
+		stats->alloc_total_bytes +=
+		    block_size * bucket_stats.alloc_total_blocks;
+	}
+
 }
+
 /*--------------------------------------------------------- */

+ 35 - 44
lub/heap/cache.h

@@ -5,58 +5,49 @@ typedef struct _lub_heap_cache_bucket lub_heap_cache_bucket_t;
 /*-------------------------------------
  * lub_heap_cache_t class
  *------------------------------------- */
-struct _lub_heap_cache
-{
-    lub_heap_align_t          m_max_block_size;
-    unsigned                  m_num_max_blocks;
-    unsigned                  m_num_buckets;
-    size_t                    m_bucket_size;
-    size_t                    m_misses;
-    lub_heap_cache_bucket_t **m_bucket_lookup;
-    lub_heap_cache_bucket_t **m_bucket_end;
-    lub_heap_cache_bucket_t  *m_bucket_start[1]; /* must be last */
+struct _lub_heap_cache {
+	lub_heap_align_t m_max_block_size;
+	unsigned m_num_max_blocks;
+	unsigned m_num_buckets;
+	size_t m_bucket_size;
+	size_t m_misses;
+	lub_heap_cache_bucket_t **m_bucket_lookup;
+	lub_heap_cache_bucket_t **m_bucket_end;
+	lub_heap_cache_bucket_t *m_bucket_start[1];	/* must be last */
 };
 
-lub_heap_cache_bucket_t *
-    lub_heap_cache_find_bucket_from_address(lub_heap_cache_t *instance,
-                                            const void       *address);
-lub_heap_cache_bucket_t *
-    lub_heap_cache_find_bucket_from_size(lub_heap_cache_t *instance,
-                                         size_t            size);
-void
-    lub_heap_cache__get_stats(lub_heap_cache_t *this,
-                              lub_heap_stats_t *stats);
+lub_heap_cache_bucket_t
+    *lub_heap_cache_find_bucket_from_address(lub_heap_cache_t * instance,
+					     const void *address);
+lub_heap_cache_bucket_t *lub_heap_cache_find_bucket_from_size(lub_heap_cache_t *
+							      instance,
+							      size_t size);
+void lub_heap_cache__get_stats(lub_heap_cache_t * this,
+			       lub_heap_stats_t * stats);
 
 /*-------------------------------------
  * lub_heap_cache_bucket_t class
  *------------------------------------- */
-struct _lub_heap_cache_bucket
-{
-    lub_blockpool_t   m_blockpool;
-    lub_heap_cache_t *m_cache;
-    char             *m_memory_end;
-    char              m_memory_start[1]; /* must be last */
+struct _lub_heap_cache_bucket {
+	lub_blockpool_t m_blockpool;
+	lub_heap_cache_t *m_cache;
+	char *m_memory_end;
+	char m_memory_start[1];	/* must be last */
 };
 
 void
-    lub_heap_cache_bucket_init(lub_heap_cache_bucket_t *instance,
-                               lub_heap_cache_t        *cache,
-                               size_t                   block_size,
-                               size_t                   bucket_size);
-void *
-    lub_heap_cache_bucket_alloc(lub_heap_cache_bucket_t *instance);
+lub_heap_cache_bucket_init(lub_heap_cache_bucket_t * instance,
+			   lub_heap_cache_t * cache,
+			   size_t block_size, size_t bucket_size);
+void *lub_heap_cache_bucket_alloc(lub_heap_cache_bucket_t * instance);
 lub_heap_status_t
-    lub_heap_cache_bucket_free(lub_heap_cache_bucket_t *instance,
-                                void                    *ptr);
-size_t
-    lub_heap_cache__get_max_free(lub_heap_cache_t *this);
+lub_heap_cache_bucket_free(lub_heap_cache_bucket_t * instance, void *ptr);
+size_t lub_heap_cache__get_max_free(lub_heap_cache_t * this);
 
-lub_heap_cache_bucket_t *
-    lub_heap_cache_find_bucket_from_address(lub_heap_cache_t *this,
-                                            const void       *address);
-size_t
-    lub_heap_cache_bucket__get_block_overhead(lub_heap_cache_bucket_t *this,
-                                              const char              *block);
-size_t
-    lub_heap_cache_bucket__get_block_size(lub_heap_cache_bucket_t *this,
-                                          const char              *block);
+lub_heap_cache_bucket_t
+    *lub_heap_cache_find_bucket_from_address(lub_heap_cache_t * this,
+					     const void *address);
+size_t lub_heap_cache_bucket__get_block_overhead(lub_heap_cache_bucket_t * this,
+						 const char *block);
+size_t lub_heap_cache_bucket__get_block_size(lub_heap_cache_bucket_t * this,
+					     const char *block);

+ 53 - 53
lub/heap/cache_bucket.c

@@ -2,76 +2,76 @@
 
 /*--------------------------------------------------------- */
 size_t
-lub_heap_cache_bucket__get_block_overhead(lub_heap_cache_bucket_t *this,
-                                          const char              *ptr)
+lub_heap_cache_bucket__get_block_overhead(lub_heap_cache_bucket_t * this,
+					  const char *ptr)
 {
-    return sizeof(lub_heap_cache_bucket_t*);
+	return sizeof(lub_heap_cache_bucket_t *);
 }
+
 /*--------------------------------------------------------- */
 size_t
-lub_heap_cache_bucket__get_block_size(lub_heap_cache_bucket_t *this,
-                                      const char              *ptr)
+lub_heap_cache_bucket__get_block_size(lub_heap_cache_bucket_t * this,
+				      const char *ptr)
 {
-    size_t size = 0;
+	size_t size = 0;
 
-    /* get the size from the cache */
-    lub_blockpool_stats_t stats;
-    lub_blockpool__get_stats(&this->m_blockpool,&stats);
-    size = stats.block_size - sizeof(lub_heap_cache_bucket_t*);
+	/* get the size from the cache */
+	lub_blockpool_stats_t stats;
+	lub_blockpool__get_stats(&this->m_blockpool, &stats);
+	size = stats.block_size - sizeof(lub_heap_cache_bucket_t *);
 
-    return size;
+	return size;
 }
+
 /*--------------------------------------------------------- */
 void
-lub_heap_cache_bucket_init(lub_heap_cache_bucket_t *this,
-                           lub_heap_cache_t        *cache,
-                           size_t                   block_size,
-                           size_t                   bucket_size)
+lub_heap_cache_bucket_init(lub_heap_cache_bucket_t * this,
+			   lub_heap_cache_t * cache,
+			   size_t block_size, size_t bucket_size)
 {
-    /* initialise the blockpool */
-    size_t num_blocks = bucket_size/block_size;
-    
-    lub_blockpool_init(&this->m_blockpool,
-                       this->m_memory_start,
-                       block_size,
-                       num_blocks);
-    this->m_memory_end = this->m_memory_start + bucket_size;
-    this->m_cache      = cache;
+	/* initialise the blockpool */
+	size_t num_blocks = bucket_size / block_size;
+
+	lub_blockpool_init(&this->m_blockpool,
+			   this->m_memory_start, block_size, num_blocks);
+	this->m_memory_end = this->m_memory_start + bucket_size;
+	this->m_cache = cache;
 }
+
 /*--------------------------------------------------------- */
-void *
-lub_heap_cache_bucket_alloc(lub_heap_cache_bucket_t *this)
+void *lub_heap_cache_bucket_alloc(lub_heap_cache_bucket_t * this)
 {
-    void                     *ptr        = 0;
-    lub_heap_cache_bucket_t **bucket_ptr = lub_blockpool_alloc(&this->m_blockpool);
-    if(bucket_ptr)
-    {
-        *bucket_ptr = this;
-        ptr = ++bucket_ptr;
-        /* make sure that released memory is tainted */
-        lub_heap_taint_memory(ptr,
-                              LUB_HEAP_TAINT_ALLOC,
-                              this->m_blockpool.m_block_size-sizeof(lub_heap_cache_bucket_t*));
-    }
-    return ptr;
+	void *ptr = 0;
+	lub_heap_cache_bucket_t **bucket_ptr =
+	    lub_blockpool_alloc(&this->m_blockpool);
+	if (bucket_ptr) {
+		*bucket_ptr = this;
+		ptr = ++bucket_ptr;
+		/* make sure that released memory is tainted */
+		lub_heap_taint_memory(ptr,
+				      LUB_HEAP_TAINT_ALLOC,
+				      this->m_blockpool.m_block_size -
+				      sizeof(lub_heap_cache_bucket_t *));
+	}
+	return ptr;
 }
+
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_cache_bucket_free(lub_heap_cache_bucket_t *this,
-                           void                    *ptr)
+lub_heap_cache_bucket_free(lub_heap_cache_bucket_t * this, void *ptr)
 {
-    lub_heap_status_t         status     = LUB_HEAP_CORRUPTED;
-    lub_heap_cache_bucket_t **bucket_ptr = ptr;
-    --bucket_ptr;
-    if(*bucket_ptr == this)
-    {
-        lub_blockpool_free(&this->m_blockpool,bucket_ptr);
-        /* make sure that released memory is tainted */
-        lub_heap_taint_memory((char*)bucket_ptr,
-                              LUB_HEAP_TAINT_FREE,
-                              this->m_blockpool.m_block_size);
-        status = LUB_HEAP_OK;
-    }
-    return status;
+	lub_heap_status_t status = LUB_HEAP_CORRUPTED;
+	lub_heap_cache_bucket_t **bucket_ptr = ptr;
+	--bucket_ptr;
+	if (*bucket_ptr == this) {
+		lub_blockpool_free(&this->m_blockpool, bucket_ptr);
+		/* make sure that released memory is tainted */
+		lub_heap_taint_memory((char *)bucket_ptr,
+				      LUB_HEAP_TAINT_FREE,
+				      this->m_blockpool.m_block_size);
+		status = LUB_HEAP_OK;
+	}
+	return status;
 }
+
 /*--------------------------------------------------------- */

+ 614 - 680
lub/heap/context.c

@@ -10,155 +10,144 @@
 
 unsigned long lub_heap_frame_count;
 
-#define CONTEXT_CHUNK_SIZE 100 /* number of contexts per chunk */
-#define CONTEXT_MAX_CHUNKS 100 /* allow upto 10000 contexts */
+#define CONTEXT_CHUNK_SIZE 100	/* number of contexts per chunk */
+#define CONTEXT_MAX_CHUNKS 100	/* allow upto 10000 contexts */
 
 /*--------------------------------------------------------- 
  * PRIVATE META FUNCTIONS
  *--------------------------------------------------------- */
-void
-lub_heap_context_meta_init(void)
+void lub_heap_context_meta_init(void)
 {
-    static bool_t initialised = BOOL_FALSE;
-    if(BOOL_FALSE == initialised)
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        initialised = BOOL_TRUE;
-
-        /* initialise the context tree */
-        lub_bintree_init(&leak->m_context_tree,
-                         offsetof(lub_heap_context_t,bt_node),
-                         lub_heap_context_compare,
-                         lub_heap_context_getkey);
-        lub_heap_leak_release(leak);
-    }
+	static bool_t initialised = BOOL_FALSE;
+	if (BOOL_FALSE == initialised) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		initialised = BOOL_TRUE;
+
+		/* initialise the context tree */
+		lub_bintree_init(&leak->m_context_tree,
+				 offsetof(lub_heap_context_t, bt_node),
+				 lub_heap_context_compare,
+				 lub_heap_context_getkey);
+		lub_heap_leak_release(leak);
+	}
 }
+
 /*--------------------------------------------------------- */
-int
-lub_heap_context_compare(const void *clientnode,
-                         const void *clientkey)
+int lub_heap_context_compare(const void *clientnode, const void *clientkey)
 {
-    int                                   i,delta=0;
-    const lub_heap_context_t             *node = clientnode;
-    const lub_heap_context_key_t         *key  = clientkey;    
-    function_t                   * const *node_fn = node->key.backtrace;
-    function_t                   * const *key_fn  = key->backtrace;
-        
-    for(i = lub_heap_frame_count;
-        i;
-        --i,++node_fn,++key_fn)
-    {
-        delta = ((unsigned long)*node_fn - (unsigned long)*key_fn);
-        if(0 != delta)
-        {
-            break;
-        }
-    }
-    return delta;
+	int i, delta = 0;
+	const lub_heap_context_t *node = clientnode;
+	const lub_heap_context_key_t *key = clientkey;
+	function_t *const *node_fn = node->key.backtrace;
+	function_t *const *key_fn = key->backtrace;
+
+	for (i = lub_heap_frame_count; i; --i, ++node_fn, ++key_fn) {
+		delta = ((unsigned long)*node_fn - (unsigned long)*key_fn);
+		if (0 != delta) {
+			break;
+		}
+	}
+	return delta;
 }
+
 /*--------------------------------------------------------- */
 /* we simply use the embedded key as the index */
-void
-lub_heap_context_getkey(const void        *clientnode,
-                        lub_bintree_key_t *key)
+void lub_heap_context_getkey(const void *clientnode, lub_bintree_key_t * key)
 {
-    const lub_heap_context_t * context = clientnode;
-    memcpy(key,&context->key,sizeof(lub_heap_context_key_t));
+	const lub_heap_context_t *context = clientnode;
+	memcpy(key, &context->key, sizeof(lub_heap_context_key_t));
 }
+
 /*--------------------------------------------------------- */
 static bool_t
-lub_heap_foreach_context(bool_t (*fn)(lub_heap_context_t *,void *),
-                         void    *arg)
+lub_heap_foreach_context(bool_t(*fn) (lub_heap_context_t *, void *), void *arg)
 {
-    bool_t                  result = BOOL_FALSE;
-    lub_heap_context_t    * context;
-    lub_bintree_iterator_t  iter;
-
-    lub_heap_context_meta_init();
-
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        for(context = lub_bintree_findfirst(&leak->m_context_tree),
-            context ? lub_bintree_iterator_init(&iter,&leak->m_context_tree,context) : (void)0;
-            context;
-            context = lub_bintree_iterator_next(&iter))
-        {
-            lub_heap_leak_release(leak);
-            /* invoke the specified method on this context */
-            result = fn(context,arg);
-            leak = lub_heap_leak_instance();
-        }
-        lub_heap_leak_release(leak);
-    }
-    return result;
+	bool_t result = BOOL_FALSE;
+	lub_heap_context_t *context;
+	lub_bintree_iterator_t iter;
+
+	lub_heap_context_meta_init();
+
+	{
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		for (context = lub_bintree_findfirst(&leak->m_context_tree),
+		     context ? lub_bintree_iterator_init(&iter,
+							 &leak->m_context_tree,
+							 context) : (void)0;
+		     context; context = lub_bintree_iterator_next(&iter)) {
+			lub_heap_leak_release(leak);
+			/* invoke the specified method on this context */
+			result = fn(context, arg);
+			leak = lub_heap_leak_instance();
+		}
+		lub_heap_leak_release(leak);
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */
-static void
-lub_heap_show_summary(void)
+static void lub_heap_show_summary(void)
 {
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    size_t ok_allocs   = leak->m_stats.allocs;
-    size_t ok_bytes    = leak->m_stats.alloc_bytes; 
-    size_t ok_overhead = leak->m_stats.alloc_overhead; 
-    
-    ok_allocs -= leak->m_stats.partials;
-    ok_allocs -= leak->m_stats.leaks;
-    
-    ok_overhead -= leak->m_stats.partial_overhead;
-    ok_overhead -= leak->m_stats.leaked_overhead;
-    
-    printf("\n"
-           "          +----------+----------+----------+----------+\n");
-    printf("  TOTALS  |    blocks|     bytes|   average|  overhead|\n");
-    printf("+---------+----------+----------+----------+----------+\n");
-    printf("|contexts |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10s|\n",
-            leak->m_stats.contexts,
-            leak->m_stats.contexts * sizeof(lub_heap_context_t),
-            leak->m_stats.contexts ? sizeof(lub_heap_context_t) : 0,
-            "");
-    printf("|allocs   |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|\n",
-            ok_allocs,
-            ok_bytes,
-            ok_allocs ? (ok_bytes / ok_allocs) : 0,
-            ok_overhead);
-    if(leak->m_stats.partials)
-    {
-        printf("|partials |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|\n",
-                leak->m_stats.partials,
-                leak->m_stats.partial_bytes,
-                leak->m_stats.partials ? (leak->m_stats.partial_bytes / leak->m_stats.partials) : 0,
-                leak->m_stats.partial_overhead);
-    }
-    if(leak->m_stats.leaks)
-    {
-        printf("|leaks    |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|\n",
-                leak->m_stats.leaks,
-                leak->m_stats.leaked_bytes,
-                leak->m_stats.leaks ? (leak->m_stats.leaked_bytes / leak->m_stats.leaks) : 0,
-                leak->m_stats.leaked_overhead);
-    }
-    printf("+---------+----------+----------+----------+----------+\n");
-    lub_heap_leak_release(leak);
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	size_t ok_allocs = leak->m_stats.allocs;
+	size_t ok_bytes = leak->m_stats.alloc_bytes;
+	size_t ok_overhead = leak->m_stats.alloc_overhead;
+
+	ok_allocs -= leak->m_stats.partials;
+	ok_allocs -= leak->m_stats.leaks;
+
+	ok_overhead -= leak->m_stats.partial_overhead;
+	ok_overhead -= leak->m_stats.leaked_overhead;
+
+	printf("\n"
+	       "          +----------+----------+----------+----------+\n");
+	printf("  TOTALS  |    blocks|     bytes|   average|  overhead|\n");
+	printf("+---------+----------+----------+----------+----------+\n");
+	printf("|contexts |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT
+	       "|%10s|\n", leak->m_stats.contexts,
+	       leak->m_stats.contexts * sizeof(lub_heap_context_t),
+	       leak->m_stats.contexts ? sizeof(lub_heap_context_t) : 0, "");
+	printf("|allocs   |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT "|%10"
+	       SIZE_FMT "|\n", ok_allocs, ok_bytes,
+	       ok_allocs ? (ok_bytes / ok_allocs) : 0, ok_overhead);
+	if (leak->m_stats.partials) {
+		printf("|partials |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT
+		       "|%10" SIZE_FMT "|\n", leak->m_stats.partials,
+		       leak->m_stats.partial_bytes,
+		       leak->m_stats.partials ? (leak->m_stats.partial_bytes /
+						 leak->m_stats.partials) : 0,
+		       leak->m_stats.partial_overhead);
+	}
+	if (leak->m_stats.leaks) {
+		printf("|leaks    |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT
+		       "|%10" SIZE_FMT "|\n", leak->m_stats.leaks,
+		       leak->m_stats.leaked_bytes,
+		       leak->m_stats.leaks ? (leak->m_stats.leaked_bytes /
+					      leak->m_stats.leaks) : 0,
+		       leak->m_stats.leaked_overhead);
+	}
+	printf("+---------+----------+----------+----------+----------+\n");
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
 
 /*--------------------------------------------------------- 
  * PRIVATE METHODS
  *--------------------------------------------------------- */
 static void
-lub_heap_context_foreach_node(lub_heap_context_t *this,
-                              void           (*fn)(lub_heap_node_t *))
+lub_heap_context_foreach_node(lub_heap_context_t * this,
+			      void (*fn) (lub_heap_node_t *))
 {
-    lub_heap_node_t * node;
- 
-    for(node = this->first_node;
-        node;
-        node = lub_heap_node__get_next(node))
-    {
-        /* invoke the specified method on this node */
-        fn(node);
-    }
+	lub_heap_node_t *node;
+
+	for (node = this->first_node;
+	     node; node = lub_heap_node__get_next(node)) {
+		/* invoke the specified method on this node */
+		fn(node);
+	}
 }
+
 /*--------------------------------------------------------- */
 
 /*--------------------------------------------------------- 
@@ -166,196 +155,178 @@ lub_heap_context_foreach_node(lub_heap_context_t *this,
  *--------------------------------------------------------- */
 void
 lub_heap_context_init(lub_heap_context_t * this,
-                      lub_heap_t         * heap,
-                      const stackframe_t * stack)
+		      lub_heap_t * heap, const stackframe_t * stack)
 {
-    lub_heap_context_meta_init();
-    
-    this->heap             = heap;
-    this->allocs           = 0;
-    this->alloc_bytes      = 0;
-    this->alloc_overhead   = 0;
-    this->leaks            = 0;
-    this->leaked_bytes     = 0;
-    this->leaked_overhead  = 0;
-    this->partials         = 0;
-    this->partial_bytes    = 0;
-    this->partial_overhead = 0;
-    this->first_node       = NULL;
-    
-    /* set the current backtrace for the context */
-    this->key = *stack;
-    
-    /* intialise this context's binary tree node */
-    lub_bintree_node_init(&this->bt_node);
-
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        /* add this context to the context_tree */
-        lub_bintree_insert(&leak->m_context_tree,this);
-        lub_heap_leak_release(leak);
-    }
+	lub_heap_context_meta_init();
+
+	this->heap = heap;
+	this->allocs = 0;
+	this->alloc_bytes = 0;
+	this->alloc_overhead = 0;
+	this->leaks = 0;
+	this->leaked_bytes = 0;
+	this->leaked_overhead = 0;
+	this->partials = 0;
+	this->partial_bytes = 0;
+	this->partial_overhead = 0;
+	this->first_node = NULL;
+
+	/* set the current backtrace for the context */
+	this->key = *stack;
+
+	/* intialise this context's binary tree node */
+	lub_bintree_node_init(&this->bt_node);
+
+	{
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		/* add this context to the context_tree */
+		lub_bintree_insert(&leak->m_context_tree, this);
+		lub_heap_leak_release(leak);
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_context_fini(lub_heap_context_t * this)
+void lub_heap_context_fini(lub_heap_context_t * this)
 {
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    /* remove this node from the context_tree */
-    lub_bintree_remove(&leak->m_context_tree,this);
-    lub_heap_leak_release(leak);
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	/* remove this node from the context_tree */
+	lub_bintree_remove(&leak->m_context_tree, this);
+	lub_heap_leak_release(leak);
 
-    /* cleanup the context */
-    lub_heap_context_clear(this);
+	/* cleanup the context */
+	lub_heap_context_clear(this);
 }
+
 /*--------------------------------------------------------- */
 void
-lub_heap_context_insert_node(lub_heap_context_t * this,
-                             lub_heap_node_t    * node)
+lub_heap_context_insert_node(lub_heap_context_t * this, lub_heap_node_t * node)
 {
-    /* add the node to the linked list */
-    lub_heap_node__set_next(node,this->first_node);
-    node->prev = NULL;
-    if(this->first_node)
-    {
-        this->first_node->prev = node;
-    }
-    this->first_node = node;
-    
+	/* add the node to the linked list */
+	lub_heap_node__set_next(node, this->first_node);
+	node->prev = NULL;
+	if (this->first_node) {
+		this->first_node->prev = node;
+	}
+	this->first_node = node;
+
 }
+
 /*--------------------------------------------------------- */
 void
-lub_heap_context_remove_node(lub_heap_context_t *this,
-                             lub_heap_node_t    *node)
+lub_heap_context_remove_node(lub_heap_context_t * this, lub_heap_node_t * node)
 {
-    lub_heap_node_t *next,*prev    = NULL;
-    
-    /* remove the node from the context list */
-    next = lub_heap_node__get_next(node);
-    prev = node->prev;
-
-    if(NULL == prev)
-    {
-        this->first_node = next;
-    }
-    else
-    {
-        lub_heap_node__set_next(prev,next);
-    }
-    if(NULL != next)
-    {
-        next->prev = prev;
-    }
-
-    /* clear the pointers */
-    lub_heap_node__set_next(node,NULL);
-    node->prev = NULL;
-    
-    /* is this the last node in a context? */
-    if(0 == this->allocs)
-    {
-        /* removing the last node deletes the context */
-        lub_heap_context_delete(this);
-    }
+	lub_heap_node_t *next, *prev = NULL;
+
+	/* remove the node from the context list */
+	next = lub_heap_node__get_next(node);
+	prev = node->prev;
+
+	if (NULL == prev) {
+		this->first_node = next;
+	} else {
+		lub_heap_node__set_next(prev, next);
+	}
+	if (NULL != next) {
+		next->prev = prev;
+	}
+
+	/* clear the pointers */
+	lub_heap_node__set_next(node, NULL);
+	node->prev = NULL;
+
+	/* is this the last node in a context? */
+	if (0 == this->allocs) {
+		/* removing the last node deletes the context */
+		lub_heap_context_delete(this);
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_context_show_frame(lub_heap_context_t * this,
-                            int                  frame)
+void lub_heap_context_show_frame(lub_heap_context_t * this, int frame)
 {
-    if(frame >= 0)
-    {
-        long address = (long)this->key.backtrace[frame]; 
-        if(address)
-        {
-            lub_heap_symShow(address);
-        }
-    }
-    printf("\n");
+	if (frame >= 0) {
+		long address = (long)this->key.backtrace[frame];
+		if (address) {
+			lub_heap_symShow(address);
+		}
+	}
+	printf("\n");
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node_post(lub_heap_node_t *node)
+void lub_heap_node_post(lub_heap_node_t * node)
 {
-    /* assume the worst */
-    if(BOOL_TRUE == lub_heap_node__get_leaked(node))
-    {
-        /* this is a full leak */
-        lub_heap_node__set_partial(node,BOOL_FALSE);
-    }
+	/* assume the worst */
+	if (BOOL_TRUE == lub_heap_node__get_leaked(node)) {
+		/* this is a full leak */
+		lub_heap_node__set_partial(node, BOOL_FALSE);
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node_prep(lub_heap_node_t *node,
-                   void            *arg)
+void lub_heap_node_prep(lub_heap_node_t * node, void *arg)
 {
-    /* assume the worst */
-    lub_heap_node__set_leaked(node,BOOL_TRUE);
-    lub_heap_node__set_partial(node,BOOL_TRUE);
-    lub_heap_node__set_scanned(node,BOOL_FALSE);
+	/* assume the worst */
+	lub_heap_node__set_leaked(node, BOOL_TRUE);
+	lub_heap_node__set_partial(node, BOOL_TRUE);
+	lub_heap_node__set_scanned(node, BOOL_FALSE);
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node_scan(lub_heap_node_t *node,
-                   void            *arg)
+void lub_heap_node_scan(lub_heap_node_t * node, void *arg)
 {
-    /* only scan nodes which have references */
-    if(   (BOOL_FALSE == lub_heap_node__get_leaked(node) )
-       && (BOOL_FALSE == lub_heap_node__get_scanned(node)) )
-    {
-        lub_heap_node__set_scanned(node,BOOL_TRUE);
-        lub_heap_scan_memory(lub_heap_node__get_ptr(node),
-                             lub_heap_node__get_size(node));
-    }
+	/* only scan nodes which have references */
+	if ((BOOL_FALSE == lub_heap_node__get_leaked(node))
+	    && (BOOL_FALSE == lub_heap_node__get_scanned(node))) {
+		lub_heap_node__set_scanned(node, BOOL_TRUE);
+		lub_heap_scan_memory(lub_heap_node__get_ptr(node),
+				     lub_heap_node__get_size(node));
+	}
 }
+
 /*--------------------------------------------------------- */
-static bool_t
-lub_heap_context_prep(lub_heap_context_t *this,
-                      void               *arg)
+static bool_t lub_heap_context_prep(lub_heap_context_t * this, void *arg)
 {
-    /* start off by assuming the worst */
-    this->partials         = this->leaks           = this->allocs;
-    this->partial_bytes    = this->leaked_bytes    = this->alloc_bytes;
-    this->partial_overhead = this->leaked_overhead = this->alloc_overhead;
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        /* initialised the global stats */
-        leak->m_stats.allocs         += this->allocs;
-        leak->m_stats.alloc_bytes    += this->alloc_bytes;
-        leak->m_stats.alloc_overhead += this->alloc_overhead;
-        lub_heap_leak_release(leak);
-    }
-    return BOOL_TRUE;
+	/* start off by assuming the worst */
+	this->partials = this->leaks = this->allocs;
+	this->partial_bytes = this->leaked_bytes = this->alloc_bytes;
+	this->partial_overhead = this->leaked_overhead = this->alloc_overhead;
+	{
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		/* initialised the global stats */
+		leak->m_stats.allocs += this->allocs;
+		leak->m_stats.alloc_bytes += this->alloc_bytes;
+		leak->m_stats.alloc_overhead += this->alloc_overhead;
+		lub_heap_leak_release(leak);
+	}
+	return BOOL_TRUE;
 }
+
 /*--------------------------------------------------------- */
-static bool_t
-lub_heap_context_post(lub_heap_context_t *this,
-                      void               *arg)
+static bool_t lub_heap_context_post(lub_heap_context_t * this, void *arg)
 {
-    /* don't count full leaks as partials */
-    this->partials         -= this->leaks;
-    this->partial_bytes    -= this->leaked_bytes;
-    this->partial_overhead -= this->leaked_overhead;
-    
-    /* post process the contained nodes */
-    lub_heap_context_foreach_node(this,lub_heap_node_post);
-
-    return BOOL_TRUE;
+	/* don't count full leaks as partials */
+	this->partials -= this->leaks;
+	this->partial_bytes -= this->leaked_bytes;
+	this->partial_overhead -= this->leaked_overhead;
+
+	/* post process the contained nodes */
+	lub_heap_context_foreach_node(this, lub_heap_node_post);
+
+	return BOOL_TRUE;
 }
+
 /*--------------------------------------------------------- */
-static void
-scan_segment(void    *ptr,
-             unsigned index,
-             size_t   size,
-             void    *arg)
+static void scan_segment(void *ptr, unsigned index, size_t size, void *arg)
 {
-    lub_heap_segment_t *segment = ptr;
-    const char         *memory  = (const char*)lub_heap_block_getfirst(segment);
-    
-    /* now scan the memory in this segment */
-    printf(".");
-    lub_heap_scan_memory(memory,size);
+	lub_heap_segment_t *segment = ptr;
+	const char *memory = (const char *)lub_heap_block_getfirst(segment);
+
+	/* now scan the memory in this segment */
+	printf(".");
+	lub_heap_scan_memory(memory, size);
 }
+
 /*--------------------------------------------------------- */
 /** 
  * This function scans all the nodes currently allocated in the
@@ -366,455 +337,418 @@ scan_segment(void    *ptr,
  * At the end of the process all nodes which are leaked then
  * update their context leak count.
  */
-void
-lub_heap_scan_all(void)
+void lub_heap_scan_all(void)
 {
-    lub_heap_t      *heap;
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    
-
-    /* clear the summary stats */
-    memset(&leak->m_stats,0,sizeof(leak->m_stats));
-
-    lub_heap_leak_release(leak);
-    /* first of all prepare the contexts for scanning */
-    lub_heap_foreach_context(lub_heap_context_prep,0);
-
-    /* then prepare all the nodes (including those who have no context) */
-    lub_heap_foreach_node(lub_heap_node_prep,0);
-    
-    printf("  Scanning memory");
-    /* clear out the stacks in the system */
-    lub_heap_clean_stacks();
-    
-    /* Scan the current stack */
-    printf(".");
-    lub_heap_scan_stack();
-    
-    /* Scan the BSS segment */
-    printf(".");
-    lub_heap_scan_bss();
-    
-    /* Scan the DATA segment */
-    printf(".");
-    lub_heap_scan_data();
-    
-    /* Scan the non-monitored blocks which are allocated in the system */
-    leak = lub_heap_leak_instance();
-    for(heap = leak->m_heap_list;
-        heap;
-        heap = heap->next) 
-    {
-        lub_heap_leak_release(leak);
-        lub_heap_foreach_segment(heap,scan_segment,0);
-        leak = lub_heap_leak_instance();
-    }
-    lub_heap_leak_release(leak);
-    
-    /* 
-     * now scan the nodes NB. only referenced nodes will be scanned 
-     * we loop until we stop scanning new nodes
-     */
-    leak = lub_heap_leak_instance();
-    do
-    {
-        leak->m_stats.scanned = 0;
-        /* scan each node */
-        lub_heap_leak_release(leak);
-        printf(".");
-        lub_heap_foreach_node(lub_heap_node_scan,NULL);
-        leak = lub_heap_leak_instance();
-    } while(leak->m_stats.scanned);
-        
-    printf("done\n\n");
-
-    /* post process each context and contained nodes */
-    lub_heap_leak_release(leak);
-    lub_heap_foreach_context(lub_heap_context_post,0);
-    leak = lub_heap_leak_instance();
-    
-    
-    lub_heap_leak_release(leak);
+	lub_heap_t *heap;
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+
+	/* clear the summary stats */
+	memset(&leak->m_stats, 0, sizeof(leak->m_stats));
+
+	lub_heap_leak_release(leak);
+	/* first of all prepare the contexts for scanning */
+	lub_heap_foreach_context(lub_heap_context_prep, 0);
+
+	/* then prepare all the nodes (including those who have no context) */
+	lub_heap_foreach_node(lub_heap_node_prep, 0);
+
+	printf("  Scanning memory");
+	/* clear out the stacks in the system */
+	lub_heap_clean_stacks();
+
+	/* Scan the current stack */
+	printf(".");
+	lub_heap_scan_stack();
+
+	/* Scan the BSS segment */
+	printf(".");
+	lub_heap_scan_bss();
+
+	/* Scan the DATA segment */
+	printf(".");
+	lub_heap_scan_data();
+
+	/* Scan the non-monitored blocks which are allocated in the system */
+	leak = lub_heap_leak_instance();
+	for (heap = leak->m_heap_list; heap; heap = heap->next) {
+		lub_heap_leak_release(leak);
+		lub_heap_foreach_segment(heap, scan_segment, 0);
+		leak = lub_heap_leak_instance();
+	}
+	lub_heap_leak_release(leak);
+
+	/* 
+	 * now scan the nodes NB. only referenced nodes will be scanned 
+	 * we loop until we stop scanning new nodes
+	 */
+	leak = lub_heap_leak_instance();
+	do {
+		leak->m_stats.scanned = 0;
+		/* scan each node */
+		lub_heap_leak_release(leak);
+		printf(".");
+		lub_heap_foreach_node(lub_heap_node_scan, NULL);
+		leak = lub_heap_leak_instance();
+	} while (leak->m_stats.scanned);
+
+	printf("done\n\n");
+
+	/* post process each context and contained nodes */
+	lub_heap_leak_release(leak);
+	lub_heap_foreach_context(lub_heap_context_post, 0);
+	leak = lub_heap_leak_instance();
+
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
-void lub_heap_node_show(lub_heap_node_t *node)
+void lub_heap_node_show(lub_heap_node_t * node)
 {
-    printf("%s%p[%"SIZE_FMT"] ",
-           lub_heap_node__get_leaked(node) ? "*" : lub_heap_node__get_partial(node) ? "+" : "",
-           lub_heap_node__get_ptr(node),
-           lub_heap_node__get_size(node));
+	printf("%s%p[%" SIZE_FMT "] ",
+	       lub_heap_node__get_leaked(node) ? "*" :
+	       lub_heap_node__get_partial(node) ? "+" : "",
+	       lub_heap_node__get_ptr(node), lub_heap_node__get_size(node));
 }
+
 /*--------------------------------------------------------- */
 static bool_t
-lub_heap_context_match(lub_heap_context_t *this,
-                       const char         *substring)
+lub_heap_context_match(lub_heap_context_t * this, const char *substring)
 {
-    bool_t result = BOOL_TRUE;
-    if(substring)
-    {
-        int  i;
-        long address;
-        result = BOOL_FALSE;
-        /* 
-         * search the stacktrace for this context
-         * to see whether it matches
-         */
-        for(i = 0;
-            (address = (long)this->key.backtrace[i]);
-            ++i)
-        {
-            if(lub_heap_symMatch(address,substring))
-            {
-                result = BOOL_TRUE;
-                break;
-            }
-        }
-    }
-    return result;
+	bool_t result = BOOL_TRUE;
+	if (substring) {
+		int i;
+		long address;
+		result = BOOL_FALSE;
+		/* 
+		 * search the stacktrace for this context
+		 * to see whether it matches
+		 */
+		for (i = 0; (address = (long)this->key.backtrace[i]); ++i) {
+			if (lub_heap_symMatch(address, substring)) {
+				result = BOOL_TRUE;
+				break;
+			}
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */
-typedef struct
-{
-    lub_heap_show_e how;
-    const char     *substring;
+typedef struct {
+	lub_heap_show_e how;
+	const char *substring;
 } context_show_arg_t;
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_context_show_fn(lub_heap_context_t *this,
-                         void                *arg)
+bool_t lub_heap_context_show_fn(lub_heap_context_t * this, void *arg)
 {
-    bool_t              result = BOOL_FALSE;
-    context_show_arg_t *show_arg = arg;
-    
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    /* add in the context details */
-    ++leak->m_stats.contexts;
-    leak->m_stats.allocs           += this->allocs;
-    leak->m_stats.alloc_bytes      += this->alloc_bytes;
-    leak->m_stats.alloc_overhead   += this->alloc_overhead;
-    leak->m_stats.partials         += this->partials;
-    leak->m_stats.partial_bytes    += this->partial_bytes;
-    leak->m_stats.partial_overhead += this->partial_overhead;
-    leak->m_stats.leaks            += this->leaks;
-    leak->m_stats.leaked_bytes     += this->leaked_bytes;
-    leak->m_stats.leaked_overhead  += this->leaked_overhead;
-    lub_heap_leak_release(leak);
-    
-    
-    if(lub_heap_context_match(this,show_arg->substring))
-    {
-        result = lub_heap_context_show(this,show_arg->how);
-    }
-    return result;
+	bool_t result = BOOL_FALSE;
+	context_show_arg_t *show_arg = arg;
+
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	/* add in the context details */
+	++leak->m_stats.contexts;
+	leak->m_stats.allocs += this->allocs;
+	leak->m_stats.alloc_bytes += this->alloc_bytes;
+	leak->m_stats.alloc_overhead += this->alloc_overhead;
+	leak->m_stats.partials += this->partials;
+	leak->m_stats.partial_bytes += this->partial_bytes;
+	leak->m_stats.partial_overhead += this->partial_overhead;
+	leak->m_stats.leaks += this->leaks;
+	leak->m_stats.leaked_bytes += this->leaked_bytes;
+	leak->m_stats.leaked_overhead += this->leaked_overhead;
+	lub_heap_leak_release(leak);
+
+	if (lub_heap_context_match(this, show_arg->substring)) {
+		result = lub_heap_context_show(this, show_arg->how);
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_context_show(lub_heap_context_t *this,
-                      lub_heap_show_e     how)
+bool_t lub_heap_context_show(lub_heap_context_t * this, lub_heap_show_e how)
 {
-    long   frame       = lub_heap_frame_count-1;
-    size_t ok_allocs   = this->allocs;
-    size_t ok_bytes    = this->alloc_bytes;
-    size_t ok_overhead = this->alloc_overhead;
-    
-    ok_allocs -= this->partials;
-    ok_allocs -= this->leaks;
-
-    ok_bytes  -= this->partial_bytes;
-    ok_bytes  -= this->leaked_bytes;
-
-    ok_overhead  -= this->partial_overhead;
-    ok_overhead  -= this->leaked_overhead;
-
-    switch(how)
-    {
-        case LUB_HEAP_SHOW_ALL:
-        {
-            /* show everything */
-            break;
-        }
-        case LUB_HEAP_SHOW_PARTIALS:
-        {
-            if(0 < this->partials)
-            {
-                /* there's at least one partial in this context */
-                break;
-            }
-            /*lint -e(616) fall through */
-        }
-        case LUB_HEAP_SHOW_LEAKS:
-        {
-            if(0 < this->leaks)
-            {
-                /* there's at least one leak in this context */
-                break;
-            }
-            /*lint -e(616) fall through */
-        }
-        default:
-        {
-            /* nothing to be shown */
-            return BOOL_FALSE;
-        }
-    }
-
-    /* find the top of the stack trace */
-    while((frame >= 0) && (0 == this->key.backtrace[frame]))
-    {
-        --frame;
-    }
-    printf("          +----------+----------+----------+----------+");
-    lub_heap_context_show_frame(this,frame--);
-    printf(      "%10p|    blocks|     bytes|   average|  overhead|",(void*)this);
-    lub_heap_context_show_frame(this,frame--);
-    printf("+---------+----------+----------+----------+----------+");
-    lub_heap_context_show_frame(this,frame--);
-
-    printf("|allocs   |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|",
-            ok_allocs,
-            ok_bytes,
-            ok_allocs ? (ok_bytes / ok_allocs) : 0,
-            ok_overhead);
-    lub_heap_context_show_frame(this,frame--);
-
-    if(this->partials)
-    {
-        printf("|partials |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|",
-                this->partials,
-                this->partial_bytes,
-                this->partials ? (this->partial_bytes/this->partials) : 0,
-                this->partial_overhead);
-        lub_heap_context_show_frame(this,frame--);
-    }
-    if(this->leaks)
-    {
-        printf("|leaks    |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|",
-                this->leaks,
-                this->leaked_bytes,
-                this->leaks ? (this->leaked_bytes/this->leaks) : 0,
-                this->leaked_overhead);
-        lub_heap_context_show_frame(this,frame--);
-    }
-
-    printf("+---------+----------+----------+----------+----------+");
-    lub_heap_context_show_frame(this,frame--);
-
-    while(frame >= 0)
-    {
-        printf("%55s","");
-        lub_heap_context_show_frame(this,frame--);
-    }
-
-    printf("ALLOCATED BLOCKS: ");
-    /* now iterate the allocated nodes */
-    lub_heap_context_foreach_node(this,lub_heap_node_show);
-    printf("\n\n");
-    
-    return BOOL_TRUE;
+	long frame = lub_heap_frame_count - 1;
+	size_t ok_allocs = this->allocs;
+	size_t ok_bytes = this->alloc_bytes;
+	size_t ok_overhead = this->alloc_overhead;
+
+	ok_allocs -= this->partials;
+	ok_allocs -= this->leaks;
+
+	ok_bytes -= this->partial_bytes;
+	ok_bytes -= this->leaked_bytes;
+
+	ok_overhead -= this->partial_overhead;
+	ok_overhead -= this->leaked_overhead;
+
+	switch (how) {
+	case LUB_HEAP_SHOW_ALL:
+		{
+			/* show everything */
+			break;
+		}
+	case LUB_HEAP_SHOW_PARTIALS:
+		{
+			if (0 < this->partials) {
+				/* there's at least one partial in this context */
+				break;
+			}
+			/*lint -e(616) fall through */
+		}
+	case LUB_HEAP_SHOW_LEAKS:
+		{
+			if (0 < this->leaks) {
+				/* there's at least one leak in this context */
+				break;
+			}
+			/*lint -e(616) fall through */
+		}
+	default:
+		{
+			/* nothing to be shown */
+			return BOOL_FALSE;
+		}
+	}
+
+	/* find the top of the stack trace */
+	while ((frame >= 0) && (0 == this->key.backtrace[frame])) {
+		--frame;
+	}
+	printf("          +----------+----------+----------+----------+");
+	lub_heap_context_show_frame(this, frame--);
+	printf("%10p|    blocks|     bytes|   average|  overhead|",
+	       (void *)this);
+	lub_heap_context_show_frame(this, frame--);
+	printf("+---------+----------+----------+----------+----------+");
+	lub_heap_context_show_frame(this, frame--);
+
+	printf("|allocs   |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT "|%10"
+	       SIZE_FMT "|", ok_allocs, ok_bytes,
+	       ok_allocs ? (ok_bytes / ok_allocs) : 0, ok_overhead);
+	lub_heap_context_show_frame(this, frame--);
+
+	if (this->partials) {
+		printf("|partials |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT
+		       "|%10" SIZE_FMT "|", this->partials, this->partial_bytes,
+		       this->partials ? (this->partial_bytes /
+					 this->partials) : 0,
+		       this->partial_overhead);
+		lub_heap_context_show_frame(this, frame--);
+	}
+	if (this->leaks) {
+		printf("|leaks    |%10" SIZE_FMT "|%10" SIZE_FMT "|%10" SIZE_FMT
+		       "|%10" SIZE_FMT "|", this->leaks, this->leaked_bytes,
+		       this->leaks ? (this->leaked_bytes / this->leaks) : 0,
+		       this->leaked_overhead);
+		lub_heap_context_show_frame(this, frame--);
+	}
+
+	printf("+---------+----------+----------+----------+----------+");
+	lub_heap_context_show_frame(this, frame--);
+
+	while (frame >= 0) {
+		printf("%55s", "");
+		lub_heap_context_show_frame(this, frame--);
+	}
+
+	printf("ALLOCATED BLOCKS: ");
+	/* now iterate the allocated nodes */
+	lub_heap_context_foreach_node(this, lub_heap_node_show);
+	printf("\n\n");
+
+	return BOOL_TRUE;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_context_clear(lub_heap_context_t *this)
+void lub_heap_context_clear(lub_heap_context_t * this)
 {
-    /* should only ever get cleared when all nodes have left the context */
-    assert(0 == this->first_node);
-    this->heap = 0;
-
-    /* reset the counters */
-    this->allocs        = 0;
-    this->alloc_bytes   = 0;
-    this->leaks         = 0;
-    this->leaked_bytes  = 0;
-    this->partials      = 0;
-    this->partial_bytes = 0;
+	/* should only ever get cleared when all nodes have left the context */
+	assert(0 == this->first_node);
+	this->heap = 0;
+
+	/* reset the counters */
+	this->allocs = 0;
+	this->alloc_bytes = 0;
+	this->leaks = 0;
+	this->leaked_bytes = 0;
+	this->partials = 0;
+	this->partial_bytes = 0;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_show_leak_trees(void)
+void lub_heap_show_leak_trees(void)
 {
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    printf("context_tree   : ");
-    lub_bintree_dump(&leak->m_context_tree);
-    printf("\n");
-    printf("node_tree      : ");
-    lub_bintree_dump(&leak->m_node_tree);
-    printf("\n");
-    printf("clear_node_tree: ");
-    lub_bintree_dump(&leak->m_clear_node_tree);
-    printf("\n");
-    lub_heap_leak_release(leak);
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	printf("context_tree   : ");
+	lub_bintree_dump(&leak->m_context_tree);
+	printf("\n");
+	printf("node_tree      : ");
+	lub_bintree_dump(&leak->m_node_tree);
+	printf("\n");
+	printf("clear_node_tree: ");
+	lub_bintree_dump(&leak->m_clear_node_tree);
+	printf("\n");
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_leak_scan(void)
+void lub_heap_leak_scan(void)
 {
-    if(0 < lub_heap_frame_count)
-    {
-        /* check for memory leaks */
-        if(!lub_heap_is_tainting())
-        {
-            printf("******************************************"
-                   "*** Memory tainting is disabled so results\n"
-                   "*** of scan may miss some real leaks!\n"
-                   "******************************************\n\n");
-        }
-        lub_heap_scan_all();
-    }
-    else
-    {
-        printf("Leak detection currently disabled\n\n");
-    }
+	if (0 < lub_heap_frame_count) {
+		/* check for memory leaks */
+		if (!lub_heap_is_tainting()) {
+			printf("******************************************"
+			       "*** Memory tainting is disabled so results\n"
+			       "*** of scan may miss some real leaks!\n"
+			       "******************************************\n\n");
+		}
+		lub_heap_scan_all();
+	} else {
+		printf("Leak detection currently disabled\n\n");
+	}
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_leak_report(lub_heap_show_e how,
-                     const char     *substring)
+bool_t lub_heap_leak_report(lub_heap_show_e how, const char *substring)
 {
-    bool_t result = BOOL_FALSE;
-    
-    if(0 < lub_heap_frame_count)
-    {
-        bool_t             bad_arg = BOOL_FALSE;
-        lub_heap_leak_t   *leak    = lub_heap_leak_instance();
-        context_show_arg_t show_arg;
-        show_arg.how       = how;
-        show_arg.substring = substring;
-        
-        /* zero these stats which will be accumulated from the contexts */
-        leak->m_stats.contexts         = 0;
-        leak->m_stats.allocs           = 0;
-        leak->m_stats.alloc_bytes      = 0;
-        leak->m_stats.alloc_overhead   = 0;
-        leak->m_stats.partials         = 0;
-        leak->m_stats.partial_bytes    = 0;
-        leak->m_stats.partial_overhead = 0;
-        leak->m_stats.leaks            = 0;
-        leak->m_stats.leaked_bytes     = 0;
-        leak->m_stats.leaked_overhead  = 0;
-        lub_heap_leak_release(leak);
-        
-        switch(how)
-        {
-            case LUB_HEAP_SHOW_LEAKS:
-            case LUB_HEAP_SHOW_PARTIALS:
-            case LUB_HEAP_SHOW_ALL:
-            {
-                result = lub_heap_foreach_context(lub_heap_context_show_fn,(void*)&show_arg);
-                break;
-            }
-            default:
-            {
-                printf("Invalid argument: 0=leaks, 1=partials and leaks, 2=all allocations\n\n");
-                bad_arg = BOOL_TRUE;
-                break;
-            }
-        }
-        if(BOOL_FALSE == bad_arg)
-        {
-            leak = lub_heap_leak_instance();
-            if(leak->m_stats.leaks)
-            {
-                printf("  '*' = leaked memory\n");
-            }
-            if(leak->m_stats.partials)
-            {
-                printf("  '+' = partially leaked memory\n");
-            }
-            lub_heap_leak_release(leak);
-            lub_heap_show_summary();
-            printf("  %lu stack frames held for each allocation.\n\n",lub_heap_frame_count);
-        }
-    }
-    else
-    {
-        printf("Leak detection currently disabled\n\n");
-    }
-    return result;
+	bool_t result = BOOL_FALSE;
+
+	if (0 < lub_heap_frame_count) {
+		bool_t bad_arg = BOOL_FALSE;
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		context_show_arg_t show_arg;
+		show_arg.how = how;
+		show_arg.substring = substring;
+
+		/* zero these stats which will be accumulated from the contexts */
+		leak->m_stats.contexts = 0;
+		leak->m_stats.allocs = 0;
+		leak->m_stats.alloc_bytes = 0;
+		leak->m_stats.alloc_overhead = 0;
+		leak->m_stats.partials = 0;
+		leak->m_stats.partial_bytes = 0;
+		leak->m_stats.partial_overhead = 0;
+		leak->m_stats.leaks = 0;
+		leak->m_stats.leaked_bytes = 0;
+		leak->m_stats.leaked_overhead = 0;
+		lub_heap_leak_release(leak);
+
+		switch (how) {
+		case LUB_HEAP_SHOW_LEAKS:
+		case LUB_HEAP_SHOW_PARTIALS:
+		case LUB_HEAP_SHOW_ALL:
+			{
+				result =
+				    lub_heap_foreach_context
+				    (lub_heap_context_show_fn,
+				     (void *)&show_arg);
+				break;
+			}
+		default:
+			{
+				printf
+				    ("Invalid argument: 0=leaks, 1=partials and leaks, 2=all allocations\n\n");
+				bad_arg = BOOL_TRUE;
+				break;
+			}
+		}
+		if (BOOL_FALSE == bad_arg) {
+			leak = lub_heap_leak_instance();
+			if (leak->m_stats.leaks) {
+				printf("  '*' = leaked memory\n");
+			}
+			if (leak->m_stats.partials) {
+				printf("  '+' = partially leaked memory\n");
+			}
+			lub_heap_leak_release(leak);
+			lub_heap_show_summary();
+			printf
+			    ("  %lu stack frames held for each allocation.\n\n",
+			     lub_heap_frame_count);
+		}
+	} else {
+		printf("Leak detection currently disabled\n\n");
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */
 
 /*--------------------------------------------------------- 
  * PUBLIC META FUNCTIONS
  *--------------------------------------------------------- */
-void
-lub_heap__set_framecount(unsigned frames)
+void lub_heap__set_framecount(unsigned frames)
 {
-    static bool_t    clear_tainting_on_disable = BOOL_FALSE;
-    lub_heap_leak_t *leak;
-    
-    if(frames == lub_heap_frame_count)
-        return;
-    
-    /* iterate around clearing all the nodes in the node_tree */
-    lub_heap_foreach_node(lub_heap_node_clear,0);
-    
-    leak = lub_heap_leak_instance();
-    if(frames)
-    {
-        static bool_t registered = BOOL_FALSE;
-        if(frames <= MAX_BACKTRACE)
-        {
-            /* change the number of frames held */
-            lub_heap_frame_count = frames;
-        }
-        else
-        {
-            fprintf(stderr,"--- leak-detection frame count set to a maximum of %d\n",MAX_BACKTRACE);
-            lub_heap_frame_count = MAX_BACKTRACE;
-        }
-        if(!lub_heap_is_tainting())
-        {
-            /* we need to taint memory to ensure old pointers don't mask leaks */
-            clear_tainting_on_disable = BOOL_TRUE;
-            lub_heap_taint(BOOL_TRUE);
-        }
-        if(BOOL_FALSE == registered)
-        {
-            registered = BOOL_TRUE;
-            /* 
-             * set up the context pool
-             */
-            lub_dblockpool_init(&leak->m_context_pool,
-                                sizeof(lub_heap_context_t),
-                                CONTEXT_CHUNK_SIZE,
-                                CONTEXT_MAX_CHUNKS);
-        }
-    }
-    else
-    {
-        /* switch off leak detection */
-        lub_heap_frame_count = 0;
-        if(clear_tainting_on_disable)
-        {
-            lub_heap_taint(BOOL_FALSE);
-            clear_tainting_on_disable = BOOL_FALSE;
-        }
-    }
-    lub_heap_leak_release(leak);   
+	static bool_t clear_tainting_on_disable = BOOL_FALSE;
+	lub_heap_leak_t *leak;
+
+	if (frames == lub_heap_frame_count)
+		return;
+
+	/* iterate around clearing all the nodes in the node_tree */
+	lub_heap_foreach_node(lub_heap_node_clear, 0);
+
+	leak = lub_heap_leak_instance();
+	if (frames) {
+		static bool_t registered = BOOL_FALSE;
+		if (frames <= MAX_BACKTRACE) {
+			/* change the number of frames held */
+			lub_heap_frame_count = frames;
+		} else {
+			fprintf(stderr,
+				"--- leak-detection frame count set to a maximum of %d\n",
+				MAX_BACKTRACE);
+			lub_heap_frame_count = MAX_BACKTRACE;
+		}
+		if (!lub_heap_is_tainting()) {
+			/* we need to taint memory to ensure old pointers don't mask leaks */
+			clear_tainting_on_disable = BOOL_TRUE;
+			lub_heap_taint(BOOL_TRUE);
+		}
+		if (BOOL_FALSE == registered) {
+			registered = BOOL_TRUE;
+			/* 
+			 * set up the context pool
+			 */
+			lub_dblockpool_init(&leak->m_context_pool,
+					    sizeof(lub_heap_context_t),
+					    CONTEXT_CHUNK_SIZE,
+					    CONTEXT_MAX_CHUNKS);
+		}
+	} else {
+		/* switch off leak detection */
+		lub_heap_frame_count = 0;
+		if (clear_tainting_on_disable) {
+			lub_heap_taint(BOOL_FALSE);
+			clear_tainting_on_disable = BOOL_FALSE;
+		}
+	}
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
-unsigned
-    lub_heap__get_framecount(void)
+unsigned lub_heap__get_framecount(void)
 {
-    return lub_heap_frame_count;
+	return lub_heap_frame_count;
 }
+
 /*--------------------------------------------------------- */
-size_t
-    lub_heap_context__get_instanceSize(void)
+size_t lub_heap_context__get_instanceSize(void)
 {
-    return (sizeof(lub_heap_context_t));
+	return (sizeof(lub_heap_context_t));
 }
+
 /*--------------------------------------------------------- */
-lub_heap_context_t *
-lub_heap_context_find(const stackframe_t *stack)
+lub_heap_context_t *lub_heap_context_find(const stackframe_t * stack)
 {
-    lub_heap_context_t    *result;
-    lub_heap_leak_t       *leak = lub_heap_leak_instance();
-    lub_heap_context_key_t key  = *stack;
+	lub_heap_context_t *result;
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	lub_heap_context_key_t key = *stack;
 
-    result = lub_bintree_find(&leak->m_context_tree,&key);
+	result = lub_bintree_find(&leak->m_context_tree, &key);
 
-    lub_heap_leak_release(leak);
-    return result;
+	lub_heap_leak_release(leak);
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 99 - 129
lub/heap/context.h

@@ -7,110 +7,102 @@
 #define MAX_BACKTRACE (32)
 
 typedef struct _lub_heap_stackframe lub_heap_context_key_t;
-typedef void (function_t)(void);
+typedef void (function_t) (void);
 
-struct _lub_heap_stackframe
-{
-    function_t * backtrace[MAX_BACKTRACE];
+struct _lub_heap_stackframe {
+	function_t *backtrace[MAX_BACKTRACE];
 };
 
 typedef struct _lub_heap_leak_stats lub_heap_leak_stats_t;
-struct _lub_heap_leak_stats
-{
-    size_t contexts;
-    size_t allocs;
-    size_t alloc_bytes;
-    size_t alloc_overhead;
-    size_t leaks;
-    size_t leaked_bytes;
-    size_t leaked_overhead;
-    size_t partials;
-    size_t partial_bytes;
-    size_t partial_overhead;
-    size_t scanned;
+struct _lub_heap_leak_stats {
+	size_t contexts;
+	size_t allocs;
+	size_t alloc_bytes;
+	size_t alloc_overhead;
+	size_t leaks;
+	size_t leaked_bytes;
+	size_t leaked_overhead;
+	size_t partials;
+	size_t partial_bytes;
+	size_t partial_overhead;
+	size_t scanned;
 };
 
-struct _lub_heap_context
-{
+struct _lub_heap_context {
     /**
      * The heap to which this context belongs
      */
-    lub_heap_t *heap;
+	lub_heap_t *heap;
     /**
      * This is needed to maintain a tree of contexts
      */
-    lub_bintree_node_t bt_node;
+	lub_bintree_node_t bt_node;
     /**
      * The first node in this context
      */
-     lub_heap_node_t *first_node;
+	lub_heap_node_t *first_node;
     /**
      * current number of allocations made from this context
      */
-    size_t allocs;
+	size_t allocs;
     /**
      * current number of bytes allocated from this context
      */
-    size_t alloc_bytes;
+	size_t alloc_bytes;
     /**
      * current overhead in bytes allocated from this context
      */
-    size_t alloc_overhead;
+	size_t alloc_overhead;
     /**
      * count of leaked allocations made from this context
      */
-    size_t leaks;
+	size_t leaks;
     /**
      * number of leaked bytes from this context
      */
-    size_t leaked_bytes;
+	size_t leaked_bytes;
     /**
      * current overhead in bytes leaked from this context
      */
-    size_t leaked_overhead;
+	size_t leaked_overhead;
     /**
      * count of partially leaked allocations made from this context
      */
-    size_t partials;
+	size_t partials;
     /**
      * number of partially leaked bytes from this context
      */
-    size_t partial_bytes;
+	size_t partial_bytes;
     /**
      * current overhead in bytes partially leaked from this context
      */
-    size_t partial_overhead;
+	size_t partial_overhead;
     /**
      * a record of the memory partition associated with this context
      */
-    lub_heap_context_key_t key;
+	lub_heap_context_key_t key;
 };
 
 extern unsigned long lub_heap_frame_count;
 
 typedef struct _lub_heap_leak lub_heap_leak_t;
-struct _lub_heap_leak
-{
-    lub_bintree_t         m_context_tree;
-    lub_bintree_t         m_node_tree;
-    lub_bintree_t         m_clear_node_tree;
-    lub_bintree_t         m_segment_tree;
-    lub_heap_leak_stats_t m_stats;
-    lub_dblockpool_t      m_context_pool;
-    lub_heap_t           *m_heap_list;
+struct _lub_heap_leak {
+	lub_bintree_t m_context_tree;
+	lub_bintree_t m_node_tree;
+	lub_bintree_t m_clear_node_tree;
+	lub_bintree_t m_segment_tree;
+	lub_heap_leak_stats_t m_stats;
+	lub_dblockpool_t m_context_pool;
+	lub_heap_t *m_heap_list;
 };
 
-extern lub_heap_leak_t *
-    lub_heap_leak_instance(void);
+extern lub_heap_leak_t *lub_heap_leak_instance(void);
 
-extern void
-    lub_heap_leak_release(lub_heap_leak_t *instance);
+extern void lub_heap_leak_release(lub_heap_leak_t * instance);
 
-extern bool_t
-    lub_heap_leak_query_node_tree(void);
+extern bool_t lub_heap_leak_query_node_tree(void);
 
-extern bool_t
-    lub_heap_leak_query_clear_node_tree(void);
+extern bool_t lub_heap_leak_query_clear_node_tree(void);
 /*---------------------------------------------------------
  * PUBLIC META ATTRIBUTES
  *--------------------------------------------------------- */
@@ -119,11 +111,10 @@ extern bool_t
  *
  * By default this is set to four.
  */
-size_t
-    lub_heap_context__get_instanceSize(void);
+size_t lub_heap_context__get_instanceSize(void);
 
 extern lub_bintree_compare_fn lub_heap_context_compare;
-extern lub_bintree_getkey_fn  lub_heap_context_getkey;
+extern lub_bintree_getkey_fn lub_heap_context_getkey;
 
 /*---------------------------------------------------------
  * PUBLIC METHODS
@@ -138,21 +129,22 @@ extern lub_bintree_getkey_fn  lub_heap_context_getkey;
  * the same stack frames will be collected together in the same context.
  *
  * \return Pointer to a context object or NULL if one cannot be found or allocated.
- */ 
- const lub_heap_context_t *
-    lub_heap_context_find_or_create(
-        /** 
+ */
+const lub_heap_context_t *lub_heap_context_find_or_create(
+	/** 
          * The heap in question
          */
-        lub_heap_t *instance,
-        /** 
+								 lub_heap_t *
+								 instance,
+	/** 
          * The memory allocation function must allocate a
          * local variable, on it's stack, which is passed in 
          * to this call as a means of getting access to the current
          * thread's call stack.
          */
-        const stackframe_t * stack
-    );
+								 const
+								 stackframe_t *
+								 stack);
 /**
  * This function finds a context based on the
  * provided leak detector and stack specification.
@@ -163,152 +155,130 @@ extern lub_bintree_getkey_fn  lub_heap_context_getkey;
  * the same stack frames will be collected together in the same context.
  *
  * \return Pointer to a context object or NULL if one cannot be found.
- */ 
-lub_heap_context_t *
-    lub_heap_context_find(
-        /** 
+ */
+lub_heap_context_t *lub_heap_context_find(
+	/** 
          * The memory allocation function must allocate a
          * local variable, on it's stack, which is passed in 
          * to this call as a means of getting access to the current
          * thread's call stack.
          */
-        const stackframe_t * stack
-    );
-    
+						 const stackframe_t * stack);
+
 /*---------------------------------------------------------
  * PUBLIC METHODS
  *--------------------------------------------------------- */
 /**
  * This function initialises an instance of a context.
  */
-void
-    lub_heap_context_init(
-        /** 
+void lub_heap_context_init(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance,
-        /** 
+				  lub_heap_context_t * instance,
+	/** 
          * heap with with to associate this context
          */
-        lub_heap_t *heap,
-        /**
+				  lub_heap_t * heap,
+	/**
          * The memory allocation function must allocate a
          * local variable, on it's stack, which is passed in 
          * to this call as a means of getting access to the current
          * thread's call stack.
          */
-        const stackframe_t * stack
-    );
+				  const stackframe_t * stack);
 /**
  * This function finalises an instance of a context.
  * 
  * \return the memory partition from which this context was allocated.
  */
-void
-    lub_heap_context_fini(
-        /** 
+void lub_heap_context_fini(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance
-    );
+				  lub_heap_context_t * instance);
 /**
  * This function deletes an instance of a context
  * 
  * \return BOOL_TRUE if deletion was sucessful
  */
-bool_t
-    lub_heap_context_delete(
-        /** 
+bool_t lub_heap_context_delete(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance
-    );
+				      lub_heap_context_t * instance);
 /**
  * This function adds the specified node to the context tree.
  *
  * \return BOOL_TRUE  if the node was added to the tree,
  *          BOOL_FALSE if node was already in the tree.
  */
-bool_t
-    lub_heap_context_add_node(
-        /** 
+bool_t lub_heap_context_add_node(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance,
-        /**
+					lub_heap_context_t * instance,
+	/**
          * The node to add to this context 
          */
-         lub_heap_node_t *node,
-        /**
+					lub_heap_node_t * node,
+	/**
          * The size in bytes of the allocation 
          */
-         size_t size
-    );
+					size_t size);
 /**
  * This function removes the specified node from the context tree.
  *
  * \return BOOL_TRUE  if the node was found and removed,
  *          BOOL_FALSE if the node was not in the tree
- */ 
-void
-    lub_heap_context_remove_node(
-        /** 
+ */
+void lub_heap_context_remove_node(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance,
-        /**
+					 lub_heap_context_t * instance,
+	/**
          * The node to add to this context 
          */
-         lub_heap_node_t *node
-    );
+					 lub_heap_node_t * node);
 /**
  * This function dumps details of the specified context to stdout
  *
  * /return
  * - BOOL_TRUE if any details were dumped.
  * - BOOL_FALSE if no details were dumped.
- */ 
-bool_t 
-    lub_heap_context_show(
-        /** 
+ */
+bool_t lub_heap_context_show(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance,
-        /** 
+				    lub_heap_context_t * instance,
+	/** 
          * How to dump the information
          */
-        lub_heap_show_e how
-    );
+				    lub_heap_show_e how);
 /**
  * This function "clears" the currently allocated blocks so that lub_heap_context_dump
  * will not show them
- */ 
-void 
-    lub_heap_context_clear(
-        /** 
+ */
+void lub_heap_context_clear(
+	/** 
          * Context instance on which to operate
          */
-        lub_heap_context_t *instance
-    );
+				   lub_heap_context_t * instance);
 /**
  * This places a node into the context's tree
  */
- void
-    lub_heap_context_insert_node(
-        lub_heap_context_t * instance,
-        lub_heap_node_t * node
-    );
+void
+lub_heap_context_insert_node(lub_heap_context_t * instance,
+			     lub_heap_node_t * node);
 /*---------------------------------------------------------
  * PUBLIC ATTRIBUTES
  *--------------------------------------------------------- */
 /**
  * Identify whether this is a valid address within the heap
  */
- bool_t
-    leaks_is_valid_heap_address(lub_heap_t *instance,
-                                const void *ptr);
+bool_t leaks_is_valid_heap_address(lub_heap_t * instance, const void *ptr);
 
-void
-    lub_heap_leak_mutex_lock(void);
-void
-    lub_heap_leak_mutex_unlock(void);
+void lub_heap_leak_mutex_lock(void);
+void lub_heap_leak_mutex_unlock(void);

+ 13 - 12
lub/heap/heap__get_max_free.c

@@ -1,18 +1,19 @@
 #include "cache.h"
 
 /*--------------------------------------------------------- */
-size_t
-lub_heap__get_max_free(lub_heap_t *this)
+size_t lub_heap__get_max_free(lub_heap_t * this)
 {
-    size_t                 result = 0;
-    lub_heap_free_block_t *free_block;
-    
-    /* get the last block in the tree */
-    free_block = lub_bintree_findlast(&this->free_tree);
-    if(NULL != free_block)
-    {
-        result = (free_block->tag.words << 2) - sizeof(lub_heap_alloc_block_t);
-    }
-    return result;
+	size_t result = 0;
+	lub_heap_free_block_t *free_block;
+
+	/* get the last block in the tree */
+	free_block = lub_bintree_findlast(&this->free_tree);
+	if (NULL != free_block) {
+		result =
+		    (free_block->tag.words << 2) -
+		    sizeof(lub_heap_alloc_block_t);
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 24 - 26
lub/heap/heap__get_stacktrace.c

@@ -8,9 +8,7 @@
 #include "context.h"
 
 /*--------------------------------------------------------- */
-void
-lub_heap__get_stackframe(stackframe_t *stack,
-                         unsigned      max)
+void lub_heap__get_stackframe(stackframe_t * stack, unsigned max)
 {
 #define GET_RETURN_ADDRESS(n)                                                       \
     {                                                                               \
@@ -29,28 +27,28 @@ lub_heap__get_stackframe(stackframe_t *stack,
         }                                                                           \
     }                                                                               \
 
-    memset(stack,0,sizeof(*stack));
-    do
-    {
-        GET_RETURN_ADDRESS(0);
-        GET_RETURN_ADDRESS(1);
-        GET_RETURN_ADDRESS(2);
-        GET_RETURN_ADDRESS(3);
-        GET_RETURN_ADDRESS(4);
-        GET_RETURN_ADDRESS(5);
-        GET_RETURN_ADDRESS(6);
-        GET_RETURN_ADDRESS(7);
-        GET_RETURN_ADDRESS(8);
-        GET_RETURN_ADDRESS(9);
-        GET_RETURN_ADDRESS(10);
-        GET_RETURN_ADDRESS(11);
-        GET_RETURN_ADDRESS(12);
-        GET_RETURN_ADDRESS(13);
-        GET_RETURN_ADDRESS(14);
-        GET_RETURN_ADDRESS(15);
-    } while(0);
+	memset(stack, 0, sizeof(*stack));
+	do {
+		GET_RETURN_ADDRESS(0);
+		GET_RETURN_ADDRESS(1);
+		GET_RETURN_ADDRESS(2);
+		GET_RETURN_ADDRESS(3);
+		GET_RETURN_ADDRESS(4);
+		GET_RETURN_ADDRESS(5);
+		GET_RETURN_ADDRESS(6);
+		GET_RETURN_ADDRESS(7);
+		GET_RETURN_ADDRESS(8);
+		GET_RETURN_ADDRESS(9);
+		GET_RETURN_ADDRESS(10);
+		GET_RETURN_ADDRESS(11);
+		GET_RETURN_ADDRESS(12);
+		GET_RETURN_ADDRESS(13);
+		GET_RETURN_ADDRESS(14);
+		GET_RETURN_ADDRESS(15);
+	} while (0);
 }
+
 /*--------------------------------------------------------- */
-#else /* not __GNUC__ */
-    #warning "Generic stack backtrace not implemented for non-GCC compiler..."
-#endif /* not __GNUC__*/
+#else				/* not __GNUC__ */
+#warning "Generic stack backtrace not implemented for non-GCC compiler..."
+#endif				/* not __GNUC__ */

+ 3 - 4
lub/heap/heap__get_stats.c

@@ -1,9 +1,8 @@
 #include "private.h"
 /*--------------------------------------------------------- */
-void 
-lub_heap__get_stats(lub_heap_t       *this,
-                    lub_heap_stats_t *stats)
+void lub_heap__get_stats(lub_heap_t * this, lub_heap_stats_t * stats)
 {
-    *stats = this->stats;
+	*stats = this->stats;
 }
+
 /*--------------------------------------------------------- */

+ 91 - 97
lub/heap/heap_add_segment.c

@@ -8,125 +8,119 @@
 #include "context.h"
 #include "lub/bintree.h"
 
-typedef struct
-{
-    const lub_heap_segment_t *segment;
+typedef struct {
+	const lub_heap_segment_t *segment;
 } lub_heap_segment_key_t;
 
 /*--------------------------------------------------------- */
 static int
-lub_heap_segment_compare(const void *clientnode,
-                         const void *clientkey)
+lub_heap_segment_compare(const void *clientnode, const void *clientkey)
 {
-    const lub_heap_segment_t     * segment = clientnode;
-    const lub_heap_segment_key_t * key     = clientkey;
-    
-    return ((long)segment - (long)key->segment);
+	const lub_heap_segment_t *segment = clientnode;
+	const lub_heap_segment_key_t *key = clientkey;
+
+	return ((long)segment - (long)key->segment);
 }
+
 /*--------------------------------------------------------- */
 /* we simply use the segment address as the index */
 static void
-lub_heap_segment_getkey(const void        *clientnode,
-                        lub_bintree_key_t *key)
+lub_heap_segment_getkey(const void *clientnode, lub_bintree_key_t * key)
 {
-    lub_heap_segment_key_t clientkey;
-    clientkey.segment = clientnode;
-    memcpy(key,&clientkey,sizeof(lub_heap_segment_key_t));
+	lub_heap_segment_key_t clientkey;
+	clientkey.segment = clientnode;
+	memcpy(key, &clientkey, sizeof(lub_heap_segment_key_t));
 }
+
 /*--------------------------------------------------------- */
-static void
-lub_heap_segment_meta_init()
+static void lub_heap_segment_meta_init()
 {
-    static bool_t initialised = BOOL_FALSE;
-    
-    if(BOOL_FALSE == initialised)
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        initialised = BOOL_TRUE;
-        /* initialise the segment tree */
-        lub_bintree_init(&leak->m_segment_tree,
-                         offsetof(lub_heap_segment_t,bt_node),
-                         lub_heap_segment_compare,
-                         lub_heap_segment_getkey);
-        lub_heap_leak_release(leak);
-    }
+	static bool_t initialised = BOOL_FALSE;
+
+	if (BOOL_FALSE == initialised) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		initialised = BOOL_TRUE;
+		/* initialise the segment tree */
+		lub_bintree_init(&leak->m_segment_tree,
+				 offsetof(lub_heap_segment_t, bt_node),
+				 lub_heap_segment_compare,
+				 lub_heap_segment_getkey);
+		lub_heap_leak_release(leak);
+	}
 }
+
 /*--------------------------------------------------------- */
-const lub_heap_segment_t *
-lub_heap_segment_findnext(const void *address)
+const lub_heap_segment_t *lub_heap_segment_findnext(const void *address)
 {
-    lub_heap_segment_t    *segment;
-    lub_heap_segment_key_t key;
-    lub_heap_leak_t       *leak = lub_heap_leak_instance();
-    key.segment = address;
-    
-    segment = lub_bintree_findnext(&leak->m_segment_tree,&key);
-    lub_heap_leak_release(leak);
-
-    return segment;
+	lub_heap_segment_t *segment;
+	lub_heap_segment_key_t key;
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	key.segment = address;
+
+	segment = lub_bintree_findnext(&leak->m_segment_tree, &key);
+	lub_heap_leak_release(leak);
+
+	return segment;
 }
+
 /*--------------------------------------------------------- */
 /*
  * as an optimisation this could be modified to merge 
  * adjacent segments together
  */
-void
-lub_heap_add_segment(lub_heap_t *this,
-                     void       *start,
-                     size_t      size)
+void lub_heap_add_segment(lub_heap_t * this, void *start, size_t size)
 {
-    lub_heap_segment_t *segment;
-    
-    /* check for simple adjacent segment as produced by sbrk() type behaviour */
-    if(this->first_segment.words)
-    {
-        lub_heap_tag_t *tail;
-        char           *ptr = (char*)&this[1];
-        ptr += (this->first_segment.words << 2);
-        if(ptr == start)
-        {
-            /* simply extend the current first segment */
-            this->first_segment.words += (size >> 2);
-            tail          = &((lub_heap_tag_t*)start)[-1];
-            tail->segment = BOOL_FALSE; /* no longer last block in segment */
-            /* convert the new memory into a free block... */
-            lub_heap_init_free_block(this,start,size,BOOL_FALSE,BOOL_TRUE);
-            /* try and merge with the previous block */
-            lub_heap_merge_with_previous(this,start);
-
-            this->stats.segs_bytes    += size;
-
-            return;
-        }
-    }
-    /* adjust the size which can be used */
-    size -= sizeof(lub_heap_segment_t);
-
-    /* set up the linked list of segments */
-    segment = start;
-    
-    /* update the stats */
-    ++this->stats.segs;
-    this->stats.segs_bytes    += size;
-    this->stats.segs_overhead += sizeof(lub_heap_segment_t);
-    
-    if(segment != &this->first_segment)
-    {
-        /* maintain the list of segments */
-        segment->next = this->first_segment.next;
-        this->first_segment.next = segment;
-    }
-    segment->words = (size >> 2);
-    
-    lub_heap_init_free_block(this,&segment[1],size,BOOL_TRUE,BOOL_TRUE);
-    
-    /* add this segment to the tree */
-    lub_heap_segment_meta_init();
-    lub_bintree_node_init(&segment->bt_node);
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        lub_bintree_insert(&leak->m_segment_tree,segment);
-        lub_heap_leak_release(leak);
-    }
+	lub_heap_segment_t *segment;
+
+	/* check for simple adjacent segment as produced by sbrk() type behaviour */
+	if (this->first_segment.words) {
+		lub_heap_tag_t *tail;
+		char *ptr = (char *)&this[1];
+		ptr += (this->first_segment.words << 2);
+		if (ptr == start) {
+			/* simply extend the current first segment */
+			this->first_segment.words += (size >> 2);
+			tail = &((lub_heap_tag_t *) start)[-1];
+			tail->segment = BOOL_FALSE;	/* no longer last block in segment */
+			/* convert the new memory into a free block... */
+			lub_heap_init_free_block(this, start, size, BOOL_FALSE,
+						 BOOL_TRUE);
+			/* try and merge with the previous block */
+			lub_heap_merge_with_previous(this, start);
+
+			this->stats.segs_bytes += size;
+
+			return;
+		}
+	}
+	/* adjust the size which can be used */
+	size -= sizeof(lub_heap_segment_t);
+
+	/* set up the linked list of segments */
+	segment = start;
+
+	/* update the stats */
+	++this->stats.segs;
+	this->stats.segs_bytes += size;
+	this->stats.segs_overhead += sizeof(lub_heap_segment_t);
+
+	if (segment != &this->first_segment) {
+		/* maintain the list of segments */
+		segment->next = this->first_segment.next;
+		this->first_segment.next = segment;
+	}
+	segment->words = (size >> 2);
+
+	lub_heap_init_free_block(this, &segment[1], size, BOOL_TRUE, BOOL_TRUE);
+
+	/* add this segment to the tree */
+	lub_heap_segment_meta_init();
+	lub_bintree_node_init(&segment->bt_node);
+	{
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		lub_bintree_insert(&leak->m_segment_tree, segment);
+		lub_heap_leak_release(leak);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 110 - 126
lub/heap/heap_align_block.c

@@ -5,145 +5,129 @@
 
 /*--------------------------------------------------------- */
 static void
-lub_heap_free_leading_memory(lub_heap_t       *this,
-                             lub_heap_block_t *block,
-                             size_t            size)
+lub_heap_free_leading_memory(lub_heap_t * this,
+			     lub_heap_block_t * block, size_t size)
 {
-    lub_heap_block_t *prev_block = lub_heap_block_getprevious(block);
-    if(prev_block && prev_block->free.tag.free)
-    {
-        /* 
-         * add the excess bytes to a preceeding free block
-         */
-        lub_heap_graft_to_top(this,
-                              prev_block,
-                              block,
-                              (size >> 2),
-                              BOOL_FALSE,
-                              BOOL_FALSE);
-    }
-    else
-    {
-        /*
-         * convert the excess bytes into a free block
-         */
-        lub_heap_init_free_block(this,
-                                 block,
-                                 size,
-                                 block->alloc.tag.segment,
-                                 BOOL_FALSE);
-    }
-    this->stats.alloc_bytes -= size;
+	lub_heap_block_t *prev_block = lub_heap_block_getprevious(block);
+	if (prev_block && prev_block->free.tag.free) {
+		/* 
+		 * add the excess bytes to a preceeding free block
+		 */
+		lub_heap_graft_to_top(this,
+				      prev_block,
+				      block,
+				      (size >> 2), BOOL_FALSE, BOOL_FALSE);
+	} else {
+		/*
+		 * convert the excess bytes into a free block
+		 */
+		lub_heap_init_free_block(this,
+					 block,
+					 size,
+					 block->alloc.tag.segment, BOOL_FALSE);
+	}
+	this->stats.alloc_bytes -= size;
 }
+
 /*--------------------------------------------------------- */
 static void
-lub_heap_free_trailing_memory(lub_heap_t       *this,
-                              lub_heap_block_t *block,
-                              size_t            size)
+lub_heap_free_trailing_memory(lub_heap_t * this,
+			      lub_heap_block_t * block, size_t size)
 {
-    bool_t            done       = BOOL_FALSE;
-    lub_heap_block_t *next_block = lub_heap_block_getnext(block);
-    lub_heap_tag_t   *tail       = lub_heap_block__get_tail(block);
-    char             *ptr        = (char*)&tail[1] - size;
-    if(next_block && next_block->free.tag.free)
-    {
-        /* 
-         * try and add the excess bytes to a following free block
-         */
-        lub_heap_graft_to_bottom(this,
-                                 next_block,
-                                 ptr,
-                                 (size >> 2),
-                                 BOOL_FALSE,
-                                 BOOL_FALSE);
-        done = BOOL_TRUE;
-    }
-    else if(size >= sizeof(lub_heap_free_block_t))
-    {
-        /*
-         * convert the excess bytes into a free block
-         */
-        lub_heap_init_free_block(this,
-                                 ptr,
-                                 size,
-                                 BOOL_FALSE,
-                                 tail->segment);
-        done = BOOL_TRUE;
-    }
+	bool_t done = BOOL_FALSE;
+	lub_heap_block_t *next_block = lub_heap_block_getnext(block);
+	lub_heap_tag_t *tail = lub_heap_block__get_tail(block);
+	char *ptr = (char *)&tail[1] - size;
+	if (next_block && next_block->free.tag.free) {
+		/* 
+		 * try and add the excess bytes to a following free block
+		 */
+		lub_heap_graft_to_bottom(this,
+					 next_block,
+					 ptr,
+					 (size >> 2), BOOL_FALSE, BOOL_FALSE);
+		done = BOOL_TRUE;
+	} else if (size >= sizeof(lub_heap_free_block_t)) {
+		/*
+		 * convert the excess bytes into a free block
+		 */
+		lub_heap_init_free_block(this,
+					 ptr, size, BOOL_FALSE, tail->segment);
+		done = BOOL_TRUE;
+	}
 
-    if(done)
-    {
-        /* correct the word count for the block */
-        block->alloc.tag.words -= (size >> 2);
-        tail          = lub_heap_block__get_tail(block);
-        tail->segment = 0;
-        tail->free    = 0;
-        tail->words   = block->alloc.tag.words;
+	if (done) {
+		/* correct the word count for the block */
+		block->alloc.tag.words -= (size >> 2);
+		tail = lub_heap_block__get_tail(block);
+		tail->segment = 0;
+		tail->free = 0;
+		tail->words = block->alloc.tag.words;
 
-        this->stats.alloc_bytes -= size;
-    }
+		this->stats.alloc_bytes -= size;
+	}
 }
+
 /*--------------------------------------------------------- */
 void
-lub_heap_align_block(lub_heap_t      *this,
-                     lub_heap_align_t alignment,
-                     char           **ptr,
-                     size_t          *size)
+lub_heap_align_block(lub_heap_t * this,
+		     lub_heap_align_t alignment, char **ptr, size_t * size)
 {
-    if(*ptr)
-    {
-        lub_heap_block_t *block = lub_heap_block_from_ptr(*ptr);
-        lub_heap_block_t *new_block;
-        lub_heap_tag_t   *new_tail;
-        /* find the second occurance of an alignment boundary */
-        size_t    tmp           = (alignment-1);
-        char     *new_ptr       = (char *)(((size_t)*ptr + tmp + alignment) & ~tmp);
-        size_t    leading_bytes = (new_ptr - *ptr);
-        size_t    trailing_bytes;
-        
-        /* check this is a allocated block */
-        assert(0 == block->alloc.tag.free);
-        /*
-         * We MUST have left sufficient space to fit a free block and
-         * leak dection node if necessary.
-         */
-        assert(leading_bytes >= (sizeof(lub_heap_free_block_t) + sizeof(lub_heap_node_t)));
+	if (*ptr) {
+		lub_heap_block_t *block = lub_heap_block_from_ptr(*ptr);
+		lub_heap_block_t *new_block;
+		lub_heap_tag_t *new_tail;
+		/* find the second occurance of an alignment boundary */
+		size_t tmp = (alignment - 1);
+		char *new_ptr =
+		    (char *)(((size_t) * ptr + tmp + alignment) & ~tmp);
+		size_t leading_bytes = (new_ptr - *ptr);
+		size_t trailing_bytes;
+
+		/* check this is a allocated block */
+		assert(0 == block->alloc.tag.free);
+		/*
+		 * We MUST have left sufficient space to fit a free block and
+		 * leak dection node if necessary.
+		 */
+		assert(leading_bytes >=
+		       (sizeof(lub_heap_free_block_t) +
+			sizeof(lub_heap_node_t)));
+
+		if ((0 < lub_heap_frame_count) && (0 == this->suppress)) {
+			/* 
+			 * If leak detection is enabled then offset the pointer back by the size of 
+			 * a lub_heap_node; which will be filled out by lub_heap_post_realloc()
+			 */
+			new_ptr -= sizeof(lub_heap_node_t);
+			leading_bytes -= sizeof(lub_heap_node_t);
+		}
+		/*
+		 * Create a new header just before the new pointer
+		 */
+		new_block = lub_heap_block_from_ptr(new_ptr);
+		new_block->alloc.tag.free = 0;
+		new_block->alloc.tag.segment = 0;
+		new_block->alloc.tag.words =
+		    block->alloc.tag.words - (leading_bytes >> 2);
 
-        if((0 < lub_heap_frame_count) && (0 == this->suppress))
-        {
-            /* 
-             * If leak detection is enabled then offset the pointer back by the size of 
-             * a lub_heap_node; which will be filled out by lub_heap_post_realloc()
-             */
-            new_ptr       -= sizeof(lub_heap_node_t);
-            leading_bytes -= sizeof(lub_heap_node_t);
-        }
-        /*
-         * Create a new header just before the new pointer
-         */
-        new_block = lub_heap_block_from_ptr(new_ptr);
-        new_block->alloc.tag.free    = 0;
-        new_block->alloc.tag.segment = 0;
-        new_block->alloc.tag.words   = block->alloc.tag.words - (leading_bytes >> 2);
+		/* fix the size in the tail */
+		new_tail = lub_heap_block__get_tail(new_block);
+		new_tail->words = new_block->alloc.tag.words;
 
-        /* fix the size in the tail */
-        new_tail        = lub_heap_block__get_tail(new_block);
-        new_tail->words = new_block->alloc.tag.words;
+		lub_heap_free_leading_memory(this, block, leading_bytes);
 
-        lub_heap_free_leading_memory(this,
-                                     block,
-                                     leading_bytes);
+		/* identify any trailing excess memory */
+		trailing_bytes =
+		    (new_block->alloc.tag.words << 2) - (*size -
+							 (alignment << 1));
 
-        /* identify any trailing excess memory */
-        trailing_bytes  = (new_block->alloc.tag.words << 2) - (*size - (alignment << 1));
-        
-        if(trailing_bytes)
-        {
-            lub_heap_free_trailing_memory(this,
-                                          new_block,
-                                          trailing_bytes);
-        }
-        *size = (new_block->alloc.tag.words << 2); /* size including the lub_heap_alloc_block_t details */
-        *ptr  = new_ptr;
-    }
+		if (trailing_bytes) {
+			lub_heap_free_trailing_memory(this,
+						      new_block,
+						      trailing_bytes);
+		}
+		*size = (new_block->alloc.tag.words << 2);	/* size including the lub_heap_alloc_block_t details */
+		*ptr = new_ptr;
+	}
 }

+ 4 - 4
lub/heap/heap_block__get_tail.c

@@ -1,10 +1,10 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_heap_tag_t *
-lub_heap_block__get_tail(lub_heap_block_t *this)
+lub_heap_tag_t *lub_heap_block__get_tail(lub_heap_block_t * this)
 {
-    lub_heap_tag_t *ptr = &this->alloc.tag;
-    return (ptr + this->alloc.tag.words - 1);
+	lub_heap_tag_t *ptr = &this->alloc.tag;
+	return (ptr + this->alloc.tag.words - 1);
 }
+
 /*--------------------------------------------------------- */

+ 4 - 4
lub/heap/heap_block_check.c

@@ -1,11 +1,11 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_block_check(lub_heap_block_t *this)
+bool_t lub_heap_block_check(lub_heap_block_t * this)
 {
-    lub_heap_tag_t *tail = lub_heap_block__get_tail(this);
+	lub_heap_tag_t *tail = lub_heap_block__get_tail(this);
 
-    return (tail->words == this->alloc.tag.words) ? BOOL_TRUE : BOOL_FALSE;
+	return (tail->words == this->alloc.tag.words) ? BOOL_TRUE : BOOL_FALSE;
 }
+
 /*--------------------------------------------------------- */

+ 3 - 3
lub/heap/heap_block_from_ptr.c

@@ -1,9 +1,9 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_heap_block_t *
-lub_heap_block_from_ptr(char *ptr)
+lub_heap_block_t *lub_heap_block_from_ptr(char *ptr)
 {
-    return ((lub_heap_block_t*)&((lub_heap_tag_t*)ptr)[-1]);
+	return ((lub_heap_block_t *) & ((lub_heap_tag_t *) ptr)[-1]);
 }
+
 /*--------------------------------------------------------- */

+ 3 - 3
lub/heap/heap_block_getfirst.c

@@ -1,9 +1,9 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_heap_block_t *
-lub_heap_block_getfirst(const lub_heap_segment_t *seg)
+lub_heap_block_t *lub_heap_block_getfirst(const lub_heap_segment_t * seg)
 {
-    return (lub_heap_block_t*)&seg[1];
+	return (lub_heap_block_t *) & seg[1];
 }
+
 /*--------------------------------------------------------- */

+ 9 - 10
lub/heap/heap_block_getkey.c

@@ -3,16 +3,15 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void
-lub_heap_block_getkey(const void        *clientnode,
-                      lub_bintree_key_t *key)
+void lub_heap_block_getkey(const void *clientnode, lub_bintree_key_t * key)
 {
-    const lub_heap_free_block_t *block = clientnode;
-    lub_heap_key_t               mykey;
-    
-    mykey.words = block->tag.words;
-    mykey.block = block;
-    
-    memcpy(key,&mykey,sizeof(lub_heap_key_t));
+	const lub_heap_free_block_t *block = clientnode;
+	lub_heap_key_t mykey;
+
+	mykey.words = block->tag.words;
+	mykey.block = block;
+
+	memcpy(key, &mykey, sizeof(lub_heap_key_t));
 }
+
 /*--------------------------------------------------------- */

+ 10 - 11
lub/heap/heap_block_getnext.c

@@ -1,18 +1,17 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_heap_block_t *
-lub_heap_block_getnext(lub_heap_block_t *this)
+lub_heap_block_t *lub_heap_block_getnext(lub_heap_block_t * this)
 {
-    lub_heap_block_t *result = NULL;
-    lub_heap_tag_t   *tail   = lub_heap_block__get_tail(this);
+	lub_heap_block_t *result = NULL;
+	lub_heap_tag_t *tail = lub_heap_block__get_tail(this);
 
-    /* only go forward if this is not the last block in the segment */
-    if(0 == tail->segment)
-    {
-        /* get a pointer to the first word in the next block */
-        result = (lub_heap_block_t *)&tail[1]; 
-    }
-    return result;
+	/* only go forward if this is not the last block in the segment */
+	if (0 == tail->segment) {
+		/* get a pointer to the first word in the next block */
+		result = (lub_heap_block_t *) & tail[1];
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 17 - 18
lub/heap/heap_block_getprevious.c

@@ -1,25 +1,24 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_heap_block_t *
-lub_heap_block_getprevious(lub_heap_block_t *this)
+lub_heap_block_t *lub_heap_block_getprevious(lub_heap_block_t * this)
 {
-    lub_heap_block_t *result = NULL;
+	lub_heap_block_t *result = NULL;
 
-    /* only go back if this is not the first block in the segment */
-    if(0 == this->alloc.tag.segment)
-    {
-        /* get the tag from the previous block */
-        lub_heap_tag_t *tag = &(&this->alloc.tag)[-1];
-        
-        /* get a pointer to the previous block 
-         * +1 required to account for header tag
-         */
-        tag = &tag[1-tag->words];
-        
-        /* now point to the start of the previous block */
-        result = (lub_heap_block_t *)tag;
-    }
-    return result;
+	/* only go back if this is not the first block in the segment */
+	if (0 == this->alloc.tag.segment) {
+		/* get the tag from the previous block */
+		lub_heap_tag_t *tag = &(&this->alloc.tag)[-1];
+
+		/* get a pointer to the previous block 
+		 * +1 required to account for header tag
+		 */
+		tag = &tag[1 - tag->words];
+
+		/* now point to the start of the previous block */
+		result = (lub_heap_block_t *) tag;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 70 - 79
lub/heap/heap_check.c

@@ -3,97 +3,88 @@
 /* flag to indicate whether to check memory or not */
 static bool_t checking = BOOL_FALSE;
 
-typedef struct 
-{
-    bool_t      result;
-    unsigned    free_blocks;
-    lub_heap_t *heap;
+typedef struct {
+	bool_t result;
+	unsigned free_blocks;
+	lub_heap_t *heap;
 } check_arg_t;
 /*--------------------------------------------------------- */
-static void
-process_segment(void    *s,
-                unsigned index,
-                size_t   size,
-                void    *a)
+static void process_segment(void *s, unsigned index, size_t size, void *a)
 {
-    lub_heap_segment_t *segment = s;
-    lub_heap_block_t   *block   = lub_heap_block_getfirst(segment);
-    check_arg_t        *arg     = a;
-    /*
-     * don't bother checking if this segment has zero blocks
-     */
-    if(arg->heap->stats.free_blocks || arg->heap->stats.alloc_blocks)
-    {
-        arg->result = lub_heap_block_check(block);
-        if(BOOL_TRUE == arg->result)
-        {
-            /* check that the first block has the segment set */
-            arg->result = (block->free.tag.segment == 1) ? BOOL_TRUE : BOOL_FALSE;
-            if(block->free.tag.free)
-            {
-                ++arg->free_blocks;
-            }
-        }
-        
-        /* now iterate along the block chain checking each one */
-        while((BOOL_TRUE == arg->result) 
-              && (block = lub_heap_block_getnext(block)))
-        {
-            /* check this block */
-            arg->result = lub_heap_block_check(block);
-            if(BOOL_TRUE == arg->result)
-            {
-                /* ensure that all non-first blocks have the segment clear */
-                arg->result = (block->free.tag.segment == 0) ? BOOL_TRUE : BOOL_FALSE;
-                if(block->free.tag.free)
-                {
-                    ++arg->free_blocks;
-                }
-            }
-        }
-    }
+	lub_heap_segment_t *segment = s;
+	lub_heap_block_t *block = lub_heap_block_getfirst(segment);
+	check_arg_t *arg = a;
+	/*
+	 * don't bother checking if this segment has zero blocks
+	 */
+	if (arg->heap->stats.free_blocks || arg->heap->stats.alloc_blocks) {
+		arg->result = lub_heap_block_check(block);
+		if (BOOL_TRUE == arg->result) {
+			/* check that the first block has the segment set */
+			arg->result =
+			    (block->free.tag.segment ==
+			     1) ? BOOL_TRUE : BOOL_FALSE;
+			if (block->free.tag.free) {
+				++arg->free_blocks;
+			}
+		}
+
+		/* now iterate along the block chain checking each one */
+		while ((BOOL_TRUE == arg->result)
+		       && (block = lub_heap_block_getnext(block))) {
+			/* check this block */
+			arg->result = lub_heap_block_check(block);
+			if (BOOL_TRUE == arg->result) {
+				/* ensure that all non-first blocks have the segment clear */
+				arg->result =
+				    (block->free.tag.segment ==
+				     0) ? BOOL_TRUE : BOOL_FALSE;
+				if (block->free.tag.free) {
+					++arg->free_blocks;
+				}
+			}
+		}
+	}
 }
 
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_check(bool_t enable)
+bool_t lub_heap_check(bool_t enable)
 {
-    bool_t result = checking;
-    
-    checking = enable;
-    
-    return result;
+	bool_t result = checking;
+
+	checking = enable;
+
+	return result;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_is_checking(void)
+bool_t lub_heap_is_checking(void)
 {
-    return checking;
+	return checking;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_check_memory(lub_heap_t *this)
+bool_t lub_heap_check_memory(lub_heap_t * this)
 {
-    check_arg_t arg;
-    
-    arg.result = BOOL_TRUE;
-    
-    if(BOOL_TRUE == checking)
-    {
-        arg.free_blocks = 0;
-        arg.heap        = this;
-        /* iterate all the segments in the system */
-        lub_heap_foreach_segment(this,process_segment,&arg);
-    
-        /* now check that the stats map to the available free blocks 
-         * checking the alloc blocks is more difficult because of the hidden
-         * overheads when performing leak detection.
-         */
-        if( (this->stats.free_blocks != arg.free_blocks) )
-        {
-            arg.result = BOOL_FALSE;
-        }
-    }
-    return arg.result;
+	check_arg_t arg;
+
+	arg.result = BOOL_TRUE;
+
+	if (BOOL_TRUE == checking) {
+		arg.free_blocks = 0;
+		arg.heap = this;
+		/* iterate all the segments in the system */
+		lub_heap_foreach_segment(this, process_segment, &arg);
+
+		/* now check that the stats map to the available free blocks 
+		 * checking the alloc blocks is more difficult because of the hidden
+		 * overheads when performing leak detection.
+		 */
+		if ((this->stats.free_blocks != arg.free_blocks)) {
+			arg.result = BOOL_FALSE;
+		}
+	}
+	return arg.result;
 }
+
 /*--------------------------------------------------------- */

+ 10 - 11
lub/heap/heap_context_delete.c

@@ -1,17 +1,16 @@
 #include "private.h"
 #include "context.h"
 
-bool_t
-lub_heap_context_delete(lub_heap_context_t *context)
+bool_t lub_heap_context_delete(lub_heap_context_t * context)
 {
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    
-    /* finalise the instance */
-    lub_heap_context_fini(context);
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
 
-    /* release the memory */
-    lub_dblockpool_free(&leak->m_context_pool,context);
-    lub_heap_leak_release(leak);
-    
-    return BOOL_TRUE;
+	/* finalise the instance */
+	lub_heap_context_fini(context);
+
+	/* release the memory */
+	lub_dblockpool_free(&leak->m_context_pool, context);
+	lub_heap_leak_release(leak);
+
+	return BOOL_TRUE;
 }

+ 22 - 23
lub/heap/heap_context_find_or_create.c

@@ -2,29 +2,28 @@
 #include "context.h"
 
 /*--------------------------------------------------------- */
-const lub_heap_context_t *
-lub_heap_context_find_or_create(lub_heap_t         * this,
-                                const stackframe_t * stack)
+const lub_heap_context_t *lub_heap_context_find_or_create(lub_heap_t * this,
+							  const stackframe_t *
+							  stack)
 {
-    lub_heap_context_t *context;
-    
-    /*
-     * look to see whether there is an 
-     * existing context for this allocation 
-     */
-    context = lub_heap_context_find(stack);
-    if(NULL == context)
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        context = lub_dblockpool_alloc(&leak->m_context_pool);
-        lub_heap_leak_release(leak);
-        
-        if(NULL != context)
-        {
-            /* initialise the instance */
-            lub_heap_context_init(context,this,stack);
-        }
-    }
-    return context;
+	lub_heap_context_t *context;
+
+	/*
+	 * look to see whether there is an 
+	 * existing context for this allocation 
+	 */
+	context = lub_heap_context_find(stack);
+	if (NULL == context) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		context = lub_dblockpool_alloc(&leak->m_context_pool);
+		lub_heap_leak_release(leak);
+
+		if (NULL != context) {
+			/* initialise the instance */
+			lub_heap_context_init(context, this, stack);
+		}
+	}
+	return context;
 }
+
 /*--------------------------------------------------------- */

+ 68 - 72
lub/heap/heap_create.c

@@ -5,82 +5,78 @@
 #include "context.h"
 
 /*--------------------------------------------------------- */
-static int
-lub_heap_block_compare(const void *clientnode,
-                       const void *clientkey)
+static int lub_heap_block_compare(const void *clientnode, const void *clientkey)
 {
-    int                           delta;
-    const lub_heap_free_block_t *block = clientnode;
-    const lub_heap_key_t        *key   = clientkey;
-    
-    delta = (block->tag.words - key->words);
-    if(0 == delta)
-    {
-        /* if same size differentiate by address */
-        delta = (block - key->block);
-    }
-    return delta;
+	int delta;
+	const lub_heap_free_block_t *block = clientnode;
+	const lub_heap_key_t *key = clientkey;
+
+	delta = (block->tag.words - key->words);
+	if (0 == delta) {
+		/* if same size differentiate by address */
+		delta = (block - key->block);
+	}
+	return delta;
 }
+
 /*--------------------------------------------------------- */
-lub_heap_t *
-lub_heap_create(void  *start,
-                size_t size)
+lub_heap_t *lub_heap_create(void *start, size_t size)
 {
-    lub_heap_t *this = NULL;
-    
-    /* we must have at least 1024 bytes for a heap */
-    if(size > (sizeof(lub_heap_t) + 4))
-    {
-        this = start;
-        /* set up the binary tree for the free blocks */
-        lub_bintree_init(&this->free_tree,
-                         offsetof(lub_heap_free_block_t,bt_node),
-                         lub_heap_block_compare,
-                         lub_heap_block_getkey);
+	lub_heap_t *this = NULL;
+
+	/* we must have at least 1024 bytes for a heap */
+	if (size > (sizeof(lub_heap_t) + 4)) {
+		this = start;
+		/* set up the binary tree for the free blocks */
+		lub_bintree_init(&this->free_tree,
+				 offsetof(lub_heap_free_block_t, bt_node),
+				 lub_heap_block_compare, lub_heap_block_getkey);
+
+		this->cache = 0;
+		this->suppress = 0;
+
+		/* initialise the statistics */
+		this->stats.segs = 0;
+		this->stats.segs_bytes = 0;
+		this->stats.segs_overhead = 0;
+		this->stats.free_blocks = 0;
+		this->stats.free_bytes = 0;
+		this->stats.free_overhead = 0;
+		this->stats.static_blocks = 0;
+		this->stats.static_bytes = 0;
+		this->stats.static_overhead = 0;
+		this->stats.alloc_blocks = 0;
+		this->stats.alloc_bytes = 0;
+		this->stats.alloc_overhead = 0;
+		this->stats.alloc_hightide_blocks = 0;
+		this->stats.alloc_hightide_bytes = 0;
+		this->stats.alloc_hightide_overhead = 0;
+		this->stats.free_hightide_blocks = 0;
+		this->stats.free_hightide_bytes = 0;
+		this->stats.free_hightide_overhead = 0;
+		this->stats.alloc_total_blocks = 0;
+		this->stats.alloc_total_bytes = 0;
 
-        this->cache    = 0;
-        this->suppress = 0;
-        
-        /* initialise the statistics */
-        this->stats.segs                    = 0;
-        this->stats.segs_bytes              = 0;
-        this->stats.segs_overhead           = 0;
-        this->stats.free_blocks             = 0;
-        this->stats.free_bytes              = 0;
-        this->stats.free_overhead           = 0;
-        this->stats.static_blocks           = 0;
-        this->stats.static_bytes            = 0;
-        this->stats.static_overhead         = 0;
-        this->stats.alloc_blocks            = 0;
-        this->stats.alloc_bytes             = 0;
-        this->stats.alloc_overhead          = 0;
-        this->stats.alloc_hightide_blocks   = 0;
-        this->stats.alloc_hightide_bytes    = 0;
-        this->stats.alloc_hightide_overhead = 0;
-        this->stats.free_hightide_blocks    = 0;
-        this->stats.free_hightide_bytes     = 0;
-        this->stats.free_hightide_overhead  = 0;
-        this->stats.alloc_total_blocks      = 0;
-        this->stats.alloc_total_bytes       = 0;
-                
-        /* initialise the first segment */
-        this->first_segment.next = NULL;
-        
-        /* create the default segment */
-        lub_heap_add_segment(this, 
-                             &this->first_segment, 
-                             size - sizeof(lub_heap_t) + sizeof(lub_heap_segment_t));
-        
-        this->stats.segs_overhead += sizeof(lub_heap_t);
-                            
-        /* add this heap to the linked list of heaps in the system */
-        {
-            lub_heap_leak_t *leak = lub_heap_leak_instance();
-            this->next            = leak->m_heap_list;
-            leak->m_heap_list = this;
-            lub_heap_leak_release(leak);
-        }
-    }
-    return this;
+		/* initialise the first segment */
+		this->first_segment.next = NULL;
+
+		/* create the default segment */
+		lub_heap_add_segment(this,
+				     &this->first_segment,
+				     size - sizeof(lub_heap_t) +
+				     sizeof(lub_heap_segment_t));
+
+		this->stats.segs_overhead += sizeof(lub_heap_t);
+
+		/* add this heap to the linked list of heaps in the system */
+		{
+			lub_heap_leak_t *leak = lub_heap_leak_instance();
+			this->next = leak->m_heap_list;
+			leak->m_heap_list = this;
+			lub_heap_leak_release(leak);
+		}
+	}
+	return this;
 }
+
 /*--------------------------------------------------------- */

+ 37 - 45
lub/heap/heap_destroy.c

@@ -7,57 +7,49 @@
 
 /*--------------------------------------------------------- */
 static void
-_lub_heap_segment_remove(void    *ptr,
-                         unsigned index,
-                         size_t   size,
-                         void    *arg)
+_lub_heap_segment_remove(void *ptr, unsigned index, size_t size, void *arg)
 {
-    lub_heap_segment_t *segment = ptr;
-    lub_heap_leak_t    *leak    = lub_heap_leak_instance();
-    /* remove segment from the leak tree */
-    lub_bintree_remove(&leak->m_segment_tree,segment);
-    lub_heap_leak_release(leak);
+	lub_heap_segment_t *segment = ptr;
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	/* remove segment from the leak tree */
+	lub_bintree_remove(&leak->m_segment_tree, segment);
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
-static void
-_lub_heap_node_remove(lub_heap_node_t *node,
-                      void            *arg)
+static void _lub_heap_node_remove(lub_heap_node_t * node, void *arg)
 {
-    lub_heap_t         *this    = arg;
-    lub_heap_context_t *context = lub_heap_node__get_context(node);
-    lub_heap_leak_t    *leak    = lub_heap_leak_instance();
-    if(context && (context->heap == this))
-    {
-        --leak->m_stats.allocs;
-        leak->m_stats.alloc_bytes -= lub_heap_node__get_size(node);
-        /* remove this node from the leak tree */
-        lub_heap_node_fini(node);
-    }
-    lub_heap_leak_release(leak);
+	lub_heap_t *this = arg;
+	lub_heap_context_t *context = lub_heap_node__get_context(node);
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	if (context && (context->heap == this)) {
+		--leak->m_stats.allocs;
+		leak->m_stats.alloc_bytes -= lub_heap_node__get_size(node);
+		/* remove this node from the leak tree */
+		lub_heap_node_fini(node);
+	}
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_destroy(lub_heap_t *this)
-{   
-    lub_heap_leak_t *leak = lub_heap_leak_instance();
-    lub_heap_t     **ptr;
-    for(ptr = &leak->m_heap_list;
-        *ptr;
-        ptr = &(*ptr)->next)
-    {
-        if((*ptr) == this)
-        {
-            /* remove from the linked list */
-            *ptr = this->next;
-            break;
-        }
-    }
-    lub_heap_leak_release(leak);
-    
-    /* remove all segments from the leak tree */
-    lub_heap_foreach_segment(this,_lub_heap_segment_remove,0);
+void lub_heap_destroy(lub_heap_t * this)
+{
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	lub_heap_t **ptr;
+	for (ptr = &leak->m_heap_list; *ptr; ptr = &(*ptr)->next) {
+		if ((*ptr) == this) {
+			/* remove from the linked list */
+			*ptr = this->next;
+			break;
+		}
+	}
+	lub_heap_leak_release(leak);
+
+	/* remove all segments from the leak tree */
+	lub_heap_foreach_segment(this, _lub_heap_segment_remove, 0);
 
-    /* remove all nodes from the leak tree */
-    lub_heap_foreach_node(_lub_heap_node_remove,this);
+	/* remove all nodes from the leak tree */
+	lub_heap_foreach_node(_lub_heap_node_remove, this);
 }
+
 /*--------------------------------------------------------- */

+ 25 - 22
lub/heap/heap_extend_both_ways.c

@@ -3,29 +3,32 @@
 
 /*--------------------------------------------------------- */
 bool_t
-lub_heap_extend_both_ways(lub_heap_t        *this,
-                          lub_heap_block_t **ptr_block,
-                          const words_t      words)
+lub_heap_extend_both_ways(lub_heap_t * this,
+			  lub_heap_block_t ** ptr_block, const words_t words)
 {
-    lub_heap_block_t      *block      = *ptr_block;
-    lub_heap_free_block_t *prev_block = (lub_heap_free_block_t*)lub_heap_block_getprevious(block);
-    lub_heap_free_block_t *next_block = (lub_heap_free_block_t*)lub_heap_block_getnext(block);
-    bool_t                 result     = BOOL_FALSE;
+	lub_heap_block_t *block = *ptr_block;
+	lub_heap_free_block_t *prev_block =
+	    (lub_heap_free_block_t *) lub_heap_block_getprevious(block);
+	lub_heap_free_block_t *next_block =
+	    (lub_heap_free_block_t *) lub_heap_block_getnext(block);
+	bool_t result = BOOL_FALSE;
 
-    if(prev_block && prev_block->tag.free && next_block && next_block->tag.free)
-    {
-        /* OK we have a free block above and below us */
-        if(words <= (prev_block->tag.words + next_block->tag.words))
-        {
-            /* There is sufficient space to extend */
-            words_t delta = words - next_block->tag.words;
-            /* maximise the upward extension */
-            result = lub_heap_extend_downwards(this,&block,delta);
-            assert(BOOL_TRUE == result);
-            result = lub_heap_extend_upwards(this,&block,next_block->tag.words);
-            assert(BOOL_TRUE == result);
-        }
-    }
-    return result;
+	if (prev_block && prev_block->tag.free && next_block
+	    && next_block->tag.free) {
+		/* OK we have a free block above and below us */
+		if (words <= (prev_block->tag.words + next_block->tag.words)) {
+			/* There is sufficient space to extend */
+			words_t delta = words - next_block->tag.words;
+			/* maximise the upward extension */
+			result = lub_heap_extend_downwards(this, &block, delta);
+			assert(BOOL_TRUE == result);
+			result =
+			    lub_heap_extend_upwards(this, &block,
+						    next_block->tag.words);
+			assert(BOOL_TRUE == result);
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 69 - 68
lub/heap/heap_extend_downwards.c

@@ -2,79 +2,80 @@
 
 /*--------------------------------------------------------- */
 bool_t
-lub_heap_extend_downwards(lub_heap_t        *this,
-                          lub_heap_block_t **ptr_block,
-                          const words_t      words)
+lub_heap_extend_downwards(lub_heap_t * this,
+			  lub_heap_block_t ** ptr_block, const words_t words)
 {
-    lub_heap_block_t      *block      = *ptr_block;
-    lub_heap_free_block_t *prev_block = (lub_heap_free_block_t*)lub_heap_block_getprevious(block);
-    bool_t                 result     = BOOL_FALSE;
-    
-    if(NULL != prev_block)
-    {
-        /* Do we have a free block below us? */
-        if(1 == prev_block->tag.free)
-        {
-            int   segment;
-            void *tmp;
-            
-            /* work out how many extra words we need */
-            words_t delta = words - block->alloc.tag.words;
-    
-            /* remove the block from the tree */
-            lub_bintree_remove(&this->free_tree,prev_block);
-            
-            /* remember the segment status */
-            segment = prev_block->tag.segment;
-            
-            /* try and slice off a chunk of memory */
-            tmp = lub_heap_slice_from_top(this,&prev_block,&delta,BOOL_FALSE);
+	lub_heap_block_t *block = *ptr_block;
+	lub_heap_free_block_t *prev_block =
+	    (lub_heap_free_block_t *) lub_heap_block_getprevious(block);
+	bool_t result = BOOL_FALSE;
 
-            if(NULL != prev_block)
-            {
-                /* put the modified free block back into the tree */
-                lub_bintree_insert(&this->free_tree,prev_block);
-                
-                /* there is still a block below us */
-                segment = 0;
-            }
-            if(NULL != tmp)
-            {
-                /* we managed to extend downwards */
-                lub_heap_tag_t   *tail      = lub_heap_block__get_tail(block);
-                lub_heap_block_t *new_block = tmp;
+	if (NULL != prev_block) {
+		/* Do we have a free block below us? */
+		if (1 == prev_block->tag.free) {
+			int segment;
+			void *tmp;
 
-                /* get ready to copy the data down accounts for the header */
-                words_t              old_words = tail->words;
-                const unsigned int  *src       = (unsigned int*)block;
-                unsigned int        *dst       = (unsigned int*)new_block;
+			/* work out how many extra words we need */
+			words_t delta = words - block->alloc.tag.words;
 
-                /* copy the old data to the new location */
-                while(old_words--)
-                {
-                    *dst++ = *src++;
-                }
-                /* taint the extra memory */
-                lub_heap_taint_memory(((char*)tail) - (delta<<2),
-                                      LUB_HEAP_TAINT_ALLOC,
-                                      (delta<<2));
-                
-                /* fill out the new block details */
-                tail->words += delta;
-                this->stats.alloc_bytes       += (delta << 2);
-                this->stats.alloc_total_bytes += (delta << 2);
+			/* remove the block from the tree */
+			lub_bintree_remove(&this->free_tree, prev_block);
 
-                new_block->alloc.tag.segment = segment;
-                new_block->alloc.tag.free    = 0;
-                new_block->alloc.tag.words   = tail->words;
-                
-                /* update the clients pointer */
-                *ptr_block = new_block;
+			/* remember the segment status */
+			segment = prev_block->tag.segment;
 
-                result = BOOL_TRUE;
-            }
-        }
-    }
-    return result;
+			/* try and slice off a chunk of memory */
+			tmp =
+			    lub_heap_slice_from_top(this, &prev_block, &delta,
+						    BOOL_FALSE);
+
+			if (NULL != prev_block) {
+				/* put the modified free block back into the tree */
+				lub_bintree_insert(&this->free_tree,
+						   prev_block);
+
+				/* there is still a block below us */
+				segment = 0;
+			}
+			if (NULL != tmp) {
+				/* we managed to extend downwards */
+				lub_heap_tag_t *tail =
+				    lub_heap_block__get_tail(block);
+				lub_heap_block_t *new_block = tmp;
+
+				/* get ready to copy the data down accounts for the header */
+				words_t old_words = tail->words;
+				const unsigned int *src = (unsigned int *)block;
+				unsigned int *dst = (unsigned int *)new_block;
+
+				/* copy the old data to the new location */
+				while (old_words--) {
+					*dst++ = *src++;
+				}
+				/* taint the extra memory */
+				lub_heap_taint_memory(((char *)tail) -
+						      (delta << 2),
+						      LUB_HEAP_TAINT_ALLOC,
+						      (delta << 2));
+
+				/* fill out the new block details */
+				tail->words += delta;
+				this->stats.alloc_bytes += (delta << 2);
+				this->stats.alloc_total_bytes += (delta << 2);
+
+				new_block->alloc.tag.segment = segment;
+				new_block->alloc.tag.free = 0;
+				new_block->alloc.tag.words = tail->words;
+
+				/* update the clients pointer */
+				*ptr_block = new_block;
+
+				result = BOOL_TRUE;
+			}
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 57 - 53
lub/heap/heap_extend_upwards.c

@@ -1,64 +1,68 @@
 #include "private.h"
 /*--------------------------------------------------------- */
 bool_t
-lub_heap_extend_upwards(lub_heap_t        *this,
-                        lub_heap_block_t **ptr_block,
-                        words_t            words)
+lub_heap_extend_upwards(lub_heap_t * this,
+			lub_heap_block_t ** ptr_block, words_t words)
 {
-    lub_heap_block_t      *block      = *ptr_block;
-    lub_heap_free_block_t *next_block = (lub_heap_free_block_t *)lub_heap_block_getnext(block);
-    bool_t                 result     = BOOL_FALSE;
-    
-    if(NULL != next_block)
-    {
-        /* Do we have a free block above us? */
-        if(1 == next_block->tag.free)
-        {
-            int             segment;
-            void           *tmp;
-            lub_heap_tag_t *tail = lub_heap_block__get_tail((lub_heap_block_t *)next_block);
+	lub_heap_block_t *block = *ptr_block;
+	lub_heap_free_block_t *next_block =
+	    (lub_heap_free_block_t *) lub_heap_block_getnext(block);
+	bool_t result = BOOL_FALSE;
 
-            /* work out how many extra words we need */
-            words -= block->alloc.tag.words;
-            
-            /* remove the block from the tree */
-            lub_bintree_remove(&this->free_tree,next_block);
-            
-            /* remember the segment status of the next block */
-            segment = tail->segment;
-            
-            /* try and slice off a chunk of memory */
-            tmp = lub_heap_slice_from_bottom(this,&next_block,&words,BOOL_FALSE);
+	if (NULL != next_block) {
+		/* Do we have a free block above us? */
+		if (1 == next_block->tag.free) {
+			int segment;
+			void *tmp;
+			lub_heap_tag_t *tail =
+			    lub_heap_block__get_tail((lub_heap_block_t *)
+						     next_block);
 
-            if(NULL != next_block)
-            {
-                /* put the modified free block back into the tree */
-                lub_bintree_insert(&this->free_tree,next_block);
+			/* work out how many extra words we need */
+			words -= block->alloc.tag.words;
 
-                /* there is still a block above us */
-                segment = 0;
-            }
-            if(NULL != tmp)
-            {
-                /* we managed to extend upwards */
-                result = BOOL_TRUE;
+			/* remove the block from the tree */
+			lub_bintree_remove(&this->free_tree, next_block);
 
-                /* taint the old tail pointer */
-                tail = lub_heap_block__get_tail(block);
-                lub_heap_taint_memory((char*)tail,LUB_HEAP_TAINT_ALLOC,sizeof(lub_heap_tag_t));
-                
-                /* fill out the new block details */
-                block->alloc.tag.words        += words;
-                this->stats.alloc_bytes       += (words << 2);
-                this->stats.alloc_total_bytes += (words << 2);
+			/* remember the segment status of the next block */
+			segment = tail->segment;
 
-                tail = lub_heap_block__get_tail(block);
-                tail->segment = segment;
-                tail->free    = 0;
-                tail->words   = block->alloc.tag.words;
-            }
-        }
-    }
-    return result;
+			/* try and slice off a chunk of memory */
+			tmp =
+			    lub_heap_slice_from_bottom(this, &next_block,
+						       &words, BOOL_FALSE);
+
+			if (NULL != next_block) {
+				/* put the modified free block back into the tree */
+				lub_bintree_insert(&this->free_tree,
+						   next_block);
+
+				/* there is still a block above us */
+				segment = 0;
+			}
+			if (NULL != tmp) {
+				/* we managed to extend upwards */
+				result = BOOL_TRUE;
+
+				/* taint the old tail pointer */
+				tail = lub_heap_block__get_tail(block);
+				lub_heap_taint_memory((char *)tail,
+						      LUB_HEAP_TAINT_ALLOC,
+						      sizeof(lub_heap_tag_t));
+
+				/* fill out the new block details */
+				block->alloc.tag.words += words;
+				this->stats.alloc_bytes += (words << 2);
+				this->stats.alloc_total_bytes += (words << 2);
+
+				tail = lub_heap_block__get_tail(block);
+				tail->segment = segment;
+				tail->free = 0;
+				tail->words = block->alloc.tag.words;
+			}
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 15 - 17
lub/heap/heap_foreach_free_block.c

@@ -4,23 +4,21 @@
 #include "private.h"
 /*--------------------------------------------------------- */
 void
-lub_heap_foreach_free_block(lub_heap_t          *this,
-                            lub_heap_foreach_fn *fn,
-                            void                *arg)
+lub_heap_foreach_free_block(lub_heap_t * this,
+			    lub_heap_foreach_fn * fn, void *arg)
 {
-    lub_heap_block_t      *block = lub_bintree_findfirst(&this->free_tree);
-    lub_bintree_iterator_t iter;
-    unsigned int           i = 1;
-    
-    for(lub_bintree_iterator_init(&iter,&this->free_tree,block);
-        block;
-        block = lub_bintree_iterator_next(&iter))
-    {
-        /* call the client function */
-        fn(&block->free,
-           i++,
-           (block->free.tag.words << 2) - sizeof(lub_heap_alloc_block_t),
-           arg);        
-    }
+	lub_heap_block_t *block = lub_bintree_findfirst(&this->free_tree);
+	lub_bintree_iterator_t iter;
+	unsigned int i = 1;
+
+	for (lub_bintree_iterator_init(&iter, &this->free_tree, block);
+	     block; block = lub_bintree_iterator_next(&iter)) {
+		/* call the client function */
+		fn(&block->free,
+		   i++,
+		   (block->free.tag.words << 2) -
+		   sizeof(lub_heap_alloc_block_t), arg);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 9 - 16
lub/heap/heap_foreach_segment.c

@@ -4,22 +4,15 @@
 #include "private.h"
 /*--------------------------------------------------------- */
 void
-lub_heap_foreach_segment(lub_heap_t          *this,
-                         lub_heap_foreach_fn *fn,
-                         void                *arg)
+lub_heap_foreach_segment(lub_heap_t * this, lub_heap_foreach_fn * fn, void *arg)
 {
-    lub_heap_segment_t *segment;
-    unsigned int        i = 1;
-    
-    for(segment = &this->first_segment;
-        segment;
-        segment = segment->next)
-    {
-        /* call the client function */
-        fn(segment,
-           i++,
-           (segment->words << 2),
-           arg);
-    }
+	lub_heap_segment_t *segment;
+	unsigned int i = 1;
+
+	for (segment = &this->first_segment; segment; segment = segment->next) {
+		/* call the client function */
+		fn(segment, i++, (segment->words << 2), arg);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 52 - 52
lub/heap/heap_graft_to_bottom.c

@@ -4,58 +4,58 @@
 
 /*--------------------------------------------------------- */
 void
-lub_heap_graft_to_bottom(lub_heap_t       *this,
-                         lub_heap_block_t *free_block,
-                         void             *ptr,
-                         words_t           words,
-                         bool_t            seg_start,
-                         bool_t            other_free_block)
+lub_heap_graft_to_bottom(lub_heap_t * this,
+			 lub_heap_block_t * free_block,
+			 void *ptr,
+			 words_t words,
+			 bool_t seg_start, bool_t other_free_block)
 {
-    lub_heap_tag_t *tail;
-    words_t         new_words;
-    bool_t          seg_end;
-
-    assert(1 == free_block->free.tag.free);
-
-    /* remove the previous block from the free tree */
-    lub_bintree_remove(&this->free_tree,free_block);
-
-    /* update the size of the next block */
-    this->stats.free_bytes += (words << 2);
-
-    new_words = words + free_block->free.tag.words;
-
-    tail    = lub_heap_block__get_tail(free_block);
-    seg_end = tail->segment;
-
-    if(BOOL_FALSE == other_free_block)
-    {
-        /* 
-         * taint the memory being given back 
-         * NB. we don't bother to taint all the memory if we are
-         * combining two free blocks; they'll already be tainted.
-         */
-        lub_heap_taint_memory((char*)ptr,LUB_HEAP_TAINT_FREE,(words << 2)+sizeof(lub_heap_free_block_t));
-    }
-    else
-    {
-        /* clear the free block details */
-        lub_heap_taint_memory((char*)free_block,LUB_HEAP_TAINT_FREE,sizeof(lub_heap_free_block_t));
-    }
-
-    free_block = ptr;
-    /* make sure we retain the segment details */
-    free_block->free.tag.free    = 1;
-    free_block->free.tag.words   = new_words;
-    free_block->free.tag.segment = seg_start;
-    lub_bintree_node_init(&free_block->free.bt_node);
-
-    /* and update the tail */
-    tail->free    = 1;
-    tail->words   = free_block->free.tag.words;
-    tail->segment = seg_end;
-
-    /* insert back into the tree */
-    lub_bintree_insert(&this->free_tree,free_block);
+	lub_heap_tag_t *tail;
+	words_t new_words;
+	bool_t seg_end;
+
+	assert(1 == free_block->free.tag.free);
+
+	/* remove the previous block from the free tree */
+	lub_bintree_remove(&this->free_tree, free_block);
+
+	/* update the size of the next block */
+	this->stats.free_bytes += (words << 2);
+
+	new_words = words + free_block->free.tag.words;
+
+	tail = lub_heap_block__get_tail(free_block);
+	seg_end = tail->segment;
+
+	if (BOOL_FALSE == other_free_block) {
+		/* 
+		 * taint the memory being given back 
+		 * NB. we don't bother to taint all the memory if we are
+		 * combining two free blocks; they'll already be tainted.
+		 */
+		lub_heap_taint_memory((char *)ptr, LUB_HEAP_TAINT_FREE,
+				      (words << 2) +
+				      sizeof(lub_heap_free_block_t));
+	} else {
+		/* clear the free block details */
+		lub_heap_taint_memory((char *)free_block, LUB_HEAP_TAINT_FREE,
+				      sizeof(lub_heap_free_block_t));
+	}
+
+	free_block = ptr;
+	/* make sure we retain the segment details */
+	free_block->free.tag.free = 1;
+	free_block->free.tag.words = new_words;
+	free_block->free.tag.segment = seg_start;
+	lub_bintree_node_init(&free_block->free.bt_node);
+
+	/* and update the tail */
+	tail->free = 1;
+	tail->words = free_block->free.tag.words;
+	tail->segment = seg_end;
+
+	/* insert back into the tree */
+	lub_bintree_insert(&this->free_tree, free_block);
 }
+
 /*--------------------------------------------------------- */

+ 44 - 47
lub/heap/heap_graft_to_top.c

@@ -4,53 +4,50 @@
 
 /*--------------------------------------------------------- */
 void
-lub_heap_graft_to_top(lub_heap_t       *this,
-                      lub_heap_block_t *free_block,
-                      void             *ptr,
-                      words_t           words,
-                      bool_t            seg_end,
-                      bool_t            other_free_block)
+lub_heap_graft_to_top(lub_heap_t * this,
+		      lub_heap_block_t * free_block,
+		      void *ptr,
+		      words_t words, bool_t seg_end, bool_t other_free_block)
 {
-    lub_heap_tag_t   *tail;
-    
-    assert(1 == free_block->free.tag.free);
-
-    /* remove the previous block from the free tree */
-    lub_bintree_remove(&this->free_tree,free_block);
-
-    if(BOOL_FALSE == other_free_block)
-    {
-        tail = lub_heap_block__get_tail(free_block);
-        /* 
-         * taint the memory being given back 
-         * NB. we don't bother to taint all the memory if we are
-         * combining two free blocks; they'll already be tainted.
-         */
-        lub_heap_taint_memory((char*)tail,
-                              LUB_HEAP_TAINT_FREE,
-                              (words << 2) + sizeof(lub_heap_tag_t));
-    }
-    else
-    {
-        char *tmp = ptr;
-        /* clear out the freeblock details */
-        lub_heap_taint_memory(tmp - sizeof(lub_heap_tag_t),
-                              LUB_HEAP_TAINT_FREE,
-                              sizeof(lub_heap_tag_t) + sizeof(lub_heap_free_block_t));
-    }
-    /* update the size of the previous block */
-    free_block->free.tag.words  += words;
-    this->stats.free_bytes      += (words << 2);
-
-    /* make sure we retain the segment details */
-    tail          = lub_heap_block__get_tail(free_block);
-    tail->segment = seg_end;
-    tail->free    = 1;
-
-    /* update the size of the tail */
-    tail->words = free_block->free.tag.words;
-
-    /* insert back into the tree */
-    lub_bintree_insert(&this->free_tree,free_block);
+	lub_heap_tag_t *tail;
+
+	assert(1 == free_block->free.tag.free);
+
+	/* remove the previous block from the free tree */
+	lub_bintree_remove(&this->free_tree, free_block);
+
+	if (BOOL_FALSE == other_free_block) {
+		tail = lub_heap_block__get_tail(free_block);
+		/* 
+		 * taint the memory being given back 
+		 * NB. we don't bother to taint all the memory if we are
+		 * combining two free blocks; they'll already be tainted.
+		 */
+		lub_heap_taint_memory((char *)tail,
+				      LUB_HEAP_TAINT_FREE,
+				      (words << 2) + sizeof(lub_heap_tag_t));
+	} else {
+		char *tmp = ptr;
+		/* clear out the freeblock details */
+		lub_heap_taint_memory(tmp - sizeof(lub_heap_tag_t),
+				      LUB_HEAP_TAINT_FREE,
+				      sizeof(lub_heap_tag_t) +
+				      sizeof(lub_heap_free_block_t));
+	}
+	/* update the size of the previous block */
+	free_block->free.tag.words += words;
+	this->stats.free_bytes += (words << 2);
+
+	/* make sure we retain the segment details */
+	tail = lub_heap_block__get_tail(free_block);
+	tail->segment = seg_end;
+	tail->free = 1;
+
+	/* update the size of the tail */
+	tail->words = free_block->free.tag.words;
+
+	/* insert back into the tree */
+	lub_bintree_insert(&this->free_tree, free_block);
 }
+
 /*--------------------------------------------------------- */

+ 35 - 36
lub/heap/heap_init_free_block.c

@@ -2,42 +2,41 @@
 
 /*--------------------------------------------------------- */
 void
-lub_heap_init_free_block(lub_heap_t *this,
-                         void       *start,
-                         size_t      size,
-                         bool_t      seg_start,
-                         bool_t      seg_end)
+lub_heap_init_free_block(lub_heap_t * this,
+			 void *start,
+			 size_t size, bool_t seg_start, bool_t seg_end)
 {
-    lub_heap_free_block_t *block = start;
-    lub_heap_tag_t        *tail;
-    words_t                words;
-    
-    /* initialise the given memory */
-    lub_heap_taint_memory(start,LUB_HEAP_TAINT_FREE,size);
-
-    /* calculate the number of words in this segment */
-    words  = (size >> 2);
-    
-    /* setup the block */
-    block->tag.segment = seg_start; /* start of a segment */
-    block->tag.free    = 1;
-    block->tag.words   = words;
-    
-    /* initialise the tree node */
-    lub_bintree_node_init(&block->bt_node);
-    
-    /* now fill out the trailing tag */
-    tail          = lub_heap_block__get_tail((lub_heap_block_t*)block);
-    tail->segment = seg_end; /* end of a segment */
-    tail->free    = 1;
-    tail->words   = words;
-
-    /* now insert this free block into the tree */
-    lub_bintree_insert(&this->free_tree,block);
-
-    ++this->stats.free_blocks;
-    this->stats.free_bytes    += (words << 2);
-    this->stats.free_bytes    -= sizeof(lub_heap_alloc_block_t);
-    this->stats.free_overhead += sizeof(lub_heap_alloc_block_t);
+	lub_heap_free_block_t *block = start;
+	lub_heap_tag_t *tail;
+	words_t words;
+
+	/* initialise the given memory */
+	lub_heap_taint_memory(start, LUB_HEAP_TAINT_FREE, size);
+
+	/* calculate the number of words in this segment */
+	words = (size >> 2);
+
+	/* setup the block */
+	block->tag.segment = seg_start;	/* start of a segment */
+	block->tag.free = 1;
+	block->tag.words = words;
+
+	/* initialise the tree node */
+	lub_bintree_node_init(&block->bt_node);
+
+	/* now fill out the trailing tag */
+	tail = lub_heap_block__get_tail((lub_heap_block_t *) block);
+	tail->segment = seg_end;	/* end of a segment */
+	tail->free = 1;
+	tail->words = words;
+
+	/* now insert this free block into the tree */
+	lub_bintree_insert(&this->free_tree, block);
+
+	++this->stats.free_blocks;
+	this->stats.free_bytes += (words << 2);
+	this->stats.free_bytes -= sizeof(lub_heap_alloc_block_t);
+	this->stats.free_overhead += sizeof(lub_heap_alloc_block_t);
 }
+
 /*--------------------------------------------------------- */

+ 27 - 29
lub/heap/heap_merge_with_next.c

@@ -4,36 +4,34 @@
 
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_merge_with_next(lub_heap_t       *this,
-                         lub_heap_block_t *block)
+lub_heap_merge_with_next(lub_heap_t * this, lub_heap_block_t * block)
 {
-    lub_heap_status_t result = LUB_HEAP_FAILED;
-    lub_heap_block_t *next_block;
-        
-    assert(0 == block->alloc.tag.free);
+	lub_heap_status_t result = LUB_HEAP_FAILED;
+	lub_heap_block_t *next_block;
 
-    do
-    {
-        /* see whether there is a free block just after us */
-        next_block = lub_heap_block_getnext(block);
-        if(   (NULL != next_block               ) 
-           && (1    == next_block->free.tag.free))
-        {
-            if(BOOL_FALSE == lub_heap_block_check(next_block))
-            {
-                result = LUB_HEAP_CORRUPTED;
-                break;
-            }
-            lub_heap_graft_to_bottom(this,
-                                     next_block,
-                                     block,
-                                     block->alloc.tag.words,
-                                     block->alloc.tag.segment,
-                                     block->free.tag.free ? BOOL_TRUE : BOOL_FALSE);
-            result = LUB_HEAP_OK;
-        }
-    } while(0);
-    
-    return result;
+	assert(0 == block->alloc.tag.free);
+
+	do {
+		/* see whether there is a free block just after us */
+		next_block = lub_heap_block_getnext(block);
+		if ((NULL != next_block)
+		    && (1 == next_block->free.tag.free)) {
+			if (BOOL_FALSE == lub_heap_block_check(next_block)) {
+				result = LUB_HEAP_CORRUPTED;
+				break;
+			}
+			lub_heap_graft_to_bottom(this,
+						 next_block,
+						 block,
+						 block->alloc.tag.words,
+						 block->alloc.tag.segment,
+						 block->free.tag.
+						 free ? BOOL_TRUE : BOOL_FALSE);
+			result = LUB_HEAP_OK;
+		}
+	} while (0);
+
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 39 - 39
lub/heap/heap_merge_with_previous.c

@@ -2,48 +2,48 @@
 
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_merge_with_previous(lub_heap_t       *this,
-                             lub_heap_block_t *block)
+lub_heap_merge_with_previous(lub_heap_t * this, lub_heap_block_t * block)
 {
-    lub_heap_status_t result = LUB_HEAP_FAILED;
-    lub_heap_block_t *prev_block;
+	lub_heap_status_t result = LUB_HEAP_FAILED;
+	lub_heap_block_t *prev_block;
 
-    do
-    {
-        /* see whether there is a free block just before us */
-        prev_block = lub_heap_block_getprevious(block);
-        if(   (NULL      != prev_block                   )           
-           && (1         == prev_block->free.tag.free    ))
-        {
-            lub_heap_tag_t *tail;
+	do {
+		/* see whether there is a free block just before us */
+		prev_block = lub_heap_block_getprevious(block);
+		if ((NULL != prev_block)
+		    && (1 == prev_block->free.tag.free)) {
+			lub_heap_tag_t *tail;
 
-            if(BOOL_FALSE == lub_heap_block_check(prev_block))
-            {
-                result = LUB_HEAP_CORRUPTED;
-                break;
-            }
-            tail = lub_heap_block__get_tail(block);
+			if (BOOL_FALSE == lub_heap_block_check(prev_block)) {
+				result = LUB_HEAP_CORRUPTED;
+				break;
+			}
+			tail = lub_heap_block__get_tail(block);
 
-            if(1 == block->free.tag.free)
-            {
-                /* remove this free block from the tree */
-                lub_bintree_remove(&this->free_tree,block);
-                --this->stats.free_blocks;
-                this->stats.free_bytes    -= (block->free.tag.words << 2);
-                this->stats.free_bytes    += sizeof(lub_heap_alloc_block_t);
-                this->stats.free_overhead -= sizeof(lub_heap_alloc_block_t);
-            }
-            /* now add this memory to the previous free block */
-            lub_heap_graft_to_top(this,
-                                  prev_block,
-                                  block,
-                                  block->alloc.tag.words,
-                                  tail->segment,
-                                  block->free.tag.free ? BOOL_TRUE : BOOL_FALSE);
-            result = LUB_HEAP_OK;
-        }
-    } while(0);
-    
-    return result;
+			if (1 == block->free.tag.free) {
+				/* remove this free block from the tree */
+				lub_bintree_remove(&this->free_tree, block);
+				--this->stats.free_blocks;
+				this->stats.free_bytes -=
+				    (block->free.tag.words << 2);
+				this->stats.free_bytes +=
+				    sizeof(lub_heap_alloc_block_t);
+				this->stats.free_overhead -=
+				    sizeof(lub_heap_alloc_block_t);
+			}
+			/* now add this memory to the previous free block */
+			lub_heap_graft_to_top(this,
+					      prev_block,
+					      block,
+					      block->alloc.tag.words,
+					      tail->segment,
+					      block->free.tag.
+					      free ? BOOL_TRUE : BOOL_FALSE);
+			result = LUB_HEAP_OK;
+		}
+	} while (0);
+
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 60 - 61
lub/heap/heap_new_alloc_block.c

@@ -1,75 +1,74 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_heap_new_alloc_block(lub_heap_t *this,
-                         words_t     words)
+void *lub_heap_new_alloc_block(lub_heap_t * this, words_t words)
 {
-    void                  *result = NULL;
-    lub_heap_free_block_t *free_block;
-    lub_heap_block_t      *new_block;
-    lub_heap_key_t         key;
-        
-    /* initialise the seach key */
-    key.words = words;
-    key.block = 0;
+	void *result = NULL;
+	lub_heap_free_block_t *free_block;
+	lub_heap_block_t *new_block;
+	lub_heap_key_t key;
 
-    /* find the smallest free block which can take this request */
-    free_block = lub_bintree_findnext(&this->free_tree,&key);
-    if(NULL != free_block)
-    {
-        lub_heap_tag_t *tail;
-        unsigned int    seg_end, seg_start;
+	/* initialise the seach key */
+	key.words = words;
+	key.block = 0;
 
-        /* remember if this is the end of a segment */
-        tail      = lub_heap_block__get_tail((lub_heap_block_t *)free_block);
-        seg_start = free_block->tag.segment;
-        seg_end   = tail->segment;
+	/* find the smallest free block which can take this request */
+	free_block = lub_bintree_findnext(&this->free_tree, &key);
+	if (NULL != free_block) {
+		lub_heap_tag_t *tail;
+		unsigned int seg_end, seg_start;
 
-        /* remove the block from the free tree */
-        lub_bintree_remove(&this->free_tree,free_block);
+		/* remember if this is the end of a segment */
+		tail =
+		    lub_heap_block__get_tail((lub_heap_block_t *) free_block);
+		seg_start = free_block->tag.segment;
+		seg_end = tail->segment;
 
-        /* now slice the bottom off for the client */
-        new_block = lub_heap_slice_from_bottom(this,&free_block,&words,BOOL_FALSE);
+		/* remove the block from the free tree */
+		lub_bintree_remove(&this->free_tree, free_block);
 
-        if(NULL != free_block)
-        {
-            /* put the block back into the tree */
-            lub_bintree_insert(&this->free_tree,free_block);
-        }
-        if(NULL != new_block)
-        {
-            /* set up the tag details */
-            new_block->alloc.tag.segment = seg_start;
-            new_block->alloc.tag.free    = 0;
-            new_block->alloc.tag.words   = words;
+		/* now slice the bottom off for the client */
+		new_block =
+		    lub_heap_slice_from_bottom(this, &free_block, &words,
+					       BOOL_FALSE);
 
-            tail = lub_heap_block__get_tail(new_block);
-            if(NULL == free_block)
-            {
-                /* we've swallowed the whole free block */
-                tail->segment = seg_end;
-            }
-            else
-            {
-                tail->segment = 0;
-            }
-            tail->free    = 0;
-            tail->words   = words;
+		if (NULL != free_block) {
+			/* put the block back into the tree */
+			lub_bintree_insert(&this->free_tree, free_block);
+		}
+		if (NULL != new_block) {
+			/* set up the tag details */
+			new_block->alloc.tag.segment = seg_start;
+			new_block->alloc.tag.free = 0;
+			new_block->alloc.tag.words = words;
 
-            /* update the stats */
-            ++this->stats.alloc_blocks;
-            this->stats.alloc_bytes       += (words << 2);
-            this->stats.alloc_bytes       -= sizeof(lub_heap_alloc_block_t);
-            ++this->stats.alloc_total_blocks;
-            this->stats.alloc_total_bytes += (words << 2);
-            this->stats.alloc_total_bytes -= sizeof(lub_heap_alloc_block_t);
-            this->stats.alloc_overhead    += sizeof(lub_heap_alloc_block_t);
+			tail = lub_heap_block__get_tail(new_block);
+			if (NULL == free_block) {
+				/* we've swallowed the whole free block */
+				tail->segment = seg_end;
+			} else {
+				tail->segment = 0;
+			}
+			tail->free = 0;
+			tail->words = words;
 
-            /* fill out the client's pointer */
-            result = new_block->alloc.memory;
-        }
-    }
-    return result;
+			/* update the stats */
+			++this->stats.alloc_blocks;
+			this->stats.alloc_bytes += (words << 2);
+			this->stats.alloc_bytes -=
+			    sizeof(lub_heap_alloc_block_t);
+			++this->stats.alloc_total_blocks;
+			this->stats.alloc_total_bytes += (words << 2);
+			this->stats.alloc_total_bytes -=
+			    sizeof(lub_heap_alloc_block_t);
+			this->stats.alloc_overhead +=
+			    sizeof(lub_heap_alloc_block_t);
+
+			/* fill out the client's pointer */
+			result = new_block->alloc.memory;
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 18 - 21
lub/heap/heap_new_free_block.c

@@ -3,28 +3,25 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-lub_heap_status_t 
-lub_heap_new_free_block(lub_heap_t       *this,
-                        lub_heap_block_t *block)
+lub_heap_status_t
+lub_heap_new_free_block(lub_heap_t * this, lub_heap_block_t * block)
 {
-    lub_heap_status_t result = LUB_HEAP_OK;
-    bool_t            seg_start, seg_end;
-    lub_heap_tag_t   *tail;
-    
-    assert(0 == block->alloc.tag.free);
-    
-    /* get the segment details from the current block */
-    seg_start = block->alloc.tag.segment;
-    tail = lub_heap_block__get_tail(block);
-    seg_end = tail->segment;
+	lub_heap_status_t result = LUB_HEAP_OK;
+	bool_t seg_start, seg_end;
+	lub_heap_tag_t *tail;
 
-    /* now set up the free block */
-    lub_heap_init_free_block(this,
-                             block,
-                             (tail->words << 2),
-                             seg_start,
-                             seg_end);
-    
-    return result;
+	assert(0 == block->alloc.tag.free);
+
+	/* get the segment details from the current block */
+	seg_start = block->alloc.tag.segment;
+	tail = lub_heap_block__get_tail(block);
+	seg_end = tail->segment;
+
+	/* now set up the free block */
+	lub_heap_init_free_block(this,
+				 block, (tail->words << 2), seg_start, seg_end);
+
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 27 - 32
lub/heap/heap_post_realloc.c

@@ -4,43 +4,38 @@
 #include "context.h"
 #include "node.h"
 
-void
-lub_heap_post_realloc(lub_heap_t        *this,
-                      char             **ptr)
+void lub_heap_post_realloc(lub_heap_t * this, char **ptr)
 {
-    /* only act if we are about to re-enable the monitoring */
-    if((0 < lub_heap_frame_count) && ( 2 > this->suppress))
-    {
-        if(NULL != *ptr)
-        {
-            stackframe_t              frame;
-            lub_heap_node_t          *node = (lub_heap_node_t*)*ptr;
-            const lub_heap_context_t *context;
-
-            lub_heap__get_stackframe(&frame,lub_heap_frame_count);
-
-            /* make sure we break any recursive behaviour */
-            ++this->suppress;
-            context = lub_heap_context_find_or_create(this,&frame);
-            --this->suppress;
-            
-            /* initialise the node instance */
-            lub_heap_node_init(node,(lub_heap_context_t*)context);
-
-            /* make sure we doctor the pointer given back to the client */
-            *ptr = lub_heap_node__get_ptr(node);
-        }
-    }
+	/* only act if we are about to re-enable the monitoring */
+	if ((0 < lub_heap_frame_count) && (2 > this->suppress)) {
+		if (NULL != *ptr) {
+			stackframe_t frame;
+			lub_heap_node_t *node = (lub_heap_node_t *) * ptr;
+			const lub_heap_context_t *context;
+
+			lub_heap__get_stackframe(&frame, lub_heap_frame_count);
+
+			/* make sure we break any recursive behaviour */
+			++this->suppress;
+			context = lub_heap_context_find_or_create(this, &frame);
+			--this->suppress;
+
+			/* initialise the node instance */
+			lub_heap_node_init(node,
+					   (lub_heap_context_t *) context);
+
+			/* make sure we doctor the pointer given back to the client */
+			*ptr = lub_heap_node__get_ptr(node);
+		}
+	}
 }
 
-void
-lub_heap_leak_suppress_detection(lub_heap_t *this)
+void lub_heap_leak_suppress_detection(lub_heap_t * this)
 {
-    ++this->suppress;
+	++this->suppress;
 }
 
-void
-lub_heap_leak_restore_detection(lub_heap_t *this)
+void lub_heap_leak_restore_detection(lub_heap_t * this)
 {
-    --this->suppress;
+	--this->suppress;
 }

+ 24 - 31
lub/heap/heap_pre_realloc.c

@@ -7,36 +7,29 @@
 /*
  * This function
  */
-void
-lub_heap_pre_realloc(lub_heap_t *this,
-                     char      **ptr,
-                     size_t     *size)
+void lub_heap_pre_realloc(lub_heap_t * this, char **ptr, size_t * size)
 {
-    if(*ptr)
-    {
-        /* is this a pointer to a node in the "leak" trees? */
-        lub_heap_node_t *node = lub_heap_node_from_start_of_block_ptr(*ptr);
-        if(NULL != node)
-        {
-            lub_heap_node_fini(node);
-             /* move the pointer to the start of the block */
-            *ptr = (char*)node;
-        }
-    }
-    if((0 < lub_heap_frame_count))
-    {
-        size_t old_size = *size;
-        if(old_size)
-        {
-            /* allocate enough bytes for a node */
-            *size += lub_heap_node__get_instanceSize();
-            if(*size < old_size)
-            {
-                /* we've wrapped the size variable
-                 * make sure we fail the allocation
-                 */
-                *size = (size_t)-1;
-            }
-        }
-    }
+	if (*ptr) {
+		/* is this a pointer to a node in the "leak" trees? */
+		lub_heap_node_t *node =
+		    lub_heap_node_from_start_of_block_ptr(*ptr);
+		if (NULL != node) {
+			lub_heap_node_fini(node);
+			/* move the pointer to the start of the block */
+			*ptr = (char *)node;
+		}
+	}
+	if ((0 < lub_heap_frame_count)) {
+		size_t old_size = *size;
+		if (old_size) {
+			/* allocate enough bytes for a node */
+			*size += lub_heap_node__get_instanceSize();
+			if (*size < old_size) {
+				/* we've wrapped the size variable
+				 * make sure we fail the allocation
+				 */
+				*size = (size_t) - 1;
+			}
+		}
+	}
 }

+ 228 - 241
lub/heap/heap_raw_realloc.c

@@ -5,247 +5,234 @@
 
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_raw_realloc(lub_heap_t      *this,
-                     char           **ptr,
-                     size_t           requested_size,
-                     lub_heap_align_t alignment)
+lub_heap_raw_realloc(lub_heap_t * this,
+		     char **ptr,
+		     size_t requested_size, lub_heap_align_t alignment)
 {
-    lub_heap_status_t result               = LUB_HEAP_FAILED;
-    lub_heap_block_t *block                = NULL;
-    words_t           words;
-    char             *new_ptr              = NULL;
-    const size_t      MIN_SIZE             = sizeof(lub_heap_free_block_t);
-    /* make the minimum alignment leave enough space for 
-     * (sizeof(lub_heap_free_block_t) + sizeof(lub_heap_alloc_block_t) + sizeof(lub_heap_node_t))
-     * approx 72 bytes on 64-bit architecture
-     */
-    const lub_heap_align_t MIN_ALIGN_NON_NATIVE = LUB_HEAP_ALIGN_2_POWER_7;
-    size_t                 size                 = requested_size;
-    
-    do
-    {
-        if(NULL == ptr) break; /* The client MUST give us a pointer to play with */
-        
-        if(LUB_HEAP_ZERO_ALLOC == *ptr)
-        {
-            *ptr = NULL;
-        }
-        if(*ptr && (BOOL_FALSE == lub_heap_validate_pointer(this,*ptr)))
-        {
-            /* This is not a valid pointer */
-            result = LUB_HEAP_INVALID_POINTER;
-            break;
-        }
-        /* check the heap integrity */
-        if(BOOL_FALSE == lub_heap_check_memory(this))
-        {
-            result = LUB_HEAP_CORRUPTED;
-            break;
-        }
-        
-        if(size)
-        {
-            /* make sure we get enough space for the header/tail */
-            size += sizeof(lub_heap_alloc_block_t);
-
-            /* make sure we are at least natively aligned */
-            size += (LUB_HEAP_ALIGN_NATIVE-1);
-            size &= ~(LUB_HEAP_ALIGN_NATIVE-1);
-
-            if(size < MIN_SIZE)
-            {
-                /* 
-                 * we must ensure that any allocated block is at least
-                 * large enough to hold a free block node when it is released
-                 */
-                size = MIN_SIZE;
-            }
-            if(LUB_HEAP_ALIGN_NATIVE != alignment)
-            {
-                if(alignment < MIN_ALIGN_NON_NATIVE)
-                {
-                    /*
-                     * ensure that we always leave enough space
-                     * to be able to collapse the block to the 
-                     * right size
-                     */
-                    alignment = MIN_ALIGN_NON_NATIVE;
-                }
-                /* add twice the alignment */
-                size  += (alignment << 1);
-            }
-        }
-        words = (size >> 2);
-
-        if(requested_size > size)
-        {
-            /* the size has wrapped when accounting for overheads */
-            break;
-        }
-        if(NULL != *ptr)
-        {
-            /* get reference to the current block */
-            block = lub_heap_block_from_ptr(*ptr);
-    
-            /* first of all check this is an allocated block */
-            if(1 == block->alloc.tag.free)
-            {
-                result = LUB_HEAP_DOUBLE_FREE;
-                break;
-            }
-            /* first of all check this is an allocated block */
-            if(BOOL_FALSE == lub_heap_block_check(block))
-            {
-                result = LUB_HEAP_CORRUPTED;
-                break;
-            }
-            /* is the current block large enough for the request */
-            if(words && (block->alloc.tag.words >= words))
-            {
-                lub_heap_block_t *next_block = lub_heap_block_getnext(block);
-                words_t           delta      = (block->alloc.tag.words - words);
-                lub_heap_tag_t   *tail;
-    
-                result = LUB_HEAP_OK;
-                new_ptr = *ptr;
-                
-                if(delta && (NULL != next_block) )
-                {
-                    /* can we graft this spare memory to a following free block? */
-                    if(1 == next_block->free.tag.free)
-                    {
-                        
-                        block->alloc.tag.words = words;
-                        tail = lub_heap_block__get_tail(block);
-                        tail->words   = words;
-                        tail->free    = 0;
-                        tail->segment = 0;
-                        
-                        lub_heap_graft_to_bottom(this,
-                                                 next_block,
-                                                 &tail[1],
-                                                 delta,
-                                                 BOOL_FALSE,
-                                                 BOOL_FALSE);
-                        this->stats.alloc_bytes -= (delta << 2);
-                        break;
-                    }
-                }
-                /* Is there enough space to turn the spare memory into a free block? */
-                if(delta >= (sizeof(lub_heap_free_block_t) >> 2))
-                {
-                    tail = lub_heap_block__get_tail(block);
-                    
-                    lub_heap_init_free_block(this,
-                                             &tail[1-delta],
-                                             (delta << 2),
-                                             BOOL_FALSE,
-                                             tail->segment);
-
-                    block->alloc.tag.words = words;
-                    tail = lub_heap_block__get_tail(block);
-                    tail->words   = words;
-                    tail->free    = 0;
-                    tail->segment = 0;
-                    this->stats.alloc_bytes -= (delta << 2);
-                 }
-                break;
-            }
-            else if(words)
-            {
-                /* can we simple extend the current allocated block? */
-                if(    (BOOL_TRUE == lub_heap_extend_upwards  (this,&block,words))
-                    || (BOOL_TRUE == lub_heap_extend_downwards(this,&block,words))
-                    || (BOOL_TRUE == lub_heap_extend_both_ways(this,&block,words)) )
-                {
-                    result  = LUB_HEAP_OK;
-                    new_ptr = (char*)block->alloc.memory;
-                    break;
-                }
-            }
-        }
-        if(words && (LUB_HEAP_FAILED == result))
-        {
-            /* need to reallocate and copy the data */
-            new_ptr = lub_heap_new_alloc_block(this,words);
-            if(NULL != new_ptr)
-            {
-                result = LUB_HEAP_OK;
-
-                if(NULL != block)
-                {
-                    /* now copy the old contents across */
-                    memcpy(new_ptr,
-                           (const char*)block->alloc.memory,
-                           (block->alloc.tag.words << 2) - sizeof(lub_heap_alloc_block_t));
-                }
-            }
-            else
-            {
-                /* couldn't find the space for the request */
-                break;
-            }
-        }
-        /* now it's time to release the memory of the old block */
-        if(NULL != block)
-        {
-            words_t old_words = block->alloc.tag.words;
-            bool_t  done      = BOOL_FALSE;
-
-            /* combine with the next block */
-            if(LUB_HEAP_OK == lub_heap_merge_with_next(this,block))
-            {
-                done = BOOL_TRUE;
-            }
-            /* combine with the previous block */
-            if(LUB_HEAP_OK == lub_heap_merge_with_previous(this,block))
-            {
-                done = BOOL_TRUE;
-            }
-            if(BOOL_FALSE == done)
-            {
-                /* create a new free block */
-                lub_heap_new_free_block(this,block);
-            }
-            result = LUB_HEAP_OK;
-            
-            --this->stats.alloc_blocks;
-            this->stats.alloc_bytes    -= (old_words << 2);
-            this->stats.alloc_bytes    += sizeof(lub_heap_alloc_block_t);
-            this->stats.alloc_overhead -= sizeof(lub_heap_alloc_block_t);
-        }
-    } while(0);
-
-    if(LUB_HEAP_OK == result)
-    {
-        unsigned delta;
-        
-        /* align the block as required */
-        if(LUB_HEAP_ALIGN_NATIVE != alignment)
-        {
-            lub_heap_align_block(this,
-                                 alignment,
-                                 &new_ptr,
-                                 &size);
-        }
-        /* revert to client space available */
-        size -= sizeof(lub_heap_alloc_block_t);
-        *ptr = new_ptr;
-        delta = (size - requested_size);
-        
-        if(*ptr && requested_size && (0 < delta))
-        {
-            /* make sure that any excess memory is tainted */
-            lub_heap_taint_memory(*ptr + requested_size,LUB_HEAP_TAINT_ALLOC,delta);
-        }
-    }
-    else if(   (LUB_HEAP_FAILED == result) 
-            && (0               == requested_size)
-            && (NULL            == *ptr))
-    {
-        /* freeing zero is always OK */
-        result = LUB_HEAP_OK;
-    }
-    return result;
+	lub_heap_status_t result = LUB_HEAP_FAILED;
+	lub_heap_block_t *block = NULL;
+	words_t words;
+	char *new_ptr = NULL;
+	const size_t MIN_SIZE = sizeof(lub_heap_free_block_t);
+	/* make the minimum alignment leave enough space for 
+	 * (sizeof(lub_heap_free_block_t) + sizeof(lub_heap_alloc_block_t) + sizeof(lub_heap_node_t))
+	 * approx 72 bytes on 64-bit architecture
+	 */
+	const lub_heap_align_t MIN_ALIGN_NON_NATIVE = LUB_HEAP_ALIGN_2_POWER_7;
+	size_t size = requested_size;
+
+	do {
+		if (NULL == ptr)
+			break;	/* The client MUST give us a pointer to play with */
+
+		if (LUB_HEAP_ZERO_ALLOC == *ptr) {
+			*ptr = NULL;
+		}
+		if (*ptr
+		    && (BOOL_FALSE == lub_heap_validate_pointer(this, *ptr))) {
+			/* This is not a valid pointer */
+			result = LUB_HEAP_INVALID_POINTER;
+			break;
+		}
+		/* check the heap integrity */
+		if (BOOL_FALSE == lub_heap_check_memory(this)) {
+			result = LUB_HEAP_CORRUPTED;
+			break;
+		}
+
+		if (size) {
+			/* make sure we get enough space for the header/tail */
+			size += sizeof(lub_heap_alloc_block_t);
+
+			/* make sure we are at least natively aligned */
+			size += (LUB_HEAP_ALIGN_NATIVE - 1);
+			size &= ~(LUB_HEAP_ALIGN_NATIVE - 1);
+
+			if (size < MIN_SIZE) {
+				/* 
+				 * we must ensure that any allocated block is at least
+				 * large enough to hold a free block node when it is released
+				 */
+				size = MIN_SIZE;
+			}
+			if (LUB_HEAP_ALIGN_NATIVE != alignment) {
+				if (alignment < MIN_ALIGN_NON_NATIVE) {
+					/*
+					 * ensure that we always leave enough space
+					 * to be able to collapse the block to the 
+					 * right size
+					 */
+					alignment = MIN_ALIGN_NON_NATIVE;
+				}
+				/* add twice the alignment */
+				size += (alignment << 1);
+			}
+		}
+		words = (size >> 2);
+
+		if (requested_size > size) {
+			/* the size has wrapped when accounting for overheads */
+			break;
+		}
+		if (NULL != *ptr) {
+			/* get reference to the current block */
+			block = lub_heap_block_from_ptr(*ptr);
+
+			/* first of all check this is an allocated block */
+			if (1 == block->alloc.tag.free) {
+				result = LUB_HEAP_DOUBLE_FREE;
+				break;
+			}
+			/* first of all check this is an allocated block */
+			if (BOOL_FALSE == lub_heap_block_check(block)) {
+				result = LUB_HEAP_CORRUPTED;
+				break;
+			}
+			/* is the current block large enough for the request */
+			if (words && (block->alloc.tag.words >= words)) {
+				lub_heap_block_t *next_block =
+				    lub_heap_block_getnext(block);
+				words_t delta =
+				    (block->alloc.tag.words - words);
+				lub_heap_tag_t *tail;
+
+				result = LUB_HEAP_OK;
+				new_ptr = *ptr;
+
+				if (delta && (NULL != next_block)) {
+					/* can we graft this spare memory to a following free block? */
+					if (1 == next_block->free.tag.free) {
+
+						block->alloc.tag.words = words;
+						tail =
+						    lub_heap_block__get_tail
+						    (block);
+						tail->words = words;
+						tail->free = 0;
+						tail->segment = 0;
+
+						lub_heap_graft_to_bottom(this,
+									 next_block,
+									 &tail
+									 [1],
+									 delta,
+									 BOOL_FALSE,
+									 BOOL_FALSE);
+						this->stats.alloc_bytes -=
+						    (delta << 2);
+						break;
+					}
+				}
+				/* Is there enough space to turn the spare memory into a free block? */
+				if (delta >=
+				    (sizeof(lub_heap_free_block_t) >> 2)) {
+					tail = lub_heap_block__get_tail(block);
+
+					lub_heap_init_free_block(this,
+								 &tail[1 -
+								       delta],
+								 (delta << 2),
+								 BOOL_FALSE,
+								 tail->segment);
+
+					block->alloc.tag.words = words;
+					tail = lub_heap_block__get_tail(block);
+					tail->words = words;
+					tail->free = 0;
+					tail->segment = 0;
+					this->stats.alloc_bytes -= (delta << 2);
+				}
+				break;
+			} else if (words) {
+				/* can we simple extend the current allocated block? */
+				if ((BOOL_TRUE ==
+				     lub_heap_extend_upwards(this, &block,
+							     words))
+				    || (BOOL_TRUE ==
+					lub_heap_extend_downwards(this, &block,
+								  words))
+				    || (BOOL_TRUE ==
+					lub_heap_extend_both_ways(this, &block,
+								  words))) {
+					result = LUB_HEAP_OK;
+					new_ptr = (char *)block->alloc.memory;
+					break;
+				}
+			}
+		}
+		if (words && (LUB_HEAP_FAILED == result)) {
+			/* need to reallocate and copy the data */
+			new_ptr = lub_heap_new_alloc_block(this, words);
+			if (NULL != new_ptr) {
+				result = LUB_HEAP_OK;
+
+				if (NULL != block) {
+					/* now copy the old contents across */
+					memcpy(new_ptr,
+					       (const char *)block->alloc.
+					       memory,
+					       (block->alloc.tag.words << 2) -
+					       sizeof(lub_heap_alloc_block_t));
+				}
+			} else {
+				/* couldn't find the space for the request */
+				break;
+			}
+		}
+		/* now it's time to release the memory of the old block */
+		if (NULL != block) {
+			words_t old_words = block->alloc.tag.words;
+			bool_t done = BOOL_FALSE;
+
+			/* combine with the next block */
+			if (LUB_HEAP_OK ==
+			    lub_heap_merge_with_next(this, block)) {
+				done = BOOL_TRUE;
+			}
+			/* combine with the previous block */
+			if (LUB_HEAP_OK ==
+			    lub_heap_merge_with_previous(this, block)) {
+				done = BOOL_TRUE;
+			}
+			if (BOOL_FALSE == done) {
+				/* create a new free block */
+				lub_heap_new_free_block(this, block);
+			}
+			result = LUB_HEAP_OK;
+
+			--this->stats.alloc_blocks;
+			this->stats.alloc_bytes -= (old_words << 2);
+			this->stats.alloc_bytes +=
+			    sizeof(lub_heap_alloc_block_t);
+			this->stats.alloc_overhead -=
+			    sizeof(lub_heap_alloc_block_t);
+		}
+	} while (0);
+
+	if (LUB_HEAP_OK == result) {
+		unsigned delta;
+
+		/* align the block as required */
+		if (LUB_HEAP_ALIGN_NATIVE != alignment) {
+			lub_heap_align_block(this, alignment, &new_ptr, &size);
+		}
+		/* revert to client space available */
+		size -= sizeof(lub_heap_alloc_block_t);
+		*ptr = new_ptr;
+		delta = (size - requested_size);
+
+		if (*ptr && requested_size && (0 < delta)) {
+			/* make sure that any excess memory is tainted */
+			lub_heap_taint_memory(*ptr + requested_size,
+					      LUB_HEAP_TAINT_ALLOC, delta);
+		}
+	} else if ((LUB_HEAP_FAILED == result)
+		   && (0 == requested_size)
+		   && (NULL == *ptr)) {
+		/* freeing zero is always OK */
+		result = LUB_HEAP_OK;
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 46 - 48
lub/heap/heap_realloc.c

@@ -5,55 +5,53 @@
 
 /*--------------------------------------------------------- */
 lub_heap_status_t
-lub_heap_realloc(lub_heap_t      *this,
-                 char           **ptr,
-                 size_t           requested_size,
-                 lub_heap_align_t alignment)
+lub_heap_realloc(lub_heap_t * this,
+		 char **ptr, size_t requested_size, lub_heap_align_t alignment)
 {
-    lub_heap_status_t status   = LUB_HEAP_FAILED;
-    size_t            size     = requested_size;
-    
-    /* opportunity for leak detection to do it's stuff */
-    lub_heap_pre_realloc(this,ptr,&size);
+	lub_heap_status_t status = LUB_HEAP_FAILED;
+	size_t size = requested_size;
 
-    if(this->cache && (LUB_HEAP_ALIGN_NATIVE == alignment))
-    {
-        /* try and get the memory from the cache */
-        status = lub_heap_cache_realloc(this,ptr,size);
-    }
-    else
-    {
-        /* get the memory directly from the dynamic heap */
-        status = lub_heap_raw_realloc(this,ptr,size,alignment);
-    }
-    
-    /* opportunity for leak detection to do it's stuff */
-    lub_heap_post_realloc(this,ptr);
-    
-    if(LUB_HEAP_OK == status)
-    {
-        /* update the high tide markers */
-        if(  (this->stats.alloc_bytes + this->stats.alloc_overhead) 
-           > (this->stats.alloc_hightide_bytes + this->stats.alloc_hightide_overhead))
-        {
-            this->stats.alloc_hightide_blocks   = this->stats.alloc_blocks;
-            this->stats.alloc_hightide_bytes    = this->stats.alloc_bytes;
-            this->stats.alloc_hightide_overhead = this->stats.alloc_overhead;
-            this->stats.free_hightide_blocks    = this->stats.free_blocks;
-            this->stats.free_hightide_bytes     = this->stats.free_bytes;
-            this->stats.free_hightide_overhead  = this->stats.free_overhead;
-        }
-    }
-    else
-    {
-        /* this call enables a debugger to catch failures */
-        lub_heap_stop_here(status,*ptr,requested_size);
-    }
-    if((0 == requested_size) && (NULL == *ptr) && (LUB_HEAP_OK == status))
-    {
-        /* make sure that client doesn't use this (non-existant memory) */
-        *ptr = LUB_HEAP_ZERO_ALLOC;
-    }
-    return status;
+	/* opportunity for leak detection to do it's stuff */
+	lub_heap_pre_realloc(this, ptr, &size);
+
+	if (this->cache && (LUB_HEAP_ALIGN_NATIVE == alignment)) {
+		/* try and get the memory from the cache */
+		status = lub_heap_cache_realloc(this, ptr, size);
+	} else {
+		/* get the memory directly from the dynamic heap */
+		status = lub_heap_raw_realloc(this, ptr, size, alignment);
+	}
+
+	/* opportunity for leak detection to do it's stuff */
+	lub_heap_post_realloc(this, ptr);
+
+	if (LUB_HEAP_OK == status) {
+		/* update the high tide markers */
+		if ((this->stats.alloc_bytes + this->stats.alloc_overhead)
+		    > (this->stats.alloc_hightide_bytes +
+		       this->stats.alloc_hightide_overhead)) {
+			this->stats.alloc_hightide_blocks =
+			    this->stats.alloc_blocks;
+			this->stats.alloc_hightide_bytes =
+			    this->stats.alloc_bytes;
+			this->stats.alloc_hightide_overhead =
+			    this->stats.alloc_overhead;
+			this->stats.free_hightide_blocks =
+			    this->stats.free_blocks;
+			this->stats.free_hightide_bytes =
+			    this->stats.free_bytes;
+			this->stats.free_hightide_overhead =
+			    this->stats.free_overhead;
+		}
+	} else {
+		/* this call enables a debugger to catch failures */
+		lub_heap_stop_here(status, *ptr, requested_size);
+	}
+	if ((0 == requested_size) && (NULL == *ptr) && (LUB_HEAP_OK == status)) {
+		/* make sure that client doesn't use this (non-existant memory) */
+		*ptr = LUB_HEAP_ZERO_ALLOC;
+	}
+	return status;
 }
+
 /*--------------------------------------------------------- */

+ 43 - 43
lub/heap/heap_scan_stack.c

@@ -7,10 +7,9 @@
 #include "private.h"
 #include "node.h"
 /*--------------------------------------------------------- */
-void
-lub_heap_scan_stack(void)
+void lub_heap_scan_stack(void)
 {
-    char *start,*end;
+	char *start, *end;
 #define FRAME_ADDRESS(n)                            \
     {                                               \
         char *fn = __builtin_return_address(n);     \
@@ -21,46 +20,47 @@ lub_heap_scan_stack(void)
         end = __builtin_frame_address(n);           \
     }                                               \
 
-    start = end = __builtin_frame_address(0);
+	start = end = __builtin_frame_address(0);
 
-    do {
-        FRAME_ADDRESS(0);
-        FRAME_ADDRESS(1);
-        FRAME_ADDRESS(2);
-        FRAME_ADDRESS(3);
-        FRAME_ADDRESS(4);
-        FRAME_ADDRESS(5);
-        FRAME_ADDRESS(6);
-        FRAME_ADDRESS(7);
-        FRAME_ADDRESS(8);
-        FRAME_ADDRESS(9);
-        FRAME_ADDRESS(10);
-        FRAME_ADDRESS(11);
-        FRAME_ADDRESS(12);
-        FRAME_ADDRESS(13);
-        FRAME_ADDRESS(14);
-        FRAME_ADDRESS(15);
-        FRAME_ADDRESS(16);
-        FRAME_ADDRESS(17);
-        FRAME_ADDRESS(18);
-        FRAME_ADDRESS(19);
-        FRAME_ADDRESS(20);
-        FRAME_ADDRESS(21);
-        FRAME_ADDRESS(22);
-        FRAME_ADDRESS(23);
-        FRAME_ADDRESS(24);
-        FRAME_ADDRESS(25);
-        FRAME_ADDRESS(26);
-        FRAME_ADDRESS(27);
-        FRAME_ADDRESS(28);
-        FRAME_ADDRESS(29);
-        FRAME_ADDRESS(30);
-        FRAME_ADDRESS(31);
-    } while(0);
-    /* now scan the memory */
-    lub_heap_scan_memory(start,end-start);
+	do {
+		FRAME_ADDRESS(0);
+		FRAME_ADDRESS(1);
+		FRAME_ADDRESS(2);
+		FRAME_ADDRESS(3);
+		FRAME_ADDRESS(4);
+		FRAME_ADDRESS(5);
+		FRAME_ADDRESS(6);
+		FRAME_ADDRESS(7);
+		FRAME_ADDRESS(8);
+		FRAME_ADDRESS(9);
+		FRAME_ADDRESS(10);
+		FRAME_ADDRESS(11);
+		FRAME_ADDRESS(12);
+		FRAME_ADDRESS(13);
+		FRAME_ADDRESS(14);
+		FRAME_ADDRESS(15);
+		FRAME_ADDRESS(16);
+		FRAME_ADDRESS(17);
+		FRAME_ADDRESS(18);
+		FRAME_ADDRESS(19);
+		FRAME_ADDRESS(20);
+		FRAME_ADDRESS(21);
+		FRAME_ADDRESS(22);
+		FRAME_ADDRESS(23);
+		FRAME_ADDRESS(24);
+		FRAME_ADDRESS(25);
+		FRAME_ADDRESS(26);
+		FRAME_ADDRESS(27);
+		FRAME_ADDRESS(28);
+		FRAME_ADDRESS(29);
+		FRAME_ADDRESS(30);
+		FRAME_ADDRESS(31);
+	} while (0);
+	/* now scan the memory */
+	lub_heap_scan_memory(start, end - start);
 }
+
 /*--------------------------------------------------------- */
-#else /* not __GNUC__ */
-    #warning "Generic stack scanning not implemented for non-GCC compiler..."
-#endif /* not __GNUC__*/
+#else				/* not __GNUC__ */
+#warning "Generic stack scanning not implemented for non-GCC compiler..."
+#endif				/* not __GNUC__ */

+ 116 - 127
lub/heap/heap_show.c

@@ -6,141 +6,130 @@
 
 /*--------------------------------------------------------- */
 static void
-process_free_block(void     *block,
-                   unsigned  index,
-                   size_t    size,
-                   void     *arg)
+process_free_block(void *block, unsigned index, size_t size, void *arg)
 {
-    /* dump the details for this block */
-    printf(" %8p[%"SIZE_FMT"]",block,size);
+	/* dump the details for this block */
+	printf(" %8p[%" SIZE_FMT "]", block, size);
 }
+
 /*--------------------------------------------------------- */
 static void
-process_segment(void     *segment,
-                unsigned  index,
-                size_t    size,
-                void     *arg)
+process_segment(void *segment, unsigned index, size_t size, void *arg)
 {
-    /* dump the details for this segment */
-    printf(" %8p[%"SIZE_FMT"]",segment,size);
+	/* dump the details for this segment */
+	printf(" %8p[%" SIZE_FMT "]", segment, size);
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_show(lub_heap_t *this,
-              bool_t      verbose)
+void lub_heap_show(lub_heap_t * this, bool_t verbose)
 {
-    static char     *state_names[] = {"DISABLED","ENABLED"};
-    lub_heap_stats_t stats;
+	static char *state_names[] = { "DISABLED", "ENABLED" };
+	lub_heap_stats_t stats;
+
+	if (BOOL_FALSE == lub_heap_check_memory(this)) {
+		printf("*** HEAP CORRUPTED!!! ***\n");
+	}
+	if (BOOL_TRUE == verbose) {
+		printf("HEAP:\n" "  %8p\n", (void *)this);
+		printf("\nHEAP SEGMENTS:\n");
+		lub_heap_foreach_segment(this, process_segment, NULL);
+		printf("\n");
+		if (this->cache) {
+			printf("\nCACHE:\n");
+			lub_heap_cache_show(this->cache);
+		}
+		/* dump each free block's details */
+		printf("\nFREE BLOCKS:\n");
+		lub_heap_foreach_free_block(this, process_free_block, NULL);
+		printf("\n");
+		printf("\nSUMMARY:\n");
+	}
+	{
+		/*lint -esym(644,cache_stats) may not have been initialized */
+		lub_heap_stats_t cache_stats;
+		/* get the stats */
+		lub_heap__get_stats(this, &stats);
+
+		/* dump the statistics for this heap */
+		printf
+		    ("  status      bytes    blocks  avg block  max block   overhead\n");
+		printf
+		    ("  ------ ---------- --------- ---------- ---------- ----------\n");
 
-    if(BOOL_FALSE == lub_heap_check_memory(this))
-    {
-        printf("*** HEAP CORRUPTED!!! ***\n");
-    }
-    if(BOOL_TRUE == verbose)
-    {
-        printf("HEAP:\n"
-               "  %8p\n",(void*)this);
-        printf("\nHEAP SEGMENTS:\n");
-        lub_heap_foreach_segment(this,process_segment,NULL);
-        printf("\n");
-        if(this->cache)
-        {
-            printf("\nCACHE:\n");
-            lub_heap_cache_show(this->cache);
-        }
-        /* dump each free block's details */
-        printf("\nFREE BLOCKS:\n");
-        lub_heap_foreach_free_block(this,process_free_block,NULL);
-        printf("\n");
-        printf("\nSUMMARY:\n");
-    }
-    {
-        /*lint -esym(644,cache_stats) may not have been initialized */
-        lub_heap_stats_t cache_stats;
-        /* get the stats */
-        lub_heap__get_stats(this,&stats);
-    
-        /* dump the statistics for this heap */
-        printf("  status      bytes    blocks  avg block  max block   overhead\n");
-        printf("  ------ ---------- --------- ---------- ---------- ----------\n");
-        
-        printf("current\n");
-        printf("    free %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT"\n",
-               stats.free_bytes,
-               stats.free_blocks,
-               stats.free_blocks ? stats.free_bytes /stats.free_blocks : 0,
-               lub_heap__get_max_free(this),
-               stats.free_overhead + stats.segs_overhead);
-        printf("   alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
-               stats.alloc_bytes,
-               stats.alloc_blocks,
-               stats.alloc_blocks ? stats.alloc_bytes / stats.alloc_blocks : 0,
-               "-",
-               stats.alloc_overhead);
-        printf("  static %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
-               stats.static_bytes,
-               stats.static_blocks,
-               stats.static_blocks ? stats.static_bytes / stats.static_blocks : 0,
-               "-",
-               stats.static_overhead);
-        if(this->cache)
-        {
-            printf(" (cache)\n");
-            lub_heap_cache__get_stats(this->cache,&cache_stats);
-            printf("    free %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT"\n",
-                   cache_stats.free_bytes,
-                   cache_stats.free_blocks,
-                   cache_stats.free_blocks ? cache_stats.free_bytes /cache_stats.free_blocks : 0,
-                   lub_heap_cache__get_max_free(this->cache),
-                   cache_stats.free_overhead);
-            printf("   alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
-                   cache_stats.alloc_bytes,
-                   cache_stats.alloc_blocks,
-                   cache_stats.alloc_blocks ? cache_stats.alloc_bytes / cache_stats.alloc_blocks : 0,
-                   "-",
-                   cache_stats.alloc_overhead);
-        }
+		printf("current\n");
+		printf("    free %10" SIZE_FMT " %9" SIZE_FMT " %10" SIZE_FMT
+		       " %10" SIZE_FMT " %10" SIZE_FMT "\n", stats.free_bytes,
+		       stats.free_blocks,
+		       stats.free_blocks ? stats.free_bytes /
+		       stats.free_blocks : 0, lub_heap__get_max_free(this),
+		       stats.free_overhead + stats.segs_overhead);
+		printf("   alloc %10" SIZE_FMT " %9" SIZE_FMT " %10" SIZE_FMT
+		       " %10s %10" SIZE_FMT "\n", stats.alloc_bytes,
+		       stats.alloc_blocks,
+		       stats.alloc_blocks ? stats.alloc_bytes /
+		       stats.alloc_blocks : 0, "-", stats.alloc_overhead);
+		printf("  static %10" SIZE_FMT " %9" SIZE_FMT " %10" SIZE_FMT
+		       " %10s %10" SIZE_FMT "\n", stats.static_bytes,
+		       stats.static_blocks,
+		       stats.static_blocks ? stats.static_bytes /
+		       stats.static_blocks : 0, "-", stats.static_overhead);
+		if (this->cache) {
+			printf(" (cache)\n");
+			lub_heap_cache__get_stats(this->cache, &cache_stats);
+			printf("    free %10" SIZE_FMT " %9" SIZE_FMT " %10"
+			       SIZE_FMT " %10" SIZE_FMT " %10" SIZE_FMT "\n",
+			       cache_stats.free_bytes, cache_stats.free_blocks,
+			       cache_stats.free_blocks ? cache_stats.
+			       free_bytes / cache_stats.free_blocks : 0,
+			       lub_heap_cache__get_max_free(this->cache),
+			       cache_stats.free_overhead);
+			printf("   alloc %10" SIZE_FMT " %9" SIZE_FMT " %10"
+			       SIZE_FMT " %10s %10" SIZE_FMT "\n",
+			       cache_stats.alloc_bytes,
+			       cache_stats.alloc_blocks,
+			       cache_stats.alloc_blocks ? cache_stats.
+			       alloc_bytes / cache_stats.alloc_blocks : 0, "-",
+			       cache_stats.alloc_overhead);
+		}
 
-        printf("cumulative\n");
-        printf("   alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10s\n",
-               stats.alloc_total_bytes,
-               stats.alloc_total_blocks,
-               stats.alloc_total_blocks ? stats.alloc_total_bytes / stats.alloc_total_blocks : 0,
-               "-",
-               "-");
-        if(this->cache)
-        {
-            printf(" (cache)\n");
-            printf("   alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10s\n",
-                   cache_stats.alloc_total_bytes,
-                   cache_stats.alloc_total_blocks,
-                   cache_stats.alloc_total_blocks ? cache_stats.alloc_total_bytes / cache_stats.alloc_total_blocks : 0,
-                   "-",
-                   "-");
-        }
-        printf("high-tide\n");
-        printf("    free %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
-               stats.free_hightide_bytes,
-               stats.free_hightide_blocks,
-               stats.free_hightide_blocks ? stats.free_hightide_bytes /stats.free_hightide_blocks : 0,
-               "-",
-               stats.free_hightide_overhead);
-        printf("   alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
-               stats.alloc_hightide_bytes,
-               stats.alloc_hightide_blocks,
-               stats.alloc_hightide_blocks ? stats.alloc_hightide_bytes / stats.alloc_hightide_blocks : 0,
-               "-",
-               stats.alloc_hightide_overhead);
-    }
-    if(BOOL_TRUE == verbose)
-    {
-        printf("\nSYSTEM:\n"
-               "  tainting(%s), full checking(%s), leak detection(%s),\n"
-               "  native alignment of %d bytes\n", 
-               state_names[lub_heap_is_tainting()],
-               state_names[lub_heap_is_checking()],
-               state_names[lub_heap__get_framecount() ? 0 : 1],
-               LUB_HEAP_ALIGN_NATIVE);
-    }
-    printf("\n");
+		printf("cumulative\n");
+		printf("   alloc %10" SIZE_FMT " %9" SIZE_FMT " %10" SIZE_FMT
+		       " %10s %10s\n", stats.alloc_total_bytes,
+		       stats.alloc_total_blocks,
+		       stats.alloc_total_blocks ? stats.alloc_total_bytes /
+		       stats.alloc_total_blocks : 0, "-", "-");
+		if (this->cache) {
+			printf(" (cache)\n");
+			printf("   alloc %10" SIZE_FMT " %9" SIZE_FMT " %10"
+			       SIZE_FMT " %10s %10s\n",
+			       cache_stats.alloc_total_bytes,
+			       cache_stats.alloc_total_blocks,
+			       cache_stats.alloc_total_blocks ? cache_stats.
+			       alloc_total_bytes /
+			       cache_stats.alloc_total_blocks : 0, "-", "-");
+		}
+		printf("high-tide\n");
+		printf("    free %10" SIZE_FMT " %9" SIZE_FMT " %10" SIZE_FMT
+		       " %10s %10" SIZE_FMT "\n", stats.free_hightide_bytes,
+		       stats.free_hightide_blocks,
+		       stats.free_hightide_blocks ? stats.free_hightide_bytes /
+		       stats.free_hightide_blocks : 0, "-",
+		       stats.free_hightide_overhead);
+		printf("   alloc %10" SIZE_FMT " %9" SIZE_FMT " %10" SIZE_FMT
+		       " %10s %10" SIZE_FMT "\n", stats.alloc_hightide_bytes,
+		       stats.alloc_hightide_blocks,
+		       stats.alloc_hightide_blocks ? stats.
+		       alloc_hightide_bytes / stats.alloc_hightide_blocks : 0,
+		       "-", stats.alloc_hightide_overhead);
+	}
+	if (BOOL_TRUE == verbose) {
+		printf("\nSYSTEM:\n"
+		       "  tainting(%s), full checking(%s), leak detection(%s),\n"
+		       "  native alignment of %d bytes\n",
+		       state_names[lub_heap_is_tainting()],
+		       state_names[lub_heap_is_checking()],
+		       state_names[lub_heap__get_framecount()? 0 : 1],
+		       LUB_HEAP_ALIGN_NATIVE);
+	}
+	printf("\n");
 }

+ 63 - 64
lub/heap/heap_slice_from_bottom.c

@@ -2,75 +2,74 @@
 
 #include "private.h"
 /*--------------------------------------------------------- */
-void *
-lub_heap_slice_from_bottom(lub_heap_t             *this,
-                           lub_heap_free_block_t **ptr_block,
-                           words_t                *words,
-                           bool_t                  seg_start)
+void *lub_heap_slice_from_bottom(lub_heap_t * this,
+				 lub_heap_free_block_t ** ptr_block,
+				 words_t * words, bool_t seg_start)
 {
-    void                  *result = NULL;
-    lub_heap_free_block_t *block  = *ptr_block;
+	void *result = NULL;
+	lub_heap_free_block_t *block = *ptr_block;
 
-    /* check this is a free block */
-    assert(1 == block->tag.free);
-    
-    if(BOOL_TRUE == lub_heap_block_check((lub_heap_block_t*)block)
-        && (*words <= block->tag.words))
-    {
-        words_t new_words = (block->tag.words - *words);
-        
-        /* get the tag at the end of the current free block */
-        lub_heap_tag_t *tail = lub_heap_block__get_tail((lub_heap_block_t*)block);
+	/* check this is a free block */
+	assert(1 == block->tag.free);
 
-        /* 
-         * Is there sufficient memory to perform the task? 
-         * we must leave enough space for the free block to work
-         */
-        if(new_words > (sizeof(lub_heap_free_block_t) >> 2))
-        {
-            lub_heap_free_block_t *new_block;
-        
-            /* adjust the free block size */
-            tail->words -= *words;
-        
-            /* create a new free block header */
-            new_block = (lub_heap_free_block_t *)(&block->tag + *words);
-            new_block->tag.segment = seg_start;
-            new_block->tag.free    = 1; /* this is a free block */
-            new_block->tag.words   = tail->words;
+	if (BOOL_TRUE == lub_heap_block_check((lub_heap_block_t *) block)
+	    && (*words <= block->tag.words)) {
+		words_t new_words = (block->tag.words - *words);
 
-            /* update the stats */
-            this->stats.free_bytes -= (*words << 2);
+		/* get the tag at the end of the current free block */
+		lub_heap_tag_t *tail =
+		    lub_heap_block__get_tail((lub_heap_block_t *) block);
 
-            /* set up the tree node */
-            lub_bintree_node_init(&new_block->bt_node);
-        
-            result     = block;
-            *ptr_block = new_block;
-        }
-        else
-        {
-            /* 
-             * there is only just enough space for the request
-             * so we also throw in the memory used for the tags
-             */
-            --this->stats.free_blocks;
-            this->stats.free_bytes    -= (block->tag.words << 2);
-            this->stats.free_bytes    += sizeof(lub_heap_alloc_block_t);
-            this->stats.free_overhead -= sizeof(lub_heap_alloc_block_t);
-            /* update the client's word count */
-            *words = block->tag.words; 
+		/* 
+		 * Is there sufficient memory to perform the task? 
+		 * we must leave enough space for the free block to work
+		 */
+		if (new_words > (sizeof(lub_heap_free_block_t) >> 2)) {
+			lub_heap_free_block_t *new_block;
 
-            /* there is no free block left!!! */
-            result     = block;
-            *ptr_block = NULL;
-        }
-        if(NULL != result)
-        {
-            /* taint the memory */
-            lub_heap_taint_memory(result,LUB_HEAP_TAINT_ALLOC,(*words << 2));
-        }
-    }
-    return result;
+			/* adjust the free block size */
+			tail->words -= *words;
+
+			/* create a new free block header */
+			new_block =
+			    (lub_heap_free_block_t *) (&block->tag + *words);
+			new_block->tag.segment = seg_start;
+			new_block->tag.free = 1;	/* this is a free block */
+			new_block->tag.words = tail->words;
+
+			/* update the stats */
+			this->stats.free_bytes -= (*words << 2);
+
+			/* set up the tree node */
+			lub_bintree_node_init(&new_block->bt_node);
+
+			result = block;
+			*ptr_block = new_block;
+		} else {
+			/* 
+			 * there is only just enough space for the request
+			 * so we also throw in the memory used for the tags
+			 */
+			--this->stats.free_blocks;
+			this->stats.free_bytes -= (block->tag.words << 2);
+			this->stats.free_bytes +=
+			    sizeof(lub_heap_alloc_block_t);
+			this->stats.free_overhead -=
+			    sizeof(lub_heap_alloc_block_t);
+			/* update the client's word count */
+			*words = block->tag.words;
+
+			/* there is no free block left!!! */
+			result = block;
+			*ptr_block = NULL;
+		}
+		if (NULL != result) {
+			/* taint the memory */
+			lub_heap_taint_memory(result, LUB_HEAP_TAINT_ALLOC,
+					      (*words << 2));
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 57 - 58
lub/heap/heap_slice_from_top.c

@@ -3,67 +3,66 @@
 #include "private.h"
 
 /*--------------------------------------------------------- */
-void *
-lub_heap_slice_from_top(lub_heap_t             *this,
-                        lub_heap_free_block_t **ptr_block,
-                        words_t                *words,
-                        bool_t                  seg_end)
+void *lub_heap_slice_from_top(lub_heap_t * this,
+			      lub_heap_free_block_t ** ptr_block,
+			      words_t * words, bool_t seg_end)
 {
-    void                  *result = NULL;
-    lub_heap_free_block_t *block  = *ptr_block;
-    
-    /* check this is a free block */
-    assert(1 == block->tag.free);
+	void *result = NULL;
+	lub_heap_free_block_t *block = *ptr_block;
 
-    if(BOOL_TRUE == lub_heap_block_check((lub_heap_block_t*)block)
-       && (*words <= block->tag.words))
-    {
-        words_t         new_words = (block->tag.words - *words);
-        lub_heap_tag_t *tail;
-    
-        /* Is there sufficient memory to perform the task? */
-        if(new_words > (sizeof(lub_heap_free_block_t) >> 2))
-        {
-            /* update the free block */
-            block->tag.words -= *words;
-        
-            /* get the new tail tag */
-            tail = lub_heap_block__get_tail((lub_heap_block_t*)block);
-        
-            /* set up the tag */
-            tail->segment = seg_end;
-            tail->free    = 1;
-            tail->words   = block->tag.words;
+	/* check this is a free block */
+	assert(1 == block->tag.free);
 
-            /* update the stats */
-            this->stats.free_bytes -= (*words << 2);
-        
-            result = &tail[1];
-        }
-        else
-        {
-            /* 
-             * there is only just enough space for the request
-             * so we also throw in the memory used for the tags
-             */
-            --this->stats.free_blocks;
-            this->stats.free_bytes    -= (block->tag.words << 2);
-            this->stats.free_bytes    += sizeof(lub_heap_alloc_block_t);
-            this->stats.free_overhead -= sizeof(lub_heap_alloc_block_t);
-            
-            /* update the client's word count */
-            *words = block->tag.words; 
+	if (BOOL_TRUE == lub_heap_block_check((lub_heap_block_t *) block)
+	    && (*words <= block->tag.words)) {
+		words_t new_words = (block->tag.words - *words);
+		lub_heap_tag_t *tail;
 
-            /* there is no free block left!!! */
-            result     = block;
-            *ptr_block = NULL;
-        }
-        if(NULL != result)
-        {
-            /* taint the memory */
-            lub_heap_taint_memory(result,LUB_HEAP_TAINT_ALLOC,(*words << 2));
-        }
-    }
-    return result;
+		/* Is there sufficient memory to perform the task? */
+		if (new_words > (sizeof(lub_heap_free_block_t) >> 2)) {
+			/* update the free block */
+			block->tag.words -= *words;
+
+			/* get the new tail tag */
+			tail =
+			    lub_heap_block__get_tail((lub_heap_block_t *)
+						     block);
+
+			/* set up the tag */
+			tail->segment = seg_end;
+			tail->free = 1;
+			tail->words = block->tag.words;
+
+			/* update the stats */
+			this->stats.free_bytes -= (*words << 2);
+
+			result = &tail[1];
+		} else {
+			/* 
+			 * there is only just enough space for the request
+			 * so we also throw in the memory used for the tags
+			 */
+			--this->stats.free_blocks;
+			this->stats.free_bytes -= (block->tag.words << 2);
+			this->stats.free_bytes +=
+			    sizeof(lub_heap_alloc_block_t);
+			this->stats.free_overhead -=
+			    sizeof(lub_heap_alloc_block_t);
+
+			/* update the client's word count */
+			*words = block->tag.words;
+
+			/* there is no free block left!!! */
+			result = block;
+			*ptr_block = NULL;
+		}
+		if (NULL != result) {
+			/* taint the memory */
+			lub_heap_taint_memory(result, LUB_HEAP_TAINT_ALLOC,
+					      (*words << 2));
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 85 - 98
lub/heap/heap_static_alloc.c

@@ -1,111 +1,98 @@
 #include "cache.h"
 
 /*--------------------------------------------------------- */
-size_t
-lub_heap__get_block_overhead(lub_heap_t *this,
-                             const void *block)
+size_t lub_heap__get_block_overhead(lub_heap_t * this, const void *block)
 {
-    size_t                   overhead;
-    lub_heap_cache_bucket_t *bucket = 0;
-    if(this->cache)
-    {
-        bucket = lub_heap_cache_find_bucket_from_address(this->cache,block);
-    }
-    if(bucket)
-    {
-        overhead = lub_heap_cache_bucket__get_block_overhead(bucket,block);
-    }
-    else
-    {
-        overhead = sizeof(lub_heap_alloc_block_t);
-    }
-    return overhead;
+	size_t overhead;
+	lub_heap_cache_bucket_t *bucket = 0;
+	if (this->cache) {
+		bucket =
+		    lub_heap_cache_find_bucket_from_address(this->cache, block);
+	}
+	if (bucket) {
+		overhead =
+		    lub_heap_cache_bucket__get_block_overhead(bucket, block);
+	} else {
+		overhead = sizeof(lub_heap_alloc_block_t);
+	}
+	return overhead;
 }
+
 /*--------------------------------------------------------- */
-size_t
-lub_heap__get_block_size(lub_heap_t *this,
-                         const void *block)
+size_t lub_heap__get_block_size(lub_heap_t * this, const void *block)
 {
-    size_t                   bytes = 0;
-    lub_heap_cache_bucket_t *bucket = 0;
-    if(this->cache)
-    {
-        bucket = lub_heap_cache_find_bucket_from_address(this->cache,block);
-    }
-    if(bucket)
-    {
-        bytes = lub_heap_cache_bucket__get_block_size(bucket,block);
-    }
-    else
-    {
-        const lub_heap_tag_t *header = block;
-        --header;
-        bytes = (header->words << 2);
-        bytes -= sizeof(lub_heap_alloc_block_t);
-    }
-    return bytes;
+	size_t bytes = 0;
+	lub_heap_cache_bucket_t *bucket = 0;
+	if (this->cache) {
+		bucket =
+		    lub_heap_cache_find_bucket_from_address(this->cache, block);
+	}
+	if (bucket) {
+		bytes = lub_heap_cache_bucket__get_block_size(bucket, block);
+	} else {
+		const lub_heap_tag_t *header = block;
+		--header;
+		bytes = (header->words << 2);
+		bytes -= sizeof(lub_heap_alloc_block_t);
+	}
+	return bytes;
 }
+
 /*--------------------------------------------------------- */
-void *
-lub_heap_static_alloc(lub_heap_t *this,
-                      size_t      requested_size)
+void *lub_heap_static_alloc(lub_heap_t * this, size_t requested_size)
 {
-    void                  *result = NULL;
-    lub_heap_free_block_t *free_block;
-    /* round up to native alignment */
-    words_t                words;
-    lub_heap_key_t         key;
-    size_t                 size = requested_size;
-    
-    size  = (size + (LUB_HEAP_ALIGN_NATIVE-1))/LUB_HEAP_ALIGN_NATIVE; 
-    size *= LUB_HEAP_ALIGN_NATIVE; 
-    words = (size >> 2);
-    
-    /* initialise the search key */
-    key.words   = words;
-    key.block   = 0;
-    
-    /* search for the start of a segment which is large enough for the request */
-    while( (free_block = lub_bintree_findnext(&this->free_tree,&key)) )
-    {
-        lub_heap_tag_t *tail = lub_heap_block__get_tail((lub_heap_block_t*)free_block);
-        
-        /* is this free block at the start of a segment? */
-        if(1 == tail->segment)
-        {
-            /* yes this is the end of a segment */
-            break;
-        }
-        /* update the search key */
-        lub_heap_block_getkey(free_block,(lub_bintree_key_t*)&key);
-    }
-    if(NULL != free_block)
-    {
-        /* remove this block from the free tree */
-        lub_bintree_remove(&this->free_tree,free_block);
-        
-        /* 
-         * get some memory from the bottom of this free block 
-         */
-        result = lub_heap_slice_from_top(this,
-                                         &free_block,
-                                         &words,
-                                         BOOL_TRUE);
+	void *result = NULL;
+	lub_heap_free_block_t *free_block;
+	/* round up to native alignment */
+	words_t words;
+	lub_heap_key_t key;
+	size_t size = requested_size;
+
+	size = (size + (LUB_HEAP_ALIGN_NATIVE - 1)) / LUB_HEAP_ALIGN_NATIVE;
+	size *= LUB_HEAP_ALIGN_NATIVE;
+	words = (size >> 2);
+
+	/* initialise the search key */
+	key.words = words;
+	key.block = 0;
 
-        if(NULL != free_block)
-        {
-            /* put the free block back into the tree */
-            lub_bintree_insert(&this->free_tree,free_block);
-        }
-        if(NULL != result)
-        {
-            size_t allocated_size = (words << 2);
-            /* update the stats */
-            ++this->stats.static_blocks;
-            this->stats.static_bytes    += size;
-            this->stats.static_overhead += (allocated_size - size); 
-        }
-    }
-    return result;
+	/* search for the start of a segment which is large enough for the request */
+	while ((free_block = lub_bintree_findnext(&this->free_tree, &key))) {
+		lub_heap_tag_t *tail =
+		    lub_heap_block__get_tail((lub_heap_block_t *) free_block);
+
+		/* is this free block at the start of a segment? */
+		if (1 == tail->segment) {
+			/* yes this is the end of a segment */
+			break;
+		}
+		/* update the search key */
+		lub_heap_block_getkey(free_block, (lub_bintree_key_t *) & key);
+	}
+	if (NULL != free_block) {
+		/* remove this block from the free tree */
+		lub_bintree_remove(&this->free_tree, free_block);
+
+		/* 
+		 * get some memory from the bottom of this free block 
+		 */
+		result = lub_heap_slice_from_top(this,
+						 &free_block,
+						 &words, BOOL_TRUE);
+
+		if (NULL != free_block) {
+			/* put the free block back into the tree */
+			lub_bintree_insert(&this->free_tree, free_block);
+		}
+		if (NULL != result) {
+			size_t allocated_size = (words << 2);
+			/* update the stats */
+			++this->stats.static_blocks;
+			this->stats.static_bytes += size;
+			this->stats.static_overhead += (allocated_size - size);
+		}
+	}
+	return result;
 }
+
 /*--------------------------------------------------------- */

+ 44 - 46
lub/heap/heap_stop_here.c

@@ -9,55 +9,53 @@
  */
 int lub_heap_verbose_errors = 0;
 
-static const char *status_names[] =
-{
-    "LUB_HEAP_OK",
-    "LUB_HEAP_FAILED",
-    "LUB_HEAP_DOUBLE_FREE",
-    "LUB_HEAP_CORRUPTED",
-    "LUB_HEAP_INVALID_POINTER"
+static const char *status_names[] = {
+	"LUB_HEAP_OK",
+	"LUB_HEAP_FAILED",
+	"LUB_HEAP_DOUBLE_FREE",
+	"LUB_HEAP_CORRUPTED",
+	"LUB_HEAP_INVALID_POINTER"
 };
+
 /*--------------------------------------------------------- */
 void
-lub_heap_stop_here(lub_heap_status_t status,
-                   char             *old_ptr,
-                   size_t            new_size)
+lub_heap_stop_here(lub_heap_status_t status, char *old_ptr, size_t new_size)
 {
-    /* This is provided as a debug aid */
-    status   = status;
-    old_ptr  = old_ptr;
-    new_size = new_size;
-    
-    if(lub_heap_verbose_errors)
-    {
-        switch(status)
-        {
-            case LUB_HEAP_OK:
-            case LUB_HEAP_FAILED:
-            {
-                /* this is normal */
-                break;
-            }
-            case LUB_HEAP_DOUBLE_FREE:
-            case LUB_HEAP_CORRUPTED:
-            case LUB_HEAP_INVALID_POINTER:
-            {
-                /* obtain the backtrace of the stack */
-                stackframe_t frame;
-                long         address;
-                int          i;
-                lub_heap__get_stackframe(&frame,MAX_BACKTRACE);
-                /* and output it */
-                printf("lub_heap_stop_here(%s,%p,%"SIZE_FMT")\n",status_names[status],old_ptr,new_size);
-                for(i = 0;
-                    (address = (long)frame.backtrace[i]);
-                    i++)
-                {
-                    lub_heap_symShow(address);
-                    printf("\n");
-                }
-            }
-        }
-    }
+	/* This is provided as a debug aid */
+	status = status;
+	old_ptr = old_ptr;
+	new_size = new_size;
+
+	if (lub_heap_verbose_errors) {
+		switch (status) {
+		case LUB_HEAP_OK:
+		case LUB_HEAP_FAILED:
+			{
+				/* this is normal */
+				break;
+			}
+		case LUB_HEAP_DOUBLE_FREE:
+		case LUB_HEAP_CORRUPTED:
+		case LUB_HEAP_INVALID_POINTER:
+			{
+				/* obtain the backtrace of the stack */
+				stackframe_t frame;
+				long address;
+				int i;
+				lub_heap__get_stackframe(&frame, MAX_BACKTRACE);
+				/* and output it */
+				printf("lub_heap_stop_here(%s,%p,%" SIZE_FMT
+				       ")\n", status_names[status], old_ptr,
+				       new_size);
+				for (i = 0;
+				     (address = (long)frame.backtrace[i]);
+				     i++) {
+					lub_heap_symShow(address);
+					printf("\n");
+				}
+			}
+		}
+	}
 }
+
 /*--------------------------------------------------------- */

+ 39 - 44
lub/heap/heap_tainted_memory.c

@@ -7,57 +7,52 @@
 static bool_t tainted = BOOL_FALSE;
 
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_taint(bool_t enable)
+bool_t lub_heap_taint(bool_t enable)
 {
-    bool_t result = tainted;
-    
-    tainted = enable;
-    
-    return result;
+	bool_t result = tainted;
+
+	tainted = enable;
+
+	return result;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_is_tainting(void)
+bool_t lub_heap_is_tainting(void)
 {
-    return tainted;
+	return tainted;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_taint_memory(char            *ptr,
-                      lub_heap_taint_t type,
-                      size_t           size)
+void lub_heap_taint_memory(char *ptr, lub_heap_taint_t type, size_t size)
 {
-    if(BOOL_TRUE == tainted)
-    {
+	if (BOOL_TRUE == tainted) {
 #ifdef __vxworks
-extern function_t taskDestroy; /* fiddle a reference to the function ...*/
-extern function_t taskSuspend; /* ...and the next function in the library */
-        /* 
-         * VxWorks taskDestroy() relies on being able to access
-         * free memory.
-         * It calls objFree() followed by objTerminate() !!!!
-         * So we have to perform a crufty hack to avoid this
-         * mistake blowing us out of the water...
-         */
-        if(LUB_HEAP_TAINT_FREE == type)
-        {
-            int i;
-            /* obtain the backtrace of the stack */
-            stackframe_t frame;
-            lub_heap__get_stackframe(&frame,10);
-            for(i = 0; i < 10; i++)
-            {
-                function_t *address = frame.backtrace[i];
-                if((address > &taskDestroy) && (address < &taskSuspend))
-                {
-                    return;
-                }
-            }
-        }
-#endif /* __vxworks */
-        /* taint the memory */
-        memset(ptr,type,size);
-    }
+		extern function_t taskDestroy;	/* fiddle a reference to the function ... */
+		extern function_t taskSuspend;	/* ...and the next function in the library */
+		/* 
+		 * VxWorks taskDestroy() relies on being able to access
+		 * free memory.
+		 * It calls objFree() followed by objTerminate() !!!!
+		 * So we have to perform a crufty hack to avoid this
+		 * mistake blowing us out of the water...
+		 */
+		if (LUB_HEAP_TAINT_FREE == type) {
+			int i;
+			/* obtain the backtrace of the stack */
+			stackframe_t frame;
+			lub_heap__get_stackframe(&frame, 10);
+			for (i = 0; i < 10; i++) {
+				function_t *address = frame.backtrace[i];
+				if ((address > &taskDestroy)
+				    && (address < &taskSuspend)) {
+					return;
+				}
+			}
+		}
+#endif				/* __vxworks */
+		/* taint the memory */
+		memset(ptr, type, size);
+	}
 }
+
 /*--------------------------------------------------------- */

+ 392 - 416
lub/heap/node.c

@@ -11,497 +11,473 @@
 
 int lub_heap_leak_verbose = 0;
 /*--------------------------------------------------------- */
-int
-lub_heap_node_compare(const void *clientnode,
-                      const void *clientkey)
+int lub_heap_node_compare(const void *clientnode, const void *clientkey)
 {
-    const lub_heap_node_t     * node = clientnode;
-    const lub_heap_node_key_t * key  = clientkey;
-    
-    return ((long)node - (long)key->node);
+	const lub_heap_node_t *node = clientnode;
+	const lub_heap_node_key_t *key = clientkey;
+
+	return ((long)node - (long)key->node);
 }
+
 /*--------------------------------------------------------- */
 /* we simply use the node address as the index */
-void
-lub_heap_node_getkey(const void        *clientnode,
-                    lub_bintree_key_t *key)
+void lub_heap_node_getkey(const void *clientnode, lub_bintree_key_t * key)
 {
-    lub_heap_node_key_t clientkey;
-    clientkey.node = clientnode;
-    memcpy(key,&clientkey,sizeof(lub_heap_node_key_t));
+	lub_heap_node_key_t clientkey;
+	clientkey.node = clientnode;
+	memcpy(key, &clientkey, sizeof(lub_heap_node_key_t));
 }
+
 /*--------------------------------------------------------- */
-static void
-lub_heap_node_meta_init(void)
+static void lub_heap_node_meta_init(void)
 {
-    static bool_t initialised = BOOL_FALSE;
-    
-    if(BOOL_FALSE == initialised)
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        initialised = BOOL_TRUE;
-        /* initialise the node tree */
-        lub_bintree_init(&leak->m_node_tree,
-                         offsetof(lub_heap_node_t,bt_node),
-                         lub_heap_node_compare,
-                         lub_heap_node_getkey);
-        /* initialise the clear node tree */
-        lub_bintree_init(&leak->m_clear_node_tree,
-                         offsetof(lub_heap_node_t,bt_node),
-                         lub_heap_node_compare,
-                         lub_heap_node_getkey);
-        lub_heap_leak_release(leak);
-    }
+	static bool_t initialised = BOOL_FALSE;
+
+	if (BOOL_FALSE == initialised) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		initialised = BOOL_TRUE;
+		/* initialise the node tree */
+		lub_bintree_init(&leak->m_node_tree,
+				 offsetof(lub_heap_node_t, bt_node),
+				 lub_heap_node_compare, lub_heap_node_getkey);
+		/* initialise the clear node tree */
+		lub_bintree_init(&leak->m_clear_node_tree,
+				 offsetof(lub_heap_node_t, bt_node),
+				 lub_heap_node_compare, lub_heap_node_getkey);
+		lub_heap_leak_release(leak);
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node_init(lub_heap_node_t    * this,
-                   lub_heap_context_t * context)
+void lub_heap_node_init(lub_heap_node_t * this, lub_heap_context_t * context)
 {
-    lub_heap_node_meta_init();
-    
-    /* initialise the control blocks */
-    lub_bintree_node_init(&this->bt_node);
-
-    lub_heap_node__set_context(this,context);
-    lub_heap_node__set_leaked(this,BOOL_FALSE);
-    lub_heap_node__set_partial(this,BOOL_FALSE);
-    lub_heap_node__set_next(this,NULL);
-    lub_heap_node__set_scanned(this,BOOL_FALSE);
-    this->prev = NULL;
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        /* add this to the node tree */
-        lub_bintree_insert(context ? &leak->m_node_tree : &leak->m_clear_node_tree,this);
-        lub_heap_leak_release(leak);
-    }
-    if(context)
-    {
-        /* add ourselves to this context */
-        lub_heap_context_insert_node(context,this);
-
-        /* maintain the leak statistics */
-        ++context->allocs;
-        context->alloc_bytes    += lub_heap_node__get_size(this);
-        context->alloc_overhead += lub_heap_node__get_overhead(this);
-    }
+	lub_heap_node_meta_init();
+
+	/* initialise the control blocks */
+	lub_bintree_node_init(&this->bt_node);
+
+	lub_heap_node__set_context(this, context);
+	lub_heap_node__set_leaked(this, BOOL_FALSE);
+	lub_heap_node__set_partial(this, BOOL_FALSE);
+	lub_heap_node__set_next(this, NULL);
+	lub_heap_node__set_scanned(this, BOOL_FALSE);
+	this->prev = NULL;
+	{
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		/* add this to the node tree */
+		lub_bintree_insert(context ? &leak->m_node_tree : &leak->
+				   m_clear_node_tree, this);
+		lub_heap_leak_release(leak);
+	}
+	if (context) {
+		/* add ourselves to this context */
+		lub_heap_context_insert_node(context, this);
+
+		/* maintain the leak statistics */
+		++context->allocs;
+		context->alloc_bytes += lub_heap_node__get_size(this);
+		context->alloc_overhead += lub_heap_node__get_overhead(this);
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node_fini(lub_heap_node_t * this)
+void lub_heap_node_fini(lub_heap_node_t * this)
 {
-    /* remove this node from this context */
-    lub_heap_node_clear(this,0);
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        /* remove this from the clear_node tree */
-        lub_bintree_remove(&leak->m_clear_node_tree,this);
-        lub_heap_leak_release(leak);
-    }
+	/* remove this node from this context */
+	lub_heap_node_clear(this, 0);
+	{
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		/* remove this from the clear_node tree */
+		lub_bintree_remove(&leak->m_clear_node_tree, this);
+		lub_heap_leak_release(leak);
+	}
 }
+
 /*--------------------------------------------------------- */
-size_t
-    lub_heap_node__get_instanceSize(void)
+size_t lub_heap_node__get_instanceSize(void)
 {
-    return sizeof(lub_heap_node_t);
+	return sizeof(lub_heap_node_t);
 }
+
 /*--------------------------------------------------------- */
-size_t
-    lub_heap_node__get_size(const lub_heap_node_t *this)
+size_t lub_heap_node__get_size(const lub_heap_node_t * this)
 {
-    size_t size = lub_heap__get_block_size(lub_heap_node__get_context(this)->heap,this);
-    size -= sizeof(lub_heap_node_t);
-    return size;
+	size_t size =
+	    lub_heap__get_block_size(lub_heap_node__get_context(this)->heap,
+				     this);
+	size -= sizeof(lub_heap_node_t);
+	return size;
 }
+
 /*--------------------------------------------------------- */
-size_t
-    lub_heap_node__get_overhead(const lub_heap_node_t *this)
+size_t lub_heap_node__get_overhead(const lub_heap_node_t * this)
 {
-    size_t overhead;
-    
-    overhead = lub_heap__get_block_overhead(lub_heap_node__get_context(this)->heap,
-                                            lub_heap_node__get_ptr(this));
-    
-    overhead += sizeof(lub_heap_node_t);
-    return overhead;
+	size_t overhead;
+
+	overhead =
+	    lub_heap__get_block_overhead(lub_heap_node__get_context(this)->heap,
+					 lub_heap_node__get_ptr(this));
+
+	overhead += sizeof(lub_heap_node_t);
+	return overhead;
 }
+
 /*--------------------------------------------------------- */
-char *
-lub_heap_node__get_ptr(const lub_heap_node_t *this)
+char *lub_heap_node__get_ptr(const lub_heap_node_t * this)
 {
-    return (char*)&this[1];
+	return (char *)&this[1];
 }
+
 /*--------------------------------------------------------- */
-bool_t 
-lub_heap_validate_pointer(lub_heap_t *this,
-                          char       *ptr)
+bool_t lub_heap_validate_pointer(lub_heap_t * this, char *ptr)
 {
-    bool_t result = BOOL_FALSE;
-    do
-    {
-        lub_heap_segment_t *seg;
-        if((0 < lub_heap_frame_count))
-        {
-            /*
-             * When leak detection is enabled we can perform detailed 
-             * validation of pointers.
-             */
-            lub_heap_node_t *node = lub_heap_node_from_start_of_block_ptr(ptr);
-            if(NULL != node)
-            {
-                lub_heap_context_t *context = lub_heap_node__get_context(node);
-                /* we've found a context so can give a definative answer */
-                if(this == context->heap)
-                {
-                    result = BOOL_TRUE;
-                }
-                break;
-            }
-        }
-        /* iterate around each of the segments belonging to this heap */
-        for(seg = &this->first_segment;
-            seg;
-            seg = seg->next)
-        {
-            char *start = (char*)&seg[1];
-            if(ptr >= start)
-            {
-                char *end = start + (seg->words << 2);
-                if(ptr < end)
-                {
-                    /* we've found a parent segment for this pointer */
-                    result = BOOL_TRUE;
-                    break;
-                }
-            }
-        }
-    } while(0);
-    ptr = 0; /* don't leave a pointer on the stack */
-    return result;
+	bool_t result = BOOL_FALSE;
+	do {
+		lub_heap_segment_t *seg;
+		if ((0 < lub_heap_frame_count)) {
+			/*
+			 * When leak detection is enabled we can perform detailed 
+			 * validation of pointers.
+			 */
+			lub_heap_node_t *node =
+			    lub_heap_node_from_start_of_block_ptr(ptr);
+			if (NULL != node) {
+				lub_heap_context_t *context =
+				    lub_heap_node__get_context(node);
+				/* we've found a context so can give a definative answer */
+				if (this == context->heap) {
+					result = BOOL_TRUE;
+				}
+				break;
+			}
+		}
+		/* iterate around each of the segments belonging to this heap */
+		for (seg = &this->first_segment; seg; seg = seg->next) {
+			char *start = (char *)&seg[1];
+			if (ptr >= start) {
+				char *end = start + (seg->words << 2);
+				if (ptr < end) {
+					/* we've found a parent segment for this pointer */
+					result = BOOL_TRUE;
+					break;
+				}
+			}
+		}
+	} while (0);
+	ptr = 0;		/* don't leave a pointer on the stack */
+	return result;
 }
+
 /*--------------------------------------------------------- */
-static lub_heap_node_t *
-_lub_heap_node_from_ptr(lub_bintree_t *tree,
-                        const char    *ptr, 
-                        bool_t         start_of_block)
+static lub_heap_node_t *_lub_heap_node_from_ptr(lub_bintree_t * tree,
+						const char *ptr,
+						bool_t start_of_block)
 {
-    lub_heap_node_t *result = 0;
-    if(tree->root)
-    {
-        lub_heap_node_key_t key;
-        
-        /* search for the node which comes immediately before this pointer */
-        key.node = (lub_heap_node_t *)ptr;
-        result = lub_bintree_findprevious(tree,&key);
-    
-        if(NULL != result)
-        {
-            char *tmp = lub_heap_node__get_ptr(result);
-            /* ensure that the pointer is within the scope of this node */
-            if(start_of_block)
-            {
-                if(ptr != tmp)
-                {
-                    /* 
-                     * this should be an exact match and isn't
-                     */
-                    result = NULL;
-                }
-            }
-            else if(ptr < tmp)
-            {
-                /* 
-                 * this is referencing part of the node header
-                 */
-                result = NULL;
-            }
-            else
-            {
-                /* compare with the end of the allocated memory */
-                tmp += lub_heap_node__get_size(result);
-                /* ensure that the pointer is within the scope of this node */
-                if(ptr >= tmp)
-                {
-                    /* out of range of this node */
-                    result = NULL;
-                }
-            }
-            tmp = 0; /* don't leave a pointer on the stack */
-        }
-    }
-    ptr = 0; /* don't leave a pointer on the stack */
-    return result;
+	lub_heap_node_t *result = 0;
+	if (tree->root) {
+		lub_heap_node_key_t key;
+
+		/* search for the node which comes immediately before this pointer */
+		key.node = (lub_heap_node_t *) ptr;
+		result = lub_bintree_findprevious(tree, &key);
+
+		if (NULL != result) {
+			char *tmp = lub_heap_node__get_ptr(result);
+			/* ensure that the pointer is within the scope of this node */
+			if (start_of_block) {
+				if (ptr != tmp) {
+					/* 
+					 * this should be an exact match and isn't
+					 */
+					result = NULL;
+				}
+			} else if (ptr < tmp) {
+				/* 
+				 * this is referencing part of the node header
+				 */
+				result = NULL;
+			} else {
+				/* compare with the end of the allocated memory */
+				tmp += lub_heap_node__get_size(result);
+				/* ensure that the pointer is within the scope of this node */
+				if (ptr >= tmp) {
+					/* out of range of this node */
+					result = NULL;
+				}
+			}
+			tmp = 0;	/* don't leave a pointer on the stack */
+		}
+	}
+	ptr = 0;		/* don't leave a pointer on the stack */
+	return result;
 }
+
 /*--------------------------------------------------------- */
-lub_heap_node_t *
-lub_heap_node_from_start_of_block_ptr(char *ptr)
+lub_heap_node_t *lub_heap_node_from_start_of_block_ptr(char *ptr)
 {
-    lub_heap_node_t *result = 0;
-    if(lub_heap_leak_query_node_tree())
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        result = _lub_heap_node_from_ptr(&leak->m_node_tree,ptr,BOOL_TRUE);
-        lub_heap_leak_release(leak);
-    }
-    if((0 == result) && lub_heap_leak_query_clear_node_tree())
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        result = _lub_heap_node_from_ptr(&leak->m_clear_node_tree,ptr,BOOL_TRUE);
-        lub_heap_leak_release(leak);
-    }
-    ptr = 0; /* don't leave a pointer on the stack */
-    return result;
+	lub_heap_node_t *result = 0;
+	if (lub_heap_leak_query_node_tree()) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		result =
+		    _lub_heap_node_from_ptr(&leak->m_node_tree, ptr, BOOL_TRUE);
+		lub_heap_leak_release(leak);
+	}
+	if ((0 == result) && lub_heap_leak_query_clear_node_tree()) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		result =
+		    _lub_heap_node_from_ptr(&leak->m_clear_node_tree, ptr,
+					    BOOL_TRUE);
+		lub_heap_leak_release(leak);
+	}
+	ptr = 0;		/* don't leave a pointer on the stack */
+	return result;
 }
+
 /*--------------------------------------------------------- */
 void
-lub_heap_node__set_context(lub_heap_node_t    *this,
-                        lub_heap_context_t *value)
+lub_heap_node__set_context(lub_heap_node_t * this, lub_heap_context_t * value)
 {
-    unsigned long mask = ((unsigned long)value & ~(LEAKED_MASK | PARTIAL_MASK));
-    this->_context = (lub_heap_context_t *)mask;
+	unsigned long mask =
+	    ((unsigned long)value & ~(LEAKED_MASK | PARTIAL_MASK));
+	this->_context = (lub_heap_context_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-lub_heap_context_t *
-lub_heap_node__get_context(const lub_heap_node_t *this)
+lub_heap_context_t *lub_heap_node__get_context(const lub_heap_node_t * this)
 {
-    unsigned long mask = (unsigned long)this->_context & ~(LEAKED_MASK | PARTIAL_MASK);
-    return (lub_heap_context_t *)mask;
+	unsigned long mask =
+	    (unsigned long)this->_context & ~(LEAKED_MASK | PARTIAL_MASK);
+	return (lub_heap_context_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node__set_next(lub_heap_node_t *this,
-                     lub_heap_node_t *value)
+void lub_heap_node__set_next(lub_heap_node_t * this, lub_heap_node_t * value)
 {
-    unsigned long mask = ((unsigned long)value & ~(SCANNED_MASK));
-    this->_next = (lub_heap_node_t *)mask;
+	unsigned long mask = ((unsigned long)value & ~(SCANNED_MASK));
+	this->_next = (lub_heap_node_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-lub_heap_node_t *
-lub_heap_node__get_next(const lub_heap_node_t *this)
+lub_heap_node_t *lub_heap_node__get_next(const lub_heap_node_t * this)
 {
-    unsigned long mask = (unsigned long)this->_next & ~(SCANNED_MASK);
-    return (lub_heap_node_t *)mask;
+	unsigned long mask = (unsigned long)this->_next & ~(SCANNED_MASK);
+	return (lub_heap_node_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_node__get_leaked(const lub_heap_node_t *this)
+bool_t lub_heap_node__get_leaked(const lub_heap_node_t * this)
 {
-    return ((unsigned long)this->_context & LEAKED_MASK) ? BOOL_TRUE : BOOL_FALSE;
+	return ((unsigned long)this->
+		_context & LEAKED_MASK) ? BOOL_TRUE : BOOL_FALSE;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_node__get_partial(const lub_heap_node_t *this)
+bool_t lub_heap_node__get_partial(const lub_heap_node_t * this)
 {
-    return ((unsigned long)this->_context & PARTIAL_MASK) ? BOOL_TRUE : BOOL_FALSE;
+	return ((unsigned long)this->
+		_context & PARTIAL_MASK) ? BOOL_TRUE : BOOL_FALSE;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_node__get_scanned(const lub_heap_node_t *this)
+bool_t lub_heap_node__get_scanned(const lub_heap_node_t * this)
 {
-    return ((unsigned long)this->_next & SCANNED_MASK) ? BOOL_TRUE : BOOL_FALSE;
+	return ((unsigned long)this->
+		_next & SCANNED_MASK) ? BOOL_TRUE : BOOL_FALSE;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node__set_leaked(lub_heap_node_t *this, bool_t value)
+void lub_heap_node__set_leaked(lub_heap_node_t * this, bool_t value)
 {
-    unsigned long mask = (unsigned long)this->_context;
-    if(BOOL_TRUE == value)
-    {
-        mask |= LEAKED_MASK;
-    }
-    else
-    {
-        mask &= ~LEAKED_MASK;
-    }
-    this->_context = (lub_heap_context_t *)mask;
+	unsigned long mask = (unsigned long)this->_context;
+	if (BOOL_TRUE == value) {
+		mask |= LEAKED_MASK;
+	} else {
+		mask &= ~LEAKED_MASK;
+	}
+	this->_context = (lub_heap_context_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node__set_partial(lub_heap_node_t *this, bool_t value)
+void lub_heap_node__set_partial(lub_heap_node_t * this, bool_t value)
 {
-    unsigned long mask = (unsigned long)this->_context;
-    if(BOOL_TRUE == value)
-    {
-        mask |= PARTIAL_MASK;
-    }
-    else
-    {
-        mask &= ~PARTIAL_MASK;
-    }
-    this->_context = (lub_heap_context_t *)mask;
+	unsigned long mask = (unsigned long)this->_context;
+	if (BOOL_TRUE == value) {
+		mask |= PARTIAL_MASK;
+	} else {
+		mask &= ~PARTIAL_MASK;
+	}
+	this->_context = (lub_heap_context_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node__set_scanned(lub_heap_node_t *this, bool_t value)
+void lub_heap_node__set_scanned(lub_heap_node_t * this, bool_t value)
 {
-    unsigned long mask = (unsigned long)this->_next;
-    if(BOOL_TRUE == value)
-    {
-        mask |= SCANNED_MASK;
-    }
-    else
-    {
-        mask &= ~SCANNED_MASK;
-    }
-    this->_next = (lub_heap_node_t *)mask;
+	unsigned long mask = (unsigned long)this->_next;
+	if (BOOL_TRUE == value) {
+		mask |= SCANNED_MASK;
+	} else {
+		mask &= ~SCANNED_MASK;
+	}
+	this->_next = (lub_heap_node_t *) mask;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_foreach_node(void (*fn)(lub_heap_node_t *,void*),
-                      void  *arg)
+void lub_heap_foreach_node(void (*fn) (lub_heap_node_t *, void *), void *arg)
 {
-    lub_heap_leak_t     *leak = lub_heap_leak_instance();
-    lub_heap_node_t     *node;
-    lub_heap_node_key_t  key;
-
-    for(node = lub_bintree_findfirst(&leak->m_node_tree);
-        node;
-        node = lub_bintree_findnext(&leak->m_node_tree,&key))
-    {
-        lub_heap_leak_release(leak);
-
-        lub_heap_node_getkey(node,(lub_bintree_key_t*)&key);
-        /* invoke the specified method on this node */
-        fn(node,arg);
-
-        leak = lub_heap_leak_instance();
-    }
-    lub_heap_leak_release(leak);
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	lub_heap_node_t *node;
+	lub_heap_node_key_t key;
+
+	for (node = lub_bintree_findfirst(&leak->m_node_tree);
+	     node; node = lub_bintree_findnext(&leak->m_node_tree, &key)) {
+		lub_heap_leak_release(leak);
+
+		lub_heap_node_getkey(node, (lub_bintree_key_t *) & key);
+		/* invoke the specified method on this node */
+		fn(node, arg);
+
+		leak = lub_heap_leak_instance();
+	}
+	lub_heap_leak_release(leak);
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_node_clear(lub_heap_node_t *this,
-                    void            *arg)
+void lub_heap_node_clear(lub_heap_node_t * this, void *arg)
 {
-    /* clearing a node removes it from it's context */
-    lub_heap_context_t *context = lub_heap_node__get_context(this);
-    if(NULL != context)
-    {
-        lub_heap_leak_t *leak = lub_heap_leak_instance();
-        /* remove from the node tree and place into the clear tree */
-        lub_bintree_remove(&leak->m_node_tree,this);
-        lub_bintree_insert(&leak->m_clear_node_tree,this);
-        lub_heap_leak_release(leak);
-
-        /* maintain the leak statistics */
-        --context->allocs;
-        context->alloc_bytes    -= lub_heap_node__get_size(this);
-        context->alloc_overhead -= lub_heap_node__get_overhead(this);
-
-        lub_heap_node__set_context(this,NULL);
-        lub_heap_node__set_leaked(this,BOOL_FALSE);
-        lub_heap_node__set_partial(this,BOOL_FALSE);
-
-        lub_heap_context_remove_node(context,this);
-    }
+	/* clearing a node removes it from it's context */
+	lub_heap_context_t *context = lub_heap_node__get_context(this);
+	if (NULL != context) {
+		lub_heap_leak_t *leak = lub_heap_leak_instance();
+		/* remove from the node tree and place into the clear tree */
+		lub_bintree_remove(&leak->m_node_tree, this);
+		lub_bintree_insert(&leak->m_clear_node_tree, this);
+		lub_heap_leak_release(leak);
+
+		/* maintain the leak statistics */
+		--context->allocs;
+		context->alloc_bytes -= lub_heap_node__get_size(this);
+		context->alloc_overhead -= lub_heap_node__get_overhead(this);
+
+		lub_heap_node__set_context(this, NULL);
+		lub_heap_node__set_leaked(this, BOOL_FALSE);
+		lub_heap_node__set_partial(this, BOOL_FALSE);
+
+		lub_heap_context_remove_node(context, this);
+	}
 }
+
 /*--------------------------------------------------------- */
 #define RELEASE_COUNT 2048
-void 
-lub_heap_scan_memory(const void  *mem,
-                     size_t       size)
+void lub_heap_scan_memory(const void *mem, size_t size)
 {
-    size_t              bytes_left = size;
-    typedef const void *void_ptr;
-    void_ptr           *ptr,last_ptr = 0;
-    lub_heap_leak_t    *leak = lub_heap_leak_instance();
-    unsigned            release_count = RELEASE_COUNT;
-    if(lub_heap_leak_verbose)
-    {
-        printf("lub_heap_scan_memory(%p,%"SIZE_FMT")\n",mem,size);
-    }
-    ++leak->m_stats.scanned;
-    
-    /* scan all the words in this allocated block of memory */
-    for(ptr = (void_ptr*)mem;
-        bytes_left;
-        )
-    {
-        if(0 == --release_count)
-        {
-            /* make space for other memory tasks to get in... */
-            lub_heap_leak_release(leak);
-            leak          = lub_heap_leak_instance();
-            release_count = RELEASE_COUNT;
-        }
-        if(*ptr != last_ptr)
-        {
-            lub_heap_node_t    *node;
-            lub_heap_node_key_t key;
-            
-            /* search for a node */
-            key.node = (lub_heap_node_t *)ptr;
-            node = lub_bintree_find(&leak->m_node_tree,&key);
-            if(NULL != node)
-            {
-                /*
-                 * If we stumble across a node don't scan it's contents as this could cause
-                 * false negatives. This situation could occur if an allocated block of memory
-                 * was used as a heap in it's own right.
-                 */
-                char  *tmp       = lub_heap_node__get_ptr(node);
-                size_t node_size = lub_heap_node__get_size(node);
-                
-                /* skip forward past the node contents */
-                ptr         = (void_ptr*)(tmp + node_size);
-                bytes_left -= (node_size + sizeof(lub_heap_node_t));
-                tmp = 0; /* don't leave pointers on our stack */
-                last_ptr = 0;
-                continue;
-            }
-            /* 
-             * see whether this is a reference to a node 
-             * NB. we only resolve a node if the pointer lies
-             * within the memory allocated to the client; any direct
-             * references to nodes will not resolve to a node
-             * object.
-             */
-            node = _lub_heap_node_from_ptr(&leak->m_node_tree,*ptr,BOOL_FALSE);
-            if( (NULL != node))
-            {
-                /* this looks like a valid node */
-                lub_heap_context_t *context       = lub_heap_node__get_context(node);
-                size_t              node_size     = lub_heap_node__get_size(node);
-                size_t              node_overhead = lub_heap_node__get_overhead(node);
-                char               *tmp           = lub_heap_node__get_ptr(node);
-                if(tmp == *ptr)
-                {
-                    /* reference to start of block */
-                    if(BOOL_TRUE == lub_heap_node__get_partial(node))
-                    {
-                        lub_heap_node__set_partial(node,BOOL_FALSE);
-    
-                        if(NULL != context)
-                        {
-                            --context->partials;
-                            context->partial_bytes    -= node_size;
-                            context->partial_overhead -= node_overhead;
-                        }
-                    }
-                }
-                tmp = 0; /* don't leave pointers on our stack */
-                /* this is definately not a leak */
-                if(BOOL_TRUE == lub_heap_node__get_leaked(node))
-                {
-                    lub_heap_node__set_leaked(node,BOOL_FALSE);
-    
-                    if(NULL != context)
-                    {
-                        --context->leaks;
-                        context->leaked_bytes    -= node_size;
-                        context->leaked_overhead -= node_overhead;
-     
-                        --leak->m_stats.leaks;
-                        leak->m_stats.leaked_bytes    -= node_size;
-                        leak->m_stats.leaked_overhead -= node_overhead;
-                    }
-                }
-            }
-        }
-        last_ptr = *ptr++;
-        bytes_left -= sizeof(void*);
-    }
-    lub_heap_leak_release(leak);
-    last_ptr = ptr = 0; /* don't leave pointers on the stack */
+	size_t bytes_left = size;
+	typedef const void *void_ptr;
+	void_ptr *ptr, last_ptr = 0;
+	lub_heap_leak_t *leak = lub_heap_leak_instance();
+	unsigned release_count = RELEASE_COUNT;
+	if (lub_heap_leak_verbose) {
+		printf("lub_heap_scan_memory(%p,%" SIZE_FMT ")\n", mem, size);
+	}
+	++leak->m_stats.scanned;
+
+	/* scan all the words in this allocated block of memory */
+	for (ptr = (void_ptr *) mem; bytes_left;) {
+		if (0 == --release_count) {
+			/* make space for other memory tasks to get in... */
+			lub_heap_leak_release(leak);
+			leak = lub_heap_leak_instance();
+			release_count = RELEASE_COUNT;
+		}
+		if (*ptr != last_ptr) {
+			lub_heap_node_t *node;
+			lub_heap_node_key_t key;
+
+			/* search for a node */
+			key.node = (lub_heap_node_t *) ptr;
+			node = lub_bintree_find(&leak->m_node_tree, &key);
+			if (NULL != node) {
+				/*
+				 * If we stumble across a node don't scan it's contents as this could cause
+				 * false negatives. This situation could occur if an allocated block of memory
+				 * was used as a heap in it's own right.
+				 */
+				char *tmp = lub_heap_node__get_ptr(node);
+				size_t node_size =
+				    lub_heap_node__get_size(node);
+
+				/* skip forward past the node contents */
+				ptr = (void_ptr *) (tmp + node_size);
+				bytes_left -=
+				    (node_size + sizeof(lub_heap_node_t));
+				tmp = 0;	/* don't leave pointers on our stack */
+				last_ptr = 0;
+				continue;
+			}
+			/* 
+			 * see whether this is a reference to a node 
+			 * NB. we only resolve a node if the pointer lies
+			 * within the memory allocated to the client; any direct
+			 * references to nodes will not resolve to a node
+			 * object.
+			 */
+			node =
+			    _lub_heap_node_from_ptr(&leak->m_node_tree, *ptr,
+						    BOOL_FALSE);
+			if ((NULL != node)) {
+				/* this looks like a valid node */
+				lub_heap_context_t *context =
+				    lub_heap_node__get_context(node);
+				size_t node_size =
+				    lub_heap_node__get_size(node);
+				size_t node_overhead =
+				    lub_heap_node__get_overhead(node);
+				char *tmp = lub_heap_node__get_ptr(node);
+				if (tmp == *ptr) {
+					/* reference to start of block */
+					if (BOOL_TRUE ==
+					    lub_heap_node__get_partial(node)) {
+						lub_heap_node__set_partial(node,
+									   BOOL_FALSE);
+
+						if (NULL != context) {
+							--context->partials;
+							context->
+							    partial_bytes -=
+							    node_size;
+							context->
+							    partial_overhead -=
+							    node_overhead;
+						}
+					}
+				}
+				tmp = 0;	/* don't leave pointers on our stack */
+				/* this is definately not a leak */
+				if (BOOL_TRUE ==
+				    lub_heap_node__get_leaked(node)) {
+					lub_heap_node__set_leaked(node,
+								  BOOL_FALSE);
+
+					if (NULL != context) {
+						--context->leaks;
+						context->leaked_bytes -=
+						    node_size;
+						context->leaked_overhead -=
+						    node_overhead;
+
+						--leak->m_stats.leaks;
+						leak->m_stats.leaked_bytes -=
+						    node_size;
+						leak->m_stats.leaked_overhead -=
+						    node_overhead;
+					}
+				}
+			}
+		}
+		last_ptr = *ptr++;
+		bytes_left -= sizeof(void *);
+	}
+	lub_heap_leak_release(leak);
+	last_ptr = ptr = 0;	/* don't leave pointers on the stack */
 }
+
 /*--------------------------------------------------------- */

+ 36 - 88
lub/heap/node.h

@@ -9,18 +9,16 @@
 
 #define SCANNED_MASK 0x00000001
 
-struct _lub_heap_node
-{
-    lub_heap_context_t *_context; /* bottom two bits used for leaked, partial flags */
-    lub_heap_node_t    *_next;    /* linked list of nodes per context (bottom bit used for scanned mask) */
-    lub_heap_node_t    *prev;
-    lub_bintree_node_t  bt_node;
+struct _lub_heap_node {
+	lub_heap_context_t *_context;	/* bottom two bits used for leaked, partial flags */
+	lub_heap_node_t *_next;	/* linked list of nodes per context (bottom bit used for scanned mask) */
+	lub_heap_node_t *prev;
+	lub_bintree_node_t bt_node;
 };
 
 typedef struct _lub_heap_node_key lub_heap_node_key_t;
-struct _lub_heap_node_key
-{
-    const lub_heap_node_t *node;
+struct _lub_heap_node_key {
+	const lub_heap_node_t *node;
 };
 
 /*---------------------------------------------------------
@@ -29,31 +27,24 @@ struct _lub_heap_node_key
 /*
  * Get the size (in bytes) of an instance of a node object
  */
-extern size_t
-    lub_heap_node__get_instanceSize(void);
+extern size_t lub_heap_node__get_instanceSize(void);
 
 /*---------------------------------------------------------
  * PUBLIC META FUNCTIONS
  *--------------------------------------------------------- */
-extern void
-    lub_heap_node_clear(lub_heap_node_t *instance,
-                        void            *arg);
+extern void lub_heap_node_clear(lub_heap_node_t * instance, void *arg);
 
 extern void
-    lub_heap_foreach_node(void (*fn)(lub_heap_node_t *, void*),void *arg);
+lub_heap_foreach_node(void (*fn) (lub_heap_node_t *, void *), void *arg);
 
-extern void 
-    lub_heap_scan_memory(const void  *mem,
-                         size_t       size);
+extern void lub_heap_scan_memory(const void *mem, size_t size);
 /*
  * Given a pointer to the start of a data area of a node obtain the
  * node reference. Returns NULL if the node is not valid. 
  */
-extern lub_heap_node_t *
-    lub_heap_node_from_start_of_block_ptr(char *ptr);
+extern lub_heap_node_t *lub_heap_node_from_start_of_block_ptr(char *ptr);
 
-extern void
-    lub_heap_node_initialise(void);
+extern void lub_heap_node_initialise(void);
 
 /*---------------------------------------------------------
  * PUBLIC METHODS
@@ -61,92 +52,49 @@ extern void
 /*
  * Given a node return a pointer to the data area allocated.
  */
-extern char *
-    lub_heap_node__get_ptr(
-        const lub_heap_node_t *instance
-    );
+extern char *lub_heap_node__get_ptr(const lub_heap_node_t * instance);
 /*
  * Given a node return the size of memory that it encapsulates.
  */
-extern size_t
-    lub_heap_node__get_size(
-        const lub_heap_node_t *instance
-    );
+extern size_t lub_heap_node__get_size(const lub_heap_node_t * instance);
 /*
  * Given a node return the size of memory overhead that it encapsulates.
  */
-extern size_t
-    lub_heap_node__get_overhead(
-        const lub_heap_node_t *instance
-    );
+extern size_t lub_heap_node__get_overhead(const lub_heap_node_t * instance);
 /*
  * Get a node to remove itself from it's allocation tree
  */
-extern void
-    lub_heap_node_fini(
-        lub_heap_node_t *instance
-    );
+extern void lub_heap_node_fini(lub_heap_node_t * instance);
 
 extern void
-    lub_heap_node_init(
-        lub_heap_node_t * instance,
-        lub_heap_context_t * context
-    );
+lub_heap_node_init(lub_heap_node_t * instance, lub_heap_context_t * context);
 
 extern lub_bintree_compare_fn lub_heap_node_compare;
-extern lub_bintree_getkey_fn  lub_heap_node_getkey;
-    
-extern bool_t
-    lub_heap_node__get_leaked(
-        const lub_heap_node_t *instance
-    );
-
-extern bool_t
-    lub_heap_node__get_partial(
-        const lub_heap_node_t *instance
-    );
-    
-extern bool_t
-    lub_heap_node__get_scanned(
-        const lub_heap_node_t *instance
-    );
+extern lub_bintree_getkey_fn lub_heap_node_getkey;
 
-extern void
-    lub_heap_node__set_leaked(
-        lub_heap_node_t *instance,
-        bool_t        value
-    );
+extern bool_t lub_heap_node__get_leaked(const lub_heap_node_t * instance);
+
+extern bool_t lub_heap_node__get_partial(const lub_heap_node_t * instance);
+
+extern bool_t lub_heap_node__get_scanned(const lub_heap_node_t * instance);
+
+extern void lub_heap_node__set_leaked(lub_heap_node_t * instance, bool_t value);
 
 extern void
-    lub_heap_node__set_partial(
-        lub_heap_node_t *instance,
-        bool_t        value
-    );
+lub_heap_node__set_partial(lub_heap_node_t * instance, bool_t value);
 
 extern void
-    lub_heap_node__set_scanned(
-        lub_heap_node_t *instance,
-        bool_t        value
-    );
+lub_heap_node__set_scanned(lub_heap_node_t * instance, bool_t value);
 
-extern lub_heap_context_t *
-    lub_heap_node__get_context(
-        const lub_heap_node_t *instance
-    );
+extern lub_heap_context_t *lub_heap_node__get_context(const lub_heap_node_t *
+						      instance);
 
 extern void
-    lub_heap_node__set_context(
-        lub_heap_node_t    *instance,
-        lub_heap_context_t *context
-    );
-    
-extern lub_heap_node_t *
-    lub_heap_node__get_next(
-        const lub_heap_node_t *instance
-    );
+lub_heap_node__set_context(lub_heap_node_t * instance,
+			   lub_heap_context_t * context);
+
+extern lub_heap_node_t *lub_heap_node__get_next(const lub_heap_node_t *
+						instance);
 
 extern void
-    lub_heap_node__set_next(
-        lub_heap_node_t *instance,
-        lub_heap_node_t *context
-    );
+lub_heap_node__set_next(lub_heap_node_t * instance, lub_heap_node_t * context);

+ 2 - 2
lub/heap/posix/heap_clean_stacks.c

@@ -1,7 +1,7 @@
 #include "../private.h"
 /*--------------------------------------------------------- */
-void
-lub_heap_clean_stacks(void)
+void lub_heap_clean_stacks(void)
 {
 }
+
 /*--------------------------------------------------------- */

+ 25 - 28
lub/heap/posix/heap_leak_mutex.c

@@ -7,49 +7,46 @@
 static pthread_mutex_t leak_mutex = PTHREAD_MUTEX_INITIALIZER;
 static lub_heap_leak_t instance;
 /*--------------------------------------------------------- */
-void
-lub_heap_leak_mutex_lock(void)
+void lub_heap_leak_mutex_lock(void)
 {
-    int status = pthread_mutex_lock(&leak_mutex);
-    if(0 != status)
-    {
-        perror("pthread_mutex_lock() failed");
-    }
+	int status = pthread_mutex_lock(&leak_mutex);
+	if (0 != status) {
+		perror("pthread_mutex_lock() failed");
+	}
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_leak_mutex_unlock(void)
+void lub_heap_leak_mutex_unlock(void)
 {
-    int status = pthread_mutex_unlock(&leak_mutex);
-    if(0 != status)
-    {
-        perror("pthread_mutex_unlock() failed");
-    }
+	int status = pthread_mutex_unlock(&leak_mutex);
+	if (0 != status) {
+		perror("pthread_mutex_unlock() failed");
+	}
 }
+
 /*--------------------------------------------------------- */
-lub_heap_leak_t *
-lub_heap_leak_instance(void)
+lub_heap_leak_t *lub_heap_leak_instance(void)
 {
-    lub_heap_leak_mutex_lock();
-    return &instance;
+	lub_heap_leak_mutex_lock();
+	return &instance;
 }
+
 /*--------------------------------------------------------- */
-void
-lub_heap_leak_release(lub_heap_leak_t *instance)
+void lub_heap_leak_release(lub_heap_leak_t * instance)
 {
-    lub_heap_leak_mutex_unlock();
+	lub_heap_leak_mutex_unlock();
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_leak_query_node_tree(void)
+bool_t lub_heap_leak_query_node_tree(void)
 {
-    return BOOL_TRUE;
+	return BOOL_TRUE;
 }
+
 /*--------------------------------------------------------- */
-bool_t
-lub_heap_leak_query_clear_node_tree(void)
+bool_t lub_heap_leak_query_clear_node_tree(void)
 {
-    return BOOL_TRUE;
+	return BOOL_TRUE;
 }
-/*--------------------------------------------------------- */
 
+/*--------------------------------------------------------- */

+ 4 - 4
lub/heap/posix/heap_scan_bss.c

@@ -12,10 +12,10 @@
 extern char START, END;
 
 /*--------------------------------------------------------- */
-void
-lub_heap_scan_bss(void)
+void lub_heap_scan_bss(void)
 {
-    /* now scan the memory */
-    lub_heap_scan_memory(&START,&END-&START);
+	/* now scan the memory */
+	lub_heap_scan_memory(&START, &END - &START);
 }
+
 /*--------------------------------------------------------- */

Some files were not shown because too many files changed in this diff