Refactor NMEM usage for conf_{service,config}.
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 7 Sep 2009 08:04:26 +0000 (10:04 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 7 Sep 2009 08:04:26 +0000 (10:04 +0200)
src/pazpar2_config.c
src/pazpar2_config.h
src/settings.c
src/test_config.c
src/test_record.c

index 40af114..6f14cff 100644 (file)
@@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "pazpar2_config.h"
 
 
-static NMEM nmem = 0;
 static char confdir[256] = ".";
 
 struct conf_config *config = 0;
@@ -96,10 +95,10 @@ struct conf_sortkey * conf_sortkey_assign(NMEM nmem,
 }
 
 
-struct conf_service * conf_service_create(NMEM nmem,
-                                          int num_metadata, int num_sortkeys)
+struct conf_service * conf_service_create(int num_metadata, int num_sortkeys)
 {
     struct conf_service * service = 0;
+    NMEM nmem = nmem_create();
 
     //assert(nmem);
     
@@ -118,13 +117,12 @@ struct conf_service * conf_service_create(NMEM nmem,
             = nmem_malloc(nmem, 
                           sizeof(struct conf_sortkey) * service->num_sortkeys);
 
-    service->nmem = 0;
+    service->nmem = nmem;
     service->dictionary = 0;
     return service; 
 }
 
-struct conf_metadata* conf_service_add_metadata(NMEM nmem, 
-                                                struct conf_service *service,
+struct conf_metadata* conf_service_add_metadata(struct conf_service *service,
                                                 int field_id,
                                                 const char *name,
                                                 enum conf_metadata_type type,
@@ -144,15 +142,14 @@ struct conf_metadata* conf_service_add_metadata(NMEM nmem,
 
     //md = &((service->metadata)[field_id]);
     md = service->metadata + field_id;
-    md = conf_metadata_assign(nmem, md, name, type, merge, setting,
+    md = conf_metadata_assign(service->nmem, md, name, type, merge, setting,
                               brief, termlist, rank, sortkey_offset,
                               mt);
     return md;
 }
 
 
-struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
-                                               struct conf_service *service,
+struct conf_sortkey * conf_service_add_sortkey(struct conf_service *service,
                                                int field_id,
                                                const char *name,
                                                enum conf_sortkey_type type)
@@ -165,7 +162,7 @@ struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
 
     //sk = &((service->sortkeys)[field_id]);
     sk = service->sortkeys + field_id;
-    sk = conf_sortkey_assign(nmem, sk, name, type);
+    sk = conf_sortkey_assign(service->nmem, sk, name, type);
 
     return sk;
 }
@@ -231,7 +228,7 @@ static struct conf_service *parse_service(xmlNode *node)
             xmlFree(sortkey);
         }
 
-    service = conf_service_create(nmem, num_metadata, num_sortkeys);    
+    service = conf_service_create(num_metadata, num_sortkeys);    
 
     for (n = node->children; n; n = n->next)
     {
@@ -374,7 +371,8 @@ static struct conf_service *parse_service(xmlNode *node)
                 }
                 sortkey_offset = sk_node;
 
-                conf_service_add_sortkey(nmem, service, sk_node,
+                conf_service_add_sortkey(
+service, sk_node,
                                          (const char *) xml_name, sk_type);
                 
                 sk_node++;
@@ -389,7 +387,7 @@ static struct conf_service *parse_service(xmlNode *node)
 
 
             // metadata known, assign values
-            conf_service_add_metadata(nmem, service, md_node,
+            conf_service_add_metadata(service, md_node,
                                       (const char *) xml_name,
                                       type, merge, setting,
                                       brief, termlist, rank, sortkey_offset,
@@ -414,7 +412,7 @@ static struct conf_service *parse_service(xmlNode *node)
     return service;
 }
 
-static char *parse_settings(xmlNode *node)
+static char *parse_settings(NMEM nmem, xmlNode *node)
 {
     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
     char *r;
@@ -439,7 +437,7 @@ static char *parse_settings(xmlNode *node)
     return r;
 }
 
-static struct conf_server *parse_server(xmlNode *node)
+static struct conf_server *parse_server(NMEM nmem, xmlNode *node)
 {
     xmlNode *n;
     struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
@@ -493,7 +491,7 @@ static struct conf_server *parse_server(xmlNode *node)
                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
                 return 0;
             }
-            if (!(server->settings = parse_settings(n)))
+            if (!(server->settings = parse_settings(nmem, n)))
                 return 0;
         }
         else if (!strcmp((const char *) n->name, "relevance"))
@@ -546,7 +544,8 @@ xsltStylesheet *conf_load_stylesheet(const char *fname)
     return xsltParseStylesheetFile((xmlChar *) path);
 }
 
-static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
+static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
+                                                        xmlNode *node)
 {
     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
@@ -584,9 +583,11 @@ static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
 
 static struct conf_config *parse_config(xmlNode *root)
 {
+    NMEM nmem = nmem_create();
     xmlNode *n;
     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
 
+    r->nmem = nmem;
     r->servers = 0;
     r->targetprofiles = 0;
 
@@ -596,7 +597,7 @@ static struct conf_config *parse_config(xmlNode *root)
             continue;
         if (!strcmp((const char *) n->name, "server"))
         {
-            struct conf_server *tmp = parse_server(n);
+            struct conf_server *tmp = parse_server(nmem, n);
             if (!tmp)
                 return 0;
             tmp->next = r->servers;
@@ -610,7 +611,7 @@ static struct conf_config *parse_config(xmlNode *root)
                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
                 return 0;
             }
-            if (!(r->targetprofiles = parse_targetprofiles(n)))
+            if (!(r->targetprofiles = parse_targetprofiles(nmem, n)))
                 return 0;
         }
         else
@@ -627,12 +628,8 @@ int read_config(const char *fname)
     xmlDoc *doc = xmlParseFile(fname);
     const char *p;
 
-    if (!nmem)  // Initialize
-    {
-        nmem = nmem_create();
-        xmlSubstituteEntitiesDefault(1);
-        xmlLoadExtDtdDefaultValue = 1;
-    }
+    xmlSubstituteEntitiesDefault(1);
+    xmlLoadExtDtdDefaultValue = 1;
     if (!doc)
     {
         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
index 8be4972..a26aeae 100644 (file)
@@ -104,11 +104,9 @@ struct conf_service
     NMEM nmem;
 };
 
-struct conf_service * conf_service_create(NMEM nmem, 
-                                          int num_metadata, int num_sortkeys);
+struct conf_service * conf_service_create(int num_metadata, int num_sortkeys);
 
-struct conf_metadata* conf_service_add_metadata(NMEM nmem, 
-                                                struct conf_service *service,
+struct conf_metadata* conf_service_add_metadata(struct conf_service *service,
                                                 int field_id,
                                                 const char *name,
                                                 enum conf_metadata_type type,
@@ -120,8 +118,7 @@ struct conf_metadata* conf_service_add_metadata(NMEM nmem,
                                                 int sortkey_offset,
                                                 enum conf_metadata_mergekey mt);
 
-struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
-                                               struct conf_service *service,
+struct conf_sortkey * conf_service_add_sortkey(struct conf_service *service,
                                                int field_id,
                                                const char *name,
                                                enum conf_sortkey_type type);
@@ -159,6 +156,7 @@ struct conf_targetprofiles
 
 struct conf_config
 {
+    NMEM nmem;
     struct conf_server *servers;
     struct conf_targetprofiles *targetprofiles;
 };
index 578bc4c..74f9d50 100644 (file)
@@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 
 #include <string.h>
+#include <assert.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include "direntz.h"
@@ -427,7 +428,7 @@ void init_settings(struct conf_service *service)
 {
     struct setting_dictionary *new;
 
-    service->nmem = nmem_create();
+    assert(service->nmem);
 
     new = nmem_malloc(service->nmem, sizeof(*new));
     memset(new, 0, sizeof(*new));
index 3d7e091..fce75c5 100644 (file)
@@ -34,43 +34,34 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 void test_conf_service(int argc, char **argv)
 {
-  NMEM         nmem = nmem_create();
-
   struct conf_service *service = 0;
-  service = conf_service_create(nmem, 4, 3);
+  service = conf_service_create(4, 3);
 
   YAZ_CHECK(service);
 
   // expected metadata failures
-  YAZ_CHECK(!conf_service_add_metadata(0, service, 0, "dead_nmem",
-                                       Metadata_type_generic,
-                                       Metadata_merge_unique,
-                                       Metadata_setting_no,
-                                       1, 1, 1, 0,
-                                       Metadata_mergekey_no));
-
-  YAZ_CHECK(!conf_service_add_metadata(nmem, 0, 0, "service_needed",
+  YAZ_CHECK(!conf_service_add_metadata(0, 0, "service_needed",
                                        Metadata_type_generic, 
                                        Metadata_merge_unique,
                                        Metadata_setting_no,
                                        1, 1, 1, 0,
                                        Metadata_mergekey_no));
 
-  YAZ_CHECK(!conf_service_add_metadata(nmem, service, -1, "out_of_bounds",
+  YAZ_CHECK(!conf_service_add_metadata(service, -1, "out_of_bounds",
                                        Metadata_type_generic,
                                        Metadata_merge_unique,
                                        Metadata_setting_no,
                                        1, 1, 1, 0,
                                        Metadata_mergekey_no));
 
-  YAZ_CHECK(!conf_service_add_metadata(nmem, service, 4, "out_of_bounds",
+  YAZ_CHECK(!conf_service_add_metadata(service, 4, "out_of_bounds",
                                        Metadata_type_generic,
                                        Metadata_merge_unique,
                                        Metadata_setting_no,
                                        1, 1, 1, 0,
                                        Metadata_mergekey_no));
 
-  YAZ_CHECK(!conf_service_add_metadata(nmem, service, 0, 0,  //missing name
+  YAZ_CHECK(!conf_service_add_metadata(service, 0, 0,  //missing name
                                        Metadata_type_generic,
                                        Metadata_merge_unique,
                                        Metadata_setting_no,
@@ -78,28 +69,28 @@ void test_conf_service(int argc, char **argv)
                                        Metadata_mergekey_no));
 
   // expected metadata sucesses
-  YAZ_CHECK(conf_service_add_metadata(nmem, service, 0, "title",
+  YAZ_CHECK(conf_service_add_metadata(service, 0, "title",
                                       Metadata_type_generic,
                                       Metadata_merge_unique,
                                        Metadata_setting_no,
                                       1, 1, 1, 0,
                                       Metadata_mergekey_no));
 
-  YAZ_CHECK(conf_service_add_metadata(nmem, service, 1, "author",
+  YAZ_CHECK(conf_service_add_metadata(service, 1, "author",
                                       Metadata_type_generic,
                                       Metadata_merge_longest,
                                       Metadata_setting_no,
                                       1, 1, 1, 0,
                                       Metadata_mergekey_no));
 
-  YAZ_CHECK(conf_service_add_metadata(nmem, service, 2, "isbn",
+  YAZ_CHECK(conf_service_add_metadata(service, 2, "isbn",
                                       Metadata_type_number,
                                       Metadata_merge_no,
                                       Metadata_setting_no,
                                       1, 1, 1, 0,
                                       Metadata_mergekey_no));
 
-  YAZ_CHECK(conf_service_add_metadata(nmem, service, 3, "year",
+  YAZ_CHECK(conf_service_add_metadata(service, 3, "year",
                                       Metadata_type_year,
                                       Metadata_merge_range,
                                       Metadata_setting_no,
@@ -108,45 +99,28 @@ void test_conf_service(int argc, char **argv)
 
 
   // expected sortkey failures
-  YAZ_CHECK(!conf_service_add_sortkey(0, service, 0, "dead_nmem",
-                                     Metadata_sortkey_relevance));
-
-  YAZ_CHECK(!conf_service_add_sortkey(nmem, 0, 0, "service_neeeded",
-                                     Metadata_sortkey_numeric));
-
-  YAZ_CHECK(!conf_service_add_sortkey(nmem, service, -1, "out_of_bounds",
+  YAZ_CHECK(!conf_service_add_sortkey(service, -1, "out_of_bounds",
                                      Metadata_sortkey_skiparticle));
 
-  YAZ_CHECK(!conf_service_add_sortkey(nmem, service, -1, "out_of_bounds",
+  YAZ_CHECK(!conf_service_add_sortkey(service, -1, "out_of_bounds",
                                      Metadata_sortkey_string));
 
-  YAZ_CHECK(!conf_service_add_sortkey(nmem, service, 3, "out_of_bounds",
+  YAZ_CHECK(!conf_service_add_sortkey(service, 3, "out_of_bounds",
                                      Metadata_sortkey_relevance));
 
-  YAZ_CHECK(!conf_service_add_sortkey(nmem, service, 0, 0, //missing name
+  YAZ_CHECK(!conf_service_add_sortkey(service, 0, 0, //missing name
                                      Metadata_sortkey_relevance));
 
 
   // expected sortkey sucess
-  YAZ_CHECK(conf_service_add_sortkey(nmem, service, 0, "relevance",
+  YAZ_CHECK(conf_service_add_sortkey(service, 0, "relevance",
                                      Metadata_sortkey_relevance));
 
-  YAZ_CHECK(conf_service_add_sortkey(nmem, service, 1, "title",
+  YAZ_CHECK(conf_service_add_sortkey(service, 1, "title",
                                      Metadata_sortkey_string));
   
-  YAZ_CHECK(conf_service_add_sortkey(nmem, service, 2, "year",
+  YAZ_CHECK(conf_service_add_sortkey(service, 2, "year",
                                      Metadata_sortkey_numeric));
-  
-
-
-
-
-
-  nmem_destroy(nmem);
-
-  //YAZ_CHECK(0 == 0);
-  //YAZ_CHECK_EQ(0, 1);
-
 }
 
 
index b55aa7c..45bf9d0 100644 (file)
@@ -53,44 +53,44 @@ void test_record(int argc, char **argv)
   data_num.number.max = 5;
 
 
-  service =  conf_service_create(nmem, 4, 3);
+  service =  conf_service_create(4, 3);
   YAZ_CHECK(service);
 
   YAZ_CHECK(conf_service_add_metadata(
-                nmem, service, 0, "title",
+                service, 0, "title",
                 Metadata_type_generic, Metadata_merge_unique,
                 Metadata_setting_no, 1, 1, 1, 0,
                 Metadata_mergekey_no
                 ));
 
   YAZ_CHECK(conf_service_add_metadata(
-                nmem, service, 1, "author",
+                service, 1, "author",
                 Metadata_type_generic, Metadata_merge_longest,
                 Metadata_setting_no,1, 1, 1, 0,
                 Metadata_mergekey_no));
 
   YAZ_CHECK(conf_service_add_metadata(
-                nmem, service, 2, "isbn",
+                service, 2, "isbn",
                 Metadata_type_number, Metadata_merge_no,
                 Metadata_setting_no, 1, 1, 1, 0,
                 Metadata_mergekey_no));
 
   YAZ_CHECK(conf_service_add_metadata(
-                nmem, service, 3, "year",
+                service, 3, "year",
                 Metadata_type_year, Metadata_merge_range,
                 Metadata_setting_no, 1, 1, 1, 0,
                 Metadata_mergekey_no));
 
   YAZ_CHECK(conf_service_add_sortkey(
-                nmem, service, 0, "relevance",
+                service, 0, "relevance",
                 Metadata_sortkey_relevance));
   
   YAZ_CHECK(conf_service_add_sortkey(
-                nmem, service, 1, "title",
+                service, 1, "title",
                 Metadata_sortkey_string));
   
   YAZ_CHECK(conf_service_add_sortkey(
-                nmem, service, 2, "year",
+                service, 2, "year",
                 Metadata_sortkey_numeric));