Cleared up GUID construction and allocation

To allocate a GUID, use guid_malloc. To allocate and construct a guid
(which is actually unique), use guid_new (and be sure to free it using
guid_free).
This commit is contained in:
lmat 2014-07-25 14:14:09 -04:00
parent 9c82a1e9bc
commit 2a408ab9ef
5 changed files with 32 additions and 42 deletions

View File

@ -42,11 +42,12 @@ static void test_null_guid(void)
GncGUID *gp;
g = guid_new_return();
gp = guid_malloc();
guid_replace(gp);
gp = guid_new();
do_test(guid_equal(guid_null(), guid_null()), "null guids equal");
do_test(!guid_equal(&g, gp), "two guids equal");
guid_free(gp);
}
static void

View File

@ -88,19 +88,10 @@ gnc_value_get_guid (const GValue *value)
/* Memory management routines ***************************************/
GncGUID *
guid_new_ptr_return (void)
{
return reinterpret_cast<GncGUID*> (new boost::uuids::uuid);
}
/* Deprecated*/
GncGUID *
guid_malloc (void)
{
/*We need to actually construct here (not just ::operator new (size_t) -- allocate)
because in guid_replace, we assume that the object has already been constructed.
Note the boost UUID is a POD, so its constructor is trivial.*/
/*Note, the Boost uuid is a POD, so its constructor is trivial*/
return reinterpret_cast<GncGUID*> (new boost::uuids::uuid);
}
@ -156,6 +147,14 @@ guid_replace(GncGUID *guid)
new (val) boost::uuids::uuid (gen ());
}
GncGUID *
guid_new (void)
{
GncGUID * ret {guid_malloc ()};
guid_replace (ret);
return ret;
}
GncGUID
guid_new_return(void)
{

View File

@ -106,16 +106,17 @@ GncGUID guid_new_return (void);
to never reference any entity. */
const GncGUID * guid_null (void);
/** Efficiently allocate & free memory for GUIDs
* XXX This routine is deprecated. Please use guid_new_return_ptr
* instead.
*/
/**
* Allocate memory for a GUID. The returned pointer must be freed with
* guid_free.
*/
GncGUID * guid_malloc (void);
/** Allocate and initialize a new guid, and return
a pointer to it. Caller must call guid_free after to
release this pointer*/
GncGUID * guid_new_ptr_return (void);
/**
* Allocate and construct a new GUID. The returned pointer must be
* released with guid_free.
*/
GncGUID * guid_new (void);
/*Free the guid pointed to. Do not use this guid any more.*/
void guid_free (GncGUID *guid);

View File

@ -101,8 +101,7 @@ populate_frame (KvpFrame *frame)
ts.tv_sec = 1;
ts.tv_nsec = 1;
guid = guid_malloc ();
guid_replace (guid);
guid = guid_new ();
g_date_set_dmy (&gdate, 26, 1, 1957);
kvp_frame_set_gint64( frame, "gint64-type", 100 );
@ -163,8 +162,7 @@ test_kvp_frame_copy( Fixture *fixture, gconstpointer pData )
test_ts.tv_sec = 1;
test_ts.tv_nsec = 1;
test_str = "abcdefghijklmnop";
test_guid = guid_malloc();
guid_replace( test_guid );
test_guid = guid_new();
test_frame = kvp_frame_new();
g_assert( fixture->frame );
@ -223,8 +221,7 @@ test_kvp_frame_set_foo( Fixture *fixture, gconstpointer pData )
test_gnc_numeric = gnc_numeric_zero();
test_ts.tv_sec = 1;
test_ts.tv_nsec = 1;
test_guid = guid_malloc();
guid_replace( test_guid );
test_guid = guid_new();
g_assert( fixture->frame );
g_assert( kvp_frame_is_empty( fixture->frame ) );
@ -512,8 +509,7 @@ test_kvp_value_copy( void )
KvpFrame *frame_orig, *frame_copy;
gnc_numeric_orig = gnc_numeric_zero();
guid_orig = guid_malloc();
guid_replace( guid_orig );
guid_orig = guid_new();
ts_orig.tv_sec = 1;
ts_orig.tv_nsec = 1;
list_orig = NULL;
@ -631,8 +627,7 @@ test_kvp_glist_copy( void )
KvpFrame *frame_orig;
gnc_numeric_orig = gnc_numeric_zero();
guid_orig = guid_malloc();
guid_replace( guid_orig );
guid_orig = guid_new();
ts_orig.tv_sec = 1;
ts_orig.tv_nsec = 1;
list_orig = NULL;
@ -703,8 +698,7 @@ test_kvp_glist_compare( void )
KvpFrame *frame_orig;
gnc_numeric_orig = gnc_numeric_zero();
guid_orig = guid_malloc();
guid_replace( guid_orig );
guid_orig = guid_new();
ts_orig.tv_sec = 1;
ts_orig.tv_nsec = 1;
list_orig = NULL;
@ -797,10 +791,8 @@ test_kvp_value_compare( void )
gnc_numeric_orig = gnc_numeric_zero();
gnc_numeric_copy = gnc_numeric_zero();
guid_orig = guid_malloc();
guid_replace( guid_orig );
guid_copy = guid_malloc();
guid_replace( guid_copy );
guid_orig = guid_new();
guid_copy = guid_new();
ts_orig.tv_sec = 1;
ts_orig.tv_nsec = 1;
ts_copy.tv_sec = 2;
@ -1007,8 +999,7 @@ test_kvp_value_to_string( void )
KvpFrame *frame_orig;
gnc_numeric_orig = gnc_numeric_zero();
guid_orig = guid_malloc();
guid_replace( guid_orig );
guid_orig = guid_new();
ts_orig.tv_sec = 1;
ts_orig.tv_nsec = 1;
list_orig = NULL;
@ -1093,8 +1084,7 @@ test_kvp_frame_to_string( Fixture *fixture, gconstpointer pData )
KvpFrame *test_frame;
test_gnc_numeric = gnc_numeric_zero();
test_guid = guid_malloc();
guid_replace( test_guid );
test_guid = guid_new();
test_ts.tv_sec = 1;
test_ts.tv_nsec = 1;
test_frame = kvp_frame_new();

View File

@ -97,8 +97,7 @@ test_instance_set_get_guid( Fixture *fixture, gconstpointer pData )
g_assert( qof_entity_get_guid( NULL ) == guid_null() );
/* set up */
gncGuid = guid_malloc();
guid_replace( gncGuid );
gncGuid = guid_new();
g_assert( QOF_IS_INSTANCE( fixture->inst ) );
g_assert( gncGuid );