memory.c (3167B)
1 /* $Id: //depot/blt/lib/memory.c#4 $ 2 ** 3 ** Copyright 1998 Brian J. Swetland 4 ** All rights reserved. 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. The name of the author may not be used to endorse or promote products 15 ** derived from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdlib.h> 30 #include <unistd.h> 31 #include <blt/types.h> 32 #include <blt/syscall.h> 33 #include <blt/libsyms.h> 34 #include "malloc.h" 35 36 weak_alias (_sbrk, sbrk) 37 weak_alias (_malloc, malloc) 38 weak_alias (_free, free) 39 weak_alias (_realloc, realloc) 40 41 static unsigned int __sbrk_max; 42 static unsigned int __sbrk_cur; 43 static int sem_malloc; 44 45 void *_sbrk(int size) 46 { 47 if(size < 0 ){ 48 __sbrk_cur -= size; 49 return (void *) __sbrk_cur; 50 } else { 51 unsigned int tmp = __sbrk_cur; 52 __sbrk_cur += size; 53 if(__sbrk_cur > __sbrk_max){ 54 __sbrk_max = __sbrk_cur; 55 os_brk(__sbrk_max); 56 } 57 return (void *) tmp; 58 } 59 60 return (void *) __sbrk_cur; 61 } 62 63 void *_malloc(size_t size) 64 { 65 void *r; 66 sem_acquire(sem_malloc); 67 r = __malloc(size); 68 sem_release(sem_malloc); 69 return r; 70 } 71 72 void _free(void *ptr) 73 { 74 sem_acquire(sem_malloc); 75 __free(ptr); 76 sem_release(sem_malloc); 77 } 78 79 void *_realloc(void *ptr, size_t size) 80 { 81 void *r; 82 sem_acquire(sem_malloc); 83 r = __realloc(ptr,size); 84 sem_release(sem_malloc); 85 return r; 86 } 87 88 89 void * _default_morecore(long size) 90 { 91 void *result; 92 93 result = sbrk(size); 94 if (result == (void *) -1) 95 return NULL; 96 return result; 97 } 98 99 void __libc_init_memory(unsigned int top_of_binary, 100 unsigned int start_bss, unsigned int bss_length) 101 { 102 int i; 103 unsigned char *x = (unsigned char *) start_bss; 104 unsigned int tob = (top_of_binary/4096+2)*4096; 105 os_brk(tob); 106 107 for (i = 0; i < bss_length; i++) 108 x[i] = 0; 109 110 __sbrk_max = __sbrk_cur = tob; 111 112 sem_malloc = sem_create(1,"libc_malloc_sem"); 113 }