55#include <assert.h>
66#include <stdio.h>
77#include <stdlib.h>
8+ #include "asm/bug.h"
89
910static struct cpu_map * cpu_map__default_new (void )
1011{
@@ -22,6 +23,7 @@ static struct cpu_map *cpu_map__default_new(void)
2223 cpus -> map [i ] = i ;
2324
2425 cpus -> nr = nr_cpus ;
26+ atomic_set (& cpus -> refcnt , 1 );
2527 }
2628
2729 return cpus ;
@@ -35,6 +37,7 @@ static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
3537 if (cpus != NULL ) {
3638 cpus -> nr = nr_cpus ;
3739 memcpy (cpus -> map , tmp_cpus , payload_size );
40+ atomic_set (& cpus -> refcnt , 1 );
3841 }
3942
4043 return cpus ;
@@ -194,14 +197,32 @@ struct cpu_map *cpu_map__dummy_new(void)
194197 if (cpus != NULL ) {
195198 cpus -> nr = 1 ;
196199 cpus -> map [0 ] = -1 ;
200+ atomic_set (& cpus -> refcnt , 1 );
197201 }
198202
199203 return cpus ;
200204}
201205
202- void cpu_map__delete (struct cpu_map * map )
206+ static void cpu_map__delete (struct cpu_map * map )
203207{
204- free (map );
208+ if (map ) {
209+ WARN_ONCE (atomic_read (& map -> refcnt ) != 0 ,
210+ "cpu_map refcnt unbalanced\n" );
211+ free (map );
212+ }
213+ }
214+
215+ struct cpu_map * cpu_map__get (struct cpu_map * map )
216+ {
217+ if (map )
218+ atomic_inc (& map -> refcnt );
219+ return map ;
220+ }
221+
222+ void cpu_map__put (struct cpu_map * map )
223+ {
224+ if (map && atomic_dec_and_test (& map -> refcnt ))
225+ cpu_map__delete (map );
205226}
206227
207228int cpu_map__get_socket (struct cpu_map * map , int idx )
@@ -263,6 +284,7 @@ static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
263284 /* ensure we process id in increasing order */
264285 qsort (c -> map , c -> nr , sizeof (int ), cmp_ids );
265286
287+ atomic_set (& cpus -> refcnt , 1 );
266288 * res = c ;
267289 return 0 ;
268290}
0 commit comments