RTEMS
tlsallocsize.c
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 
3 /*
4  * Copyright (C) 2014, 2020 embedded brains GmbH (http://www.embedded-brains.de)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <rtems/score/tls.h>
33 #include <rtems/score/interr.h>
34 #include <rtems/score/thread.h>
35 
36 static uintptr_t _TLS_Allocation_size;
37 
38 uintptr_t _TLS_Get_allocation_size( void )
39 {
40  uintptr_t size;
41  uintptr_t allocation_size;
42  uintptr_t alignment;
43 
44  size = _TLS_Get_size();
45 
46  if ( size == 0 ) {
47  return 0;
48  }
49 
50  allocation_size = _TLS_Allocation_size;
51 
52  if ( allocation_size == 0 ) {
53  allocation_size = _TLS_Heap_align_up( size );
54  alignment = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
55 
56  /*
57  * The stack allocator does not support aligned allocations. Allocate
58  * enough to do the alignment manually.
59  */
60  if ( alignment > CPU_HEAP_ALIGNMENT ) {
61  allocation_size += alignment;
62  }
63 
64  allocation_size += _TLS_Get_thread_control_block_area_size( alignment );
65 
66 #ifndef __i386__
67  allocation_size += sizeof(TLS_Dynamic_thread_vector);
68 #endif
69 
70  if ( _Thread_Maximum_TLS_size != 0 ) {
71  if ( allocation_size <= _Thread_Maximum_TLS_size ) {
72  allocation_size = _Thread_Maximum_TLS_size;
73  } else {
74  _Internal_error( INTERNAL_ERROR_TOO_LARGE_TLS_SIZE );
75  }
76  }
77 
78  _TLS_Allocation_size = allocation_size;
79  }
80 
81  return allocation_size;
82 }
Thread-Local Storage (TLS)
Constants and Prototypes Related to the Internal Error Handler.
uintptr_t _TLS_Get_allocation_size(void)
Return the TLS area allocation size.
Definition: tlsallocsize.c:38
const size_t _Thread_Maximum_TLS_size
If this constant is greater than zero, then it defines the maximum thread-local storage size...
char _TLS_Alignment[]
The TLS section alignment.
static uintptr_t _TLS_Get_size(void)
Gets the TLS size.
Definition: tls.h:108
#define CPU_HEAP_ALIGNMENT
Definition: cpu.h:755
void _Internal_error(Internal_errors_Core_list core_error) RTEMS_NO_RETURN
Terminates the system with an INTERNAL_ERROR_CORE fatal source and the specified core error code...
Definition: interr.c:51
static uintptr_t _TLS_Heap_align_up(uintptr_t val)
Returns the value aligned up to the heap alignment.
Definition: tls.h:129
Constants and Structures Related with the Thread Control Block.
static uintptr_t _TLS_Get_thread_control_block_area_size(uintptr_t alignment)
Returns the size of the thread control block area size for this alignment, or the minimum size if ali...
Definition: tls.h:144