util.c revision 1.8
1/*	$OpenBSD: util.c,v 1.8 2002/07/24 04:00:44 deraadt Exp $	*/
2
3/*
4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed under OpenBSD by
17 *	Per Fogelstrom, Opsycon AB, Sweden.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 */
34
35#include <sys/types.h>
36#include <sys/mman.h>
37#include <string.h>
38#include "archdep.h"
39
40/*
41 * Static vars usable after bootstrapping.
42 */
43static void *_dl_malloc_base;
44static void *_dl_malloc_pool = 0;
45static long *_dl_malloc_free = 0;
46
47char *
48_dl_strdup(const char *orig)
49{
50	char *newstr;
51	int len;
52
53	len = _dl_strlen(orig)+1;
54	newstr = _dl_malloc(len);
55	_dl_strlcpy(newstr, orig, len);
56	return (newstr);
57}
58
59/*
60 *  The following malloc/free code is a very simplified implementation
61 *  of a malloc function. However, we do not need to be very complex here
62 *  because we only free memory when 'dlclose()' is called and we can
63 *  reuse at least the memory allocated for the object descriptor. We have
64 *  one dynamic string allocated, the library name and it is likely that
65 *  we can reuse that one to without a lot of complex colapsing code.
66 */
67
68void *
69_dl_malloc(int size)
70{
71	long *p, *t, *n;
72
73	size = (size + 8 + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1);
74
75	if ((t = _dl_malloc_free) != 0) {	/* Try free list first */
76		n = (long *)&_dl_malloc_free;
77		while (t && t[-1] < size) {
78			n = t;
79			t = (long *)*t;
80		}
81		if (t) {
82			*n = *t;
83			_dl_memset(t, 0, t[-1] - 4);
84			return((void *)t);
85		}
86	}
87	if (_dl_malloc_pool == 0 ||
88	    _dl_malloc_pool + size > _dl_malloc_base + 4096) {
89		_dl_malloc_pool = (void *)_dl_mmap((void *)0, 4096,
90		    PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
91		if (_dl_malloc_pool == 0 || _dl_malloc_pool == MAP_FAILED ) {
92			_dl_printf("Dynamic loader failure: malloc.\n");
93			_dl_exit(7);
94		}
95		_dl_malloc_base = _dl_malloc_pool;
96	}
97	p = _dl_malloc_pool;
98	_dl_malloc_pool += size;
99	_dl_memset(p, 0, size);
100	*p = size;
101	return((void *)(p + 1));
102}
103
104void
105_dl_free(void *p)
106{
107	long *t = (long *)p;
108
109	*t = (long)_dl_malloc_free;
110	_dl_malloc_free = p;
111}
112