xref: /titanic_52/usr/src/lib/libc/port/gen/tsdalloc.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "synonyms.h"
30 #include <stdlib.h>
31 #include <errno.h>
32 #include "mtlib.h"
33 #include "libc.h"
34 #include "tsd.h"
35 
36 typedef void (*pfrv_t)(void *);
37 
38 typedef struct {
39 	void	*buf;
40 	size_t	size;
41 	pfrv_t	destructor;
42 } tsdent_t;
43 
44 static void
45 _free_tsdbuf(void *ptr)
46 {
47 	tsdent_t *loc = ptr;
48 	pfrv_t destructor;
49 	void *p;
50 	int i;
51 
52 	if (loc != NULL) {
53 		for (i = 0; i < _T_NUM_ENTRIES; i++) {
54 			if ((p = loc[i].buf) != NULL) {
55 				destructor = loc[i].destructor;
56 				if (destructor != NULL)
57 					destructor(p);
58 				lfree(p, loc[i].size);
59 			}
60 		}
61 		lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
62 	}
63 }
64 
65 void *
66 tsdalloc(__tsd_item_t n, size_t size, pfrv_t destructor)
67 {
68 	static int		once_key = 0;
69 	static mutex_t		key_lock = DEFAULTMUTEX;
70 	static thread_key_t	key;
71 	tsdent_t		*loc;
72 	void			*p;
73 	int			error;
74 
75 	if ((uint_t)n >= _T_NUM_ENTRIES) {
76 		errno = ENOTSUP;
77 		return (NULL);
78 	}
79 
80 	if (once_key == 0) {
81 		lmutex_lock(&key_lock);
82 		if (once_key == 0) {
83 			if ((error = _thr_keycreate(&key, _free_tsdbuf)) != 0) {
84 				lmutex_unlock(&key_lock);
85 				errno = error;
86 				return (NULL);
87 			}
88 			once_key = 1;
89 		}
90 		lmutex_unlock(&key_lock);
91 	}
92 
93 	if ((loc = _pthread_getspecific(key)) != NULL) {
94 		if ((p = loc[n].buf) != NULL)
95 			return (p);
96 	} else {
97 		/* allocate our array of pointers */
98 		loc = lmalloc(_T_NUM_ENTRIES * sizeof (tsdent_t));
99 		if (loc == NULL)
100 			return (NULL);
101 		if ((error = _thr_setspecific(key, loc)) != 0) {
102 			lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
103 			errno = error;
104 			return (NULL);
105 		}
106 	}
107 
108 	/* allocate item n */
109 	loc[n].buf = p = lmalloc(size);
110 	loc[n].size = size;
111 	loc[n].destructor = destructor;
112 	return (p);
113 }
114