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