From 70cdb5e208e33b88f2a95e45c36605753de4edd5 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Tue, 24 Apr 2007 22:17:05 +0000 Subject: [PATCH] changed prototype of union data_types * data_types_assign(NMEM nmem, union data_types * data1, union data_types data2) to union data_types * data_types_assign(NMEM nmem, union data_types ** data1, union data_types data2) to make sure that side effects are transmitted outside. added tests for side effects. added code for proper initialization of record->metadata[i] and record->sortkeys[i] - valgrind my your friend! --- src/record.c | 32 ++++++++++++++++---------------- src/record.h | 4 ++-- src/test_record.c | 8 +++++++- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/record.c b/src/record.c index d8e0ff1..016cf24 100644 --- a/src/record.c +++ b/src/record.c @@ -1,4 +1,4 @@ -/* $Id: record.c,v 1.4 2007-04-24 13:50:07 marc Exp $ +/* $Id: record.c,v 1.5 2007-04-24 22:17:05 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -19,7 +19,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: record.c,v 1.4 2007-04-24 13:50:07 marc Exp $ */ +/* $Id: record.c,v 1.5 2007-04-24 22:17:05 marc Exp $ */ #include @@ -38,26 +38,27 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA union data_types * data_types_assign(NMEM nmem, - union data_types * data1, + union data_types ** data1, union data_types data2) { // assert(nmem); - if (!data1){ + if (!*data1){ if (!nmem) return 0; else - data1 = nmem_malloc(nmem, sizeof(union data_types)); + *data1 = nmem_malloc(nmem, sizeof(union data_types)); } - *data1 = data2; - return data1; + **data1 = data2; + return *data1; } struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys) { struct record * record = 0; + int i = 0; // assert(nmem); @@ -70,15 +71,14 @@ struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys) record->metadata = nmem_malloc(nmem, sizeof(struct record_metadata*) * num_metadata); - //memset(record->metadata, 0, - //(( sizeof(struct record_metadata*) * num_metadata); + for (i = 0; i < num_metadata; i++) + record->metadata[i] = 0; record->sortkeys = nmem_malloc(nmem, sizeof(union data_types*) * num_sortkeys); - //memset(record->metadata, 0, - // sizeof(union data_types*) * num_sortkeys); - + for (i = 0; i < num_sortkeys; i++) + record->sortkeys[i] = 0; return record; } @@ -99,11 +99,11 @@ struct record_metadata * record_metadata_insert(NMEM nmem, tmp_rmd->data = data; - // insert in *rmd's place + // insert in *rmd's place, moving *rmd one down the list tmp_rmd->next = *rmd; *rmd = tmp_rmd; - return tmp_rmd; + return *rmd; } struct record_metadata * record_add_metadata_field_id(NMEM nmem, @@ -150,7 +150,7 @@ union data_types * record_assign_sortkey_field_id(NMEM nmem, if (field_id < 0 || !record || !record->sortkeys) return 0; - return data_types_assign(nmem, record->sortkeys[field_id], data); + return data_types_assign(nmem, &(record->sortkeys[field_id]), data); } @@ -168,7 +168,7 @@ union data_types * record_assign_sortkey(NMEM nmem, field_id = conf_service_sortkey_field_id(service, name); - if (!(field_id < service->num_sortkeys)) + if (!(-1 < field_id) || !(field_id < service->num_sortkeys)) return 0; return record_assign_sortkey_field_id(nmem, record, field_id, data); diff --git a/src/record.h b/src/record.h index 552450a..dec3772 100644 --- a/src/record.h +++ b/src/record.h @@ -1,4 +1,4 @@ -/* $Id: record.h,v 1.4 2007-04-23 12:33:00 marc Exp $ +/* $Id: record.h,v 1.5 2007-04-24 22:17:05 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -44,7 +44,7 @@ struct record_metadata { }; union data_types * data_types_assign(NMEM nmem, - union data_types * data1, + union data_types ** data1, union data_types data2); diff --git a/src/test_record.c b/src/test_record.c index 54317e1..b5643d0 100644 --- a/src/test_record.c +++ b/src/test_record.c @@ -1,4 +1,4 @@ -/* $Id: test_record.c,v 1.2 2007-04-24 13:50:07 marc Exp $ +/* $Id: test_record.c,v 1.3 2007-04-24 22:17:05 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -125,9 +125,15 @@ void test_record(int argc, char **argv) YAZ_CHECK(record->metadata[1]->next); + YAZ_CHECK(0 == record->sortkeys[0]); YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 0, data_text)); + YAZ_CHECK(record->sortkeys[0]); + YAZ_CHECK(0 == record->sortkeys[1]); YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 1, data_text)); + YAZ_CHECK(record->sortkeys[1]); + YAZ_CHECK(0 == record->sortkeys[2]); YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 2, data_num)); + YAZ_CHECK(record->sortkeys[2]); YAZ_CHECK(record_assign_sortkey(nmem, record, service, "relevance", data_text)); -- 1.7.10.4