[PATCH 04/13] gc: use fixed length buffer for storing consumer name

Vladimir Davydov vdavydov.dev at gmail.com
Thu Oct 4 20:20:06 MSK 2018


The length of a consumer name never exceeds 64 characters so no use to
allocate a string. This is a mere code simplification.
---
 src/box/gc.c | 11 ++---------
 src/box/gc.h |  4 +++-
 2 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/box/gc.c b/src/box/gc.c
index a45806b6..7de50e84 100644
--- a/src/box/gc.c
+++ b/src/box/gc.c
@@ -34,7 +34,7 @@
 
 #include <stdint.h>
 #include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
 
 #define RB_COMPACT 1
 #include <small/rb.h>
@@ -82,13 +82,7 @@ gc_consumer_new(const char *name, const struct vclock *vclock,
 			 "malloc", "struct gc_consumer");
 		return NULL;
 	}
-	consumer->name = strdup(name);
-	if (consumer->name == NULL) {
-		diag_set(OutOfMemory, strlen(name) + 1,
-			 "malloc", "struct gc_consumer");
-		free(consumer);
-		return NULL;
-	}
+	snprintf(consumer->name, GC_NAME_MAX, "%s", name);
 	vclock_copy(&consumer->vclock, vclock);
 	consumer->type = type;
 	return consumer;
@@ -98,7 +92,6 @@ gc_consumer_new(const char *name, const struct vclock *vclock,
 static void
 gc_consumer_delete(struct gc_consumer *consumer)
 {
-	free(consumer->name);
 	TRASH(consumer);
 	free(consumer);
 }
diff --git a/src/box/gc.h b/src/box/gc.h
index 2c03770d..fde4facf 100644
--- a/src/box/gc.h
+++ b/src/box/gc.h
@@ -42,6 +42,8 @@ extern "C" {
 
 struct gc_consumer;
 
+enum { GC_NAME_MAX = 64 };
+
 /** Consumer type: WAL consumer, or SNAP */
 enum gc_consumer_type {
 	GC_CONSUMER_WAL = 1,
@@ -59,7 +61,7 @@ struct gc_consumer {
 	/** Link in gc_state::consumers. */
 	gc_node_t node;
 	/** Human-readable name. */
-	char *name;
+	char name[GC_NAME_MAX];
 	/** The vclock tracked by this consumer. */
 	struct vclock vclock;
 	/** Consumer type, indicating that consumer only consumes
-- 
2.11.0




More information about the Tarantool-patches mailing list