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
27*7257d1b4Sraf #include "lint.h"
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7257d1b4Sraf #include <pthread.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include "mtlib.h"
327c478bd9Sstevel@tonic-gate #include "libc.h"
337c478bd9Sstevel@tonic-gate #include "tsd.h"
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate typedef void (*pfrv_t)(void *);
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate typedef struct {
387c478bd9Sstevel@tonic-gate void *buf;
397c478bd9Sstevel@tonic-gate size_t size;
407c478bd9Sstevel@tonic-gate pfrv_t destructor;
417c478bd9Sstevel@tonic-gate } tsdent_t;
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static void
_free_tsdbuf(void * ptr)447c478bd9Sstevel@tonic-gate _free_tsdbuf(void *ptr)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate tsdent_t *loc = ptr;
477c478bd9Sstevel@tonic-gate pfrv_t destructor;
487c478bd9Sstevel@tonic-gate void *p;
497c478bd9Sstevel@tonic-gate int i;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (loc != NULL) {
527c478bd9Sstevel@tonic-gate for (i = 0; i < _T_NUM_ENTRIES; i++) {
537c478bd9Sstevel@tonic-gate if ((p = loc[i].buf) != NULL) {
547c478bd9Sstevel@tonic-gate destructor = loc[i].destructor;
557c478bd9Sstevel@tonic-gate if (destructor != NULL)
567c478bd9Sstevel@tonic-gate destructor(p);
577c478bd9Sstevel@tonic-gate lfree(p, loc[i].size);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate void *
tsdalloc(__tsd_item_t n,size_t size,pfrv_t destructor)657c478bd9Sstevel@tonic-gate tsdalloc(__tsd_item_t n, size_t size, pfrv_t destructor)
667c478bd9Sstevel@tonic-gate {
67cb620785Sraf static thread_key_t key = THR_ONCE_KEY;
687c478bd9Sstevel@tonic-gate tsdent_t *loc;
697c478bd9Sstevel@tonic-gate void *p;
707c478bd9Sstevel@tonic-gate int error;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate if ((uint_t)n >= _T_NUM_ENTRIES) {
737c478bd9Sstevel@tonic-gate errno = ENOTSUP;
747c478bd9Sstevel@tonic-gate return (NULL);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
77*7257d1b4Sraf if ((error = thr_keycreate_once(&key, _free_tsdbuf)) != 0) {
78cb620785Sraf errno = error;
79cb620785Sraf return (NULL);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
82*7257d1b4Sraf if ((loc = pthread_getspecific(key)) != NULL) {
837c478bd9Sstevel@tonic-gate if ((p = loc[n].buf) != NULL)
847c478bd9Sstevel@tonic-gate return (p);
857c478bd9Sstevel@tonic-gate } else {
867c478bd9Sstevel@tonic-gate /* allocate our array of pointers */
877c478bd9Sstevel@tonic-gate loc = lmalloc(_T_NUM_ENTRIES * sizeof (tsdent_t));
887c478bd9Sstevel@tonic-gate if (loc == NULL)
897c478bd9Sstevel@tonic-gate return (NULL);
90*7257d1b4Sraf if ((error = thr_setspecific(key, loc)) != 0) {
917c478bd9Sstevel@tonic-gate lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t));
927c478bd9Sstevel@tonic-gate errno = error;
937c478bd9Sstevel@tonic-gate return (NULL);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /* allocate item n */
987c478bd9Sstevel@tonic-gate loc[n].buf = p = lmalloc(size);
997c478bd9Sstevel@tonic-gate loc[n].size = size;
1007c478bd9Sstevel@tonic-gate loc[n].destructor = destructor;
1017c478bd9Sstevel@tonic-gate return (p);
1027c478bd9Sstevel@tonic-gate }
103