1*5772Sas200622 /* 2*5772Sas200622 * CDDL HEADER START 3*5772Sas200622 * 4*5772Sas200622 * The contents of this file are subject to the terms of the 5*5772Sas200622 * Common Development and Distribution License (the "License"). 6*5772Sas200622 * You may not use this file except in compliance with the License. 7*5772Sas200622 * 8*5772Sas200622 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*5772Sas200622 * or http://www.opensolaris.org/os/licensing. 10*5772Sas200622 * See the License for the specific language governing permissions 11*5772Sas200622 * and limitations under the License. 12*5772Sas200622 * 13*5772Sas200622 * When distributing Covered Code, include this CDDL HEADER in each 14*5772Sas200622 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*5772Sas200622 * If applicable, add the following below this CDDL HEADER, with the 16*5772Sas200622 * fields enclosed by brackets "[]" replaced with your own identifying 17*5772Sas200622 * information: Portions Copyright [yyyy] [name of copyright owner] 18*5772Sas200622 * 19*5772Sas200622 * CDDL HEADER END 20*5772Sas200622 */ 21*5772Sas200622 /* 22*5772Sas200622 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*5772Sas200622 * Use is subject to license terms. 24*5772Sas200622 */ 25*5772Sas200622 26*5772Sas200622 #pragma ident "%Z%%M% %I% %E% SMI" 27*5772Sas200622 28*5772Sas200622 /* 29*5772Sas200622 * MLRPC heap management. The heap is used for temporary storage by 30*5772Sas200622 * both the client and server side library routines. In order to 31*5772Sas200622 * support the different requirements of the various RPCs, the heap 32*5772Sas200622 * can grow dynamically if required. We start with a single block 33*5772Sas200622 * and perform sub-allocations from it. If an RPC requires more space 34*5772Sas200622 * we will continue to add it a block at a time. This means that we 35*5772Sas200622 * don't hog lots of memory on every call to support the few times 36*5772Sas200622 * that we actually need a lot heap space. 37*5772Sas200622 * 38*5772Sas200622 * Note that there is no individual free function. Once space has been 39*5772Sas200622 * allocated, it remains allocated until the heap is destroyed. This 40*5772Sas200622 * shouldn't be an issue because the heap is being filled with data to 41*5772Sas200622 * be marshalled or unmarshalled and we need it all to be there until 42*5772Sas200622 * the point that the entire heap is no longer required. 43*5772Sas200622 */ 44*5772Sas200622 45*5772Sas200622 #include <sys/errno.h> 46*5772Sas200622 #include <stdlib.h> 47*5772Sas200622 #include <string.h> 48*5772Sas200622 #include <strings.h> 49*5772Sas200622 #include <sys/uio.h> 50*5772Sas200622 51*5772Sas200622 #include <smbsrv/libsmb.h> 52*5772Sas200622 #include <smbsrv/mlrpc.h> 53*5772Sas200622 54*5772Sas200622 /* 55*5772Sas200622 * Allocate a heap structure and the first heap block. For many RPC 56*5772Sas200622 * operations this will be the only time we need to malloc memory 57*5772Sas200622 * in this instance of the heap. The only point of note here is that 58*5772Sas200622 * we put the heap management data in the first block to avoid a 59*5772Sas200622 * second malloc. Make sure that sizeof(mlrpc_heap_t) is smaller 60*5772Sas200622 * than MLRPC_HEAP_BLKSZ. 61*5772Sas200622 * 62*5772Sas200622 * Note that the heap management data is at the start of the first block. 63*5772Sas200622 * 64*5772Sas200622 * Returns a pointer to the newly created heap, which is used like an 65*5772Sas200622 * opaque handle with the rest of the heap management interface.. 66*5772Sas200622 */ 67*5772Sas200622 mlrpc_heap_t * 68*5772Sas200622 mlrpc_heap_create(void) 69*5772Sas200622 { 70*5772Sas200622 mlrpc_heap_t *heap; 71*5772Sas200622 char *base; 72*5772Sas200622 73*5772Sas200622 if ((base = (char *)malloc(MLRPC_HEAP_BLKSZ)) == NULL) 74*5772Sas200622 return (NULL); 75*5772Sas200622 76*5772Sas200622 /*LINTED E_BAD_PTR_CAST_ALIGN*/ 77*5772Sas200622 heap = (mlrpc_heap_t *)base; 78*5772Sas200622 bzero(heap, sizeof (mlrpc_heap_t)); 79*5772Sas200622 80*5772Sas200622 heap->iovcnt = MLRPC_HEAP_MAXIOV; 81*5772Sas200622 heap->iov = heap->iovec; 82*5772Sas200622 heap->iov->iov_base = base; 83*5772Sas200622 heap->iov->iov_len = sizeof (mlrpc_heap_t); 84*5772Sas200622 heap->top = base + MLRPC_HEAP_BLKSZ; 85*5772Sas200622 heap->next = base + sizeof (mlrpc_heap_t); 86*5772Sas200622 87*5772Sas200622 return (heap); 88*5772Sas200622 } 89*5772Sas200622 90*5772Sas200622 /* 91*5772Sas200622 * Deallocate all of the memory associated with a heap. This is the 92*5772Sas200622 * only way to deallocate heap memory, it isn't possible to free the 93*5772Sas200622 * space obtained by individual malloc calls. 94*5772Sas200622 * 95*5772Sas200622 * Note that the first block contains the heap management data, which 96*5772Sas200622 * is deleted last. 97*5772Sas200622 */ 98*5772Sas200622 void 99*5772Sas200622 mlrpc_heap_destroy(mlrpc_heap_t *heap) 100*5772Sas200622 { 101*5772Sas200622 int i; 102*5772Sas200622 char *p; 103*5772Sas200622 104*5772Sas200622 if (heap) { 105*5772Sas200622 for (i = 1; i < MLRPC_HEAP_MAXIOV; ++i) { 106*5772Sas200622 if ((p = heap->iovec[i].iov_base) != NULL) 107*5772Sas200622 free(p); 108*5772Sas200622 } 109*5772Sas200622 110*5772Sas200622 free(heap); 111*5772Sas200622 } 112*5772Sas200622 } 113*5772Sas200622 114*5772Sas200622 /* 115*5772Sas200622 * Allocate space in the specified heap. All requests are padded, if 116*5772Sas200622 * required, to ensure dword alignment. If the current iov will be 117*5772Sas200622 * exceeded, we allocate a new block and setup the next iov. Otherwise 118*5772Sas200622 * all we have to do is move the next pointer and update the current 119*5772Sas200622 * iov length. 120*5772Sas200622 * 121*5772Sas200622 * On success, a pointer to the allocated (dword aligned) area is 122*5772Sas200622 * returned. Otherwise a null pointer is returned. 123*5772Sas200622 */ 124*5772Sas200622 void * 125*5772Sas200622 mlrpc_heap_malloc(mlrpc_heap_t *heap, unsigned size) 126*5772Sas200622 { 127*5772Sas200622 char *p; 128*5772Sas200622 int align; 129*5772Sas200622 int incr_size; 130*5772Sas200622 131*5772Sas200622 align = (4 - size) & 3; 132*5772Sas200622 size += align; 133*5772Sas200622 134*5772Sas200622 if (heap == NULL || size == 0) 135*5772Sas200622 return (NULL); 136*5772Sas200622 137*5772Sas200622 p = heap->next; 138*5772Sas200622 139*5772Sas200622 if (p + size > heap->top) { 140*5772Sas200622 if ((heap->iovcnt == 0) || ((--heap->iovcnt) == 0)) 141*5772Sas200622 return (NULL); 142*5772Sas200622 143*5772Sas200622 incr_size = (size < MLRPC_HEAP_BLKSZ) ? MLRPC_HEAP_BLKSZ : size; 144*5772Sas200622 145*5772Sas200622 if ((p = (char *)malloc(incr_size)) == NULL) 146*5772Sas200622 return (NULL); 147*5772Sas200622 148*5772Sas200622 ++heap->iov; 149*5772Sas200622 heap->iov->iov_base = p; 150*5772Sas200622 heap->iov->iov_len = 0; 151*5772Sas200622 heap->top = p + incr_size; 152*5772Sas200622 } 153*5772Sas200622 154*5772Sas200622 heap->next = p + size; 155*5772Sas200622 heap->iov->iov_len += size; 156*5772Sas200622 return ((void *)p); 157*5772Sas200622 } 158*5772Sas200622 159*5772Sas200622 /* 160*5772Sas200622 * Convenience function to do heap strdup. 161*5772Sas200622 */ 162*5772Sas200622 void * 163*5772Sas200622 mlrpc_heap_strsave(mlrpc_heap_t *heap, char *s) 164*5772Sas200622 { 165*5772Sas200622 int len; 166*5772Sas200622 void *p; 167*5772Sas200622 168*5772Sas200622 if (s == NULL) 169*5772Sas200622 return (NULL); 170*5772Sas200622 171*5772Sas200622 /* 172*5772Sas200622 * We don't need to clutter the heap with empty strings. 173*5772Sas200622 */ 174*5772Sas200622 if ((len = strlen(s)) == 0) 175*5772Sas200622 return (""); 176*5772Sas200622 177*5772Sas200622 if ((p = mlrpc_heap_malloc(heap, len+1)) != NULL) 178*5772Sas200622 (void) strcpy((char *)p, s); 179*5772Sas200622 180*5772Sas200622 return (p); 181*5772Sas200622 } 182*5772Sas200622 183*5772Sas200622 /* 184*5772Sas200622 * Our regular string marshalling always creates null terminated strings 185*5772Sas200622 * but some Windows clients and servers are pedantic about the string 186*5772Sas200622 * formats they will accept and require non-null terminated strings. 187*5772Sas200622 * This function can be used to build a wide-char, non-null terminated 188*5772Sas200622 * string in the heap as a varying/conformant array. We need to do the 189*5772Sas200622 * wide-char conversion here because the marshalling code won't be 190*5772Sas200622 * aware that this is really a string. 191*5772Sas200622 */ 192*5772Sas200622 void 193*5772Sas200622 mlrpc_heap_mkvcs(mlrpc_heap_t *heap, char *s, mlrpc_vcbuf_t *vcs) 194*5772Sas200622 { 195*5772Sas200622 int mlen; 196*5772Sas200622 197*5772Sas200622 vcs->wclen = mts_wcequiv_strlen(s); 198*5772Sas200622 vcs->wcsize = vcs->wclen; 199*5772Sas200622 200*5772Sas200622 mlen = sizeof (struct mlrpc_vcb) + vcs->wcsize + sizeof (mts_wchar_t); 201*5772Sas200622 202*5772Sas200622 vcs->vcb = (struct mlrpc_vcb *)mlrpc_heap_malloc(heap, mlen); 203*5772Sas200622 204*5772Sas200622 if (vcs->vcb) { 205*5772Sas200622 vcs->vcb->vc_first_is = 0; 206*5772Sas200622 vcs->vcb->vc_length_is = vcs->wclen / sizeof (mts_wchar_t); 207*5772Sas200622 (void) mts_mbstowcs((mts_wchar_t *)vcs->vcb->buffer, s, 208*5772Sas200622 vcs->vcb->vc_length_is); 209*5772Sas200622 } 210*5772Sas200622 } 211*5772Sas200622 212*5772Sas200622 int 213*5772Sas200622 mlrpc_heap_used(mlrpc_heap_t *heap) 214*5772Sas200622 { 215*5772Sas200622 int used = 0; 216*5772Sas200622 int i; 217*5772Sas200622 218*5772Sas200622 for (i = 0; i < MLRPC_HEAP_MAXIOV; ++i) 219*5772Sas200622 used += heap->iovec[i].iov_len; 220*5772Sas200622 221*5772Sas200622 return (used); 222*5772Sas200622 } 223*5772Sas200622 224*5772Sas200622 int 225*5772Sas200622 mlrpc_heap_avail(mlrpc_heap_t *heap) 226*5772Sas200622 { 227*5772Sas200622 int avail; 228*5772Sas200622 int count; 229*5772Sas200622 230*5772Sas200622 count = (heap->iovcnt == 0) ? 0 : (heap->iovcnt - 1); 231*5772Sas200622 232*5772Sas200622 avail = count * MLRPC_HEAP_BLKSZ; 233*5772Sas200622 avail += (heap->top - heap->next); 234*5772Sas200622 235*5772Sas200622 return (avail); 236*5772Sas200622 } 237