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