tls.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00040 #include <tls.h>
00041 #include <malloc.h>
00042 #include <str.h>
00043 #include <align.h>
00044 
00052 tcb_t *__make_tls(void)
00053 {
00054         void *data;
00055         tcb_t *tcb;
00056         size_t tls_size = &_tbss_end - &_tdata_start;
00057         
00058         tcb = __alloc_tls(&data, tls_size);
00059         
00060         /*
00061          * Copy thread local data from the initialization image.
00062          */
00063         memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start);
00064         /*
00065          * Zero out the thread local uninitialized data.
00066          */
00067         memset(data + (&_tbss_start - &_tdata_start), 0,
00068             &_tbss_end - &_tbss_start);
00069 
00070         return tcb;
00071 }
00072 
00073 void __free_tls(tcb_t *tcb)
00074 {
00075         size_t tls_size = &_tbss_end - &_tdata_start;
00076         __free_tls_arch(tcb, tls_size);
00077 }
00078 
00079 #ifdef CONFIG_TLS_VARIANT_1
00080 
00086 tcb_t *tls_alloc_variant_1(void **data, size_t size)
00087 {
00088         tcb_t *result;
00089 
00090         result = malloc(sizeof(tcb_t) + size);
00091         *data = ((void *)result) + sizeof(tcb_t);
00092         return result;
00093 }
00094 
00100 void tls_free_variant_1(tcb_t *tcb, size_t size)
00101 {
00102         free(tcb);
00103 }
00104 #endif
00105 
00106 #ifdef CONFIG_TLS_VARIANT_2
00107 
00114 tcb_t * tls_alloc_variant_2(void **data, size_t size)
00115 {
00116         tcb_t *tcb;
00117         
00118         size = ALIGN_UP(size, &_tls_alignment);
00119         *data = memalign((uintptr_t) &_tls_alignment, sizeof(tcb_t) + size);
00120 
00121         tcb = (tcb_t *) (*data + size);
00122         tcb->self = tcb;
00123 
00124         return tcb;
00125 }
00126 
00132 void tls_free_variant_2(tcb_t *tcb, size_t size)
00133 {
00134         size = ALIGN_UP(size, &_tls_alignment);
00135         void *start = ((void *) tcb) - size;
00136         free(start);
00137 }
00138 #endif
00139 

Generated on Thu Jun 2 07:45:45 2011 for HelenOS/USB by  doxygen 1.4.7