util.c revision 1.3
1/* $OpenBSD: util.c,v 1.3 2002/05/24 03:44:37 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 bootsrapping. 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 newstr = _dl_malloc(_dl_strlen(orig)+1); 52 _dl_strcpy(newstr, orig); 53 return (newstr); 54} 55 56/* 57 * The following malloc/free code is a very simplified implementation 58 * of a malloc function. However, we do not need to be very complex here 59 * because we only free memory when 'dlclose()' is called and we can 60 * reuse at least the memory allocated for the object descriptor. We have 61 * one dynamic string allocated, the library name and it is likely that 62 * we can reuse that one to without a lot of complex colapsing code. 63 */ 64 65void * 66_dl_malloc(int size) 67{ 68 long *p; 69 long *t, *n; 70 71 size = (size + 8 + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1); 72 73 if ((t = _dl_malloc_free) != 0) { /* Try free list first */ 74 n = (long *)&_dl_malloc_free; 75 while (t && t[-1] < size) { 76 n = t; 77 t = (long *)*t; 78 } 79 if (t) { 80 *n = *t; 81 _dl_memset(t, 0, t[-1] - 4); 82 return((void *)t); 83 } 84 } 85 if (_dl_malloc_pool == 0 || 86 _dl_malloc_pool + size > _dl_malloc_base + 4096) { 87 _dl_malloc_pool = (void *)_dl_mmap((void *)0, 4096, 88 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 89 if (_dl_malloc_pool == 0 || _dl_malloc_pool == MAP_FAILED ) { 90 _dl_printf("Dynamic loader failure: malloc.\n"); 91 _dl_exit(7); 92 } 93 _dl_malloc_base = _dl_malloc_pool; 94 } 95 p = _dl_malloc_pool; 96 _dl_malloc_pool += size; 97 _dl_memset(p, 0, size); 98 *p = size; 99 return((void *)(p + 1)); 100} 101 102void 103_dl_free(void *p) 104{ 105 long *t = (long *)p; 106 107 *t = (long)_dl_malloc_free; 108 _dl_malloc_free = p; 109} 110