5
5
#include <assert.h>
6
6
#include <stdio.h>
7
7
#include <stdlib.h>
8
+ #include "asm/bug.h"
8
9
9
10
static struct cpu_map * cpu_map__default_new (void )
10
11
{
@@ -22,6 +23,7 @@ static struct cpu_map *cpu_map__default_new(void)
22
23
cpus -> map [i ] = i ;
23
24
24
25
cpus -> nr = nr_cpus ;
26
+ atomic_set (& cpus -> refcnt , 1 );
25
27
}
26
28
27
29
return cpus ;
@@ -35,6 +37,7 @@ static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
35
37
if (cpus != NULL ) {
36
38
cpus -> nr = nr_cpus ;
37
39
memcpy (cpus -> map , tmp_cpus , payload_size );
40
+ atomic_set (& cpus -> refcnt , 1 );
38
41
}
39
42
40
43
return cpus ;
@@ -194,14 +197,32 @@ struct cpu_map *cpu_map__dummy_new(void)
194
197
if (cpus != NULL ) {
195
198
cpus -> nr = 1 ;
196
199
cpus -> map [0 ] = -1 ;
200
+ atomic_set (& cpus -> refcnt , 1 );
197
201
}
198
202
199
203
return cpus ;
200
204
}
201
205
202
- void cpu_map__delete (struct cpu_map * map )
206
+ static void cpu_map__delete (struct cpu_map * map )
203
207
{
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 );
205
226
}
206
227
207
228
int 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,
263
284
/* ensure we process id in increasing order */
264
285
qsort (c -> map , c -> nr , sizeof (int ), cmp_ids );
265
286
287
+ atomic_set (& cpus -> refcnt , 1 );
266
288
* res = c ;
267
289
return 0 ;
268
290
}
0 commit comments