Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Split global option processing code into a separate file
[palacios.git] / palacios / src / palacios / vmm_options.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2013, Patrick G. Bridges <bridges@cs.unm.edu> 
11  * Copyright (c) 2013, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Patrick G. Bridges <bridges@cs.unm.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_config.h>
23 #include <palacios/vm_guest.h>
24 #include <palacios/vmm_sprintf.h>
25 #include <palacios/vmm_options.h>
26
27 /* Options are space-separated values of the form "X=Y", for example
28  * scheduler=EDF CPUs=1,2,3,4
29  * THe following code pushes them into a hashtable for each of access
30  * by other code. Storage is allocated for keys and values as part
31  * of this process. XXX Need a way to deallocate this storage if the 
32  * module is removed XXX
33  */
34 static char *option_storage;
35 static struct hashtable *option_table;
36 static char *truevalue = "true";
37
38 static uint_t option_hash_fn(addr_t key) {
39     char * name = (char *)key;
40     return v3_hash_buffer((uint8_t *)name, strlen(name));
41 }
42 static int option_eq_fn(addr_t key1, addr_t key2) {
43     char * name1 = (char *)key1;
44     char * name2 = (char *)key2;
45
46     return (strcmp(name1, name2) == 0);
47 }
48
49 void v3_parse_options(char *options)
50 {
51     char *currKey = NULL, *currVal = NULL;
52     int parseKey = 1;
53     int len;
54     char *c;
55     if (!options) {
56         return; 
57     }
58
59     len = strlen(options);
60     option_storage = V3_Malloc(len + 1);
61     strcpy(option_storage, options);
62     c = option_storage;
63
64     option_table = v3_create_htable(0, option_hash_fn, option_eq_fn);
65     while (c && *c) {
66         /* Skip whitespace */
67         if ((*c == ' ')
68             || (*c == '\t')) {
69             *c = 0;
70             if (currKey) {
71                 if (!currVal) {
72                     currVal = truevalue;
73                 }
74                 v3_htable_insert(option_table, (addr_t)currKey, (addr_t)currVal);
75                 parseKey = 1;
76                 currKey = NULL;
77                 currVal = NULL;
78             } 
79             c++;
80         } else if (parseKey) {
81             if (!currKey) {
82                 currKey = c;
83             } 
84             if (*c == '=') {
85                 parseKey = 0;
86                 *c = 0;
87             }
88             c++;
89         } else /* !parseKey */ {
90             if (!currVal) {
91                 currVal = c;
92             }
93             c++;
94         }
95     }
96     if (currKey) {
97         if (!currVal) {
98             currVal = truevalue;
99         } 
100         v3_htable_insert(option_table, (addr_t)currKey, (addr_t)currVal);
101     }
102     return;
103 }
104
105 char *v3_lookup_option(char *key) {
106     return (char *)v3_htable_search(option_table, (addr_t)(key));
107 }