2008-09-12 17:06:58 +02:00
|
|
|
/* $Id$
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
*
|
|
|
|
* Lasso - A free implementation of the Liberty Alliance specifications.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2007 Entr'ouvert
|
|
|
|
* http://lasso.entrouvert.org
|
2008-09-12 17:06:58 +02:00
|
|
|
*
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
* Authors: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2008-09-12 17:06:58 +02:00
|
|
|
*
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2008-09-12 17:06:58 +02:00
|
|
|
*
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LASSO_UTILS_H__
|
|
|
|
#define __LASSO_UTILS_H__
|
|
|
|
|
2010-02-04 01:02:16 +01:00
|
|
|
#include <stdio.h>
|
2008-11-05 15:38:00 +01:00
|
|
|
#include <glib.h>
|
2010-01-04 10:13:46 +01:00
|
|
|
#include <glib-object.h>
|
2009-03-27 16:06:34 +01:00
|
|
|
#include "debug.h"
|
2009-12-16 10:28:04 +01:00
|
|
|
#include "./backward_comp.h"
|
2010-06-10 15:37:56 +02:00
|
|
|
#include "./xml/private.h"
|
|
|
|
#include "./xml/tools.h"
|
2010-06-12 02:43:49 +02:00
|
|
|
#include "./logging.h"
|
2008-11-05 15:38:00 +01:00
|
|
|
|
2009-01-24 10:33:20 +01:00
|
|
|
#ifdef LASSO_DEBUG
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define lasso_check_type_equality(a,b) \
|
|
|
|
{ \
|
|
|
|
enum { TYPE_MISMATCH = (1 / __builtin_types_compatible_p(typeof(a), typeof(b))) }; \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define lasso_check_type_equality(a,b)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define lasso_check_type_equality(a,b)
|
|
|
|
#endif
|
|
|
|
|
2009-01-24 10:33:21 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define lasso_check_type_equality2(a,b,c) \
|
2008-05-30 14:41:11 +02:00
|
|
|
{ \
|
2009-01-24 10:33:21 +01:00
|
|
|
enum { TYPE_MISMATCH = (1 / (__builtin_types_compatible_p(typeof(a), typeof(b))+__builtin_types_compatible_p(typeof(a), typeof(c)))) }; \
|
2008-05-30 14:41:11 +02:00
|
|
|
}
|
2009-01-24 10:33:21 +01:00
|
|
|
#else
|
|
|
|
#define lasso_check_type_equality2(a,b,c)
|
|
|
|
#endif
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
|
2009-08-26 17:13:55 +02:00
|
|
|
#define lasso_private_data(object) ((object)->private_data)
|
|
|
|
|
2010-02-01 20:50:00 +01:00
|
|
|
/**
|
|
|
|
* lasso_ref:
|
|
|
|
* @object: an object whose reference count must be incremented.
|
|
|
|
*
|
|
|
|
* Increment the reference count of an object, do not emit warning if it is NULL.
|
|
|
|
*
|
|
|
|
* Return value: the @object.
|
|
|
|
*/
|
|
|
|
#define lasso_ref(object) ((object) != NULL ? (g_object_ref(object), object) : NULL)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lasso_unref:
|
|
|
|
* @object: an object whose reference count must be decremented.
|
|
|
|
*
|
|
|
|
* Decrement the reference count of an object, do not emit warnings if it is NULL.
|
|
|
|
*
|
|
|
|
* Return value: the @object.
|
|
|
|
*/
|
|
|
|
#define lasso_unref(object) ((object) != NULL ? (g_object_unref(object), object) : NULL)
|
2010-01-04 10:13:46 +01:00
|
|
|
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
/* Freeing */
|
2010-02-01 20:50:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lasso_release_xxx are macros which ensure you do not get 'double free' errors, they first check
|
|
|
|
* that the variable is not NULL before calling the deallocation function, and after deallocation
|
|
|
|
* they reset the variable to NULL, preventing 'double free'.
|
|
|
|
*/
|
2008-08-05 16:52:20 +02:00
|
|
|
#define lasso_release(dest) \
|
2008-05-30 14:41:11 +02:00
|
|
|
{ \
|
|
|
|
if (dest) { \
|
|
|
|
g_free(dest); dest = NULL; \
|
|
|
|
} \
|
|
|
|
}
|
2008-08-01 16:10:34 +02:00
|
|
|
|
2008-08-05 16:52:31 +02:00
|
|
|
#define lasso_release_full(dest, free_function) \
|
2008-05-30 14:41:11 +02:00
|
|
|
{ \
|
|
|
|
if (dest) { \
|
2008-08-05 16:52:31 +02:00
|
|
|
free_function(dest); dest = NULL; \
|
2008-05-30 14:41:11 +02:00
|
|
|
} \
|
|
|
|
}
|
2008-08-01 16:10:34 +02:00
|
|
|
|
2009-01-24 10:33:20 +01:00
|
|
|
#define lasso_release_full2(dest, free_function, type) \
|
|
|
|
{ \
|
|
|
|
lasso_check_type_equality(dest, type); \
|
|
|
|
if (dest) { \
|
|
|
|
free_function(dest); dest = NULL; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2008-08-05 16:52:31 +02:00
|
|
|
#define lasso_release_gobject(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
{ \
|
|
|
|
if (G_IS_OBJECT(dest) || dest == NULL) { \
|
|
|
|
lasso_release_full(dest, g_object_unref); \
|
|
|
|
} else { \
|
2010-06-09 18:54:55 +02:00
|
|
|
message(G_LOG_LEVEL_CRITICAL, "Trying to unref a non GObject pointer file=%s:%u pointerbybname=%s pointer=%p", __FILE__, __LINE__, #dest, dest); \
|
2009-01-24 10:33:20 +01:00
|
|
|
} \
|
|
|
|
}
|
2008-08-05 16:52:31 +02:00
|
|
|
|
|
|
|
#define lasso_release_string(dest) \
|
2008-08-05 16:52:48 +02:00
|
|
|
lasso_release_full(dest, g_free)
|
|
|
|
|
|
|
|
#define lasso_release_list(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(dest, g_list_free, GList*)
|
2008-08-05 16:52:31 +02:00
|
|
|
|
2008-08-05 16:52:48 +02:00
|
|
|
#define lasso_release_list_of_full(dest, free_function) \
|
2008-05-30 14:41:11 +02:00
|
|
|
{ \
|
2010-02-17 11:14:31 +01:00
|
|
|
GList **__tmp = &(dest); \
|
|
|
|
if (*__tmp) { \
|
|
|
|
g_list_foreach(*__tmp, (GFunc)free_function, NULL); \
|
|
|
|
lasso_release_list(*__tmp); \
|
2008-05-30 14:41:11 +02:00
|
|
|
} \
|
|
|
|
}
|
2008-08-01 16:10:34 +02:00
|
|
|
|
2008-08-05 16:52:48 +02:00
|
|
|
#define lasso_release_list_of_strings(dest) \
|
|
|
|
lasso_release_list_of_full(dest, g_free)
|
|
|
|
|
2008-08-05 16:52:20 +02:00
|
|
|
#define lasso_release_list_of_gobjects(dest) \
|
2008-08-05 16:52:48 +02:00
|
|
|
lasso_release_list_of_full(dest, g_object_unref)
|
2008-08-01 16:10:34 +02:00
|
|
|
|
2009-08-26 17:13:49 +02:00
|
|
|
#define lasso_release_list_of_xml_node(dest) \
|
2008-08-05 16:52:48 +02:00
|
|
|
lasso_release_list_of_full(dest, xmlFreeNode)
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
|
2010-06-29 16:14:59 +02:00
|
|
|
#define lasso_release_list_of_xml_node_list(dest) \
|
|
|
|
lasso_release_list_of_full(dest, xmlFreeNodeList)
|
|
|
|
|
2009-01-24 10:33:21 +01:00
|
|
|
#define lasso_release_xml_node(node) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(node, xmlFreeNode, xmlNodePtr)
|
2008-08-01 16:10:34 +02:00
|
|
|
|
2010-06-29 16:15:02 +02:00
|
|
|
#define lasso_release_xml_node_list(node) \
|
|
|
|
lasso_release_full2(node, xmlFreeNodeList, xmlNodePtr)
|
|
|
|
|
2008-08-05 16:52:20 +02:00
|
|
|
#define lasso_release_doc(doc) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(doc, xmlFreeDoc, xmlDocPtr)
|
2008-08-05 16:52:31 +02:00
|
|
|
|
2009-03-27 16:05:37 +01:00
|
|
|
#define lasso_release_xml_string(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(dest, xmlFree, xmlChar*)
|
2008-08-01 16:10:34 +02:00
|
|
|
|
2008-08-05 16:52:32 +02:00
|
|
|
#define lasso_release_encrypt_context(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(dest, xmlSecEncCtxDestroy, xmlSecEncCtxPtr)
|
2008-08-05 16:52:32 +02:00
|
|
|
|
|
|
|
#define lasso_release_signature_context(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(dest, xmlSecDSigCtxDestroy,xmlSecDSigCtxPtr)
|
2008-08-05 16:52:32 +02:00
|
|
|
|
|
|
|
#define lasso_release_key_manager(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(dest, xmlSecKeysMngrDestroy, xmlSecKeysMngrPtr)
|
2008-08-05 16:52:32 +02:00
|
|
|
|
2008-11-04 02:58:42 +01:00
|
|
|
#define lasso_release_output_buffer(dest) \
|
2009-01-24 10:33:20 +01:00
|
|
|
lasso_release_full2(dest, xmlOutputBufferClose, xmlOutputBufferPtr)
|
|
|
|
|
|
|
|
#define lasso_release_xpath_object(dest) \
|
|
|
|
lasso_release_full2(dest, xmlXPathFreeObject, xmlXPathObjectPtr)
|
|
|
|
|
|
|
|
#define lasso_release_xpath_context(dest) \
|
|
|
|
lasso_release_full2(dest, xmlXPathFreeContext, xmlXPathContextPtr)
|
|
|
|
|
|
|
|
#define lasso_release_xpath_job(xpathObject, xpathContext, xmlDocument) \
|
|
|
|
lasso_release_xpath_object(xpathObject); \
|
|
|
|
lasso_release_xpath_context(xpathContext); \
|
|
|
|
lasso_release_doc(xmlDocument)
|
|
|
|
|
2009-03-27 16:06:32 +01:00
|
|
|
#define lasso_release_sec_key(dest) \
|
|
|
|
lasso_release_full2(dest, xmlSecKeyDestroy, xmlSecKeyPtr)
|
|
|
|
|
2010-04-22 02:44:15 +02:00
|
|
|
#define lasso_release_ghashtable(dest) \
|
2010-04-22 02:45:02 +02:00
|
|
|
lasso_release_full(dest, g_hash_table_destroy)
|
2010-04-22 02:44:15 +02:00
|
|
|
|
2010-04-22 02:44:19 +02:00
|
|
|
#define lasso_release_gstring(dest, b) \
|
|
|
|
{ \
|
|
|
|
GString **__tmp = &(dest); \
|
|
|
|
if (*__tmp) {\
|
|
|
|
g_string_free(*__tmp, (b)); \
|
|
|
|
*__tmp = NULL; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2009-01-24 10:33:21 +01:00
|
|
|
/* Assignment and list appending */
|
2010-02-01 20:50:00 +01:00
|
|
|
/*
|
|
|
|
* lasso_assign_xxx macros ensure that you dot leak previous value of assigned things, they use
|
|
|
|
* lasso_release_xxx macros to deallocate, they also ensure proper reference counting on passed by
|
|
|
|
* references values and proper copying on passed by value values.
|
|
|
|
*/
|
2009-01-24 10:33:21 +01:00
|
|
|
#define lasso_assign_string(dest,src) \
|
|
|
|
{ \
|
|
|
|
char *__tmp = g_strdup(src);\
|
|
|
|
lasso_release_string(dest); \
|
|
|
|
dest = __tmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_xml_string(dest,src) \
|
|
|
|
{ \
|
2010-01-04 10:13:46 +01:00
|
|
|
xmlChar *__tmp = xmlStrdup(src); \
|
2009-01-24 10:33:21 +01:00
|
|
|
lasso_release_xml_string(dest); \
|
|
|
|
dest = __tmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_new_string(dest,src) \
|
|
|
|
{ \
|
2009-03-27 16:05:35 +01:00
|
|
|
char *__tmp = src; \
|
|
|
|
if (dest != __tmp) \
|
2009-03-27 16:05:34 +01:00
|
|
|
lasso_release_string(dest); \
|
2009-03-27 16:05:35 +01:00
|
|
|
dest = __tmp; \
|
2009-01-24 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_gobject(dest,src) \
|
|
|
|
{ \
|
2009-03-27 16:05:35 +01:00
|
|
|
GObject *__tmp = G_OBJECT(src); \
|
|
|
|
if (__tmp) \
|
|
|
|
g_object_ref(__tmp); \
|
|
|
|
lasso_release_gobject(dest); \
|
|
|
|
dest = (void*)(__tmp); \
|
2009-01-24 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_new_gobject(dest,src) \
|
|
|
|
{ \
|
2009-03-27 16:05:35 +01:00
|
|
|
GObject *__tmp = G_OBJECT(src); \
|
|
|
|
if (dest != (void*)__tmp) \
|
|
|
|
lasso_release_gobject(dest); \
|
|
|
|
dest = (void*)(__tmp); \
|
2009-01-24 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_xml_node(dest,src) \
|
|
|
|
{ \
|
2009-03-27 16:05:35 +01:00
|
|
|
xmlNode *__tmp = (src); \
|
2009-01-24 10:33:21 +01:00
|
|
|
lasso_check_type_equality(dest, src); \
|
|
|
|
if (dest) \
|
|
|
|
xmlFreeNode(dest); \
|
2009-03-27 16:05:35 +01:00
|
|
|
dest = xmlCopyNode(__tmp, 1); \
|
2009-01-24 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
2010-01-28 16:31:41 +01:00
|
|
|
#define lasso_assign_new_xml_node(dest,src) \
|
|
|
|
{ \
|
|
|
|
xmlNode *__tmp = (src); \
|
|
|
|
lasso_check_type_equality(dest, src); \
|
|
|
|
if (dest) \
|
|
|
|
xmlFreeNode(dest); \
|
|
|
|
dest = __tmp; \
|
|
|
|
}
|
|
|
|
|
2010-06-29 16:14:58 +02:00
|
|
|
#define lasso_assign_xml_node_list(dest,src) \
|
|
|
|
{ \
|
|
|
|
xmlNode *__tmp = (src); \
|
|
|
|
lasso_check_type_equality(dest, src); \
|
|
|
|
if (dest) \
|
|
|
|
xmlFreeNode(dest); \
|
|
|
|
dest = xmlCopyNodeList(__tmp); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_new_xml_node_list(dest,src) \
|
|
|
|
lasso_assign_new_xml(dest, src)
|
|
|
|
|
2010-04-22 02:44:22 +02:00
|
|
|
#define lasso_assign_list(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList **__tmp = &(dest); \
|
|
|
|
if (*__tmp) \
|
|
|
|
g_list_free(*__tmp); \
|
|
|
|
*__tmp = g_list_copy((src)); \
|
|
|
|
}
|
|
|
|
|
2009-01-24 10:33:21 +01:00
|
|
|
#define lasso_assign_new_list_of_gobjects(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList *__tmp = (src); \
|
2010-01-28 16:31:41 +01:00
|
|
|
lasso_release_list_of_gobjects(dest); \
|
|
|
|
dest = (GList*)__tmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_new_list_of_strings(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList *__tmp = (src); \
|
|
|
|
lasso_release_list_of_strings(dest); \
|
|
|
|
dest = (GList*)__tmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_new_list_of_xml_node(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList *__tmp = (src); \
|
|
|
|
lasso_release_list_of_xml_node(dest); \
|
2010-01-04 10:13:46 +01:00
|
|
|
dest = (GList*)__tmp; \
|
2009-01-24 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_list_of_gobjects(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList *__tmp = (src); \
|
|
|
|
lasso_release_list_of_gobjects(dest); \
|
|
|
|
dest = g_list_copy(__tmp); \
|
|
|
|
for (;__tmp != NULL; __tmp = g_list_next(__tmp)) { \
|
|
|
|
if (G_IS_OBJECT(__tmp->data)) { \
|
|
|
|
g_object_ref(__tmp->data); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_list_of_strings(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList *__tmp = src; \
|
|
|
|
GList *__iter_dest; \
|
|
|
|
lasso_release_list_of_strings(dest); \
|
|
|
|
dest = g_list_copy(__tmp); \
|
|
|
|
for (__iter_dest = dest ; __iter_dest != NULL ; __iter_dest = g_list_next(__iter_dest)) { \
|
|
|
|
__iter_dest->data = g_strdup(__iter_dest->data); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2009-03-27 16:06:32 +01:00
|
|
|
#define lasso_assign_new_sec_key(dest, src) \
|
|
|
|
{ \
|
|
|
|
xmlSecKey *__tmp = (src); \
|
|
|
|
if (dest) \
|
|
|
|
lasso_release_sec_key(dest); \
|
|
|
|
dest = __tmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_assign_sec_key(dest, src) \
|
|
|
|
{ \
|
|
|
|
xmlSecKey *__tmp = xmlSecKeyDup(src); \
|
|
|
|
if (dest) \
|
|
|
|
lasso_release_sec_key(dest); \
|
|
|
|
dest = __tmp; \
|
|
|
|
}
|
|
|
|
|
2009-01-24 10:33:21 +01:00
|
|
|
/* List appending */
|
2010-02-01 20:50:00 +01:00
|
|
|
|
|
|
|
/* lasso_list_add_xxx macros, simplify code around list manipulation (g_list_append needs to be
|
|
|
|
* used like this 'l = g_list_appen(l, value)' ) and ensure proper reference count or copying of
|
|
|
|
* values.
|
|
|
|
*/
|
2009-01-24 10:33:21 +01:00
|
|
|
#define lasso_list_add(dest, src) \
|
|
|
|
{ \
|
|
|
|
lasso_check_type_equality((src), void*); \
|
|
|
|
dest = g_list_append(dest, (src)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_non_null(dest, src) \
|
|
|
|
{ \
|
|
|
|
void *__tmp_non_null_src = (src); \
|
|
|
|
if (__tmp_non_null_src != NULL) { \
|
|
|
|
dest = g_list_append(dest, __tmp_non_null_src); \
|
|
|
|
} else { \
|
2010-06-09 18:54:55 +02:00
|
|
|
message(G_LOG_LEVEL_CRITICAL, "Adding a NULL value to a non-NULL content list: dest=%s src=%s", #dest, #src); \
|
2009-01-24 10:33:21 +01:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_string(dest, src) \
|
|
|
|
{ \
|
|
|
|
lasso_list_add_non_null(dest, g_strdup(src));\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_new_string(dest, src) \
|
|
|
|
{ \
|
|
|
|
gchar *__tmp = src; \
|
|
|
|
lasso_list_add_non_null(dest, __tmp); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_xml_string(dest, src) \
|
|
|
|
{\
|
|
|
|
xmlChar *__tmp_src = (src);\
|
|
|
|
lasso_list_add_non_null(dest, (void*)g_strdup((char*)__tmp_src));\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_gobject(dest, src) \
|
|
|
|
{ \
|
|
|
|
void *__tmp_src = (src); \
|
|
|
|
if (G_IS_OBJECT(__tmp_src)) { \
|
|
|
|
dest = g_list_append(dest, g_object_ref(__tmp_src)); \
|
|
|
|
} else { \
|
2010-06-09 18:54:55 +02:00
|
|
|
message(G_LOG_LEVEL_CRITICAL, "Trying to add to a GList* a non GObject pointer dest=%s src=%s", #dest, #src); \
|
2009-01-24 10:33:21 +01:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_new_gobject(dest, src) \
|
|
|
|
{ \
|
|
|
|
void *__tmp_src = (src); \
|
|
|
|
if (G_IS_OBJECT(__tmp_src)) { \
|
|
|
|
dest = g_list_append(dest, __tmp_src); \
|
|
|
|
} else { \
|
2010-06-09 18:54:55 +02:00
|
|
|
message(G_LOG_LEVEL_CRITICAL, "Trying to add to a GList* a non GObject pointer dest=%s src=%s", #dest, #src); \
|
2009-01-24 10:33:21 +01:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_xml_node(dest, src) \
|
2010-01-26 00:47:43 +01:00
|
|
|
{ \
|
|
|
|
xmlNode *__tmp_src = xmlCopyNode(src, 1); \
|
|
|
|
lasso_list_add_non_null(dest, __tmp_src); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_new_xml_node(dest, src) \
|
2009-01-24 10:33:21 +01:00
|
|
|
{ \
|
|
|
|
xmlNode *__tmp_src = src; \
|
|
|
|
lasso_list_add_non_null(dest, __tmp_src); \
|
|
|
|
}
|
|
|
|
|
2010-06-29 16:14:58 +02:00
|
|
|
#define lasso_list_add_xml_node_list(dest, src) \
|
|
|
|
{ \
|
|
|
|
xmlNode *__tmp_src = xmlCopyNodeList(src); \
|
|
|
|
lasso_list_add_non_null(dest, __tmp_src); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_list_add_new_xml_node_list(dest, src) \
|
|
|
|
lasso_list_add_new_xml_node(dest, src)
|
|
|
|
|
2010-01-04 10:13:46 +01:00
|
|
|
#define lasso_list_add_gstrv(dest, src) \
|
|
|
|
{ \
|
|
|
|
GList **__tmp_dest = &(dest); \
|
|
|
|
const char **__iter = (const char**)(src); \
|
|
|
|
while (__iter && *__iter) { \
|
|
|
|
lasso_list_add_string(*__tmp_dest, *__iter); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2010-03-02 12:58:00 +01:00
|
|
|
/* List element removal */
|
|
|
|
#define lasso_list_remove_gobject(list, gobject) \
|
|
|
|
do { void *__tmp = gobject; GList **__tmp_list = &(list); \
|
|
|
|
*__tmp_list = g_list_remove(*__tmp_list, __tmp); \
|
|
|
|
lasso_unref(__tmp); } while(0)
|
|
|
|
|
2009-01-24 10:33:21 +01:00
|
|
|
/* Pointer ownership transfer */
|
2010-02-01 20:50:00 +01:00
|
|
|
|
|
|
|
/* lasso_transfer_xxx macros are like lasso_assign_xxx but they do not increment reference count or
|
|
|
|
* copy the source value, instead they steal the value (and set the source to NULL, preventing stale
|
|
|
|
* references).
|
|
|
|
*/
|
2009-01-24 10:33:20 +01:00
|
|
|
#define lasso_transfer_full(dest, src, kind) \
|
|
|
|
{\
|
|
|
|
lasso_release_##kind((dest)); \
|
|
|
|
lasso_check_type_equality(dest, src); \
|
|
|
|
(dest) = (void*)(src); \
|
|
|
|
(src) = NULL; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define lasso_transfer_xpath_object(dest, src) \
|
|
|
|
lasso_transfer_full(dest, src, xpath_object)
|
|
|
|
|
|
|
|
#define lasso_transfer_string(dest, src) \
|
|
|
|
lasso_transfer_full(dest, src, string)
|
|
|
|
|
|
|
|
#define lasso_transfer_gobject(dest, src) \
|
|
|
|
lasso_transfer_full(dest, src, gobject)
|
2008-11-04 02:58:42 +01:00
|
|
|
|
2009-03-27 16:05:06 +01:00
|
|
|
/* Node extraction */
|
|
|
|
#define lasso_extract_node_or_fail(to, from, kind, error) \
|
2009-04-27 10:19:25 +02:00
|
|
|
{\
|
|
|
|
void *__tmp = (from); \
|
|
|
|
if (LASSO_IS_##kind(__tmp)) { \
|
|
|
|
to = LASSO_##kind(__tmp); \
|
|
|
|
} else { \
|
|
|
|
rc = error; \
|
|
|
|
goto cleanup; \
|
|
|
|
}\
|
2009-03-27 16:05:06 +01:00
|
|
|
}
|
|
|
|
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
/* Bad param handling */
|
2008-08-05 16:52:26 +02:00
|
|
|
#define lasso_return_val_if_invalid_param(kind, name, val) \
|
|
|
|
g_return_val_if_fail(LASSO_IS_##kind(name), val)
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
|
2008-08-05 16:52:26 +02:00
|
|
|
#define lasso_bad_param(kind, name) \
|
|
|
|
lasso_return_val_if_invalid_param(kind, name, \
|
2008-08-01 16:10:34 +02:00
|
|
|
LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
|
|
|
|
|
2008-08-05 16:52:26 +02:00
|
|
|
#define lasso_null_param(name) \
|
2008-08-01 16:10:34 +02:00
|
|
|
g_return_val_if_fail(name != NULL, LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
|
|
|
|
|
2010-01-04 10:13:46 +01:00
|
|
|
/**
|
|
|
|
* lasso_check_non_empty_string:
|
|
|
|
* @str: a char pointer
|
|
|
|
*
|
|
|
|
* Check that @str is non-NULL and not empty, otherwise jump to cleanup and return
|
|
|
|
* LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ.
|
|
|
|
*/
|
|
|
|
#define lasso_check_non_empty_string(str) \
|
2010-10-01 12:33:24 +02:00
|
|
|
goto_cleanup_if_fail_with_rc(! lasso_strisempty(str), \
|
2010-01-04 10:13:46 +01:00
|
|
|
LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
|
|
|
|
|
2010-02-01 20:50:00 +01:00
|
|
|
/*
|
|
|
|
* We extensively use goto operator but in a formalized way, i.e. only for error checking code
|
|
|
|
* paths.
|
|
|
|
*
|
|
|
|
* The next macros goto_cleanup_xxxx encapsulate idioms used in lasso, like checking for a condition
|
|
|
|
* or setting the return code which must be called 'rc' and be of an 'int' type.
|
|
|
|
*/
|
2010-01-04 10:13:46 +01:00
|
|
|
|
2010-01-18 11:03:54 +01:00
|
|
|
/*
|
2009-04-23 01:49:29 +02:00
|
|
|
* The following macros are made to create some formalism for function's cleanup code.
|
|
|
|
*
|
|
|
|
* The exit label should be called 'cleanup'. And for functions returning an integer error code, the
|
|
|
|
* error code should be named 'rc' and 'return rc;' should be the last statement of the function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* goto_cleanup_with_rc:
|
|
|
|
* @rc_value: integer return value
|
|
|
|
*
|
|
|
|
* This macro jump to the 'cleanup' label and set the return value to @rc_value.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define goto_cleanup_with_rc(rc_value) \
|
2010-03-02 12:57:34 +01:00
|
|
|
do {\
|
2008-11-04 02:58:42 +01:00
|
|
|
rc = (rc_value); \
|
2009-04-23 01:49:29 +02:00
|
|
|
goto cleanup; \
|
2010-03-02 12:57:34 +01:00
|
|
|
} while(0);
|
2008-11-04 02:58:42 +01:00
|
|
|
|
2009-04-23 01:49:29 +02:00
|
|
|
/**
|
|
|
|
* goto_cleanup_if_fail:
|
|
|
|
* @condition: a boolean condition
|
|
|
|
*
|
|
|
|
* Jump to the 'cleanup' label if the @condition is FALSE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define goto_cleanup_if_fail(condition) \
|
|
|
|
{\
|
|
|
|
if (! (condition) ) {\
|
|
|
|
goto cleanup; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* goto_cleanup_if_fail_with_rc:
|
|
|
|
* @condition: a boolean condition
|
|
|
|
* @rc_value: integer return value
|
|
|
|
*
|
|
|
|
* Jump to the 'cleanup' label if the @condition is FALSE and set the return value to
|
|
|
|
* @rc_value.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define goto_cleanup_if_fail_with_rc(condition, rc_value) \
|
2008-08-01 16:10:34 +02:00
|
|
|
{\
|
|
|
|
if (! (condition) ) {\
|
|
|
|
rc = (rc_value); \
|
2009-04-23 01:49:29 +02:00
|
|
|
goto cleanup; \
|
2008-08-01 16:10:34 +02:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2009-04-23 01:49:29 +02:00
|
|
|
/**
|
|
|
|
* goto_cleanup_if_fail_with_rc_with_warning:
|
|
|
|
* @condition: a boolean condition
|
|
|
|
* @rc_value: integer return value
|
|
|
|
*
|
|
|
|
* Jump to the 'cleanup' label if the @condition is FALSE and set the return value to
|
|
|
|
* @rc_value. Also emit a warning, showing the condition and the return value.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define goto_cleanup_if_fail_with_rc_with_warning(condition, rc_value) \
|
2009-01-24 10:33:18 +01:00
|
|
|
{\
|
|
|
|
if (! (condition) ) {\
|
2010-06-09 18:54:55 +02:00
|
|
|
message(G_LOG_LEVEL_WARNING, "%s failed, returning %s", #condition, #rc_value);\
|
2009-01-24 10:33:18 +01:00
|
|
|
rc = (rc_value); \
|
2009-04-23 01:49:29 +02:00
|
|
|
goto cleanup; \
|
2009-01-24 10:33:18 +01:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2010-01-04 10:13:46 +01:00
|
|
|
/**
|
|
|
|
* check_good_rc:
|
|
|
|
* @what: a call to a function returning a lasso error code
|
|
|
|
*
|
|
|
|
* Check if return code is 0, if not store it in rc and jump to cleanup label.
|
|
|
|
*/
|
|
|
|
#define lasso_check_good_rc(what) \
|
|
|
|
{ \
|
|
|
|
int __rc = (what);\
|
|
|
|
goto_cleanup_if_fail_with_rc(__rc == 0, __rc); \
|
|
|
|
}
|
|
|
|
|
2009-03-27 16:06:34 +01:00
|
|
|
#define lasso_mem_debug(who, what, where) \
|
|
|
|
{ \
|
|
|
|
if (lasso_flag_memory_debug) \
|
2009-04-30 16:58:01 +02:00
|
|
|
fprintf(stderr, " freeing %s/%s (at %p)\n", who, what, (void*)where); \
|
2009-03-27 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:50:00 +01:00
|
|
|
/**
|
|
|
|
* lasso_foreach:
|
|
|
|
* @_iter: a #GList variable, which will server to traverse @_list
|
|
|
|
* @_list: a #GList value, which we will traverse
|
|
|
|
*
|
|
|
|
* Traverse a #GList list using 'for' construct. It must be followed by a block or a statement.
|
|
|
|
*/
|
2009-08-26 17:13:49 +02:00
|
|
|
#define lasso_foreach(_iter, _list) \
|
|
|
|
for (_iter = (_list); _iter; _iter = g_list_next(_iter))
|
|
|
|
|
2009-09-11 17:51:51 +02:00
|
|
|
/**
|
|
|
|
* lasso_foreach_full_begin:
|
|
|
|
* @_type: the type of the variable @_data
|
|
|
|
* @_data: the name of the variable to define to store data values
|
|
|
|
* @_iter: the name of the variable to define to store the iterator
|
|
|
|
* @_list: the GList* to iterate
|
|
|
|
*
|
|
|
|
* Traverse a GList* @_list, using @_iter as iteration variable extract data field to variable
|
|
|
|
* @_data of type @_type.
|
|
|
|
*/
|
|
|
|
#define lasso_foreach_full_begin(_type, _data, _iter, _list) \
|
|
|
|
{ \
|
|
|
|
_type _data = NULL; \
|
|
|
|
GList *_iter = NULL; \
|
|
|
|
for (_iter = (_list); _iter && ((_data = _iter->data), 1); _iter = g_list_next(_iter)) \
|
|
|
|
{
|
|
|
|
|
|
|
|
#define lasso_foreach_full_end() \
|
|
|
|
} }
|
|
|
|
|
2010-02-01 20:50:00 +01:00
|
|
|
/**
|
|
|
|
* lasso_list_get_first_child:
|
|
|
|
* @list:(allowed-none): a #GList node or NULL.
|
|
|
|
*
|
|
|
|
* Return the first child in a list, or NULL.
|
|
|
|
*/
|
2010-01-04 10:13:46 +01:00
|
|
|
#define lasso_list_get_first_child(list) \
|
|
|
|
((list) ? (list)->data : NULL)
|
|
|
|
|
2008-11-05 12:23:27 +01:00
|
|
|
/* Get a printable extract for error messages */
|
|
|
|
char* lasso_safe_prefix_string(const char *str, gsize length);
|
2010-06-09 18:54:49 +02:00
|
|
|
|
2010-01-04 10:13:46 +01:00
|
|
|
int lasso_gobject_is_of_type(GObject *a, GType b);
|
2010-06-09 18:54:49 +02:00
|
|
|
|
2010-02-22 14:30:23 +01:00
|
|
|
GObject *lasso_extract_gtype_from_list(GType type, GList *list);
|
2010-01-04 10:13:46 +01:00
|
|
|
|
2010-06-09 18:54:49 +02:00
|
|
|
GObject * lasso_extract_gtype_from_list_or_new(GType type, GList **list, gboolean create);
|
|
|
|
|
2010-01-04 10:13:46 +01:00
|
|
|
/* Get first node of this type in a list */
|
|
|
|
/* ex: lasso_extract_node (LassoNode, LASSO_TYPE_NODE, list) */
|
|
|
|
#define lasso_extract_gobject_from_list(type, gobjecttype, list) \
|
2010-02-22 14:30:23 +01:00
|
|
|
((type*) lasso_extract_gtype_from_list(gobjecttype, list))
|
2008-11-05 12:23:27 +01:00
|
|
|
|
2010-02-01 20:50:04 +01:00
|
|
|
/*
|
|
|
|
* Simplify simple accessors argument checking.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define lasso_return_val_if_fail(assertion, value) \
|
|
|
|
if (!(assertion)) return (value);
|
|
|
|
|
|
|
|
#define lasso_return_null_if_fail(assertion) \
|
|
|
|
lasso_return_val_if_fail(assertion, NULL)
|
|
|
|
|
|
|
|
#define lasso_return_if_fail(assertion) \
|
|
|
|
if (!(assertion)) return;
|
2010-02-04 01:02:16 +01:00
|
|
|
|
|
|
|
#define lasso_trace(args...) \
|
|
|
|
if (lasso_flag_memory_debug) { \
|
|
|
|
fprintf(stderr, ## args); \
|
|
|
|
}
|
|
|
|
|
2010-10-01 12:23:57 +02:00
|
|
|
/* Lasso string data helpers */
|
|
|
|
inline static gboolean
|
|
|
|
lasso_strisequal(const char *a, const char *b) {
|
|
|
|
return (g_strcmp0(a,b) == 0);
|
|
|
|
}
|
|
|
|
inline static gboolean
|
|
|
|
lasso_strisnotequal(const char *a, const char *b) {
|
|
|
|
return ! lasso_strisequal(a,b);
|
|
|
|
}
|
|
|
|
inline static gboolean
|
|
|
|
lasso_strisempty(const char *str) {
|
|
|
|
return ((str) == NULL || (str)[0] == '\0');
|
|
|
|
}
|
|
|
|
inline static gboolean
|
|
|
|
lasso_xmlstrisnotequal(const xmlChar *a, const xmlChar *b) {
|
|
|
|
return lasso_strisnotequal((char*)a, (char*)b);
|
|
|
|
}
|
|
|
|
|
add a new headers <lasso/utils.h> with macros to handle memory like assigning a
gobject field (work flow, is you augment ref of the new value, decrease the one
from the old, and then assign), assigning strings, releasing lists of strings, of
gobjects,etc..
the free macros are all called g_release_somehting and they set the released
variable to NULL.
add a macro to validate GObject parameter types
This header is intended to concentre every bit of wisdom for handling pointers
and memory, try to use thoses macros in your code.
They are not intended to work with weak references (references you do not own),
so always g_object_ref in this case (or use g_assign_gobject that does it,
when your variable is not needed anymore to g_assign_gobject(var, NULL));
2008-05-22 16:54:13 +02:00
|
|
|
#endif /* __LASSO_UTILS_H__ */
|