Commit 3e44cbb7 authored by Rusty Russell's avatar Rusty Russell

idtree: use ccan/tal instead of talloc

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 59208d61
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* *
* Example: * Example:
* #include <ccan/idtree/idtree.h> * #include <ccan/idtree/idtree.h>
* #include <ccan/talloc/talloc.h> * #include <ccan/tal/tal.h>
* #include <stdlib.h> * #include <stdlib.h>
* #include <stdio.h> * #include <stdio.h>
* *
...@@ -46,7 +46,7 @@ int main(int argc, char *argv[]) ...@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
return 1; return 1;
if (strcmp(argv[1], "depends") == 0) { if (strcmp(argv[1], "depends") == 0) {
printf("ccan/talloc\n"); printf("ccan/tal\n");
return 0; return 0;
} }
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
*/ */
#include <ccan/idtree/idtree.h> #include <ccan/idtree/idtree.h>
#include <ccan/talloc/talloc.h> #include <ccan/tal/tal.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
...@@ -87,7 +87,7 @@ static void free_layer(struct idtree *idp, struct idtree_layer *p) ...@@ -87,7 +87,7 @@ static void free_layer(struct idtree *idp, struct idtree_layer *p)
static int idtree_pre_get(struct idtree *idp) static int idtree_pre_get(struct idtree *idp)
{ {
while (idp->id_free_cnt < IDTREE_FREE_MAX) { while (idp->id_free_cnt < IDTREE_FREE_MAX) {
struct idtree_layer *pn = talloc_zero(idp, struct idtree_layer); struct idtree_layer *pn = talz(idp, struct idtree_layer);
if(pn == NULL) if(pn == NULL)
return (0); return (0);
free_layer(idp, pn); free_layer(idp, pn);
...@@ -313,14 +313,14 @@ bool idtree_remove(struct idtree *idp, int id) ...@@ -313,14 +313,14 @@ bool idtree_remove(struct idtree *idp, int id)
} }
while (idp->id_free_cnt >= IDTREE_FREE_MAX) { while (idp->id_free_cnt >= IDTREE_FREE_MAX) {
p = alloc_layer(idp); p = alloc_layer(idp);
talloc_free(p); tal_free(p);
} }
return true; return true;
} }
struct idtree *idtree_new(void *mem_ctx) struct idtree *idtree_new(void *mem_ctx)
{ {
return talloc_zero(mem_ctx, struct idtree); return talz(mem_ctx, struct idtree);
} }
int idtree_add(struct idtree *idp, const void *ptr, int limit) int idtree_add(struct idtree *idp, const void *ptr, int limit)
......
...@@ -17,6 +17,6 @@ int main(int argc, char *argv[]) ...@@ -17,6 +17,6 @@ int main(int argc, char *argv[])
ok1(idtree_remove(idtree, INT_MAX-1) == true); ok1(idtree_remove(idtree, INT_MAX-1) == true);
ok1(idtree_add_above(idtree, &i, INT_MAX-1, INT_MAX) == INT_MAX-1); ok1(idtree_add_above(idtree, &i, INT_MAX-1, INT_MAX) == INT_MAX-1);
ok1(idtree_add_above(idtree, &i, INT_MAX-1, INT_MAX) == -1); ok1(idtree_add_above(idtree, &i, INT_MAX-1, INT_MAX) == -1);
talloc_free(idtree); tal_free(idtree);
exit(exit_status()); exit(exit_status());
} }
...@@ -4,6 +4,16 @@ ...@@ -4,6 +4,16 @@
#define ALLOC_MAX (2 * IDTREE_SIZE) #define ALLOC_MAX (2 * IDTREE_SIZE)
static bool check_tal_parent(const tal_t *parent, const tal_t *ctx)
{
while (ctx) {
if (ctx == parent)
return true;
ctx = tal_parent(ctx);
}
return false;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned int i; unsigned int i;
...@@ -12,9 +22,9 @@ int main(int argc, char *argv[]) ...@@ -12,9 +22,9 @@ int main(int argc, char *argv[])
void *ctx; void *ctx;
plan_tests(ALLOC_MAX * 5 + 2); plan_tests(ALLOC_MAX * 5 + 2);
ctx = talloc_named_const(NULL, 1, "test root"); ctx = tal(NULL, char);
idtree = idtree_new(ctx); idtree = idtree_new(ctx);
ok1(talloc_find_parent_byname(idtree, "test root") == ctx); ok1(check_tal_parent(ctx, idtree));
for (i = 0; i < ALLOC_MAX; i++) { for (i = 0; i < ALLOC_MAX; i++) {
int id = idtree_add(idtree, &allocated[i], ALLOC_MAX-1); int id = idtree_add(idtree, &allocated[i], ALLOC_MAX-1);
...@@ -42,6 +52,6 @@ int main(int argc, char *argv[]) ...@@ -42,6 +52,6 @@ int main(int argc, char *argv[])
for (i = 0; i < ALLOC_MAX; i++) { for (i = 0; i < ALLOC_MAX; i++) {
ok1(idtree_lookup(idtree, i) == &allocated[i]); ok1(idtree_lookup(idtree, i) == &allocated[i]);
} }
talloc_free(ctx); tal_free(ctx);
exit(exit_status()); exit(exit_status());
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment