mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccoll...@apache.org
Subject [1/2] incubator-mynewt-larva git commit: (MYNEWT-121) sys/stats - Allow access from multiple source files
Date Wed, 17 Feb 2016 00:11:44 GMT
Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/develop ba752314b -> e4cb7b20f


(MYNEWT-121) sys/stats - Allow access from multiple source files

1. The STATS_SECT_START and STATS_SECT_END macros just define a struct;
they don't create an instance.  Generally, these macros would be used
in a header file so that source files can have access to the struct
definition.

2. The addition of a STATS_SECT_DECL macro.  This macro would be used in
two places:
    * In source files to instantiate a stats struct.
    * In header files to expose an extern declaration of a stats
      instance.

3. As a consequence of the above two points: the names of struct
instances are no longer auto-generated.  The user needs to specify the
exact name.  All macros which derive the instance name from the struct
name are changed: now they just accept the instance name directly.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/2de95453
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/2de95453
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/2de95453

Branch: refs/heads/develop
Commit: 2de95453d4075c8bb86f6615d58d6fa0b19c35eb
Parents: ba75231
Author: Christopher Collins <ccollins476ad@gmail.com>
Authored: Tue Feb 16 16:03:24 2016 -0800
Committer: Christopher Collins <ccollins476ad@gmail.com>
Committed: Tue Feb 16 16:03:24 2016 -0800

----------------------------------------------------------------------
 sys/stats/include/stats/stats.h | 36 ++++++++++++++----------------
 sys/stats/src/stats.c           | 43 ++++++++++++++++++++----------------
 2 files changed, 40 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2de95453/sys/stats/include/stats/stats.h
----------------------------------------------------------------------
diff --git a/sys/stats/include/stats/stats.h b/sys/stats/include/stats/stats.h
index b66bf3f..f5c79c4 100644
--- a/sys/stats/include/stats/stats.h
+++ b/sys/stats/include/stats/stats.h
@@ -39,22 +39,19 @@ struct stats_hdr {
     STAILQ_ENTRY(stats_hdr) s_next;
 };
 
+#define STATS_SECT_DECL(__name)             \
+    struct stats_ ## __name
 
-#define STATS_SECT_START(__name)           \
-struct stats_ ## __name {                  \
+#define STATS_SECT_START(__name)            \
+STATS_SECT_DECL(__name) {                   \
     struct stats_hdr s_hdr;
 
-
-#define STATS_SECT_END(__name)           \
-} g_stats_ ## __name;
-
-#define STATS_SECT_NAME(__name) \
-    g_stats_ ## __name
-
-#define STATS_HDR(__name) ((struct stats_hdr *) &STATS_SECT_NAME(__name))
+#define STATS_SECT_END };
 
 #define STATS_SECT_VAR(__var) \
-    s##__var 
+    s##__var
+
+#define STATS_HDR(__varname) &(__varname).s_hdr
 
 #define STATS_SIZE_16 (sizeof(uint16_t))
 #define STATS_SIZE_32 (sizeof(uint32_t))
@@ -65,16 +62,15 @@ struct stats_ ## __name {                  \
 #define STATS_SECT_ENTRY32(__var) uint32_t STATS_SECT_VAR(__var);
 #define STATS_SECT_ENTRY64(__var) uint64_t STATS_SECT_VAR(__var);
 
-#define STATS_SIZE_INIT_PARMS(__name, __size)                              \
-    __size,                                                                \
-    ((sizeof(STATS_SECT_NAME(__name)) - sizeof(struct stats_hdr)) / __size)
-
+#define STATS_SIZE_INIT_PARMS(__varname, __size)                            \
+    (__size),                                                               \
+    ((sizeof (__varname)) - sizeof (struct stats_hdr)) / (__size)
 
-#define STATS_INC(__name, __var) \
-    (STATS_SECT_NAME(__name).STATS_SECT_VAR(__var)++)
+#define STATS_INC(__varname, __var) \
+    ((__varname).STATS_SECT_VAR(__var)++)
 
-#define STATS_INCN(__name, __var, __n) \
-    (STATS_SECT_NAME(__name).STATS_SECT_VAR(__var) += (__n))
+#define STATS_INCN(__varname, __var, __n) \
+    ((__varname).STATS_SECT_VAR(__var) += (__n))
 
 #ifdef STATS_NAME_ENABLE
 
