Skip to content

Commit 7fd12a5

Browse files
authored
Add likely() hints for gcc
1 parent 2ba9a56 commit 7fd12a5

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

driver/others/memory.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7373

7474
#include "common.h"
7575

76+
#ifndef likely
77+
#ifdef __GNUC__
78+
#define likely(x) __builtin_expect(!!(x), 1)
79+
#else
80+
#define likely(x) (x)
81+
#endif
82+
#endif
83+
7684
#if defined(USE_TLS) && defined(SMP)
7785
#define COMPILE_TLS
7886

@@ -2111,7 +2119,7 @@ static void *alloc_mmap(void *address){
21112119
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
21122120
LOCK_COMMAND(&alloc_lock);
21132121
#endif
2114-
if (release_pos < NUM_BUFFERS) {
2122+
if (likely(release_pos < NUM_BUFFERS)) {
21152123
release_info[release_pos].address = map_address;
21162124
release_info[release_pos].func = alloc_mmap_free;
21172125
} else {
@@ -2280,7 +2288,7 @@ static void *alloc_mmap(void *address){
22802288
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
22812289
LOCK_COMMAND(&alloc_lock);
22822290
#endif
2283-
if (release_pos < NUM_BUFFERS) {
2291+
if (likely(release_pos < NUM_BUFFERS)) {
22842292
release_info[release_pos].address = map_address;
22852293
release_info[release_pos].func = alloc_mmap_free;
22862294
} else {
@@ -2318,7 +2326,7 @@ static void *alloc_malloc(void *address){
23182326
if (map_address == (void *)NULL) map_address = (void *)-1;
23192327

23202328
if (map_address != (void *)-1) {
2321-
if (release_pos < NUM_BUFFERS) {
2329+
if (likely(release_pos < NUM_BUFFERS)) {
23222330
release_info[release_pos].address = map_address;
23232331
release_info[release_pos].func = alloc_malloc_free;
23242332
} else {
@@ -2357,7 +2365,7 @@ static void *alloc_qalloc(void *address){
23572365
if (map_address == (void *)NULL) map_address = (void *)-1;
23582366

23592367
if (map_address != (void *)-1) {
2360-
if (release_pos < NUM_BUFFERS) {
2368+
if (likely(release_pos < NUM_BUFFERS)) {
23612369
release_info[release_pos].address = map_address;
23622370
release_info[release_pos].func = alloc_qalloc_free;
23632371
} else {
@@ -2391,7 +2399,7 @@ static void *alloc_windows(void *address){
23912399
if (map_address == (void *)NULL) map_address = (void *)-1;
23922400

23932401
if (map_address != (void *)-1) {
2394-
if (release_pos < NUM_BUFFERS) {
2402+
if (likely(release_pos < NUM_BUFFERS)) {
23952403
release_info[release_pos].address = map_address;
23962404
release_info[release_pos].func = alloc_windows_free;
23972405
} else {
@@ -2440,7 +2448,7 @@ static void *alloc_devicedirver(void *address){
24402448
fd, 0);
24412449

24422450
if (map_address != (void *)-1) {
2443-
if (release_pos < NUM_BUFFERS) {
2451+
if (likely(release_pos < NUM_BUFFERS)) {
24442452
release_info[release_pos].address = map_address;
24452453
release_info[release_pos].attr = fd;
24462454
release_info[release_pos].func = alloc_devicedirver_free;
@@ -2482,7 +2490,7 @@ static void *alloc_shm(void *address){
24822490

24832491
shmctl(shmid, IPC_RMID, 0);
24842492

2485-
if (release_pos < NUM_BUFFERS) {
2493+
if (likely(release_pos < NUM_BUFFERS)) {
24862494
release_info[release_pos].address = map_address;
24872495
release_info[release_pos].attr = shmid;
24882496
release_info[release_pos].func = alloc_shm_free;
@@ -2594,7 +2602,7 @@ static void *alloc_hugetlb(void *address){
25942602
#endif
25952603

25962604
if (map_address != (void *)-1){
2597-
if (release_pos < NUM_BUFFERS) {
2605+
if (likely(release_pos < NUM_BUFFERS)) {
25982606
release_info[release_pos].address = map_address;
25992607
release_info[release_pos].func = alloc_hugetlb_free;
26002608
} else {
@@ -2647,7 +2655,7 @@ static void *alloc_hugetlbfile(void *address){
26472655
fd, 0);
26482656

26492657
if (map_address != (void *)-1) {
2650-
if (release_pos < NUM_BUFFERS) {
2658+
if (likely(release_pos < NUM_BUFFERS)) {
26512659
release_info[release_pos].address = map_address;
26522660
release_info[release_pos].attr = fd;
26532661
release_info[release_pos].func = alloc_hugetlbfile_free;
@@ -3153,7 +3161,7 @@ void blas_shutdown(void){
31533161
LOCK_COMMAND(&alloc_lock);
31543162

31553163
for (pos = 0; pos < release_pos; pos ++) {
3156-
if (pos < NUM_BUFFERS)
3164+
if (likely(pos < NUM_BUFFERS))
31573165
release_info[pos].func(&release_info[pos]);
31583166
else
31593167
new_release_info[pos-NUM_BUFFERS].func(&new_release_info[pos-NUM_BUFFERS]);

0 commit comments

Comments
 (0)