[m-rev.] diff: Change types in memory attribution profile structures.

Peter Wang novalazy at gmail.com
Fri Aug 10 15:31:58 AEST 2018


runtime/mercury_heap_profile.c:
    Use MR_Unsigned for fields that count the number of cells or words
    allocated. Since they do not represent object sizes, the use of
    size_t was not appropriate. More importantly, to print size_t values
    we should use the 'z' length modifier to fprintf, but that may not
    be supported on all platforms we need to work on.

    Print unsigned values with 'u' format specifier.

    For the MR_vsc_size field, which does represent an object size, cast
    the value to MR_Unsigned and print it with the 'u' format specifier.

diff --git a/runtime/mercury_heap_profile.c b/runtime/mercury_heap_profile.c
index f1e8ee1c3..7626a01a5 100644
--- a/runtime/mercury_heap_profile.c
+++ b/runtime/mercury_heap_profile.c
@@ -193,31 +193,31 @@ MR_prof_turn_off_heap_profiling(void)
 #ifdef  MR_MPROF_PROFILE_MEMORY_ATTRIBUTION
 
 typedef struct MR_AttribCount_Struct    MR_AttribCount;
 typedef struct MR_VarSizeCount_Struct   MR_VarSizeCount;
 
 // Counts for attributed objects.
 
 struct MR_AttribCount_Struct {
     unsigned                MR_atc_id;
     MR_AllocSiteInfo const  *MR_atc_alloc_site;
-    size_t                  MR_atc_num_cells;
-    size_t                  MR_atc_num_words;
+    MR_Unsigned             MR_atc_num_cells;
+    MR_Unsigned             MR_atc_num_words;
 };
 
 // Objects which are unattributed, or explicitly attributed as runtime
 // structures, may come in many different sizes. We store the counters for
 // each different size as a separate node in a binary search tree.
 
 struct MR_VarSizeCount_Struct {
     size_t              MR_vsc_size;
-    size_t              MR_vsc_count;
+    MR_Unsigned         MR_vsc_count;
     MR_VarSizeCount     *MR_vsc_left;
     MR_VarSizeCount     *MR_vsc_right;
 };
 
 #define SNAPSHOTS_FILENAME              "Prof.Snapshots"
 #define KNOWN_COUNT_TABLE_INITIAL_SIZE  (1 << 8)    // power of two
 
 static MR_AttribCount   *attrib_count_table;
 static size_t           attrib_count_table_size;
 static size_t           attrib_count_table_used;
@@ -227,21 +227,21 @@ static int              snapshot_counter;
 static FILE             *snapshot_file;
 
 static void     add_attrib_count_entry(MR_AttribCount *table,
                     size_t table_size, size_t *table_used, unsigned id,
                     const MR_AllocSiteInfo *alloc_site);
 static void     rehash_attrib_count_table(void);
 static unsigned hash_addr(MR_Word key);
 static void * GC_CALLBACK enumerate_reachable_objects_locked(void *data);
 static GC_CALLBACK void
                 reachable_object_callback(void *p, size_t bytes, void *data);
-static MR_bool  increment_attrib_count(MR_Word addr, size_t num_words);
+static MR_bool  increment_attrib_count(MR_Word addr, unsigned num_words);
 static void     increment_var_size_count(MR_VarSizeCount **node, size_t words);
 static void     finish_reachable_report(const char *label);
 static void     write_attrib_counts(FILE *fp, MR_AttribCount *table,
                     size_t table_size);
 static void     write_var_size_counts(FILE *fp, const char *prefix,
                     MR_VarSizeCount *node);
 static const char *maybe_filename(const char *s);
 
 #define MR_NUM_BUILTIN_ALLOC_SITES  9
 
