[lttng-dev] [PATCH 4/6] cleanup duplicated code

Lai Jiangshan laijs at cn.fujitsu.com
Fri Dec 2 05:07:36 EST 2011


Signed-off-by: Lai Jiangshan <laijs at cn.fujitsu.com>
---
 rculfhash-internal.h |   22 ++++++++++++++++++++++
 rculfhash-mm-chunk.c |   16 ++++------------
 rculfhash-mm-mmap.c  |   19 +++++--------------
 rculfhash-mm-order.c |   16 +++-------------
 4 files changed, 34 insertions(+), 39 deletions(-)

diff --git a/rculfhash-internal.h b/rculfhash-internal.h
index 38a0317..a8a1ce4 100644
--- a/rculfhash-internal.h
+++ b/rculfhash-internal.h
@@ -152,4 +152,26 @@ extern int get_count_order_ulong(unsigned long x);
 #define poison_free(ptr)	free(ptr)
 #endif
 
+static inline
+struct cds_lfht *__default_alloc_cds_lfht(
+		const struct cds_lfht_mm_type *mm,
+		unsigned long cds_lfht_size,
+		unsigned long min_nr_alloc_buckets,
+		unsigned long max_nr_buckets)
+{
+	struct cds_lfht *ht;
+
+	ht = calloc(1, cds_lfht_size);
+	assert(ht);
+
+	ht->mm = mm;
+	ht->bucket_at = mm->bucket_at;
+	ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
+	ht->min_alloc_buckets_order =
+		get_count_order_ulong(min_nr_alloc_buckets);
+	ht->max_nr_buckets = max_nr_buckets;
+
+	return ht;
+}
+
 #endif /* _URCU_RCULFHASH_INTERNAL_H */
diff --git a/rculfhash-mm-chunk.c b/rculfhash-mm-chunk.c
index 38dde3a..a7a9b76 100644
--- a/rculfhash-mm-chunk.c
+++ b/rculfhash-mm-chunk.c
@@ -75,26 +75,18 @@ static
 struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
 		unsigned long max_nr_buckets)
 {
-	struct cds_lfht *ht;
 	unsigned long nr_chunks, cds_lfht_size;
 
 	min_nr_alloc_buckets = max(min_nr_alloc_buckets,
 				max_nr_buckets / MAX_CHUNK_TABLE);
 	nr_chunks = max_nr_buckets / min_nr_alloc_buckets;
 	cds_lfht_size = offsetof(struct cds_lfht, tbl_chunk) +
-			sizeof(ht->tbl_chunk[0]) * nr_chunks;
+			sizeof(struct cds_lfht_node *) * nr_chunks;
 	cds_lfht_size = max(cds_lfht_size, sizeof(struct cds_lfht));
-	ht = calloc(1, cds_lfht_size);
-	assert(ht);
 
-	ht->bucket_at = bucket_at;
-	ht->mm = &cds_lfht_mm_chunk;
-	ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
-	ht->min_alloc_buckets_order =
-		get_count_order_ulong(min_nr_alloc_buckets);
-	ht->max_nr_buckets = max_nr_buckets;
-
-	return ht;
+	return __default_alloc_cds_lfht(
+			&cds_lfht_mm_chunk, cds_lfht_size,
+			min_nr_alloc_buckets, max_nr_buckets);
 }
 
 const struct cds_lfht_mm_type cds_lfht_mm_chunk = {
diff --git a/rculfhash-mm-mmap.c b/rculfhash-mm-mmap.c
index dba4524..4554ed6 100644
--- a/rculfhash-mm-mmap.c
+++ b/rculfhash-mm-mmap.c
@@ -131,9 +131,9 @@ static
 struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
 		unsigned long max_nr_buckets)
 {
-	struct cds_lfht *ht;
-	unsigned long page_bucket_size = getpagesize() / sizeof(*ht->tbl_mmap);
+	unsigned long page_bucket_size;
 
+	page_bucket_size = getpagesize() / sizeof(struct cds_lfht_node);
 	if (max_nr_buckets <= page_bucket_size) {
 		/* small table */
 		min_nr_alloc_buckets = max_nr_buckets;
@@ -143,18 +143,9 @@ struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
 					page_bucket_size);
 	}
 
-	ht = calloc(1, sizeof(struct cds_lfht));
-	assert(ht);
-
-	ht->bucket_at = bucket_at;
-	ht->mm = &cds_lfht_mm_mmap;
-	ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
-	ht->min_alloc_buckets_order =
-			get_count_order_ulong(min_nr_alloc_buckets);
-	ht->max_nr_buckets = max_nr_buckets;
-
-
-	return ht;
+	return __default_alloc_cds_lfht(
+			&cds_lfht_mm_mmap, sizeof(struct cds_lfht),
+			min_nr_alloc_buckets, max_nr_buckets);
 }
 
 const struct cds_lfht_mm_type cds_lfht_mm_mmap = {
diff --git a/rculfhash-mm-order.c b/rculfhash-mm-order.c
index 9c0c70e..237d4cf 100644
--- a/rculfhash-mm-order.c
+++ b/rculfhash-mm-order.c
@@ -77,19 +77,9 @@ static
 struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
 		unsigned long max_nr_buckets)
 {
-	struct cds_lfht *ht;
-
-	ht = calloc(1, sizeof(struct cds_lfht));
-	assert(ht);
-
-	ht->bucket_at = bucket_at;
-	ht->mm = &cds_lfht_mm_order;
-	ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
-	ht->min_alloc_buckets_order =
-		get_count_order_ulong(min_nr_alloc_buckets);
-	ht->max_nr_buckets = max_nr_buckets;
-
-	return ht;
+	return __default_alloc_cds_lfht(
+			&cds_lfht_mm_order, sizeof(struct cds_lfht),
+			min_nr_alloc_buckets, max_nr_buckets);
 }
 
 const struct cds_lfht_mm_type cds_lfht_mm_order = {
-- 
1.7.4.4




More information about the lttng-dev mailing list