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 5*3864Sraf * Common Development and Distribution License (the "License"). 6*3864Sraf * 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 */ 21*3864Sraf 220Sstevel@tonic-gate /* 23*3864Sraf * Copyright 2007 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 290Sstevel@tonic-gate #include "synonyms.h" 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <errno.h> 320Sstevel@tonic-gate #include "mtlib.h" 330Sstevel@tonic-gate #include "libc.h" 340Sstevel@tonic-gate #include "tsd.h" 350Sstevel@tonic-gate 360Sstevel@tonic-gate typedef void (*pfrv_t)(void *); 370Sstevel@tonic-gate 380Sstevel@tonic-gate typedef struct { 390Sstevel@tonic-gate void *buf; 400Sstevel@tonic-gate size_t size; 410Sstevel@tonic-gate pfrv_t destructor; 420Sstevel@tonic-gate } tsdent_t; 430Sstevel@tonic-gate 440Sstevel@tonic-gate static void 450Sstevel@tonic-gate _free_tsdbuf(void *ptr) 460Sstevel@tonic-gate { 470Sstevel@tonic-gate tsdent_t *loc = ptr; 480Sstevel@tonic-gate pfrv_t destructor; 490Sstevel@tonic-gate void *p; 500Sstevel@tonic-gate int i; 510Sstevel@tonic-gate 520Sstevel@tonic-gate if (loc != NULL) { 530Sstevel@tonic-gate for (i = 0; i < _T_NUM_ENTRIES; i++) { 540Sstevel@tonic-gate if ((p = loc[i].buf) != NULL) { 550Sstevel@tonic-gate destructor = loc[i].destructor; 560Sstevel@tonic-gate if (destructor != NULL) 570Sstevel@tonic-gate destructor(p); 580Sstevel@tonic-gate lfree(p, loc[i].size); 590Sstevel@tonic-gate } 600Sstevel@tonic-gate } 610Sstevel@tonic-gate lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t)); 620Sstevel@tonic-gate } 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate void * 660Sstevel@tonic-gate tsdalloc(__tsd_item_t n, size_t size, pfrv_t destructor) 670Sstevel@tonic-gate { 68*3864Sraf static thread_key_t key = THR_ONCE_KEY; 690Sstevel@tonic-gate tsdent_t *loc; 700Sstevel@tonic-gate void *p; 710Sstevel@tonic-gate int error; 720Sstevel@tonic-gate 730Sstevel@tonic-gate if ((uint_t)n >= _T_NUM_ENTRIES) { 740Sstevel@tonic-gate errno = ENOTSUP; 750Sstevel@tonic-gate return (NULL); 760Sstevel@tonic-gate } 770Sstevel@tonic-gate 78*3864Sraf if ((error = _thr_keycreate_once(&key, _free_tsdbuf)) != 0) { 79*3864Sraf errno = error; 80*3864Sraf return (NULL); 810Sstevel@tonic-gate } 820Sstevel@tonic-gate 830Sstevel@tonic-gate if ((loc = _pthread_getspecific(key)) != NULL) { 840Sstevel@tonic-gate if ((p = loc[n].buf) != NULL) 850Sstevel@tonic-gate return (p); 860Sstevel@tonic-gate } else { 870Sstevel@tonic-gate /* allocate our array of pointers */ 880Sstevel@tonic-gate loc = lmalloc(_T_NUM_ENTRIES * sizeof (tsdent_t)); 890Sstevel@tonic-gate if (loc == NULL) 900Sstevel@tonic-gate return (NULL); 910Sstevel@tonic-gate if ((error = _thr_setspecific(key, loc)) != 0) { 920Sstevel@tonic-gate lfree(loc, _T_NUM_ENTRIES * sizeof (tsdent_t)); 930Sstevel@tonic-gate errno = error; 940Sstevel@tonic-gate return (NULL); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate } 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* allocate item n */ 990Sstevel@tonic-gate loc[n].buf = p = lmalloc(size); 1000Sstevel@tonic-gate loc[n].size = size; 1010Sstevel@tonic-gate loc[n].destructor = destructor; 1020Sstevel@tonic-gate return (p); 1030Sstevel@tonic-gate } 104