git/vcs-svn
Jonathan Nieder 97a5e3453a treap: make treap_insert return inserted node
Suppose I try the following:

	struct int_node *node = node_pointer(node_alloc(1));
	node->n = 5;
	treap_insert(&root, node);
	printf("%d\n", node->n);

Usually the result will be 5.  But since treap_insert draws memory
from the node pool, if the caller is unlucky then (1) the node pool
will be full and (2) realloc will be forced to move the node pool to
find room, so the node address becomes invalid and the result of
dereferencing it is undefined.

So we ought to use offsets in preference to pointers for references
that would remain valid after a call to treap_insert.  Tweak the
signature to hint at a certain special case: since the inserted node
can change address (though not offset), as a convenience teach
treap_insert to return its new address.

So the motivational example could be fixed by adding "node =".

	struct int_node *node = node_pointer(node_alloc(1));
	node->n = 5;
	node = treap_insert(&root, node);
	printf("%d\n", node->n);

Based on a true story.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-12-07 16:03:55 -08:00
..
fast_export.c vcs-svn: Avoid %z in format string 2010-08-14 19:35:38 -07:00
fast_export.h Infrastructure to write revisions in fast-export format 2010-08-14 19:35:37 -07:00
LICENSE SVN dump parser 2010-08-14 19:35:38 -07:00
line_buffer.c Add stream helper library 2010-08-14 19:35:37 -07:00
line_buffer.h Add stream helper library 2010-08-14 19:35:37 -07:00
line_buffer.txt Add stream helper library 2010-08-14 19:35:37 -07:00
obj_pool.h Add memory pool library 2010-08-14 19:35:37 -07:00
repo_tree.c vcs-svn: Rename dirent pool to build on Windows 2010-08-14 19:35:38 -07:00
repo_tree.h Infrastructure to write revisions in fast-export format 2010-08-14 19:35:37 -07:00
string_pool.c Add string-specific memory pool 2010-08-14 19:35:37 -07:00
string_pool.h Add string-specific memory pool 2010-08-14 19:35:37 -07:00
string_pool.txt Add string-specific memory pool 2010-08-14 19:35:37 -07:00
svndump.c SVN dump parser 2010-08-14 19:35:38 -07:00
svndump.h SVN dump parser 2010-08-14 19:35:38 -07:00
trp.h treap: make treap_insert return inserted node 2010-12-07 16:03:55 -08:00
trp.txt treap: make treap_insert return inserted node 2010-12-07 16:03:55 -08:00