vhost.c

00001 #include <klone/vhost.h>
00002 
00003 int vhost_create(vhost_t **pv)
00004 {
00005     vhost_t *v = NULL;
00006 
00007     dbg_err_if(pv == NULL);
00008 
00009     v = u_zalloc(sizeof(vhost_t));
00010     dbg_err_if(v == NULL);
00011 
00012     *pv = v;
00013 
00014     return 0;
00015 err:
00016     return ~0;
00017 
00018 }
00019 
00020 int vhost_free(vhost_t *v)
00021 {
00022     dbg_err_if(v == NULL);
00023 
00024     u_free(v);
00025 
00026     return 0;
00027 err:
00028     return ~0;
00029 }
00030 
00031 /* insert the element at the tail of the list */
00032 int vhost_list_add(vhost_list_t *vs, vhost_t *vhost)
00033 {
00034     vhost_t *first, *last, *elm;
00035 
00036     last = NULL;
00037     LIST_FOREACH(elm, vs, np)
00038         last = elm;
00039 
00040     if(last) 
00041     {
00042         LIST_INSERT_AFTER(last, vhost, np);
00043         vhost->id = last->id + 1;
00044     } else {
00045         LIST_INSERT_HEAD(vs, vhost, np);
00046         vhost->id = 0;
00047     }
00048 
00049     return 0;
00050 err:
00051     return ~0;
00052 }
00053 
00054 vhost_t* vhost_list_get(vhost_list_t *vs, const char *host)
00055 {
00056     vhost_t *item;
00057 
00058     LIST_FOREACH(item, vs, np)
00059     {
00060         if(strcmp(item->host, host) == 0)
00061             return item;  /* found */
00062     }
00063 
00064     return NULL; /* not found */
00065 }
00066 
00067 vhost_t* vhost_list_get_n(vhost_list_t *vs, int n)
00068 {
00069     vhost_t *item;
00070 
00071     LIST_FOREACH(item, vs, np)
00072     {
00073         if(n-- == 0)
00074             return item;
00075     }
00076 
00077     return NULL; /* not found */
00078 }
00079 
00080 int vhost_list_create(vhost_list_t **pvs)
00081 {
00082     vhost_list_t *vs = NULL;
00083 
00084     dbg_err_if(pvs == NULL);
00085 
00086     vs = u_zalloc(sizeof(vhost_list_t));
00087     dbg_err_if(vs == NULL);
00088 
00089     LIST_INIT(vs);
00090 
00091     *pvs = vs;
00092 
00093     return 0;
00094 err:
00095     if(pvs)
00096         u_free(pvs);
00097     return ~0;
00098 }
00099 
00100 int vhost_list_free(vhost_list_t *vs)
00101 {
00102     dbg_err_if(vs == NULL);
00103 
00104     u_free(vs);
00105 
00106     return 0;
00107 err:
00108     return ~0;
00109 }
00110 

←Products
Copyright © 2005-2012 - KoanLogic S.r.l. - All rights reserved