"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "gmemusage-0.2/hash.c" of archive gmemusage-0.2.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 * hash.c
3 * Process hashing functions for gmemusage.
4 *
5 * Copyright (C) 1997, 1998 by Raju Mathur (raju@sgi.com)
6 *
7 * See file COPYING (included in this distribution) for copyright information.
8 */
9 #include <stdio.h>
10 #include <malloc.h>
11 #include "common.h"
12
13 /*
14 {
15 */
16 const int
17 minProcs = 16 ;
18 static struct ProcInfo
19 *procs = NULL ;
20 static struct ProcInfo
21 *nextproc = NULL ;
22 static int
23 nProcs = 0 ;
24 static int
25 lastallocated ;
26 /*
27 }
28 */
29 /*
30 * Find a process with matching name in the process array.
31 * Currently using linear search, enhance to any method you
32 * choose. Hash may not be a bad idea.
33 */
34 struct ProcInfo
35 *LookupProc ( char *name )
36 {
37 register struct ProcInfo
38 *pi ;
39
40 for ( pi = procs ; pi - procs < nProcs ; pi++ )
41 {
42 if ( !strcmp ( pi -> procname , name ) )
43 {
44 return ( pi ) ;
45 }
46 }
47 return NULL ;
48 }
49 /*
50 * Update existing process entry, or add a process to the
51 * process array.
52 */
53 void
54 addProc ( char *procname , int mem , int rss )
55 {
56 register struct ProcInfo
57 *thisproc ;
58 /*
59 * is the array empty?
60 */
61 if ( !procs )
62 {
63 lastallocated = minProcs ;
64 if ( ( procs = calloc ( minProcs , sizeof ( struct ProcInfo ) ) )
65 == NULL )
66 {
67 fprintf ( stderr , "%s: cannot alloc %d processes" , progname ,
68 minProcs ) ;
69 perror ( "" ) ;
70 exit ( 1 ) ;
71 }
72 /* printf("allocated %d procs\n",lastallocated); */
73 thisproc = nextproc = procs ;
74 strcpy ( thisproc -> procname , procname ) ;
75 thisproc -> totMem = mem ;
76 thisproc -> totRSS = rss ;
77 thisproc -> nProcs = 1 ;
78 nProcs = 1 ;
79 }
80 /*
81 * if a process with that name doesn't already exist in the
82 * array, make a new entry. Allocate more space if necessary.
83 */
84 else if ( !( thisproc = LookupProc ( procname ) ) )
85 {
86 if ( nProcs == lastallocated ) /* no more space */
87 {
88 lastallocated *= 2 ;
89 if ( ( procs = realloc
90 ( procs , lastallocated * sizeof ( struct ProcInfo) ) )
91 == NULL )
92 {
93 fprintf ( stderr , "%s: cannot alloc %d processes" ,
94 progname , lastallocated ) ;
95 perror ( "" ) ;
96 exit ( 1 ) ;
97 }
98 /* printf("allocated %d procs\n",lastallocated); */
99 }
100 thisproc = procs + nProcs ;
101 strcpy ( thisproc -> procname , procname ) ;
102 thisproc -> totMem = mem ;
103 thisproc -> totRSS = rss ;
104 thisproc -> nProcs = 1 ;
105 nProcs++ ;
106 }
107 else
108 {
109 thisproc -> totMem += mem ;
110 thisproc -> totRSS += rss ;
111 thisproc -> nProcs++ ;
112 }
113 }
114 /*
115 * Return the next process linearly from the process
116 * array, or NULL if no more.
117 */
118 struct ProcInfo
119 *NextProc ( void )
120 {
121 if ( nextproc - procs == nProcs )
122 {
123 return NULL ;
124 }
125 else
126 {
127 return nextproc++ ;
128 }
129 }
130 struct ProcInfo
131 *AllProcs ( int *n )
132 {
133 *n = nProcs ;
134 return procs ;
135 }
136 /*
137 * Clear out and deallocate the process list and
138 * associated variables.
139 */
140 void
141 ClearProcs ( void )
142 {
143 if ( procs )
144 {
145 free ( procs ) ;
146 procs = NULL ;
147 lastallocated = nProcs = 0 ;
148 nextproc = NULL ;
149 }
150 }