resource: add source files
authorJanos Kovacs <jankovac503@gmail.com>
Tue, 11 Sep 2012 00:20:54 +0000 (03:20 +0300)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Fri, 26 Oct 2012 16:03:51 +0000 (19:03 +0300)
16 files changed:
src/resource/attribute.c [new file with mode: 0644]
src/resource/attribute.h [new file with mode: 0644]
src/resource/data-types.h [new file with mode: 0644]
src/resource/resource-api.h [new file with mode: 0644]
src/resource/resource-class.c [new file with mode: 0644]
src/resource/resource-class.h [new file with mode: 0644]
src/resource/resource-client.c [new file with mode: 0644]
src/resource/resource-client.h [new file with mode: 0644]
src/resource/resource-owner.c [new file with mode: 0644]
src/resource/resource-owner.h [new file with mode: 0644]
src/resource/resource-set.c [new file with mode: 0644]
src/resource/resource-set.h [new file with mode: 0644]
src/resource/resource.c [new file with mode: 0644]
src/resource/resource.h [new file with mode: 0644]
src/resource/zone.c [new file with mode: 0644]
src/resource/zone.h [new file with mode: 0644]

diff --git a/src/resource/attribute.c b/src/resource/attribute.c
new file mode 100644 (file)
index 0000000..af2bd5b
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+
+#include "attribute.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n", ## args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n", ## args) 
+
+static mrp_attr_value_t *get_attr_value_from_list(mrp_attr_def_t *,
+                                                  const char*,mqi_data_type_t);
+
+
+int mrp_attribute_copy_definitions(mrp_attr_def_t *from, mrp_attr_def_t *to)
+{
+    mrp_attr_def_t *s, *d;
+
+    MRP_ASSERT(to,"invalid argument");
+
+    if (from) {
+        for (s = from, d = to;   s->name;   s++, d++) {
+
+            if (!(d->name = mrp_strdup(s->name)))
+                goto no_memory;
+            
+            if ((d->type = s->type) != mqi_string)
+                d->value = s->value;
+            else {
+                if (!(d->value.string = mrp_strdup(s->value.string))) {
+                    mrp_free((void *)d->name);
+                    memset(d, 0, sizeof(*d));
+                    goto no_memory;
+                }
+            }
+        }
+    }
+            
+    return 0;
+
+ no_memory:
+    mrp_log_error("Memory alloc failure. Can't copy attribute definition");
+    return -1;
+}
+
+
+int mrp_attribute_set_values(mrp_attr_def_t   *values,
+                             uint32_t          nattr,
+                             mrp_attr_def_t   *defs,
+                             mrp_attr_value_t *attrs)
+{
+    mrp_attr_def_t *adef;
+    mrp_attr_value_t *vsrc;
+    mrp_attr_value_t *vdst;
+    uint32_t i;
+
+    
+    for (i = 0;  i < nattr;  i++) {
+        adef = defs  + i;
+        vdst = attrs + i;
+
+        if (!(vsrc = get_attr_value_from_list(values, adef->name, adef->type)))
+            vsrc = &adef->value; /* default value */
+
+        if (adef->type !=  mqi_string)
+            *vdst = *vsrc;
+        else {
+            if (!(vdst->string = mrp_strdup(vsrc->string)))
+                return -1;
+        }
+    }
+
+    return 0;
+}
+
+
+int mrp_attribute_print(uint32_t          nattr,
+                        mrp_attr_def_t   *adefs,
+                        mrp_attr_value_t *avals,
+                        char             *buf,
+                        int               len)
+{
+#define PRINT(fmt, args...)  if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
+    
+    mrp_attr_def_t *adef;
+    mrp_attr_value_t *aval;
+    uint32_t i;
+    char *p, *e;
+
+    MRP_ASSERT(adefs && avals && buf && len > 0, "invalid argument");
+
+    e = (p = buf) + len;
+
+    for (i = 0;  i < nattr;  i++) {
+        adef = adefs + i;
+        aval = avals + i;
+
+        PRINT(" %s:", adef->name);
+
+        switch (adef->type) {
+        case mqi_string:    PRINT("'%s'", aval->string  );   break;
+        case mqi_integer:   PRINT("%d"  , aval->integer );   break;
+        case mqi_unsignd:   PRINT("%u"  , aval->unsignd );   break;
+        case mqi_floating:  PRINT("%lf" , aval->floating);   break;
+        default:            PRINT(" <unsupported type>" );   break;
+        }
+
+    }
+
+    return p - buf;
+
+#undef PRINT
+}
+
+
+static mrp_attr_value_t *get_attr_value_from_list(mrp_attr_def_t *list,
+                                                  const char     *name,
+                                                  mqi_data_type_t type)
+{
+    mrp_attr_def_t *adef;
+
+    MRP_ASSERT(name, "invalid argument");
+
+    if (list) {
+        for (adef = list;   adef->name;   adef++) {
+            if (!strcasecmp(name, adef->name) && type == adef->type)
+                return &adef->value;
+        }
+    }
+        
+    return NULL;
+}
+
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/attribute.h b/src/resource/attribute.h
new file mode 100644 (file)
index 0000000..e21d9f9
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_ATTRIBUTE_H__
+#define __MURPHY_ATTRIBUTE_H__
+
+#include "data-types.h"
+
+
+int mrp_attribute_copy_definitions(mrp_attr_def_t *, mrp_attr_def_t *);
+int mrp_attribute_set_values(mrp_attr_def_t *, uint32_t, mrp_attr_def_t *,
+                             mrp_attr_value_t *);
+
+int mrp_attribute_print(uint32_t, mrp_attr_def_t *, mrp_attr_value_t *,
+                        char *, int);
+
+
+#endif  /* __MURPHY_ATTRIBUTE_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/data-types.h b/src/resource/data-types.h
new file mode 100644 (file)
index 0000000..ec2aba7
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_DATA_TYPES_H__
+#define __MURPHY_DATA_TYPES_H__
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <murphy-db/mqi-types.h>
+
+#define MRP_ZONE_MAX            8
+
+#define MRP_KEY_STAMP_BITS      27
+#define MRP_KEY_REQUEST_BITS    1
+#define MRP_KEY_USAGE_BITS      1
+#define MRP_KEY_PRIORITY_BITS   3
+
+#define MRP_ZONE_ID_INVALID        (~(uint32_t)0)
+#define MRP_RESOURCE_ID_INVALID    (~(uint32_t)0)
+#define MRP_RESOURCE_REQNO_INVALID (~(uint32_t)0)
+
+
+typedef struct mrp_resource_client_s   mrp_resource_client_t;
+typedef union  mrp_attr_value_u        mrp_attr_value_t;
+typedef struct mrp_attr_def_s          mrp_attr_def_t;
+typedef struct mrp_zone_def_s          mrp_zone_def_t;
+typedef struct mrp_zone_s              mrp_zone_t;
+typedef struct mrp_resource_owner_s    mrp_resource_owner_t;
+typedef struct mrp_resource_class_s    mrp_resource_class_t;
+typedef struct mrp_resource_set_s      mrp_resource_set_t;
+typedef struct mrp_resource_def_s      mrp_resource_def_t;
+typedef struct mrp_resource_s          mrp_resource_t;
+typedef uint32_t                       mrp_resource_mask_t;
+
+union mrp_attr_value_u {
+    const char  *string;
+    int32_t      integer;
+    uint32_t     unsignd;
+    double       floating;
+};
+
+struct mrp_attr_def_s {
+    const char      *name;
+    mqi_data_type_t  type;
+    mrp_attr_value_t value;
+};
+
+
+#endif  /* __MURPHY_DATA_TYPES_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-api.h b/src/resource/resource-api.h
new file mode 100644 (file)
index 0000000..b75e4cb
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_RESOURCE_API_H__
+#define __MURPHY_RESOURCE_API_H__
+
+#include <resource/data-types.h>
+
+mrp_resource_client_t *mrp_resource_client_create(const char *name,
+                                                  void *user_data);
+void mrp_resource_client_destroy(mrp_resource_client_t *client);
+
+int mrp_zone_definition_create(mrp_attr_def_t *attrdefs);
+uint32_t mrp_zone_create(const char *name, mrp_attr_def_t *attrs);
+
+mrp_resource_class_t *mrp_resource_class_create(const char *name,
+                                                uint32_t priority);
+void mrp_resource_class_add_resource_set(mrp_resource_class_t *class,
+                                         uint32_t zone_id,
+                                         mrp_resource_set_t *resource_set);
+int mrp_resource_class_print(char *buf, int len);
+
+uint32_t mrp_resource_definition_create(const char *name,
+                                        bool shareable,
+                                        mrp_attr_def_t *attrdefs);
+mrp_resource_t *mrp_resource_create(const char *name, bool shared,
+                                    mrp_attr_def_t *attrs);
+
+mrp_resource_set_t *mrp_resource_set_create(uint32_t client_id,
+                                            void *client_data,
+                                            uint32_t priority);
+int mrp_resource_set_add_resource(mrp_resource_set_t *resource_set,
+                                  const char *resource_name,
+                                  bool shared,
+                                  mrp_attr_def_t *attrs,
+                                  bool mandatory);
+void mrp_resource_set_acquire(mrp_resource_set_t *resource_set);
+void mrp_resource_set_release(mrp_resource_set_t *resource_set);
+
+
+int mrp_resource_owner_print(char *buf, int len);
+
+
+
+#endif  /* __MURPHY_RESOURCE_API_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-class.c b/src/resource/resource-class.c
new file mode 100644 (file)
index 0000000..e97591f
--- /dev/null
@@ -0,0 +1,365 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+#include <murphy/common/hashtbl.h>
+#include <murphy/common/utils.h>
+
+#include <resource/resource-api.h>
+
+#include "resource-class.h"
+#include "resource-set.h"
+#include "zone.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n" , ##args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n" , ##args) 
+
+
+/*
+ * sorting key bit layout
+ * 
+ * +---------+----+----+--------+
+ * | 31 - 29 | 28 | 27 | 26 - 0 |  
+ * +---------+----+----+--------+
+ *      |      |    |       |
+ *      |      |    |       +---- 0x07ffffff stamp of the last request
+ *      |      |    +------------ 0x08000000 request (set if acquiring)
+ *      |      +----------------- 0x10000000 usage (set if shared)
+ *      +------------------------ 0xe0000000 priority (0-7)
+ */
+#define MASK(b)   (((uint32_t)1 << (b)) - (uint32_t)1)
+
+#define STAMP_SHIFT     0
+#define REQUEST_SHIFT   (STAMP_SHIFT   + MRP_KEY_STAMP_BITS)
+#define USAGE_SHIFT     (REQUEST_SHIFT + MRP_KEY_REQUEST_BITS)
+#define PRIORITY_SHIFT  (USAGE_SHIFT   + MRP_KEY_USAGE_BITS)
+
+#define STAMP_MASK      MASK(MRP_KEY_STAMP_BITS)
+#define REQUEST_MASK    MASK(MRP_KEY_REQUEST_BITS)
+#define USAGE_MASK      MASK(MRP_KEY_USAGE_BITS)
+#define PRIORITY_MASK   MASK(MRP_KEY_PRIORITY_BITS)
+
+#define STAMP_KEY(p)    (((uint32_t)(p) & STAMP_MASK)    << STAMP_SHIFT)
+#define REQUEST_KEY(p)  (((uint32_t)(p) & REQUEST_MASK)  << REQUEST_SHIFT)
+#define USAGE_KEY(p)    (((uint32_t)(p) & USAGE_MASK)    << USAGE_SHIFT)
+#define PRIORITY_KEY(p) (((uint32_t)(p) & PRIORITY_MASK) << PRIORITY_SHIFT)
+
+
+static MRP_LIST_HOOK(class_list);
+static mrp_htbl_t *name_hash;
+
+static void init_name_hash(void);
+static int  add_to_name_hash(mrp_resource_class_t *);
+#if 0
+static void remove_from_name_hash(mrp_resource_class_t *);
+#endif
+
+
+mrp_resource_class_t *mrp_resource_class_create(const char *name, uint32_t pri)
+{
+    mrp_resource_class_t *class;
+    mrp_list_hook_t *insert_before, *clhook, *n;
+    uint32_t zone;
+
+    MRP_ASSERT(name, "invalid argument");
+
+
+    /* looping through all classes to check the uniqueness of the
+       name & priority of the new class and find the insertion point */
+    insert_before = &class_list;
+
+    mrp_list_foreach_back(&class_list, clhook, n) {
+        class = mrp_list_entry(clhook, mrp_resource_class_t, list);
+
+        if (!strcasecmp(name, class->name)) {
+            mrp_log_warning("Multiple definitions for class '%s'", name);
+            return NULL;
+        }
+
+        if (pri == class->priority) {
+            mrp_log_error("Priority clash. Classes '%s' and '%s' would have "
+                          "the same priority", name, class->name);
+        }
+
+        if (pri < class->priority)
+            insert_before = &class->list;
+    }
+
+    if (!(class = mrp_allocz(sizeof(mrp_resource_class_t)))) {
+        mrp_log_error("Memory alloc failure. Can't create resource class '%s'",
+                      name);
+        return NULL;
+    }
+
+    class->name = mrp_strdup(name);
+    class->priority = pri;
+
+    for (zone = 0;  zone < MRP_ZONE_MAX;  zone++)
+        mrp_list_init(&class->resource_sets[zone]);
+
+    /* list do not have insert_before function,
+       so don't be mislead by the name */
+    mrp_list_append(insert_before, &class->list); 
+
+    add_to_name_hash(class);
+
+    return class;
+}
+
+
+mrp_resource_class_t *mrp_resource_class_find(const char *name)
+{
+    mrp_resource_class_t *class = NULL;
+
+    if (name_hash && name)
+        class = mrp_htbl_lookup(name_hash, (void *)name);
+
+    return class;
+}
+
+mrp_resource_class_t *mrp_resource_class_iterate_classes(void **cursor)
+{
+    mrp_list_hook_t *entry;
+
+    MRP_ASSERT(cursor, "invalid argument");
+
+    entry = (*cursor == NULL) ? class_list.prev : (mrp_list_hook_t *)*cursor;
+
+    if (entry == &class_list)
+        return NULL;
+    *cursor = entry->prev;
+
+    return mrp_list_entry(entry, mrp_resource_class_t, list);
+}
+
+mrp_resource_set_t *
+mrp_resource_class_iterate_rsets(mrp_resource_class_t *class,
+                                 uint32_t              zone,
+                                 void                **cursor)
+{
+    mrp_list_hook_t *list, *entry;
+
+    MRP_ASSERT(class && zone < MRP_ZONE_MAX && cursor, "invalid argument");
+
+    list  = class->resource_sets + zone;
+    entry = (*cursor == NULL) ? list->prev : (mrp_list_hook_t *)*cursor;
+
+    if (entry == list)
+        return NULL;
+
+    *cursor = entry->prev;
+    
+    return mrp_list_entry(entry, mrp_resource_set_t, class.list);
+}
+
+
+void mrp_resource_class_add_resource_set(mrp_resource_class_t *class,
+                                         uint32_t zone,
+                                         mrp_resource_set_t *rset)
+{
+    MRP_ASSERT(class && rset && zone < MRP_ZONE_MAX, "invalid argument");
+    MRP_ASSERT(!rset->class.ptr || !mrp_list_empty(&rset->class.list),
+               "attempt to add multiple times the same resource set");
+
+    rset->class.ptr = class;
+    rset->zone = zone;
+
+    mrp_resource_class_move_resource_set(rset);
+}
+
+void mrp_resource_class_move_resource_set(mrp_resource_set_t *rset)
+{
+    mrp_resource_class_t *class;
+    mrp_list_hook_t *list, *lentry, *n, *insert_before;
+    mrp_resource_set_t *rentry;
+    uint32_t key;
+    uint32_t zone;
+
+    MRP_ASSERT(rset, "invalid argument");
+
+    mrp_list_delete(&rset->class.list);
+
+    class = rset->class.ptr;
+    zone  = rset->zone;
+
+    list = insert_before = class->resource_sets + zone;
+    key  = mrp_resource_class_get_sorting_key(rset);
+
+    mrp_list_foreach_back(list, lentry, n) {
+        rentry = mrp_list_entry(lentry, mrp_resource_set_t, class.list);
+
+         if (key >= mrp_resource_class_get_sorting_key(rentry))
+             break;
+
+         insert_before = lentry;
+    }
+
+    mrp_list_append(insert_before, &rset->class.list);
+}
+
+uint32_t mrp_resource_class_get_sorting_key(mrp_resource_set_t *rset)
+{
+    uint32_t priority;
+    uint32_t usage;
+    uint32_t request;
+    uint32_t stamp;
+    uint32_t key;
+
+    MRP_ASSERT(rset, "invalid argument");
+
+    priority = PRIORITY_KEY(rset->class.priority);
+    usage    = USAGE_KEY(rset->resource.share ? 1 : 0);
+    request  = REQUEST_KEY(rset->request.type == mrp_resource_acquire ? 1 : 0);
+    stamp    = STAMP_KEY(rset->request.stamp);
+
+    key = priority | usage | request | stamp;
+
+    return key;
+}
+
+
+int mrp_resource_class_print(char *buf, int len)
+{
+#define PRINT(fmt, args...)  if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
+
+    mrp_zone_t *zone;
+    mrp_resource_class_t *class;
+    mrp_resource_set_t *rset;
+    mrp_list_hook_t *clen, *n;
+    mrp_list_hook_t *list, *rsen, *m;
+    uint32_t zid;
+    char *p, *e;
+    int clcnt, rscnt;
+
+    MRP_ASSERT(buf && len > 0, "invalid argument");
+
+    e = (p = buf) + len;
+    clcnt = rscnt = 0;
+
+    PRINT("Resource classes:\n");
+
+    mrp_list_foreach_back(&class_list, clen, n) {
+        class = mrp_list_entry(clen, mrp_resource_class_t, list);
+        PRINT("  %3u - %s\n", class->priority, class->name);
+
+        for (zid = 0;   zid < MRP_ZONE_MAX;   zid++) {
+            zone = mrp_zone_find_by_id(zid);
+            list = class->resource_sets + zid;
+
+            if (!mrp_list_empty(list)) {
+                if (!zone) {
+                    PRINT("           Resource-sets in zone %u:\n", zid);
+                }
+                else {
+                    PRINT("           Resource-sets in %s zone:", zone->name);
+                    p += mrp_zone_attribute_print(zone, p, e-p);
+                    PRINT("\n");
+                }
+
+                mrp_list_foreach_back(list, rsen, m) {
+                    rset = mrp_list_entry(rsen, mrp_resource_set_t,class.list);
+                    p += mrp_resource_set_print(rset, 13, p, e-p);
+                }
+            }
+        }
+
+        clcnt++;
+    }
+
+    if (!clcnt)
+        PRINT("   <none>\n");
+
+    return p - buf;
+
+#undef PRINT
+}
+
+
+static void init_name_hash(void)
+{
+    mrp_htbl_config_t  cfg;
+
+    if (!name_hash) {
+        cfg.nentry  = 32;
+        cfg.comp    = mrp_string_comp;
+        cfg.hash    = mrp_string_hash;
+        cfg.free    = NULL;
+        cfg.nbucket = cfg.nentry / 2;
+
+        name_hash = mrp_htbl_create(&cfg);
+
+        MRP_ASSERT(name_hash, "failed to make name_hash for resource classes");
+    }
+}
+
+
+static int add_to_name_hash(mrp_resource_class_t *class)
+{
+    MRP_ASSERT(class && class->name, "invalid argument");
+    
+    init_name_hash();
+
+    if (!mrp_htbl_insert(name_hash, (void *)class->name, class))
+        return -1;
+
+    return 0;
+}
+
+#if 0
+static void remove_from_name_hash(mrp_resource_class_t *class)
+{
+    mrp_resource_class_t *deleted;
+
+    if (class && class->name && name_hash) {
+        deleted = mrp_htbl_remove(name_hash, (void *)class->name, false);
+
+        MRP_ASSERT(deleted == class, "confused with data structures when "
+                   "deleting resource-class from name hash");
+
+        /* in case we were not compiled with debug enabled */
+        if (deleted != class) {
+            mrp_log_error("confused with data structures when deleting "
+                          "resource-class '%s' from name hash", class->name);
+        }
+    }
+}
+#endif
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-class.h b/src/resource/resource-class.h
new file mode 100644 (file)
index 0000000..71f88ff
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_RESOURCE_CLASS_H__
+#define __MURPHY_RESOURCE_CLASS_H__
+
+#include <murphy/common/list.h>
+
+#include "data-types.h"
+
+
+
+struct mrp_resource_class_s {
+    mrp_list_hook_t  list;
+    const char      *name;
+    uint32_t         priority;
+    mrp_list_hook_t  resource_sets[MRP_ZONE_MAX];
+};
+
+mrp_resource_class_t *mrp_resource_class_find(const char *);
+mrp_resource_class_t *mrp_resource_class_iterate_classes(void **);
+mrp_resource_set_t   *mrp_resource_class_iterate_rsets(mrp_resource_class_t *,
+                                                       uint32_t, void **);
+
+void     mrp_resource_class_move_resource_set(mrp_resource_set_t *);
+
+uint32_t mrp_resource_class_get_sorting_key(mrp_resource_set_t *);
+
+
+#endif  /* __MURPHY_RESOURCE_CLASS_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-client.c b/src/resource/resource-client.c
new file mode 100644 (file)
index 0000000..05da827
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+
+#include <resource/resource-api.h>
+
+#include "resource-client.h"
+#include "resource-set.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n", ## args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n", ## args) 
+
+
+static MRP_LIST_HOOK(client_list);
+
+
+mrp_resource_client_t *mrp_resource_client_create(const char *name,
+                                                  void *user_data)
+{
+    mrp_resource_client_t *client;
+    const char *dup_name;
+
+    MRP_ASSERT(name, "invalid argument");
+
+    if (!(client = mrp_allocz(sizeof(*client))) ||
+        !(dup_name = mrp_strdup(name)))
+    {
+        mrp_log_error("Memory alloc failure. Can't create client '%s'", name);
+        return NULL;
+    }
+
+    client->name = dup_name;
+    client->user_data = user_data;
+    mrp_list_init(&client->resource_sets);
+
+    mrp_list_append(&client_list, &client->list);
+
+    return client;
+}
+
+void mrp_resource_client_destroy(mrp_resource_client_t *client)
+{
+    mrp_list_hook_t *entry, *n;
+    mrp_resource_set_t *rset;
+
+    if (client) {
+        mrp_list_delete(&client->list);
+
+        mrp_list_foreach(&client->resource_sets, entry, n) {
+            rset = mrp_list_entry(entry, mrp_resource_set_t, client.list);
+        }
+
+        mrp_free(client);
+    }
+}
+
+
+
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-client.h b/src/resource/resource-client.h
new file mode 100644 (file)
index 0000000..7d8369b
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_RESOURCE_CLIENT_H__
+#define __MURPHY_RESOURCE_CLIENT_H__
+
+#include <murphy/common/list.h>
+
+#include "data-types.h"
+
+
+struct mrp_resource_client_s {
+    mrp_list_hook_t  list;
+    const char      *name;
+    void            *user_data;
+    mrp_list_hook_t  resource_sets;
+};
+
+
+
+#endif  /* __MURPHY_RESOURCE_CLIENT_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-owner.c b/src/resource/resource-owner.c
new file mode 100644 (file)
index 0000000..35dc2f8
--- /dev/null
@@ -0,0 +1,293 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+#include <murphy/common/hashtbl.h>
+#include <murphy/common/utils.h>
+
+#include <resource/resource-api.h>
+
+#include "resource-owner.h"
+#include "resource-class.h"
+#include "resource-set.h"
+#include "resource.h"
+#include "zone.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n" , ##args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n" , ##args) 
+
+#define RESOURCE_MAX   (sizeof(mrp_resource_mask_t) * 8)
+
+static mrp_resource_owner_t  resource_owners[MRP_ZONE_MAX * RESOURCE_MAX];
+
+static mrp_resource_owner_t *get_owner(uint32_t, uint32_t);
+static void reset_owners(uint32_t, mrp_resource_owner_t *);
+static bool grant_ownership(mrp_resource_owner_t *, mrp_resource_class_t *,
+                            mrp_resource_set_t *, mrp_resource_t *, bool);
+
+
+void mrp_resource_owner_update_zone(uint32_t zone)
+{
+    mrp_resource_owner_t oldowners[RESOURCE_MAX];
+    mrp_resource_owner_t backup[RESOURCE_MAX];
+    mrp_resource_class_t *class;
+    mrp_resource_set_t *rset;
+    mrp_resource_t *res;
+    mrp_resource_def_t *rdef;
+    mrp_resource_owner_t *owner, *old;
+    mrp_resource_mask_t mandatory;
+    mrp_resource_mask_t grant;
+    mrp_resource_mask_t advice;
+    void *clc, *rsc, *rc;
+    uint32_t rid;
+    uint32_t rcnt;
+
+    MRP_ASSERT(zone < MRP_ZONE_MAX, "invalid argument");
+
+    reset_owners(zone, oldowners);
+
+    rcnt = mrp_resource_definition_count();
+    clc  = NULL;
+
+    while ((class = mrp_resource_class_iterate_classes(&clc))) {
+        rsc = NULL;
+
+        while ((rset = mrp_resource_class_iterate_rsets(class,zone,&rsc))) {
+            mandatory = rset->resource.mask.mandatory;
+            grant = 0;
+            advice = 0;
+            rc = NULL;
+
+            switch (rset->request.type) {
+
+            case mrp_resource_acquire:
+                while ((res = mrp_resource_set_iterate_resources(rset, &rc))) {
+                    rdef  = res->def;
+                    rid   = rdef->id;
+                    owner = get_owner(zone, rid); 
+                    
+                    backup[rid] = *owner;
+                    
+                    if (grant_ownership(owner, class, rset, res, false))
+                        grant |= ((uint32_t)1 << rid);
+                }
+                if (mandatory && (grant & mandatory) != mandatory) {
+                    grant = 0;
+                    rc = NULL;
+
+                    /* rollback, ie. restore the backed up state */
+                    while ((res=mrp_resource_set_iterate_resources(rset,&rc))){
+                         rdef  = res->def;
+                         rid   = rdef->id;
+                         owner = get_owner(zone, rid);
+                        *owner = backup[rid]; 
+                    }
+                }
+                advice = grant;
+                break;
+
+            case mrp_resource_release:
+                while ((res = mrp_resource_set_iterate_resources(rset, &rc))) {
+                    rdef  = res->def;
+                    rid   = rdef->id;
+                    owner = get_owner(zone, rid); 
+
+                    if (grant_ownership(owner, class, rset, res, true))
+                        advice |= ((uint32_t)1 << rid);
+                }
+                if (mandatory && (advice & mandatory) != mandatory)
+                    advice = 0;
+                break;
+
+            default:
+                break;
+            }
+
+            if (grant != rset->resource.mask.grant) {
+                rset->resource.mask.grant = grant;
+            }
+
+            if (advice != rset->resource.mask.advice) {
+                rset->resource.mask.advice = advice;
+            }
+
+        } /* while rset */
+    } /* while class */
+
+    for (rid = 0;  rid < rcnt;  rid++) {
+        owner = get_owner(zone, rid);
+        old   = oldowners + rid;
+
+        if (owner->class != old->class ||
+            owner->rset  != old->rset  ||
+            owner->res   != old->res     )
+        {
+            
+        }
+    }    
+}
+
+int mrp_resource_owner_print(char *buf, int len)
+{
+#define PRINT(fmt, args...)  if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
+
+    mrp_zone_t *zone;
+    mrp_resource_owner_t *owner;
+    mrp_resource_class_t *class;
+    mrp_resource_set_t *rset;
+    mrp_resource_t *res;
+    mrp_resource_def_t *rdef;
+    uint32_t rcnt, rid;
+    uint32_t zcnt, zid;
+    char *p, *e;
+
+    MRP_ASSERT(buf && len > 0, "invalid argument");
+
+    rcnt = mrp_resource_definition_count();
+    zcnt = mrp_zone_count();
+
+    e = (p = buf) + len;
+
+    PRINT("Resource owners:\n");
+
+    for (zid = 0;  zid < zcnt;  zid++) {
+        zone = mrp_zone_find_by_id(zid);
+
+        if (!zone) {
+            PRINT("   Zone %u:\n", zid);
+        }
+        else {
+            PRINT("   Zone %s:", zone->name);
+            p += mrp_zone_attribute_print(zone, p, e-p);
+            PRINT("\n");
+        }
+
+        for (rid = 0;   rid < rcnt;   rid++) {
+            if (!(rdef = mrp_resource_definition_find_by_id(rid)))
+                continue;
+
+            PRINT("      %-15s: ", rdef->name);
+            
+            owner = get_owner(zid, rid);
+
+            if (!(class = owner->class) ||
+                !(rset  = owner->rset ) ||
+                !(res   = owner->res  )    )
+            {
+                PRINT("<nobody>");
+            }
+            else {
+                MRP_ASSERT(rdef == res->def, "confused with data structures");
+
+                PRINT("%-15s", class->name);
+                
+                p += mrp_resource_attribute_print(res, p, e-p);
+            }
+
+            PRINT("\n");
+        }
+    }
+
+    return p - buf;
+
+#undef PRINT
+}
+
+
+static mrp_resource_owner_t *get_owner(uint32_t zone, uint32_t resid)
+{
+    MRP_ASSERT(zone < MRP_ZONE_MAX && resid < RESOURCE_MAX,"invalid argument");
+
+    return resource_owners + (zone * RESOURCE_MAX + resid);
+}
+
+static void reset_owners(uint32_t zone, mrp_resource_owner_t *oldowners)
+{
+    void   *ptr  = get_owner(zone, 0);
+    size_t  size = sizeof(mrp_resource_owner_t) * RESOURCE_MAX;
+
+    if (oldowners)
+        memcpy(oldowners, ptr, size);
+
+    memset(ptr, 0, size);
+}
+
+static bool grant_ownership(mrp_resource_owner_t *owner,
+                            mrp_resource_class_t *class,
+                            mrp_resource_set_t   *rset,
+                            mrp_resource_t       *res,
+                            bool                  advice)
+{
+
+    /*
+      if (fake_grant())
+        return true;
+      if (forbid_grant())
+        return false;
+     */
+
+    if (!owner->class && !owner->rset) {
+        /* nobody owns this, so grab it */
+        if (!advice) {
+            owner->class = class;
+            owner->rset  = rset;
+            owner->res   = res;
+            owner->share = res->shared;
+        }
+        return true;
+    }
+
+    if (owner->class == class && owner->rset == rset) {
+        /* we happen to won it already */
+        return true;
+    }
+
+    if (owner->share) {
+        /* OK, someone else owns it but
+           the owner is ready to share it with us */
+        if (!advice)
+            owner->share = res->shared;
+        return true;
+    }
+
+    return false;
+}
+
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-owner.h b/src/resource/resource-owner.h
new file mode 100644 (file)
index 0000000..383713c
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_RESOURCE_OWNER_H__
+#define __MURPHY_RESOURCE_OWNER_H__
+
+#include "data-types.h"
+
+
+
+struct mrp_resource_owner_s {
+    mrp_resource_class_t *class;  /**< owner class */
+    mrp_resource_set_t   *rset;   /**< owner resource set  */
+    mrp_resource_t       *res;    /**< owner resource */
+    bool                  share;  /**< do not use this  */
+};
+
+
+void mrp_resource_owner_update_zone(uint32_t);
+
+
+#endif  /* __MURPHY_RESOURCE_OWNER_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-set.c b/src/resource/resource-set.c
new file mode 100644 (file)
index 0000000..7fb5d7b
--- /dev/null
@@ -0,0 +1,253 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+#include <murphy/common/hashtbl.h>
+#include <murphy/common/utils.h>
+
+#include <resource/resource-api.h>
+
+#include "resource-set.h"
+#include "resource-class.h"
+#include "resource.h"
+#include "resource-owner.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n" , ##args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n" , ##args) 
+#define mrp_log_info(fmt, args...) printf(fmt "\n" , ##args) 
+
+
+#define STAMP_MAX     ((uint32_t)1 << MRP_KEY_STAMP_BITS)
+#define PRIORITY_MAX  ((uint32_t)1 << MRP_KEY_PRIORITY_BITS)
+
+static MRP_LIST_HOOK(resource_set_list);
+
+
+static uint32_t    get_request_stamp(void);
+static const char *request_str(mrp_resource_request_t);
+
+
+mrp_resource_set_t *mrp_resource_set_create(uint32_t client_id,
+                                            void *client_data,
+                                            uint32_t priority)
+{
+    static uint32_t our_id;
+
+    mrp_resource_set_t *rset;
+
+    MRP_ASSERT(client_data, "invalid argument");
+
+    if (priority >= PRIORITY_MAX)
+        priority = PRIORITY_MAX - 1;
+
+    if (!(rset = mrp_allocz(sizeof(mrp_resource_set_t))))
+        mrp_log_error("Memory alloc failure. Can't create resource set");
+    else {
+        rset->id = our_id++;
+
+        mrp_list_init(&rset->resource.list);
+        rset->resource.share = false;
+
+        mrp_list_init(&rset->client.list);
+        rset->client.id   = client_id;
+        rset->client.data = client_data;
+
+        mrp_list_init(&rset->class.list);
+        rset->class.priority = priority;
+
+        mrp_list_append(&resource_set_list, &rset->list);
+    }
+
+    return rset;
+}
+
+
+mrp_resource_t *mrp_resource_set_iterate_resources(mrp_resource_set_t *rset,
+                                                   void **cursor)
+{
+    mrp_list_hook_t *list, *entry;
+
+    MRP_ASSERT(rset && cursor, "invalid argument");
+
+    list  = &rset->resource.list;
+    entry = (*cursor == NULL) ? list->next : (mrp_list_hook_t *)*cursor;
+
+    if (entry == list)
+        return NULL;
+
+    *cursor = entry->next;
+
+    return mrp_list_entry(entry, mrp_resource_t, list);
+}
+
+
+int mrp_resource_set_add_resource(mrp_resource_set_t *rset,
+                                  const char         *name,
+                                  bool                shared,
+                                  mrp_attr_def_t     *attrdefs,
+                                  bool                mandatory)
+{
+    uint32_t mask;
+    mrp_resource_t *res;
+
+    MRP_ASSERT(rset && name, "invalid argument");
+
+    if (!(res = mrp_resource_create(name, shared, attrdefs))) {
+        mrp_log_error("Can't add resource '%s' name to resource set %u",
+                      name, rset->id);
+        return -1;
+    }
+
+    mask = mrp_resource_get_mask(res);
+
+    rset->resource.mask.all       |= mask;
+    rset->resource.mask.mandatory |= mandatory ? mask : 0;
+    rset->resource.share          |= mrp_resource_is_shared(res); 
+
+
+    mrp_list_append(&rset->resource.list, &res->list);
+        
+    return 0;
+}
+
+void mrp_resource_set_acquire(mrp_resource_set_t *rset)
+{
+    MRP_ASSERT(rset, "invalid argument");
+
+    if (rset->request.type != mrp_resource_acquire) {
+        rset->request.type  = mrp_resource_acquire;
+        rset->request.stamp = get_request_stamp();
+
+        mrp_resource_class_move_resource_set(rset);
+        mrp_resource_owner_update_zone(rset->zone);
+    }
+}
+
+void mrp_resource_set_release(mrp_resource_set_t *rset)
+{
+    MRP_ASSERT(rset, "invalid argument");
+
+    if (rset->request.type != mrp_resource_release) {
+        rset->request.type  = mrp_resource_release;
+        rset->request.stamp = get_request_stamp();
+
+        mrp_resource_class_move_resource_set(rset);
+        mrp_resource_owner_update_zone(rset->zone);
+    }
+}
+
+
+int mrp_resource_set_print(mrp_resource_set_t *rset, int indent,
+                           char *buf, int len)
+{
+#define PRINT(fmt, args...)  if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
+
+    mrp_resource_t *res;
+    mrp_list_hook_t *resen, *n;
+    uint32_t mandatory;
+    char gap[] = "                         ";
+    char *p, *e;
+
+    MRP_ASSERT(rset && indent >= 0 && indent < sizeof(gap)-1 && buf && len > 0,
+               "invalid argument");
+
+    gap[indent] = '\0';
+
+    e = (p = buf) + len;
+
+    PRINT("%s%3u - 0x%02x/0x%02x 0x%02x/0x%02x 0x%08x %d %s %s\n",
+          gap, rset->id,
+          rset->resource.mask.all, (mandatory = rset->resource.mask.mandatory),
+          rset->resource.mask.grant, rset->resource.mask.advice,
+          mrp_resource_class_get_sorting_key(rset), rset->class.priority,
+          rset->resource.share ? "shared   ":"exclusive",
+          request_str(rset->request.type));
+
+    mrp_list_foreach(&rset->resource.list, resen, n) {
+        res = mrp_list_entry(resen, mrp_resource_t, list);
+        p  += mrp_resource_print(res, mandatory, indent+6, p, e-p);
+    }
+    
+    return p - buf;
+
+#undef PRINT
+}
+
+static uint32_t get_request_stamp(void)
+{
+    static uint32_t  stamp;
+
+    mrp_list_hook_t *entry, *n;
+    mrp_resource_set_t *rset;
+    uint32_t min;
+
+    if ((min = stamp) >= STAMP_MAX) {
+        mrp_log_info("rebasing resource set stamps");
+
+        mrp_list_foreach(&resource_set_list, entry, n) {
+            rset = mrp_list_entry(entry, mrp_resource_set_t, list);
+            if (rset->request.stamp < min)
+                min = rset->request.stamp;
+        }
+
+        stamp -= min;
+
+        mrp_list_foreach(&resource_set_list, entry, n) {
+            rset = mrp_list_entry(entry, mrp_resource_set_t, list);
+            rset->request.stamp -= min;
+        }
+    }
+
+    MRP_ASSERT(stamp < STAMP_MAX, "Request stamp overflow");
+
+    return stamp++;
+}
+
+static const char *request_str(mrp_resource_request_t request)
+{
+    switch(request) {
+    case mrp_resource_no_request:     return "no-request";
+    case mrp_resource_release:        return "release";
+    case mrp_resource_acquire:        return "acquire";
+    default:                          return "< ??? >";
+    }
+}
+
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource-set.h b/src/resource/resource-set.h
new file mode 100644 (file)
index 0000000..3a0f487
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_RESOURCE_SET_H__
+#define __MURPHY_RESOURCE_SET_H__
+
+#include <murphy/common/list.h>
+
+#include "data-types.h"
+
+
+typedef enum mrp_resource_request_e {
+    mrp_resource_no_request = 0,
+    mrp_resource_release,
+    mrp_resource_acquire,
+} mrp_resource_request_t;
+
+struct mrp_resource_set_s {
+    mrp_list_hook_t                 list;
+    uint32_t                        id;
+    struct {
+        struct {
+            mrp_resource_mask_t all;
+            mrp_resource_mask_t mandatory;
+            mrp_resource_mask_t grant;
+            mrp_resource_mask_t advice;
+        } mask; 
+        mrp_list_hook_t list;
+        bool share;
+    }                               resource;
+    struct {
+        mrp_list_hook_t list;
+        uint32_t id;
+        uint32_t reqno;
+        void *data;
+    }                               client;
+    struct {
+        mrp_list_hook_t list;
+        mrp_resource_class_t *ptr;
+        uint32_t priority;
+    }                               class;
+    uint32_t                        zone;
+    struct {
+        mrp_resource_request_t type;
+        uint32_t stamp;
+    }                               request;
+};
+
+
+mrp_resource_set_t *mrp_resource_set_create(uint32_t, void *, uint32_t);
+mrp_resource_t     *mrp_resource_set_iterate_resources(mrp_resource_set_t *,
+                                                       void **);
+int                 mrp_resource_set_print(mrp_resource_set_t *, int,
+                                           char *, int);
+
+
+#endif  /* __MURPHY_RESOURCE_SET_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource.c b/src/resource/resource.c
new file mode 100644 (file)
index 0000000..fae07f2
--- /dev/null
@@ -0,0 +1,369 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+
+#include <resource/resource-api.h>
+
+#include "resource.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n", ## args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n", ## args) 
+
+#define RESOURCE_MAX   (sizeof(mrp_resource_mask_t) * 8)
+#define ATTRIBUTE_MAX  32
+
+#define VALID_TYPE(t) ((t) == mqi_string  || \
+                       (t) == mqi_integer || \
+                       (t) == mqi_unsignd || \
+                       (t) == mqi_floating  )
+
+
+
+static uint32_t            resource_def_count;
+static mrp_resource_def_t *resource_def_table[RESOURCE_MAX];
+
+static uint32_t add_resource_definition(const char *, bool, uint32_t);
+
+#if 0
+static uint32_t find_resource_attribute_id(mrp_resource_t *, const char *);
+
+static mqi_data_type_t   get_resource_attribute_value_type(mrp_resource_t *,
+                                                           uint32_t);
+
+static mrp_attr_value_t *get_resource_attribute_default_value(mrp_resource_t*,
+                                                              uint32_t);
+#endif
+
+
+
+uint32_t mrp_resource_definition_create(const char *name, bool shareable, 
+                                        mrp_attr_def_t *attrdefs)
+{
+    uint32_t nattr;
+    uint32_t id;
+    mrp_resource_def_t *def;
+
+    MRP_ASSERT(name, "invalid argument");
+
+    if (mrp_resource_definition_find_by_name(name)) {
+        mrp_log_error("attmpt to redefine resource '%s'", name);
+        return MRP_RESOURCE_ID_INVALID;
+    }
+
+    for (nattr = 0;  attrdefs && attrdefs[nattr].name;  nattr++)
+        ;
+
+    id = add_resource_definition(name, shareable, nattr);
+
+    if (id != MRP_RESOURCE_ID_INVALID) {
+        def = mrp_resource_definition_find_by_id(id);
+
+        MRP_ASSERT(def, "got confused with data structures");
+
+        if (mrp_attribute_copy_definitions(attrdefs, def->attrdefs) < 0)
+            return MRP_RESOURCE_ID_INVALID;
+    }
+    
+    return id;
+}
+
+uint32_t mrp_resource_definition_count(void)
+{
+    return resource_def_count;
+}
+
+mrp_resource_def_t *mrp_resource_definition_find_by_name(const char *name)
+{
+    mrp_resource_def_t *def;
+    uint32_t            i;
+
+    for (i = 0;  i < resource_def_count;  i++) {
+        def = resource_def_table[i];
+
+        if (def && !strcasecmp(name, def->name))
+            return def;
+    }
+
+    return NULL;
+}
+
+mrp_resource_def_t *mrp_resource_definition_find_by_id(uint32_t id)
+{
+    if (id < resource_def_count)
+        return resource_def_table[id];
+
+    return NULL;
+}
+
+
+mrp_resource_t *mrp_resource_create(const char     *name,
+                                    bool            shared,
+                                    mrp_attr_def_t *attrs)
+{
+    mrp_resource_t *res = NULL;
+    mrp_resource_def_t *rdef;
+    size_t base_size;
+    size_t attr_size;
+    size_t total_size;
+    int sts;
+
+    MRP_ASSERT(name, "invalid argument");
+
+    if (!(rdef = mrp_resource_definition_find_by_name(name))) {
+        mrp_log_warning("Can't find resource definition '%s'. "
+                        "No resource created", name);
+    }
+    else {
+        base_size  = sizeof(mrp_resource_t);
+        attr_size  = sizeof(mrp_attr_value_t) * rdef->nattr;
+        total_size = base_size + attr_size;
+
+        if (!(res = mrp_allocz(total_size))) {
+            mrp_log_error("Memory alloc failure. Can't create "
+                          "resource '%s'", name);
+        }
+        else {
+            mrp_list_init(&res->list);
+
+            res->def = rdef;
+            res->shared = rdef->shareable ?  shared : false;
+
+            sts = mrp_attribute_set_values(attrs, rdef->nattr,
+                                           rdef->attrdefs, res->attrs);
+            if (sts < 0) {
+                mrp_log_error("Memory alloc failure. No '%s' "
+                              "resource created", name);
+                return NULL;
+            }
+        }
+    }
+
+    return res;
+}
+
+void mrp_resource_destroy(mrp_resource_t *res)
+{
+    mrp_resource_def_t *rdef;
+    mqi_data_type_t type;
+    uint32_t id;
+
+    if (res) {
+        rdef = res->def;
+
+        MRP_ASSERT(rdef, "invalid_argument");
+        
+        mrp_list_delete(&res->list);
+
+        for (id = 0;  id < rdef->nattr;  id++) {
+            type = rdef->attrdefs[id].type;
+        
+            if (type == mqi_string)
+                mrp_free((void *)res->attrs[id].string);
+        }
+
+        mrp_free(res);
+    }
+}
+                                    
+
+uint32_t mrp_resource_get_mask(mrp_resource_t *res)
+{
+    mrp_resource_def_t *def;
+    uint32_t mask = 0;
+
+    if (res) {
+        def = res->def;
+
+        MRP_ASSERT(def, "confused with internal data structures");
+
+        mask = (uint32_t)1 << def->id;
+    }
+
+    return mask;
+}
+
+bool mrp_resource_is_shared(mrp_resource_t *res)
+{
+    if (res)
+        return res->shared;
+
+    return false;
+}
+
+
+
+int mrp_resource_print(mrp_resource_t *res, uint32_t mandatory,
+                       int indent, char *buf, int len)
+{
+#define PRINT(fmt, args...)  if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
+
+    mrp_resource_def_t *rdef;
+    char gap[] = "                         ";
+    char *p, *e;
+    uint32_t m;
+
+    MRP_ASSERT(res && indent >= 0 && indent < sizeof(gap)-1 && buf && len > 0,
+               "invalid argument");
+
+    rdef = res->def;
+
+    MRP_ASSERT(rdef, "Confused with data structures");
+
+    gap[indent] = '\0';
+    
+    e = (p = buf) + len;
+    m = ((uint32_t)1 << rdef->id);
+
+    PRINT("%s%s: 0x%02x %s %s", gap, rdef->name, m,
+          (m & mandatory) ? "mandatory":"optional ",
+          res->shared ? "shared  ":"exlusive");
+
+    p += mrp_resource_attribute_print(res, p, e-p);
+
+    PRINT("\n");
+
+
+    return p - buf;
+
+#undef PRINT
+}
+
+int mrp_resource_attribute_print(mrp_resource_t *res, char *buf, int len)
+{
+    mrp_resource_def_t *rdef;
+
+    MRP_ASSERT(res && buf && len > 0, "invalid argument");
+
+    rdef = res->def;
+
+    MRP_ASSERT(rdef, "Confused with data structures");
+
+    return mrp_attribute_print(rdef->nattr,rdef->attrdefs,res->attrs, buf,len);
+}
+
+
+static uint32_t add_resource_definition(const char *name, bool shareable,
+                                        uint32_t nattr)
+{
+    mrp_resource_def_t *def;
+    const char         *dup_name;
+    size_t              size;
+    uint32_t            id;
+
+    MRP_ASSERT(name && nattr < ATTRIBUTE_MAX, "invalid argument");
+
+    if (resource_def_count >= RESOURCE_MAX) {
+        mrp_log_error("Resource table overflow. Can't add resource '%s'",name);
+        return MRP_RESOURCE_ID_INVALID;
+    }
+
+    size = sizeof(mrp_resource_def_t) + sizeof(mrp_attr_def_t) * nattr;
+
+    if (!(def = mrp_allocz(size)) || !(dup_name = mrp_strdup(name))) {
+        mrp_log_error("Memory alloc failure. Can't add resource '%s'", name);
+        return MRP_RESOURCE_ID_INVALID;
+    }
+
+    id = resource_def_count++;
+
+    def->id        = id;
+    def->name      = dup_name;
+    def->shareable = shareable;
+    def->nattr     = nattr;
+
+    resource_def_table[id] = def;
+    
+    return id;
+}
+
+
+#if 0
+static uint32_t find_resource_attribute_id(mrp_resource_t *res, 
+                                           const char *attrnam)
+{
+    mrp_resource_def_t *rdef;
+    mrp_attr_def_t *adef;
+    uint32_t id;
+
+    if (res && (rdef = res->def) && attrnam) {
+        for (id = 0;  id < rdef->nattr;  id++) {
+            adef = rdef->attrdefs + id;
+
+            if (!strcasecmp(attrnam, adef->name))
+                return id;
+        }
+    }
+
+    return MRP_RESOURCE_ID_INVALID;
+}
+
+static mqi_data_type_t
+get_resource_attribute_value_type(mrp_resource_t *res, uint32_t id)
+{
+    mrp_resource_def_t *rdef;
+
+    MRP_ASSERT(res, "invalid argument");
+
+    rdef = res->def;
+
+    MRP_ASSERT(rdef, "confused with data structures");
+    MRP_ASSERT(id < rdef->nattr, "invalid argument");
+
+    return rdef->attrdefs[id].type;    
+}
+
+static mrp_attr_value_t *
+get_resource_attribute_default_value(mrp_resource_t *res, uint32_t id)
+{
+    mrp_resource_def_t *rdef;
+
+    MRP_ASSERT(res, "invalid argument");
+
+    rdef = res->def;
+
+    MRP_ASSERT(rdef, "confused with data structures");
+    MRP_ASSERT(id < rdef->nattr, "invalid argument");
+
+    return &rdef->attrdefs[id].value;        
+}
+#endif
+
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/resource.h b/src/resource/resource.h
new file mode 100644 (file)
index 0000000..21c50bf
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_RESOURCE_H__
+#define __MURPHY_RESOURCE_H__
+
+#include <murphy/common/list.h>
+
+#include "attribute.h"
+
+
+struct mrp_resource_def_s {
+    uint32_t            id;
+    const char         *name;
+    bool                shareable;
+    uint32_t            nattr;
+    mrp_attr_def_t      attrdefs[0];
+};
+
+struct mrp_resource_s {
+    mrp_list_hook_t     list;
+    mrp_resource_def_t *def;
+    bool                shared;
+    mrp_attr_value_t    attrs[0];
+};
+
+
+
+uint32_t            mrp_resource_definition_count(void);
+mrp_resource_def_t *mrp_resource_definition_find_by_name(const char *);
+mrp_resource_def_t *mrp_resource_definition_find_by_id(uint32_t);
+
+
+void                mrp_resource_destroy(mrp_resource_t *);
+
+uint32_t            mrp_resource_get_mask(mrp_resource_t *);
+bool                mrp_resource_is_shared(mrp_resource_t *);
+
+int                 mrp_resource_print(mrp_resource_t*,uint32_t,int,char*,int);
+int                 mrp_resource_attribute_print(mrp_resource_t *, char *,int);
+
+
+#endif  /* __MURPHY_RESOURCE_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/zone.c b/src/resource/zone.c
new file mode 100644 (file)
index 0000000..1928020
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <murphy/common/mm.h>
+
+#include <resource/resource-api.h>
+
+#include "zone.h"
+
+/* temporary!!! */
+#define mrp_log_warning(fmt, args...) printf(fmt "\n", ## args) 
+#define mrp_log_error(fmt, args...) printf(fmt "\n", ## args) 
+
+#define ATTRIBUTE_MAX  32
+
+#define VALID_TYPE(t) ((t) == mqi_string  || \
+                       (t) == mqi_integer || \
+                       (t) == mqi_unsignd || \
+                       (t) == mqi_floating  )
+
+
+static mrp_zone_def_t *zone_def;
+static uint32_t        zone_count;
+static mrp_zone_t     *zone_table[MRP_ZONE_MAX];
+
+
+int mrp_zone_definition_create(mrp_attr_def_t *attrdefs)
+{
+    uint32_t nattr;
+    size_t size;
+    mrp_zone_def_t *def;
+
+    for (nattr = 0;  attrdefs && attrdefs[nattr].name;  nattr++)
+        ;
+
+    size = sizeof(mrp_zone_def_t) + sizeof(mrp_attr_def_t) * nattr;
+
+    if (!(def = mrp_allocz(size))) {
+        mrp_log_error("Memory alloc failure. Can't create zone definition");
+        return -1;
+    }
+
+    def->nattr = nattr;
+    zone_def = def;
+
+    if (mrp_attribute_copy_definitions(attrdefs, def->attrdefs) < 0)
+        return -1;
+    
+    return 0;
+}
+
+uint32_t mrp_zone_count(void)
+{
+    return zone_count;
+}
+
+uint32_t mrp_zone_create(const char *name, mrp_attr_def_t *attrs)
+{
+    uint32_t id;
+    size_t size;
+    mrp_zone_t *zone;
+    const char *dup_name;
+    int sts;
+
+    MRP_ASSERT(name, "invalid argument");
+    
+    if (!zone_def) {
+        mrp_log_error("Zone definition must preceed zone creation. "
+                      "can't create zone '%s'", name);
+        return MRP_ZONE_ID_INVALID;
+    }
+
+    if (zone_count >= MRP_ZONE_MAX) {
+        mrp_log_error("Zone table overflow. Can't create zone '%s'", name);
+        return MRP_ZONE_ID_INVALID;
+    }
+
+    size = sizeof(mrp_zone_t) + sizeof(mrp_attr_def_t) * zone_def->nattr;
+
+    if (!(zone = mrp_allocz(size)) || !(dup_name = mrp_strdup(name))) {
+        mrp_log_error("Memory alloc failure. Can't create zone '%s'", name);
+        return MRP_ZONE_ID_INVALID;
+    }
+
+
+    zone->name = dup_name;
+
+    sts = mrp_attribute_set_values(attrs, zone_def->nattr,
+                                   zone_def->attrdefs, zone->attrs);
+    if (sts < 0) {
+        mrp_log_error("Memory alloc failure. Can't create zone '%s'", name);
+        return MRP_ZONE_ID_INVALID;
+    }
+
+    id = zone_count++;
+
+    zone_table[id] = zone;
+    
+    return id;
+}
+
+mrp_zone_t *mrp_zone_find_by_id(uint32_t id)
+{
+    if (id < zone_count)
+        return zone_table[id];
+
+    return NULL;
+}
+
+int mrp_zone_attribute_print(mrp_zone_t *zone, char *buf, int len)
+{
+    MRP_ASSERT(zone && buf && len > 0, "invalid argument");
+
+    return mrp_attribute_print(zone_def->nattr, zone_def->attrdefs,
+                               zone->attrs, buf,len);
+}
+
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */
diff --git a/src/resource/zone.h b/src/resource/zone.h
new file mode 100644 (file)
index 0000000..400895e
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2012, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *  * Neither the name of Intel Corporation nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MURPHY_ZONE_H__
+#define __MURPHY_ZONE_H__
+
+#include "attribute.h"
+
+struct mrp_zone_def_s {
+    uint32_t         nattr;
+    mrp_attr_def_t   attrdefs[0];
+};
+
+struct mrp_zone_s {
+    const char       *name;
+    mrp_attr_value_t  attrs[0];
+};
+
+
+uint32_t mrp_zone_count(void);
+mrp_zone_t *mrp_zone_find_by_id(uint32_t);
+
+int mrp_zone_attribute_print(mrp_zone_t *, char *, int);
+
+
+#endif  /* __MURPHY_ZONE_H__ */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ */