1// RUN: %libomptarget-compile-run-and-check-generic
2
3// REQUIRES: libc
4
5#include <stdio.h>
6#include <stdlib.h>
7
8#pragma omp declare target to(malloc)
9#pragma omp declare target to(free)
10
11int main() {
12 unsigned h_x;
13 unsigned *d_x;
14#pragma omp target map(from : d_x)
15 {
16 d_x = (unsigned *)malloc(size: sizeof(unsigned));
17 *d_x = 1;
18 }
19
20#pragma omp target is_device_ptr(d_x) map(from : h_x)
21 { h_x = *d_x; }
22
23#pragma omp target is_device_ptr(d_x)
24 { free(ptr: d_x); }
25
26#pragma omp target teams num_teams(64)
27#pragma omp parallel num_threads(32)
28 {
29 int *ptr = (int *)malloc(size: sizeof(int));
30 *ptr = 42;
31 free(ptr: ptr);
32 }
33
34 // CHECK: PASS
35 if (h_x == 1)
36 fputs(s: "PASS\n", stdout);
37}
38

source code of offload/test/libc/malloc.c