xref: /llvm-project/offload/test/env/base_ptr_ref_count.c (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 // RUN: %libomptarget-compile-generic && env LIBOMPTARGET_DEBUG=1 %libomptarget-run-generic 2>&1 | %fcheck-generic
2 // REQUIRES: libomptarget-debug
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
allocate(size_t n)7 int *allocate(size_t n) {
8   int *ptr = malloc(sizeof(int) * n);
9 #pragma omp target enter data map(to : ptr[ : n])
10   return ptr;
11 }
12 
deallocate(int * ptr,size_t n)13 void deallocate(int *ptr, size_t n) {
14 #pragma omp target exit data map(delete : ptr[ : n])
15   free(ptr);
16 }
17 
18 #pragma omp declare target
19 int *cnt;
foo()20 void foo() { ++(*cnt); }
21 #pragma omp end declare target
22 
main(void)23 int main(void) {
24   int *A = allocate(10);
25   int *V = allocate(10);
26   deallocate(A, 10);
27   deallocate(V, 10);
28   // CHECK-NOT: RefCount=2
29   cnt = malloc(sizeof(int));
30   *cnt = 0;
31 #pragma omp target map(cnt[ : 1])
32   foo();
33   printf("Cnt = %d.\n", *cnt);
34   // CHECK: Cnt = 1.
35   *cnt = 0;
36 #pragma omp target data map(cnt[ : 1])
37 #pragma omp target
38   foo();
39   printf("Cnt = %d.\n", *cnt);
40   // CHECK: Cnt = 1.
41   free(cnt);
42 
43   return 0;
44 }
45