Merge pull request #1821 from iainx/replace-getline
authorJoão Matos <joao@tritao.eu>
Tue, 19 May 2015 17:14:29 +0000 (18:14 +0100)
committerJoão Matos <joao@tritao.eu>
Tue, 19 May 2015 17:14:29 +0000 (18:14 +0100)
[Profiler] Replace getline(3)

mono/profiler/proflog.c

index 76cbc677f33b52d0aafc5cf9446f1e0146268820..cc1941494bb7b2ee47e3e55b46e8feee8f6b5277 100644 (file)
@@ -3044,25 +3044,62 @@ coverage_filter (MonoProfiler *prof, MonoMethod *method)
        return TRUE;
 }
 
+#define LINE_BUFFER_SIZE 4096
+static char *
+get_file_content (FILE *stream)
+{
+       GString *builder = g_string_sized_new (LINE_BUFFER_SIZE);
+       char buffer[LINE_BUFFER_SIZE];
+       ssize_t bytes_read;
+
+       while ((bytes_read = fread (buffer, 1, LINE_BUFFER_SIZE, stream)) > 0) {
+               g_string_append_len (builder, buffer, bytes_read);
+       }
+
+       return g_string_free (builder, FALSE);
+}
+
+static char *
+get_next_line (char *contents, char **next_start)
+{
+       char *p = contents;
+
+       if (p == NULL || *p == '\0') {
+               *next_start = NULL;
+               return NULL;
+       }
+
+       while (*p != '\n' && *p != '\0')
+               p++;
+
+       if (*p == '\n') {
+               *p = '\0';
+               *next_start = p + 1;
+       } else
+               *next_start = NULL;
+
+       return contents;
+}
+
 static void
 init_suppressed_assemblies (void)
 {
-       size_t n;
+       char *content;
        char *line;
+       FILE *sa_file;
 
        mono_mutex_init (&suppressed_assemblies_mutex);
        suppressed_assemblies = mono_conc_hashtable_new (&suppressed_assemblies_mutex, g_str_hash, g_str_equal);
-       FILE *sa_file = fopen (SUPPRESSION_DIR "/mono-profiler-log.suppression", "r");
+       sa_file = fopen (SUPPRESSION_DIR "/mono-profiler-log.suppression", "r");
        if (sa_file == NULL)
                return;
 
-       n = 0;
-       line = NULL;
-       while (getline (&line, &n, sa_file) > -1) {
+       /* Don't need to free @content as it is referred to by the lines stored in @suppressed_assemblies */
+       content = get_file_content (sa_file);
+
+       while ((line = get_next_line (content, &content))) {
                line = g_strchomp (g_strchug (line));
                mono_conc_hashtable_insert (suppressed_assemblies, line, line);
-               n = 0;
-               line = NULL;
        }
 
        fclose (sa_file);
@@ -3922,8 +3959,7 @@ mono_profiler_startup (const char *desc)
                }
                if ((opt = match_option (p, "covfilter-file", &val)) != p) {
                        FILE *filter_file;
-                       char *line;
-                       size_t n;
+                       char *line, *content;
 
                        if (filters == NULL)
                                filters = g_ptr_array_new ();
@@ -3934,13 +3970,10 @@ mono_profiler_startup (const char *desc)
                                exit (0);
                        }
 
-                       n = 0;
-                       line = NULL;
-                       while (getline (&line, &n, filter_file) > -1) {
+                       /* Don't need to free content as it is referred to by the lines stored in @filters */
+                       content = get_file_content (filter_file);
+                       while ((line = get_next_line (content, &content)))
                                g_ptr_array_add (filters, g_strchug (g_strchomp (line)));
-                               n = 0;
-                               line = NULL;
-                       }
 
                        fclose (filter_file);
                        continue;