1 // RUN: %libomptarget-compile-generic && env LIBOMPTARGET_DEBUG=1 %libomptarget-run-generic 2>&1 | %fcheck-generic
2 // REQUIRES: libomptarget-debug
3 
4 #include <stdlib.h>
5 #include <stdio.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() {
21   ++(*cnt);
22 }
23 #pragma omp end declare target
24 
main(void)25 int main(void) {
26   int *A = allocate(10);
27   int *V = allocate(10);
28   deallocate(A, 10);
29   deallocate(V, 10);
30 // CHECK-NOT: RefCount=2
31   cnt = malloc(sizeof(int));
32   *cnt = 0;
33 #pragma omp target map(cnt[:1])
34   foo();
35   printf("Cnt = %d.\n", *cnt);
36 // CHECK: Cnt = 1.
37   *cnt = 0;
38 #pragma omp target data map(cnt[:1])
39 #pragma omp target
40   foo();
41   printf("Cnt = %d.\n", *cnt);
42 // CHECK: Cnt = 1.
43   free(cnt);
44 
45   return 0;
46 }
47 
48