1 /* Copyright (C) 2020 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This file contains wrappers for the system allocation routines. Most
27 places in the OpenMP API do not make any provision for failure, so in
28 general we cannot allow memory allocation to fail. */
34 #define omp_max_predefined_alloc omp_thread_mem_alloc
36 struct omp_allocator_data
38 omp_memspace_handle_t memspace
;
39 omp_uintptr_t alignment
;
40 omp_uintptr_t pool_size
;
41 omp_uintptr_t used_pool_size
;
42 omp_allocator_handle_t fb_data
;
43 unsigned int sync_hint
: 8;
44 unsigned int access
: 8;
45 unsigned int fallback
: 8;
46 unsigned int pinned
: 1;
47 unsigned int partition
: 7;
48 #ifndef HAVE_SYNC_BUILTINS
57 omp_allocator_handle_t allocator
;
61 omp_allocator_handle_t
62 omp_init_allocator (omp_memspace_handle_t memspace
, int ntraits
,
63 const omp_alloctrait_t traits
[])
65 struct omp_allocator_data data
66 = { memspace
, 1, ~(uintptr_t) 0, 0, 0, omp_atv_contended
, omp_atv_all
,
67 omp_atv_default_mem_fb
, omp_atv_false
, omp_atv_environment
};
68 struct omp_allocator_data
*ret
;
71 if (memspace
> omp_low_lat_mem_space
)
72 return omp_null_allocator
;
73 for (i
= 0; i
< ntraits
; i
++)
74 switch (traits
[i
].key
)
76 case omp_atk_sync_hint
:
77 switch (traits
[i
].value
)
80 data
.sync_hint
= omp_atv_contended
;
82 case omp_atv_contended
:
83 case omp_atv_uncontended
:
84 case omp_atv_sequential
:
86 data
.sync_hint
= traits
[i
].value
;
89 return omp_null_allocator
;
92 case omp_atk_alignment
:
93 if (traits
[i
].value
== omp_atv_default
)
98 if ((traits
[i
].value
& (traits
[i
].value
- 1)) != 0
100 return omp_null_allocator
;
101 data
.alignment
= traits
[i
].value
;
104 switch (traits
[i
].value
)
106 case omp_atv_default
:
107 data
.access
= omp_atv_all
;
113 data
.access
= traits
[i
].value
;
116 return omp_null_allocator
;
119 case omp_atk_pool_size
:
120 if (traits
[i
].value
== omp_atv_default
)
121 data
.pool_size
= ~(uintptr_t) 0;
123 data
.pool_size
= traits
[i
].value
;
125 case omp_atk_fallback
:
126 switch (traits
[i
].value
)
128 case omp_atv_default
:
129 data
.fallback
= omp_atv_default_mem_fb
;
131 case omp_atv_default_mem_fb
:
132 case omp_atv_null_fb
:
133 case omp_atv_abort_fb
:
134 case omp_atv_allocator_fb
:
135 data
.fallback
= traits
[i
].value
;
138 return omp_null_allocator
;
141 case omp_atk_fb_data
:
142 data
.fb_data
= traits
[i
].value
;
145 switch (traits
[i
].value
)
147 case omp_atv_default
:
149 data
.pinned
= omp_atv_false
;
152 data
.pinned
= omp_atv_true
;
155 return omp_null_allocator
;
158 case omp_atk_partition
:
159 switch (traits
[i
].value
)
161 case omp_atv_default
:
162 data
.partition
= omp_atv_environment
;
164 case omp_atv_environment
:
165 case omp_atv_nearest
:
166 case omp_atv_blocked
:
167 case omp_atv_interleaved
:
168 data
.partition
= traits
[i
].value
;
171 return omp_null_allocator
;
175 return omp_null_allocator
;
178 if (data
.alignment
< sizeof (void *))
179 data
.alignment
= sizeof (void *);
181 /* No support for these so far (for hbw will use memkind). */
182 if (data
.pinned
|| data
.memspace
== omp_high_bw_mem_space
)
183 return omp_null_allocator
;
185 ret
= gomp_malloc (sizeof (struct omp_allocator_data
));
187 #ifndef HAVE_SYNC_BUILTINS
188 gomp_mutex_init (&ret
->lock
);
190 return (omp_allocator_handle_t
) ret
;
194 omp_destroy_allocator (omp_allocator_handle_t allocator
)
196 if (allocator
!= omp_null_allocator
)
198 #ifndef HAVE_SYNC_BUILTINS
199 gomp_mutex_destroy (&((struct omp_allocator_data
*) allocator
)->lock
);
201 free ((void *) allocator
);
205 ialias (omp_init_allocator
)
206 ialias (omp_destroy_allocator
)
209 omp_alloc (size_t size
, omp_allocator_handle_t allocator
)
211 struct omp_allocator_data
*allocator_data
;
212 size_t alignment
, new_size
;
215 if (__builtin_expect (size
== 0, 0))
219 if (allocator
== omp_null_allocator
)
221 struct gomp_thread
*thr
= gomp_thread ();
222 if (thr
->ts
.def_allocator
== omp_null_allocator
)
223 thr
->ts
.def_allocator
= gomp_def_allocator
;
224 allocator
= (omp_allocator_handle_t
) thr
->ts
.def_allocator
;
227 if (allocator
> omp_max_predefined_alloc
)
229 allocator_data
= (struct omp_allocator_data
*) allocator
;
230 alignment
= allocator_data
->alignment
;
234 allocator_data
= NULL
;
235 alignment
= sizeof (void *);
238 new_size
= sizeof (struct omp_mem_header
);
239 if (alignment
> sizeof (void *))
240 new_size
+= alignment
- sizeof (void *);
241 if (__builtin_add_overflow (size
, new_size
, &new_size
))
244 if (__builtin_expect (allocator_data
245 && allocator_data
->pool_size
< ~(uintptr_t) 0, 0))
247 uintptr_t used_pool_size
;
248 if (new_size
> allocator_data
->pool_size
)
250 #ifdef HAVE_SYNC_BUILTINS
251 used_pool_size
= __atomic_load_n (&allocator_data
->used_pool_size
,
255 uintptr_t new_pool_size
;
256 if (__builtin_add_overflow (used_pool_size
, new_size
,
258 || new_pool_size
> allocator_data
->pool_size
)
260 if (__atomic_compare_exchange_n (&allocator_data
->used_pool_size
,
261 &used_pool_size
, new_pool_size
,
262 true, MEMMODEL_RELAXED
,
268 gomp_mutex_lock (&allocator_data
->lock
);
269 if (__builtin_add_overflow (allocator_data
->used_pool_size
, new_size
,
271 || used_pool_size
> allocator_data
->pool_size
)
273 gomp_mutex_unlock (&allocator_data
->lock
);
276 allocator_data
->used_pool_size
= used_pool_size
;
277 gomp_mutex_unlock (&allocator_data
->lock
);
279 ptr
= malloc (new_size
);
282 #ifdef HAVE_SYNC_BUILTINS
283 __atomic_add_fetch (&allocator_data
->used_pool_size
, -new_size
,
286 gomp_mutex_lock (&allocator_data
->lock
);
287 allocator_data
->used_pool_size
-= new_size
;
288 gomp_mutex_unlock (&allocator_data
->lock
);
295 ptr
= malloc (new_size
);
300 if (alignment
> sizeof (void *))
301 ret
= (void *) (((uintptr_t) ptr
302 + sizeof (struct omp_mem_header
)
303 + alignment
- sizeof (void *)) & ~(alignment
- 1));
305 ret
= (char *) ptr
+ sizeof (struct omp_mem_header
);
306 ((struct omp_mem_header
*) ret
)[-1].ptr
= ptr
;
307 ((struct omp_mem_header
*) ret
)[-1].size
= new_size
;
308 ((struct omp_mem_header
*) ret
)[-1].allocator
= allocator
;
314 switch (allocator_data
->fallback
)
316 case omp_atv_default_mem_fb
:
317 if (alignment
> sizeof (void *)
319 && allocator_data
->pool_size
< ~(uintptr_t) 0))
321 allocator
= omp_default_mem_alloc
;
324 /* Otherwise, we've already performed default mem allocation
325 and if that failed, it won't succeed again (unless it was
326 intermitent. Return NULL then, as that is the fallback. */
328 case omp_atv_null_fb
:
331 case omp_atv_abort_fb
:
332 gomp_fatal ("Out of memory allocating %lu bytes",
333 (unsigned long) size
);
334 case omp_atv_allocator_fb
:
335 allocator
= allocator_data
->fb_data
;
343 omp_free (void *ptr
, omp_allocator_handle_t allocator
)
345 struct omp_mem_header
*data
;
350 data
= &((struct omp_mem_header
*) ptr
)[-1];
351 if (data
->allocator
> omp_max_predefined_alloc
)
353 struct omp_allocator_data
*allocator_data
354 = (struct omp_allocator_data
*) (data
->allocator
);
355 if (allocator_data
->pool_size
< ~(uintptr_t) 0)
357 #ifdef HAVE_SYNC_BUILTINS
358 __atomic_add_fetch (&allocator_data
->used_pool_size
, -data
->size
,
361 gomp_mutex_lock (&allocator_data
->lock
);
362 allocator_data
->used_pool_size
-= data
->size
;
363 gomp_mutex_unlock (&allocator_data
->lock
);