1010#include <ctype.h>
1111#include <limits.h>
1212
13- struct perf_cpu_map * perf_cpu_map__dummy_new ( void )
13+ static struct perf_cpu_map * perf_cpu_map__alloc ( int nr_cpus )
1414{
15- struct perf_cpu_map * cpus = malloc (sizeof (* cpus ) + sizeof (int ) );
15+ struct perf_cpu_map * cpus = malloc (sizeof (* cpus ) + sizeof (struct perf_cpu ) * nr_cpus );
1616
1717 if (cpus != NULL ) {
18- cpus -> nr = 1 ;
19- cpus -> map [0 ] = -1 ;
18+ cpus -> nr = nr_cpus ;
2019 refcount_set (& cpus -> refcnt , 1 );
20+
2121 }
22+ return cpus ;
23+ }
24+
25+ struct perf_cpu_map * perf_cpu_map__dummy_new (void )
26+ {
27+ struct perf_cpu_map * cpus = perf_cpu_map__alloc (1 );
28+
29+ if (cpus )
30+ cpus -> map [0 ].cpu = -1 ;
2231
2332 return cpus ;
2433}
@@ -54,15 +63,12 @@ static struct perf_cpu_map *cpu_map__default_new(void)
5463 if (nr_cpus < 0 )
5564 return NULL ;
5665
57- cpus = malloc ( sizeof ( * cpus ) + nr_cpus * sizeof ( int ) );
66+ cpus = perf_cpu_map__alloc ( nr_cpus );
5867 if (cpus != NULL ) {
5968 int i ;
6069
6170 for (i = 0 ; i < nr_cpus ; ++ i )
62- cpus -> map [i ] = i ;
63-
64- cpus -> nr = nr_cpus ;
65- refcount_set (& cpus -> refcnt , 1 );
71+ cpus -> map [i ].cpu = i ;
6672 }
6773
6874 return cpus ;
@@ -73,39 +79,40 @@ struct perf_cpu_map *perf_cpu_map__default_new(void)
7379 return cpu_map__default_new ();
7480}
7581
76- static int cmp_int (const void * a , const void * b )
82+
83+ static int cmp_cpu (const void * a , const void * b )
7784{
78- return * (const int * )a - * (const int * )b ;
85+ const struct perf_cpu * cpu_a = a , * cpu_b = b ;
86+
87+ return cpu_a -> cpu - cpu_b -> cpu ;
7988}
8089
81- static struct perf_cpu_map * cpu_map__trim_new (int nr_cpus , int * tmp_cpus )
90+ static struct perf_cpu_map * cpu_map__trim_new (int nr_cpus , const struct perf_cpu * tmp_cpus )
8291{
83- size_t payload_size = nr_cpus * sizeof (int );
84- struct perf_cpu_map * cpus = malloc ( sizeof ( * cpus ) + payload_size );
92+ size_t payload_size = nr_cpus * sizeof (struct perf_cpu );
93+ struct perf_cpu_map * cpus = perf_cpu_map__alloc ( nr_cpus );
8594 int i , j ;
8695
8796 if (cpus != NULL ) {
8897 memcpy (cpus -> map , tmp_cpus , payload_size );
89- qsort (cpus -> map , nr_cpus , sizeof (int ), cmp_int );
98+ qsort (cpus -> map , nr_cpus , sizeof (struct perf_cpu ), cmp_cpu );
9099 /* Remove dups */
91100 j = 0 ;
92101 for (i = 0 ; i < nr_cpus ; i ++ ) {
93- if (i == 0 || cpus -> map [i ] != cpus -> map [i - 1 ])
94- cpus -> map [j ++ ] = cpus -> map [i ];
102+ if (i == 0 || cpus -> map [i ]. cpu != cpus -> map [i - 1 ]. cpu )
103+ cpus -> map [j ++ ]. cpu = cpus -> map [i ]. cpu ;
95104 }
96105 cpus -> nr = j ;
97106 assert (j <= nr_cpus );
98- refcount_set (& cpus -> refcnt , 1 );
99107 }
100-
101108 return cpus ;
102109}
103110
104111struct perf_cpu_map * perf_cpu_map__read (FILE * file )
105112{
106113 struct perf_cpu_map * cpus = NULL ;
107114 int nr_cpus = 0 ;
108- int * tmp_cpus = NULL , * tmp ;
115+ struct perf_cpu * tmp_cpus = NULL , * tmp ;
109116 int max_entries = 0 ;
110117 int n , cpu , prev ;
111118 char sep ;
@@ -124,24 +131,24 @@ struct perf_cpu_map *perf_cpu_map__read(FILE *file)
124131
125132 if (new_max >= max_entries ) {
126133 max_entries = new_max + MAX_NR_CPUS / 2 ;
127- tmp = realloc (tmp_cpus , max_entries * sizeof (int ));
134+ tmp = realloc (tmp_cpus , max_entries * sizeof (struct perf_cpu ));
128135 if (tmp == NULL )
129136 goto out_free_tmp ;
130137 tmp_cpus = tmp ;
131138 }
132139
133140 while (++ prev < cpu )
134- tmp_cpus [nr_cpus ++ ] = prev ;
141+ tmp_cpus [nr_cpus ++ ]. cpu = prev ;
135142 }
136143 if (nr_cpus == max_entries ) {
137144 max_entries += MAX_NR_CPUS ;
138- tmp = realloc (tmp_cpus , max_entries * sizeof (int ));
145+ tmp = realloc (tmp_cpus , max_entries * sizeof (struct perf_cpu ));
139146 if (tmp == NULL )
140147 goto out_free_tmp ;
141148 tmp_cpus = tmp ;
142149 }
143150
144- tmp_cpus [nr_cpus ++ ] = cpu ;
151+ tmp_cpus [nr_cpus ++ ]. cpu = cpu ;
145152 if (n == 2 && sep == '-' )
146153 prev = cpu ;
147154 else
@@ -179,7 +186,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
179186 unsigned long start_cpu , end_cpu = 0 ;
180187 char * p = NULL ;
181188 int i , nr_cpus = 0 ;
182- int * tmp_cpus = NULL , * tmp ;
189+ struct perf_cpu * tmp_cpus = NULL , * tmp ;
183190 int max_entries = 0 ;
184191
185192 if (!cpu_list )
@@ -220,17 +227,17 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
220227 for (; start_cpu <= end_cpu ; start_cpu ++ ) {
221228 /* check for duplicates */
222229 for (i = 0 ; i < nr_cpus ; i ++ )
223- if (tmp_cpus [i ] == (int )start_cpu )
230+ if (tmp_cpus [i ]. cpu == (int )start_cpu )
224231 goto invalid ;
225232
226233 if (nr_cpus == max_entries ) {
227234 max_entries += MAX_NR_CPUS ;
228- tmp = realloc (tmp_cpus , max_entries * sizeof (int ));
235+ tmp = realloc (tmp_cpus , max_entries * sizeof (struct perf_cpu ));
229236 if (tmp == NULL )
230237 goto invalid ;
231238 tmp_cpus = tmp ;
232239 }
233- tmp_cpus [nr_cpus ++ ] = (int )start_cpu ;
240+ tmp_cpus [nr_cpus ++ ]. cpu = (int )start_cpu ;
234241 }
235242 if (* p )
236243 ++ p ;
@@ -250,12 +257,16 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
250257 return cpus ;
251258}
252259
253- int perf_cpu_map__cpu (const struct perf_cpu_map * cpus , int idx )
260+ struct perf_cpu perf_cpu_map__cpu (const struct perf_cpu_map * cpus , int idx )
254261{
262+ struct perf_cpu result = {
263+ .cpu = -1
264+ };
265+
255266 if (cpus && idx < cpus -> nr )
256267 return cpus -> map [idx ];
257268
258- return -1 ;
269+ return result ;
259270}
260271
261272int perf_cpu_map__nr (const struct perf_cpu_map * cpus )
@@ -265,10 +276,10 @@ int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
265276
266277bool perf_cpu_map__empty (const struct perf_cpu_map * map )
267278{
268- return map ? map -> map [0 ] == -1 : true;
279+ return map ? map -> map [0 ]. cpu == -1 : true;
269280}
270281
271- int perf_cpu_map__idx (const struct perf_cpu_map * cpus , int cpu )
282+ int perf_cpu_map__idx (const struct perf_cpu_map * cpus , struct perf_cpu cpu )
272283{
273284 int low , high ;
274285
@@ -278,13 +289,13 @@ int perf_cpu_map__idx(const struct perf_cpu_map *cpus, int cpu)
278289 low = 0 ;
279290 high = cpus -> nr ;
280291 while (low < high ) {
281- int idx = (low + high ) / 2 ,
282- cpu_at_idx = cpus -> map [idx ];
292+ int idx = (low + high ) / 2 ;
293+ struct perf_cpu cpu_at_idx = cpus -> map [idx ];
283294
284- if (cpu_at_idx == cpu )
295+ if (cpu_at_idx . cpu == cpu . cpu )
285296 return idx ;
286297
287- if (cpu_at_idx > cpu )
298+ if (cpu_at_idx . cpu > cpu . cpu )
288299 high = idx ;
289300 else
290301 low = idx + 1 ;
@@ -293,15 +304,19 @@ int perf_cpu_map__idx(const struct perf_cpu_map *cpus, int cpu)
293304 return -1 ;
294305}
295306
296- bool perf_cpu_map__has (const struct perf_cpu_map * cpus , int cpu )
307+ bool perf_cpu_map__has (const struct perf_cpu_map * cpus , struct perf_cpu cpu )
297308{
298309 return perf_cpu_map__idx (cpus , cpu ) != -1 ;
299310}
300311
301- int perf_cpu_map__max (struct perf_cpu_map * map )
312+ struct perf_cpu perf_cpu_map__max (struct perf_cpu_map * map )
302313{
314+ struct perf_cpu result = {
315+ .cpu = -1
316+ };
317+
303318 // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well.
304- return map -> nr > 0 ? map -> map [map -> nr - 1 ] : -1 ;
319+ return map -> nr > 0 ? map -> map [map -> nr - 1 ] : result ;
305320}
306321
307322/*
@@ -315,7 +330,7 @@ int perf_cpu_map__max(struct perf_cpu_map *map)
315330struct perf_cpu_map * perf_cpu_map__merge (struct perf_cpu_map * orig ,
316331 struct perf_cpu_map * other )
317332{
318- int * tmp_cpus ;
333+ struct perf_cpu * tmp_cpus ;
319334 int tmp_len ;
320335 int i , j , k ;
321336 struct perf_cpu_map * merged ;
@@ -329,19 +344,19 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
329344 if (!other )
330345 return orig ;
331346 if (orig -> nr == other -> nr &&
332- !memcmp (orig -> map , other -> map , orig -> nr * sizeof (int )))
347+ !memcmp (orig -> map , other -> map , orig -> nr * sizeof (struct perf_cpu )))
333348 return orig ;
334349
335350 tmp_len = orig -> nr + other -> nr ;
336- tmp_cpus = malloc (tmp_len * sizeof (int ));
351+ tmp_cpus = malloc (tmp_len * sizeof (struct perf_cpu ));
337352 if (!tmp_cpus )
338353 return NULL ;
339354
340355 /* Standard merge algorithm from wikipedia */
341356 i = j = k = 0 ;
342357 while (i < orig -> nr && j < other -> nr ) {
343- if (orig -> map [i ] <= other -> map [j ]) {
344- if (orig -> map [i ] == other -> map [j ])
358+ if (orig -> map [i ]. cpu <= other -> map [j ]. cpu ) {
359+ if (orig -> map [i ]. cpu == other -> map [j ]. cpu )
345360 j ++ ;
346361 tmp_cpus [k ++ ] = orig -> map [i ++ ];
347362 } else
0 commit comments