xref: /titanic_50/usr/src/lib/libc/port/gen/tsdalloc.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21cb620785Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29*7257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7257d1b4Sraf #include <pthread.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include "mtlib.h"
347c478bd9Sstevel@tonic-gate #include "libc.h"
357c478bd9Sstevel@tonic-gate #include "tsd.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate typedef void (*pfrv_t)(void *);
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate typedef struct {
407c478bd9Sstevel@tonic-gate 	void	*buf;
417c478bd9Sstevel@tonic-gate 	size_t	size;
427c478bd9Sstevel@tonic-gate 	pfrv_t	destructor;
437c478bd9Sstevel@tonic-gate } tsdent_t;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static void
_free_tsdbuf(void * ptr)467c478bd9Sstevel@tonic-gate _free_tsdbuf(void *ptr)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	tsdent_t *loc = ptr;
497c478bd9Sstevel@tonic-gate 	pfrv_t destructor;
507c478bd9Sstevel@tonic-gate 	void *p;
517c478bd9Sstevel@tonic-gate 	int i;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (loc != NULL) {
547c478bd9Sstevel@tonic-gate 		for (i = 0; i < _T_NUM_ENTRIES; i++) {
557c478bd9Sstevel@tonic-gate 			if ((p = loc[i].buf) != NULL) {
567c478bd9Sstevel@tonic-gate 				destructor = loc[i].destructor;
577c478bd9Sstevel@tonic-gate 				if (destructor != NULL)
587c478bd9Sstevel@tonic-gate 					destructor(p);
597c478bd9Sstevel@tonic-gate 				lfree(p, loc[i].size);
607c478bd9Sstevel@tonic-gate 			}
617c478bd9Sstevel@tonic-gate 		}
627c478bd9Sstevel@tonic-gate 		lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate void *
tsdalloc(__tsd_item_t n,size_t size,pfrv_t destructor)677c478bd9Sstevel@tonic-gate tsdalloc(__tsd_item_t n, size_t size, pfrv_t destructor)
687c478bd9Sstevel@tonic-gate {
69cb620785Sraf 	static thread_key_t	key = THR_ONCE_KEY;
707c478bd9Sstevel@tonic-gate 	tsdent_t		*loc;
717c478bd9Sstevel@tonic-gate 	void			*p;
727c478bd9Sstevel@tonic-gate 	int			error;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((uint_t)n >= _T_NUM_ENTRIES) {
757c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
767c478bd9Sstevel@tonic-gate 		return (NULL);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
79*7257d1b4Sraf 	if ((error = thr_keycreate_once(&key, _free_tsdbuf)) != 0) {
807c478bd9Sstevel@tonic-gate 		errno = error;
817c478bd9Sstevel@tonic-gate 		return (NULL);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
84*7257d1b4Sraf 	if ((loc = pthread_getspecific(key)) != NULL) {
857c478bd9Sstevel@tonic-gate 		if ((p = loc[n].buf) != NULL)
867c478bd9Sstevel@tonic-gate 			return (p);
877c478bd9Sstevel@tonic-gate 	} else {
887c478bd9Sstevel@tonic-gate 		/* allocate our array of pointers */
897c478bd9Sstevel@tonic-gate 		loc = lmalloc(_T_NUM_ENTRIES * sizeof (tsdent_t));
907c478bd9Sstevel@tonic-gate 		if (loc == NULL)
917c478bd9Sstevel@tonic-gate 			return (NULL);
92*7257d1b4Sraf 		if ((error = thr_setspecific(key, loc)) != 0) {
937c478bd9Sstevel@tonic-gate 			lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
947c478bd9Sstevel@tonic-gate 			errno = error;
957c478bd9Sstevel@tonic-gate 			return (NULL);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* allocate item n */
1007c478bd9Sstevel@tonic-gate 	loc[n].buf = p = lmalloc(size);
1017c478bd9Sstevel@tonic-gate 	loc[n].size = size;
1027c478bd9Sstevel@tonic-gate 	loc[n].destructor = destructor;
1037c478bd9Sstevel@tonic-gate 	return (p);
1047c478bd9Sstevel@tonic-gate }
105