Re: [PATCH] btree: Add custom allocator interface.

Stuart Longland <[email protected]> Mon, 8 Feb 2016 12:37:11 +1000
Newsgroups org.ozlabs.lists.ccan
Message-ID <[email protected]>
On 08/02/16 10:45, Rusty Russell wrote:
> [email protected] writes:
>> From: Stuart Longland <[email protected]>
>>
>> This provides a way for btree to be used with external allocator
>> libraries such as the tal or talloc modules.
> 
> Sure.  In my libraries I generally use a global allocator override;
> it's not thread-safe, but much easier to use in practice.
> 
> Since I don't use btree, I'm agnostic about it though!

Indeed, actually I'm half considering whether we make it a separate
module that `btree` (and others) can pull into their code.  There are
lots of cases where `tal` or `talloc` is desirable over the standard C
malloc/free.

> Minor nits below:
> …
>>  /*
>>   * If iter->node has parent, returns 1 and ascends the iterator such that
>> @@ -65,11 +68,19 @@ static int node_walk_forward(const struct btree_node *node,
>>  
>>  struct btree *btree_new(btree_search_t search)
>>  {
>> -	struct btree *btree = calloc(1, sizeof(struct btree));
>> -	struct btree_node *node = node_alloc(0);
>> +	return btree_new_with_alloc(search, &BTREE_DEFAULT_ALLOCATOR);
>> +}
>> +
>> +struct btree *btree_new_with_alloc(btree_search_t search,
>> +		const struct btree_allocator *alloc)
>> +{
>> +	struct btree *btree = (*(alloc->malloc))(alloc, sizeof(struct btree));
> 
> You can call a function pointer, so alloc->malloc(alloc, sizeof(struct btree));
> should work.  

Ahh, didn't know about that.  I presume a more recent C standard?  I'll
admit I had to look up the syntax for function pointers off Wikipedia as
I don't do it often enough.

(In fact, I don't do *C* often enough.)

> Lots of whitespace cleanups.  Not a problem, but prefer a separate patch
> for clarity.

Yep, I often do a whitespace cleanup first up and ordinarily do that as
a first commit.  I'll split these out.

> Put default_malloc and default_free above the BTREE_DEFAULT_ALLOCATOR
> definition, and you don't need to pre-declare them.

Will do.
Regards,
-- 
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
  ...it's backed up on a tape somewhere.

_______________________________________________
ccan mailing list
[email protected]
https://lists.ozlabs.org/listinfo/ccan
signature.asc (application/pgp-signature, 819 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJWt/9XAAoJEE36GRQQveO3EpEP/1Go0x5zq4OgKYyvUJYnKMzr
H3JIKwOmIh8rY5/Y7UwEa1ygDGfRvyZml73RHnNpvfzZPS28sJbbSm7IGh33QWAU
5K128Oq1zS8MMUqzFWl/2ChJxSLVU1QrAbCFrBMC/JIZweOgBPkJyERhRun8MbVV
pPLoES664i9/LfsqJkPdcGwMvWci5ndPwRdWQU76G4M+hYOyVV1vI2ikTppXSaRM
Tw4x7juEYVAmji0L6HzDSgwP3OATgTGgBIPI69mwIwcxjfWZlMYb7q46QrAvJ/Zg
I/jNbB8GawjksOPxrm+fnayIZf5yEmvn/Qgy5anZ1U9NLJMw8Nz7BFWstEp6Gj0Z
yf2ACNwT8xGnqxcDkkBxicaf/q5B1kZsGpvjI1ks5vxnpv0n3eNIDQMojRhpewJt
cQn5lqtsZVi1acSt+SXHobwGZNN/JuQALpqPHrFE2IulHE9bo4XkM8zgKQijkHxG
GxZjtKMs08t4yBtmL1Wk/8Zvb8sGyew9SCnB0L8ruqnrYjZ9BQwAhCDp0Xhw8qgu
wACQL6nlTcDomIXPJ0ZwFojDpdhDDHx9Y+IKEnExRZWZQoq/hb2aZ/fPT51P2fcp
JVsqx4kYyNnJus/A/B56ZdJRJWcLG4qduMsnRPjUAHKiwo0Il7dXLt6bnblbrCN/
u1h/5wLHHbbH7LoUY/h1
=yMeG
-----END PGP SIGNATURE-----