xref: /onnv-gate/usr/src/lib/libc/port/gen/tsdalloc.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53864Sraf  * Common Development and Distribution License (the "License").
63864Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213864Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include <stdlib.h>
31*6812Sraf #include <pthread.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include "mtlib.h"
340Sstevel@tonic-gate #include "libc.h"
350Sstevel@tonic-gate #include "tsd.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate typedef void (*pfrv_t)(void *);
380Sstevel@tonic-gate 
390Sstevel@tonic-gate typedef struct {
400Sstevel@tonic-gate 	void	*buf;
410Sstevel@tonic-gate 	size_t	size;
420Sstevel@tonic-gate 	pfrv_t	destructor;
430Sstevel@tonic-gate } tsdent_t;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static void
_free_tsdbuf(void * ptr)460Sstevel@tonic-gate _free_tsdbuf(void *ptr)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	tsdent_t *loc = ptr;
490Sstevel@tonic-gate 	pfrv_t destructor;
500Sstevel@tonic-gate 	void *p;
510Sstevel@tonic-gate 	int i;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if (loc != NULL) {
540Sstevel@tonic-gate 		for (i = 0; i < _T_NUM_ENTRIES; i++) {
550Sstevel@tonic-gate 			if ((p = loc[i].buf) != NULL) {
560Sstevel@tonic-gate 				destructor = loc[i].destructor;
570Sstevel@tonic-gate 				if (destructor != NULL)
580Sstevel@tonic-gate 					destructor(p);
590Sstevel@tonic-gate 				lfree(p, loc[i].size);
600Sstevel@tonic-gate 			}
610Sstevel@tonic-gate 		}
620Sstevel@tonic-gate 		lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate void *
tsdalloc(__tsd_item_t n,size_t size,pfrv_t destructor)670Sstevel@tonic-gate tsdalloc(__tsd_item_t n, size_t size, pfrv_t destructor)
680Sstevel@tonic-gate {
693864Sraf 	static thread_key_t	key = THR_ONCE_KEY;
700Sstevel@tonic-gate 	tsdent_t		*loc;
710Sstevel@tonic-gate 	void			*p;
720Sstevel@tonic-gate 	int			error;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if ((uint_t)n >= _T_NUM_ENTRIES) {
750Sstevel@tonic-gate 		errno = ENOTSUP;
760Sstevel@tonic-gate 		return (NULL);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 
79*6812Sraf 	if ((error = thr_keycreate_once(&key, _free_tsdbuf)) != 0) {
803864Sraf 		errno = error;
813864Sraf 		return (NULL);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
84*6812Sraf 	if ((loc = pthread_getspecific(key)) != NULL) {
850Sstevel@tonic-gate 		if ((p = loc[n].buf) != NULL)
860Sstevel@tonic-gate 			return (p);
870Sstevel@tonic-gate 	} else {
880Sstevel@tonic-gate 		/* allocate our array of pointers */
890Sstevel@tonic-gate 		loc = lmalloc(_T_NUM_ENTRIES * sizeof (tsdent_t));
900Sstevel@tonic-gate 		if (loc == NULL)
910Sstevel@tonic-gate 			return (NULL);
92*6812Sraf 		if ((error = thr_setspecific(key, loc)) != 0) {
930Sstevel@tonic-gate 			lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
940Sstevel@tonic-gate 			errno = error;
950Sstevel@tonic-gate 			return (NULL);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/* allocate item n */
1000Sstevel@tonic-gate 	loc[n].buf = p = lmalloc(size);
1010Sstevel@tonic-gate 	loc[n].size = size;
1020Sstevel@tonic-gate 	loc[n].destructor = destructor;
1030Sstevel@tonic-gate 	return (p);
1040Sstevel@tonic-gate }
105