@@ -389,38 +389,38 @@ MR_report_memory_attribution(const char *label, MR_bool run_collect)
 static void * GC_CALLBACK enumerate_reachable_objects_locked(void *data)
 {
     GC_enumerate_reachable_objects_inner(reachable_object_callback, NULL);
     return NULL;
 }
 
 static GC_CALLBACK void
 reachable_object_callback(void *p, size_t bytes, void *data)
 {
     MR_Word addr;
-    size_t words = bytes/MR_BYTES_PER_WORD;
+    unsigned words = bytes / MR_BYTES_PER_WORD;
 
-    addr = ((MR_Word*)p)[0];
+    addr = ((MR_Word *) p)[0];
 
     if ((void *) addr == MR_ALLOC_SITE_RUNTIME) {
         increment_var_size_count(&runtime_count_tree, words);
         return;
     }
 
     if (addr == (MR_Word) NULL ||
         !increment_attrib_count(addr, words))
     {
         increment_var_size_count(&unknown_count_tree, words);
     }
 }
 
 static MR_bool
-increment_attrib_count(MR_Word addr, size_t num_words)
+increment_attrib_count(MR_Word addr, unsigned num_words)
 {
     MR_AttribCount   *entry;
     MR_Unsigned     orig;
     MR_Unsigned     i;
 
     orig = i = hash_addr(addr) & (attrib_count_table_size - 1);
     do {
         assert(i < attrib_count_table_size);
         entry = &attrib_count_table[i];
         if ((MR_Word) entry->MR_atc_alloc_site == addr) {
@@ -479,46 +479,46 @@ finish_reachable_report(const char *label)
 
 static void
 write_attrib_counts(FILE *fp, MR_AttribCount *table, size_t table_size)
 {
     size_t i;
 
     for (i = 0; i < table_size; i++) {
         if (table[i].MR_atc_alloc_site != NULL &&
             table[i].MR_atc_num_cells != 0)
         {
-            fprintf(fp, "%d "
+            fprintf(fp, "%u "
                 "%" MR_INTEGER_LENGTH_MODIFIER "u "
                 "%" MR_INTEGER_LENGTH_MODIFIER "u\n",
                 table[i].MR_atc_id,
                 table[i].MR_atc_num_cells,
                 table[i].MR_atc_num_words);
 
             table[i].MR_atc_num_cells = 0;
             table[i].MR_atc_num_words = 0;
         }
     }
 }
 
 static void
 write_var_size_counts(FILE *fp, const char *prefix, MR_VarSizeCount *node)
 {
     while (node != NULL) {
         write_var_size_counts(fp, prefix, node->MR_vsc_left);
 
         if (node->MR_vsc_count != 0) {
             fprintf(fp, "%s "
-                "%" MR_INTEGER_LENGTH_MODIFIER "d "
-                "%" MR_INTEGER_LENGTH_MODIFIER "d\n",
+                "%" MR_INTEGER_LENGTH_MODIFIER "u "
+                "%" MR_INTEGER_LENGTH_MODIFIER "u\n",
                 prefix,
                 node->MR_vsc_count,
-                node->MR_vsc_size);
+                (MR_Unsigned) node->MR_vsc_size);
             node->MR_vsc_count = 0;
         }
 
         node = node->MR_vsc_right;
     }
 }
 
 void
 MR_finish_prof_snapshots_file(void)
 {
@@ -530,21 +530,21 @@ MR_finish_prof_snapshots_file(void)
         return;
     }
 
     fprintf(fp, "size_map");
     GC_mercury_write_size_map(fp);
     fprintf(fp, "\n");
 
     for (i = 0; i < attrib_count_table_size; i++) {
         site = attrib_count_table[i].MR_atc_alloc_site;
         if (site != NULL) {
-            fprintf(fp, "%d\t", attrib_count_table[i].MR_atc_id);
+            fprintf(fp, "%u\t", attrib_count_table[i].MR_atc_id);
             fprintf(fp, "%s\t", MR_lookup_entry_or_internal(site->MR_asi_proc));
             fprintf(fp, "%s\t", maybe_filename(site->MR_asi_file_name));
             fprintf(fp, "%d\t", site->MR_asi_line_number);
             fprintf(fp, "%s\t", site->MR_asi_type);
             fprintf(fp, "%d\n", site->MR_asi_words);
         }
     }
 
     MR_checked_fclose(snapshot_file, SNAPSHOTS_FILENAME);
     snapshot_file = NULL;
-- 
2.18.0



More information about the reviews mailing list