@@ -84,7 +80,7 @@ struct stats_ ## __name {                  \
 struct stats_name_map STATS_NAME_MAP_NAME(__name)[] = {
 
 #define STATS_NAME(__name, __entry)                   \
-    { &STATS_SECT_NAME(__name).STATS_SECT_VAR(__entry), #__entry },
+    { &STATS_SECT_NAME(__name).(__entry), #__entry },
 
 #define STATS_NAME_END(__name)                        \
 };

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2de95453/sys/stats/src/stats.c
----------------------------------------------------------------------
diff --git a/sys/stats/src/stats.c b/sys/stats/src/stats.c
index 1c193ed..6380ae8 100644
--- a/sys/stats/src/stats.c
+++ b/sys/stats/src/stats.c
@@ -6,7 +6,7 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
@@ -27,16 +27,20 @@
 
 STATS_SECT_START(stats)
     STATS_SECT_ENTRY(num_registered)
-STATS_SECT_END(stats)
+STATS_SECT_END
+
+STATS_SECT_DECL(stats) g_stats_stats;
 
 STATS_NAME_START(stats)
     STATS_NAME(stats, num_registered)
 STATS_NAME_END(stats)
 
-STAILQ_HEAD(, stats_hdr) g_stats_registry = 
+STAILQ_HEAD(, stats_hdr) g_stats_registry =
     STAILQ_HEAD_INITIALIZER(g_stats_registry);
 
-int 
+static uint8_t stats_module_inited;
+
+int
 stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
 {
     char *name;
@@ -55,7 +59,7 @@ stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
 
     while (cur < end) {
         /*
-         * Access and display the statistic name.  Pass that to the 
+         * Access and display the statistic name.  Pass that to the
          * walk function
          */
         name = NULL;
@@ -66,7 +70,7 @@ stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
                 break;
             }
         }
-#endif 
+#endif
         if (name == NULL) {
             ent_n = (cur - ((uint8_t *) hdr + sizeof(*hdr))) / hdr->s_size;
             len = snprintf(name_buf, sizeof(name_buf), "s%d", ent_n);
@@ -81,14 +85,14 @@ stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
 
         cur += hdr->s_size;
     }
-    
+
     return (0);
 err:
     return (rc);
 }
 
 
-int 
+int
 stats_module_init(void)
 {
     int rc;
@@ -100,20 +104,21 @@ stats_module_init(void)
     }
 #endif
 
-#ifdef NEWTMGR_PRESENT 
+#ifdef NEWTMGR_PRESENT
     rc = stats_nmgr_register_group();
     if (rc != 0) {
         goto err;
     }
-#endif 
+#endif
 
-    rc = stats_init(STATS_HDR(stats), STATS_SIZE_INIT_PARMS(stats, STATS_SIZE_32), 
-            STATS_NAME_INIT_PARMS(stats));
+    rc = stats_init(STATS_HDR(g_stats_stats),
+                    STATS_SIZE_INIT_PARMS(g_stats_stats, STATS_SIZE_32),
+                    STATS_NAME_INIT_PARMS(stats));
     if (rc != 0) {
         goto err;
     }
-    
-    rc = stats_register("stat", STATS_HDR(stats));
+
+    rc = stats_register("stat", STATS_HDR(g_stats_stats));
     if (rc != 0) {
         goto err;
     }
@@ -125,7 +130,7 @@ err:
 
 
 int
-stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, 
+stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt,
         struct stats_name_map *map, uint8_t map_cnt)
 {
     memset((uint8_t *) shdr, 0, sizeof(*shdr) + (size * cnt));
@@ -140,7 +145,7 @@ stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt,
     return (0);
 }
 
-int 
+int
 stats_group_walk(stats_group_walk_func_t walk_func, void *arg)
 {
     struct stats_hdr *hdr;
@@ -157,7 +162,7 @@ err:
     return (rc);
 }
 
-struct stats_hdr * 
+struct stats_hdr *
 stats_group_find(char *name)
 {
     struct stats_hdr *cur;
@@ -178,7 +183,7 @@ stats_register(char *name, struct stats_hdr *shdr)
     struct stats_hdr *cur;
     int rc;
 
-    /* Don't allow duplicate entries, return an error if this stat 
+    /* Don't allow duplicate entries, return an error if this stat
      * is already registered.
      */
     STAILQ_FOREACH(cur, &g_stats_registry, s_next) {
@@ -192,7 +197,7 @@ stats_register(char *name, struct stats_hdr *shdr)
 
     STAILQ_INSERT_TAIL(&g_stats_registry, shdr, s_next);
 
-    STATS_INC(stats, num_registered);
+    STATS_INC(g_stats_stats, num_registered);
 
     return (0);
 err:


Mime
View raw message