00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00083 extern char _heap;
00084
00091 void *sbrk(ssize_t incr)
00092 {
00093 int rc;
00094 void *res;
00095
00096
00097 if (incr < 0 && -incr > heapsize)
00098 return NULL;
00099
00100
00101 if (incr > 0 && incr+heapsize < heapsize)
00102 return NULL;
00103
00104
00105 if (incr < 0 && incr+heapsize > heapsize)
00106 return NULL;
00107
00108
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
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
00129 return ((void *) &_heap + maxheapsize);
00130
00131 }
00132
00138 void * as_get_mappable_page(size_t sz)
00139 {
00140 void *res;
00141
00142
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