ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/disk_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/disk_stats.c (file contents):
Revision 1.54 by ats, Fri Mar 19 23:44:30 2004 UTC vs.
Revision 1.58 by ats, Sun Apr 4 23:26:23 2004 UTC

# Line 30 | Line 30
30   #include <string.h>
31   #include <time.h>
32   #include "statgrab.h"
33 + #include "vector.h"
34  
35   #ifdef SOLARIS
36   #include <sys/mnttab.h>
# Line 75 | Line 76
76                          "ntfs"}
77   #endif
78  
79 < #define START_VAL 1
79 > static char *copy_string(char **dest, const char *src) {
80 >        char *new;
81  
82 < char *copy_string(char *orig_ptr, const char *newtext){
82 >        new = realloc(*dest, strlen(src) + 1);
83 >        if (new == NULL) {
84 >                return NULL;
85 >        }
86  
87 <        /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
88 <        orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
89 <        if(orig_ptr==NULL){
90 <                return NULL;
86 <        }
87 <        strcpy(orig_ptr, newtext);
87 >        strcpy(new, src);
88 >        *dest = new;
89 >        return new;
90 > }
91  
92 <        return orig_ptr;
92 > static void disk_stat_init(disk_stat_t *d) {
93 >        d->device_name = NULL;
94 >        d->fs_type = NULL;
95 >        d->mnt_point = NULL;
96   }
97  
98 <
99 < void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
100 <
101 <        for(disk_stats+=start; start<=end; start++){
96 <                disk_stats->device_name=NULL;
97 <                disk_stats->fs_type=NULL;
98 <                disk_stats->mnt_point=NULL;
99 <                
100 <                disk_stats++;
101 <        }
98 > static void disk_stat_destroy(disk_stat_t *d) {
99 >        free(d->device_name);
100 >        free(d->fs_type);
101 >        free(d->mnt_point);
102   }
103  
104   int is_valid_fs_type(const char *type) {
# Line 114 | Line 114 | int is_valid_fs_type(const char *type) {
114   }
115  
116   disk_stat_t *get_disk_stats(int *entries){
117 +        VECTOR_DECLARE_STATIC(disk_stats, disk_stat_t, 10,
118 +                              disk_stat_init, disk_stat_destroy);
119  
118        static disk_stat_t *disk_stats;
119        static int watermark=-1;
120
120          int valid_type;
121          int num_disks=0;
122   #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
# Line 139 | Line 138 | disk_stat_t *get_disk_stats(int *entries){
138          struct statfs *mp;
139   #endif
140  
142        if(watermark==-1){
143                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
144                if(disk_stats==NULL){
145                        return NULL;
146                }
147                watermark=START_VAL;
148                init_disk_stat(0, watermark-1, disk_stats);
149        }
141   #ifdef ALLBSD
142          nummnt=getmntinfo(&mp , MNT_LOCAL);
143          if (nummnt<=0){
# Line 181 | Line 172 | disk_stat_t *get_disk_stats(int *entries){
172   #endif
173  
174                  if(valid_type){
175 <                        if(num_disks>watermark-1){
176 <                                disk_ptr=disk_stats;
186 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
187 <                                        disk_stats=disk_ptr;
188 <                                        return NULL;
189 <                                }
190 <
191 <                                watermark=watermark*2;
192 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
175 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
176 >                                return NULL;
177                          }
194
178                          disk_ptr=disk_stats+num_disks;
179 +
180   #ifdef ALLBSD
181 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
181 >                        if (copy_string(&disk_ptr->device_name, mp->f_mntfromname) == NULL) {
182                                  return NULL;
183                          }
184 <
201 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
184 >                        if (copy_string(&disk_ptr->fs_type, mp->f_fstypename) == NULL) {
185                                  return NULL;
186                          }
187 <
205 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
187 >                        if (copy_string(&disk_ptr->mnt_point, mp->f_mntonname) == NULL) {
188                                  return NULL;
189                          }
190  
# Line 216 | Line 198 | disk_stat_t *get_disk_stats(int *entries){
198                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
199   #endif
200   #if defined(LINUX) || defined(CYGWIN)
201 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
201 >                        if (copy_string(&disk_ptr->device_name, mp->mnt_fsname) == NULL) {
202                                  return NULL;
203                          }
204                                  
205 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
205 >                        if (copy_string(&disk_ptr->fs_type, mp->mnt_type) == NULL) {    
206                                  return NULL;
207                          }
208  
209 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
209 >                        if (copy_string(&disk_ptr->mnt_point, mp->mnt_dir) == NULL) {
210                                  return NULL;
211                          }
212                          disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
# Line 238 | Line 220 | disk_stat_t *get_disk_stats(int *entries){
220   #endif
221  
222   #ifdef SOLARIS
241                        /* Memory leak in event of realloc failing */
223                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
224                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
225                           * be upwards of a k each
226                           */
227 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
227 >                        if (copy_string(&disk_ptr->device_name, mp.mnt_special) == NULL) {
228                                  return NULL;
229                          }
230  
231 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
231 >                        if (copy_string(&disk_ptr->fs_type, mp.mnt_fstype) == NULL) {
232                                  return NULL;
233                          }
234          
235 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
235 >                        if (copy_string(&disk_ptr->mnt_point, mp.mnt_mountp) == NULL) {
236                                  return NULL;
237                          }
238                          
# Line 281 | Line 262 | disk_stat_t *get_disk_stats(int *entries){
262          return disk_stats;
263  
264   }
284 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
265  
266 <        for(diskio_stats+=start; start<end; start++){
267 <                diskio_stats->disk_name=NULL;
288 <                
289 <                diskio_stats++;
290 <        }
266 > static void diskio_stat_init(diskio_stat_t *d) {
267 >        d->disk_name = NULL;
268   }
269  
270 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
271 <
295 <        if(diskio_stats==NULL){
296 <
297 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
298 <                        return NULL;
299 <                }
300 <                diskio_stat_init(0, needed_entries, diskio_stats);
301 <                *cur_entries=needed_entries;
302 <
303 <                return diskio_stats;
304 <        }
305 <
306 <
307 <        if(*cur_entries<needed_entries){
308 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
309 <                if(diskio_stats==NULL){
310 <                        return NULL;
311 <                }
312 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
313 <                *cur_entries=needed_entries;
314 <        }
315 <
316 <        return diskio_stats;
270 > static void diskio_stat_destroy(diskio_stat_t *d) {
271 >        free(d->disk_name);
272   }
273  
274 < static diskio_stat_t *diskio_stats=NULL;        
275 < static int num_diskio=0;        
274 > VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10,
275 >                      diskio_stat_init, diskio_stat_destroy);
276  
277   #ifdef LINUX
278   typedef struct {
# Line 327 | Line 282 | typedef struct {
282   #endif
283  
284   diskio_stat_t *get_diskio_stats(int *entries){
285 <
331 <        static int sizeof_diskio_stats=0;
285 >        int num_diskio;
286   #ifndef LINUX
287          diskio_stat_t *diskio_stats_ptr;
288   #endif
# Line 343 | Line 297 | diskio_stat_t *get_diskio_stats(int *entries){
297          char *line_ptr;
298          int major, minor;
299          int has_pp_stats = 1;
300 <        static partition *parts = NULL;
347 <        static int alloc_parts = 0;
300 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
301          int i, n;
302          time_t now;
303          const char *format;
# Line 459 | Line 412 | diskio_stat_t *get_diskio_stats(int *entries){
412                          continue;
413                  }
414  
415 <                diskio_stats = diskio_stat_malloc(num_diskio + 1,
463 <                                                  &sizeof_diskio_stats,
464 <                                                  diskio_stats);
465 <                if (diskio_stats == NULL) {
415 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
416                          return NULL;
417                  }
418                  diskio_stats_ptr = diskio_stats + num_diskio;
# Line 524 | Line 474 | diskio_stat_t *get_diskio_stats(int *entries){
474   #else
475                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
476   #endif
477 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
477 >
478 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
479                          return NULL;
480                  }
481                  diskio_stats_ptr=diskio_stats+num_diskio;
# Line 559 | Line 510 | diskio_stat_t *get_diskio_stats(int *entries){
510                          if((kstat_read(kc, ksp, &kios))==-1){  
511                          }
512                          
513 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
513 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
514                                  kstat_close(kc);
515                                  return NULL;
516                          }
# Line 620 | Line 571 | diskio_stat_t *get_diskio_stats(int *entries){
571                          wsect = 0;
572                  }
573  
574 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
575 <                        diskio_stats);
625 <                if (diskio_stats == NULL) goto out;
626 <                if (n >= alloc_parts) {
627 <                        alloc_parts += 16;
628 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
629 <                        if (parts == NULL) {
630 <                                alloc_parts = 0;
631 <                                goto out;
632 <                        }
574 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
575 >                        goto out;
576                  }
577 +                if (VECTOR_RESIZE(parts, n + 1) < 0) {
578 +                        goto out;
579 +                }
580  
581                  if (diskio_stats[n].disk_name != NULL)
582                          free(diskio_stats[n].disk_name);
# Line 729 | Line 675 | out:
675   }
676  
677   diskio_stat_t *get_diskio_stats_diff(int *entries){
678 <        static diskio_stat_t *diff = NULL;
679 <        static int diff_count = 0;
680 <        diskio_stat_t *src, *dest;
681 <        int i, j, new_count;
678 >        VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1,
679 >                              diskio_stat_init, diskio_stat_destroy);
680 >        diskio_stat_t *src = NULL, *dest;
681 >        int i, j, diff_count, new_count;
682  
683          if (diskio_stats == NULL) {
684                  /* No previous stats, so we can't calculate a difference. */
# Line 740 | Line 686 | diskio_stat_t *get_diskio_stats_diff(int *entries){
686          }
687  
688          /* Resize the results array to match the previous stats. */
689 <        diff = diskio_stat_malloc(num_diskio, &diff_count, diff);
690 <        if (diff == NULL) {
689 >        diff_count = VECTOR_SIZE(diskio_stats);
690 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
691                  return NULL;
692          }
693  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines