Lines Matching +full:align +full:- +full:size

1 //===---------- emutls.c - Implements __emutls_get_address ---------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
18 // - Android versions older than M lack __cxa_thread_atexit_impl, so apps
20 // - Apps might use __thread/thread_local variables in pthread destructors.
22 // after the final malloc/free call to free its thread-specific data (see
31 // sized element in this array. Disable this for warn-as-error builds.
38 uintptr_t size; // number of elements in the 'data' array member
65 static __inline void *emutls_memalign_alloc(size_t align, size_t size) { in emutls_memalign_alloc() argument
68 if (posix_memalign(&base, align, size) != 0) in emutls_memalign_alloc()
71 #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void *)) in emutls_memalign_alloc()
73 if ((object = (char *)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL) in emutls_memalign_alloc()
76 ~(uintptr_t)(align - 1)); in emutls_memalign_alloc()
78 ((void **)base)[-1] = object; in emutls_memalign_alloc()
87 // The mallocated address is in ((void**)base)[-1] in emutls_memalign_free()
88 free(((void **)base)[-1]); in emutls_memalign_free()
102 if (array->skip_destructor_rounds > 0) { in emutls_key_destructor()
106 // Delay the emutls deallocation to accommodate other end-of-thread in emutls_key_destructor()
109 array->skip_destructor_rounds--; in emutls_key_destructor()
163 static __inline void *emutls_memalign_alloc(size_t align, size_t size) { in emutls_memalign_alloc() argument
164 void *base = _aligned_malloc(size, align); in emutls_memalign_alloc()
239 // These return the previous value - but since we do an OR with 0, in __atomic_load_n()
263 for (i = 0; i < array->size; ++i) { in emutls_shutdown()
264 if (array->data[i]) in emutls_shutdown()
265 emutls_memalign_free(array->data[i]); in emutls_shutdown()
272 // If xyz has non-zero initial value, __emutls_v.xyz's "value"
277 // zeros. We can use variables of size_t to operate on size and
278 // align.
279 gcc_word size; // size of the object in bytes member
280 gcc_word align; // alignment of the object in bytes member
282 uintptr_t index; // data[index-1] is the object address
285 void *value; // null or non-zero initial value for the object
288 // Emulated TLS objects are always allocated at run-time.
294 size_t size = control->size; in emutls_allocate_object() local
295 size_t align = control->align; in emutls_allocate_object() local
297 if (align < sizeof(void *)) in emutls_allocate_object()
298 align = sizeof(void *); in emutls_allocate_object()
299 // Make sure that align is power of 2. in emutls_allocate_object()
300 if ((align & (align - 1)) != 0) in emutls_allocate_object()
303 base = emutls_memalign_alloc(align, size); in emutls_allocate_object()
304 if (control->value) in emutls_allocate_object()
305 memcpy(base, control->value, size); in emutls_allocate_object()
307 memset(base, 0, size); in emutls_allocate_object()
311 // Returns control->object.index; set index if not allocated yet.
313 uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE); in emutls_get_index()
317 index = control->object.index; in emutls_get_index()
320 __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE); in emutls_get_index()
329 uintptr_t size) { in emutls_check_array_set_size() argument
332 array->size = size; in emutls_check_array_set_size()
336 // Returns the new 'data' array size, number of elements,
341 // Round up the emutls_address_array size to multiple of 16. in emutls_new_data_array_size()
343 return ((index + header_words + 15) & ~((uintptr_t)15)) - header_words; in emutls_new_data_array_size()
346 // Returns the size in bytes required for an emutls_address_array with
353 // Extends its size if necessary to hold address at index.
361 memset(array->data, 0, new_size * sizeof(void *)); in emutls_get_address_array()
362 array->skip_destructor_rounds = EMUTLS_SKIP_DESTRUCTOR_ROUNDS; in emutls_get_address_array()
365 } else if (index > array->size) { in emutls_get_address_array()
366 uintptr_t orig_size = array->size; in emutls_get_address_array()
370 memset(array->data + orig_size, 0, in emutls_get_address_array()
371 (new_size - orig_size) * sizeof(void *)); in emutls_get_address_array()
386 // with -Bsymbolic or -Bsymbolic-functions, and it also won't work on Windows,
394 emutls_address_array *array = emutls_get_address_array(index--); in __emutls_get_address()
395 if (array->data[index] == NULL) in __emutls_get_address()
396 array->data[index] = emutls_allocate_object(control); in __emutls_get_address()
397 return array->data[index]; in __emutls_get_address()