[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-ha-dev
Subject:    [Linux-ha-dev] [PATCH] Low: add and use UNCONST_CAST_POINTER macro
From:       Lars Ellenberg <lars.ellenberg () linbit ! com>
Date:       2010-09-28 15:01:09
Message-ID: 066f484922ec549f84ea.1285686069 () soda ! linbit
[Download RAW message or body]

# HG changeset patch
# User Lars Ellenberg <lars@linbit.com>
# Date 1285672353 -7200
# Node ID 066f484922ec549f84ea59c138d4e6cf16b2e14b
# Parent  5e309d18204045b882616fedeed7e315fc4907bf
Low: add and use UNCONST_CAST_POINTER macro to avoid -Wqual-cast warnings

This also removes unnecessary memcopy and excessive stack usage of 256k on each
invokation of cl_msg_list_add_string, called e.g. from lrmd add_rid_to_msg().

diff -r 5e309d182040 -r 066f484922ec include/lha_internal.h
--- a/include/lha_internal.h	Tue Sep 28 13:06:35 2010 +0200
+++ b/include/lha_internal.h	Tue Sep 28 13:12:33 2010 +0200
@@ -136,6 +136,15 @@
 #define	POINTER_TO_SIZE_T(p)	((size_t)(p)) /*pointer cast as size_t*/
 #define	POINTER_TO_SSIZE_T(p)	((ssize_t)(p)) /*pointer cast as ssize_t*/
 #define	POINTER_TO_ULONG(p)	((unsigned long)(p)) /*pointer cast as unsigned long*/
+	/* Sometimes we get a const g_something *, but need to pass it internally
+	 * to other functions taking a non-const g_something *, which results
+	 * with gcc and -Wcast-qual in a compile time warning, and with -Werror
+	 * even to a compile time error.
+	 * Workarounds have been to e.g. memcpy(&list, _list); or similar,
+	 * the reason of which is non-obvious to the casual reader.
+	 * This macro achieves the same, and annotates why it is done.
+	 */
+#define UNCONST_CAST_POINTER(t, p)	((t)(unsigned long)(p))
 
 #define	HAURL(url)	HA_URLBASE url
 
diff -r 5e309d182040 -r 066f484922ec lib/clplumbing/cl_msg.c
--- a/lib/clplumbing/cl_msg.c	Tue Sep 28 13:06:35 2010 +0200
+++ b/lib/clplumbing/cl_msg.c	Tue Sep 28 13:12:33 2010 +0200
@@ -951,7 +951,6 @@
 {
 	GList* list = NULL;
 	int ret;
-	char buf[MAXMSG];
 	
 	if(!msg || !name || !value){
 		cl_log(LOG_ERR, "cl_msg_list_add_string: input invalid");
@@ -959,8 +958,7 @@
 	}
 	
 	
-	strncpy(buf, value, MAXMSG);
-	list = g_list_append(list, buf);
+	list = g_list_append(list, UNCONST_CAST_POINTER(gpointer, value));
 	if (!list){
 		cl_log(LOG_ERR, "cl_msg_list_add_string: append element to"
 		       "a glist failed");
diff -r 5e309d182040 -r 066f484922ec lib/clplumbing/cl_msg_types.c
--- a/lib/clplumbing/cl_msg_types.c	Tue Sep 28 13:06:35 2010 +0200
+++ b/lib/clplumbing/cl_msg_types.c	Tue Sep 28 13:12:33 2010 +0200
@@ -168,12 +168,9 @@
 string_list_pack_length(const GList* _list)
 {
 	size_t i;
-	GList* list = NULL;
+	GList* list = UNCONST_CAST_POINTER(GList *, _list);
 	size_t total_length = 0;
 	
-	memcpy(&list, &_list, sizeof(GList*));
-	(void)list;
-
 	if (list == NULL){
 		cl_log(LOG_WARNING, "string_list_pack_length():"
 		       "list is NULL");
@@ -438,16 +435,14 @@
 {
 	size_t i;
 	GList* newlist = NULL;
-	GList* list;
-
-	memcpy(&list, &_list, sizeof(GList*));
+	GList* list = UNCONST_CAST_POINTER(GList *, _list);
 
 	for (i = 0; i < g_list_length(list); i++){
 		char* dup_element = NULL;
 		char* element = g_list_nth_data(list, i);
 		int len;
 		if (element == NULL){
-			cl_log(LOG_WARNING, "list_cleanup:"
+			cl_log(LOG_WARNING, "list_copy:"
 			       "element is NULL");
 			continue;
 		}
_______________________________________________________
Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic