Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / perf / util / sharded_mutex.c
blobe11e8d0945a753069efeffe24f7f5522174151c7
1 // SPDX-License-Identifier: GPL-2.0
2 #include "sharded_mutex.h"
4 #include <stdlib.h>
6 struct sharded_mutex *sharded_mutex__new(size_t num_shards)
8 struct sharded_mutex *result;
9 size_t size;
10 unsigned int bits;
12 for (bits = 0; ((size_t)1 << bits) < num_shards; bits++)
15 size = sizeof(*result) + sizeof(struct mutex) * (1 << bits);
16 result = malloc(size);
17 if (!result)
18 return NULL;
20 result->cap_bits = bits;
21 for (size_t i = 0; i < ((size_t)1 << bits); i++)
22 mutex_init(&result->mutexes[i]);
24 return result;
27 void sharded_mutex__delete(struct sharded_mutex *sm)
29 for (size_t i = 0; i < ((size_t)1 << sm->cap_bits); i++)
30 mutex_destroy(&sm->mutexes[i]);
32 free(sm);