as.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <as.h>
00036 #include <libc.h>
00037 #include <unistd.h>
00038 #include <align.h>
00039 
00048 void *as_area_create(void *address, size_t size, int flags)
00049 {
00050         return (void *) __SYSCALL3(SYS_AS_AREA_CREATE, (sysarg_t ) address, (sysarg_t) size, (sysarg_t) flags);
00051 }
00052 
00061 int as_area_resize(void *address, size_t size, int flags)
00062 {
00063         return __SYSCALL3(SYS_AS_AREA_RESIZE, (sysarg_t ) address, (sysarg_t) size, (sysarg_t) flags);
00064 }
00065 
00072 int as_area_destroy(void *address)
00073 {
00074         return __SYSCALL1(SYS_AS_AREA_DESTROY, (sysarg_t ) address);
00075 }
00076 
00077 static size_t heapsize = 0;
00078 static size_t maxheapsize = (size_t) (-1);
00079 
00080 static void * last_allocated = 0;
00081 
00082 /* Start of heap linker symbol */
00083 extern char _heap;
00084 
00091 void *sbrk(ssize_t incr)
00092 {
00093         int rc;
00094         void *res;
00095         
00096         /* Check for invalid values */
00097         if (incr < 0 && -incr > heapsize)
00098                 return NULL;
00099         
00100         /* Check for too large value */
00101         if (incr > 0 && incr+heapsize < heapsize)
00102                 return NULL;
00103         
00104         /* Check for too small values */
00105         if (incr < 0 && incr+heapsize > heapsize)
00106                 return NULL;
00107         
00108         /* Check for user limit */
00109         if ((maxheapsize != (size_t) (-1)) && (heapsize + incr) > maxheapsize)
00110                 return NULL;
00111         
00112         rc = as_area_resize(&_heap, heapsize + incr, 0);
00113         if (rc != 0)
00114                 return NULL;
00115         
00116         /* Compute start of new area */
00117         res = (void *) &_heap + heapsize;
00118 
00119         heapsize += incr;
00120 
00121         return res;
00122 }
00123 
00125 void *set_maxheapsize(size_t mhs)
00126 {
00127         maxheapsize = mhs;
00128         /* Return pointer to area not managed by sbrk */
00129         return ((void *) &_heap + maxheapsize);
00130 
00131 }
00132 
00138 void * as_get_mappable_page(size_t sz)
00139 {
00140         void *res;
00141 
00142         /* Set heapsize to some meaningful value */
00143         if (maxheapsize == -1)
00144                 set_maxheapsize(ALIGN_UP(USER_ADDRESS_SPACE_SIZE_ARCH >> 1, PAGE_SIZE));
00145         
00146         if (!last_allocated)
00147                 last_allocated = (void *) ALIGN_UP((void *) &_heap + maxheapsize, PAGE_SIZE);
00148         
00149         sz = ALIGN_UP(sz, PAGE_SIZE);
00150         res = last_allocated;
00151         last_allocated += sz;
00152 
00153         return res;
00154 }
00155 

Generated on Sun Jun 18 18:00:17 2006 for HelenOS Userspace (ia64) by  doxygen 1.4.6