[ccan] [PATCH] grab_file: Convert from talloc to tal

David Gibson david at gibson.dropbear.id.au
Sat Aug 16 18:44:01 EST 2014


This switches the grab_file module from using talloc, to the newer tal
module.  Since the tal interface can optionally be implemented in terms
of talloc, compatibility with existing talloc users can be maintained.

Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
---
 ccan/grab_file/_info           |  6 +++---
 ccan/grab_file/grab_file.c     | 11 +++++------
 ccan/grab_file/test/run-grab.c | 11 ++++++-----
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/ccan/grab_file/_info b/ccan/grab_file/_info
index 2508a2e..f9ac7df 100644
--- a/ccan/grab_file/_info
+++ b/ccan/grab_file/_info
@@ -12,7 +12,7 @@
  *	#include <stdio.h>
  *	#include <string.h>
  *	#include <ccan/grab_file/grab_file.h>
- *	#include <ccan/talloc/talloc.h>	// For talloc_free()
+ *	#include <ccan/tal/tal.h>	// For tal_free()
  *
  *	int main(int argc, char *argv[])
  *	{
@@ -30,7 +30,7 @@
  *			printf("File contains multiple lines\n");
  *		else
  *			printf("File contains one line\n");
- *		talloc_free(file);
+ *		tal_free(file);
  *
  *		return 0;
  *	}
@@ -44,7 +44,7 @@ int main(int argc, char *argv[])
 		return 1;
 
 	if (strcmp(argv[1], "depends") == 0) {
-		printf("ccan/talloc\n");
+		printf("ccan/tal\n");
 		printf("ccan/noerr\n");
 		return 0;
 	}
diff --git a/ccan/grab_file/grab_file.c b/ccan/grab_file/grab_file.c
index 2a9d0cd..1383a60 100644
--- a/ccan/grab_file/grab_file.c
+++ b/ccan/grab_file/grab_file.c
@@ -1,6 +1,6 @@
 /* Licensed under LGPLv2+ - see LICENSE file for details */
 #include "grab_file.h"
-#include <ccan/talloc/talloc.h>
+#include <ccan/tal/tal.h>
 #include <ccan/noerr/noerr.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -23,14 +23,13 @@ void *grab_fd(const void *ctx, int fd, size_t *size)
 	else
 		max = 16384;
 
-	buffer = talloc_array(ctx, char, max+1);
+	buffer = tal_arr(ctx, char, max+1);
 	while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
 		*size += ret;
 		if (*size == max) {
-			buffer = talloc_realloc(ctx, buffer, char, max*2+1);
+			tal_resize(&buffer, max*2+1);
 			if (!buffer) {
-				buffer = talloc_realloc(ctx, buffer, char,
-							max + 1024*1024 + 1);
+				tal_resize(&buffer, max + 1024*1024 + 1);
 				if (!buffer)
 					return NULL;
 				max += 1024*1024;
@@ -39,7 +38,7 @@ void *grab_fd(const void *ctx, int fd, size_t *size)
 		}
 	}
 	if (ret < 0) {
-		talloc_free(buffer);
+		tal_free(buffer);
 		buffer = NULL;
 	} else
 		buffer[*size] = '\0';
diff --git a/ccan/grab_file/test/run-grab.c b/ccan/grab_file/test/run-grab.c
index c9f2426..16d5067 100644
--- a/ccan/grab_file/test/run-grab.c
+++ b/ccan/grab_file/test/run-grab.c
@@ -14,22 +14,23 @@ static char **strsplit(const void *ctx, const char *string, const char *delims)
 	char **lines = NULL;
 	unsigned int max = 64, num = 0;
 
-	lines = talloc_array(ctx, char *, max+1);
+	lines = tal_arr(ctx, char *, max+1);
 
 	while (*string != '\0') {
 		unsigned int len = strcspn(string, delims);
-		lines[num] = talloc_array(lines, char, len + 1);
+		lines[num] = tal_arr(lines, char, len + 1);
 		memcpy(lines[num], string, len);
 		lines[num][len] = '\0';
 		string += len;
 		string += strspn(string, delims) ? 1 : 0;
 		if (++num == max)
-			lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
+			tal_resize(&lines, max*=2 + 1);
 	}
 	lines[num] = NULL;
 
 	/* Shrink, so talloc_get_size works */
-	return talloc_realloc(ctx, lines, char *, num+1);
+	tal_resize(&lines, num+1);
+	return lines;
 }
 
 int 
@@ -52,7 +53,7 @@ main(int argc, char *argv[])
 		if (stat("ccan/grab_file/test/run-grab.c", &st) != 0) 
 			err(1, "Could not stat self");
 	ok1(st.st_size == length + i);
-	talloc_free(str);
+	tal_free(str);
 	
 	return 0;
 }
-- 
1.9.3



More information about the ccan mailing list