SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /userland/libc.c (Article 7.5) and /userland/libc.c (Article 8)


001 /* Copyright (C) 2005 David Decotigny             001 /* Copyright (C) 2005 David Decotigny
002                                                   002 
003    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
004    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
005    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
006    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
007                                                   007    
008    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU General Public License for more details    011    GNU General Public License for more details.
012                                                   012    
013    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
014    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
015    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
016    USA.                                           016    USA. 
017 */                                                017 */
018                                                   018 
019 #include "crt.h"                                  019 #include "crt.h"
020 #include "libc.h"                                 020 #include "libc.h"
                                                   >> 021 #include "stdarg.h"
021                                                   022 
022                                                   023 
023 void * mmap(void *start, size_t length, int pr !! 024 void * fakemmap(void *start, size_t length, int prot , int flags,
024             const char *path, loff_t offset)   !! 025                 const char *path, loff_t offset)
025 {                                                 026 {
026   /* At kernel side, offset is considered posi    027   /* At kernel side, offset is considered positive */
027   if (offset < 0)                                 028   if (offset < 0)
028     return NULL;                                  029     return NULL;
029                                                   030 
030   if (0 != _sos_mmap(& start, length, prot, fl !! 031   if (0 != _sos_fakemmap(& start, length, prot, flags, path, offset))
031     return NULL;                                  032     return NULL;
032                                                   033 
033   return start;                                   034   return start;
034 }                                                 035 }
035                                                   036 
036                                                   037 
037 void * mremap(void * old_addr, size_t old_len,    038 void * mremap(void * old_addr, size_t old_len,
038               size_t new_len,                     039               size_t new_len,
039               unsigned long flags)                040               unsigned long flags)
040 {                                                 041 {
041   void * new_uaddr = old_addr;                    042   void * new_uaddr = old_addr;
042   if (0 != _sos_mresize(old_addr, old_len, & n    043   if (0 != _sos_mresize(old_addr, old_len, & new_uaddr, new_len, flags))
043     return NULL;                                  044     return NULL;
044                                                   045 
045   return new_uaddr;                               046   return new_uaddr;
046 }                                                 047 }
047                                                   048 
048                                                   049 
049 /**                                               050 /**
050  * The presence of this global variable withou    051  * The presence of this global variable without any protected access
051  * to it explains why the "brk/sbrk" functions    052  * to it explains why the "brk/sbrk" functions below are MT-unsafe !
052  */                                               053  */
053 static void * kernel_heap_top = NULL;             054 static void * kernel_heap_top = NULL;
054 int brk(void *end_data_seg)                       055 int brk(void *end_data_seg)
055 {                                                 056 {
056   if (! end_data_seg)                             057   if (! end_data_seg)
057     return -1;                                    058     return -1;
058                                                   059 
059   kernel_heap_top = _sos_brk(end_data_seg);       060   kernel_heap_top = _sos_brk(end_data_seg);
060   return 0;                                       061   return 0;
061 }                                                 062 }
062                                                   063 
063                                                   064 
064 void *sbrk(ptrdiff_t increment)                   065 void *sbrk(ptrdiff_t increment)
065 {                                                 066 {
066   if (! kernel_heap_top)                          067   if (! kernel_heap_top)
067     kernel_heap_top = _sos_brk(0);                068     kernel_heap_top = _sos_brk(0);
068                                                   069 
069   kernel_heap_top = _sos_brk(kernel_heap_top +    070   kernel_heap_top = _sos_brk(kernel_heap_top + increment);
070   return kernel_heap_top;                         071   return kernel_heap_top;
071 }                                                 072 }
072                                                   073 
073                                                   074 
074 void * calloc (size_t nmemb, size_t size)         075 void * calloc (size_t nmemb, size_t size)
075 {                                                 076 {
076   return malloc(nmemb * size);                    077   return malloc(nmemb * size);
077 }                                                 078 }
078                                                   079 
079                                                   080 
080 /**                                               081 /**
081  * The presence of this global variable withou    082  * The presence of this global variable without any protected access
082  * to it explains why the "malloc/calloc" func    083  * to it explains why the "malloc/calloc" functions below are
083  * MT-unsafe !                                    084  * MT-unsafe !
084  */                                               085  */
085 static void * malloc_heap_top = NULL;             086 static void * malloc_heap_top = NULL;
086 void * malloc (size_t size)                       087 void * malloc (size_t size)
087 {                                                 088 {
088   void * retval;                                  089   void * retval;
089                                                   090 
090   if (size <= 0)                                  091   if (size <= 0)
091     return NULL;                                  092     return NULL;
092                                                   093 
093   /* Align on a 4B boundary */                    094   /* Align on a 4B boundary */
094   size = ((size-1) & ~3) + 4;                     095   size = ((size-1) & ~3) + 4;
095                                                   096 
096   if (! kernel_heap_top)                          097   if (! kernel_heap_top)
097     kernel_heap_top = _sos_brk(0);                098     kernel_heap_top = _sos_brk(0);
098                                                   099 
099   if (! malloc_heap_top)                          100   if (! malloc_heap_top)
100     malloc_heap_top = kernel_heap_top;            101     malloc_heap_top = kernel_heap_top;
101                                                   102 
102   retval = malloc_heap_top;                       103   retval = malloc_heap_top;
103   malloc_heap_top += size;                        104   malloc_heap_top += size;
104                                                   105 
                                                   >> 106   _sos_brk(malloc_heap_top);
105   return retval;                                  107   return retval;
                                                   >> 108 }
                                                   >> 109 
                                                   >> 110 
                                                   >> 111 void free(void *ptr)
                                                   >> 112 {
                                                   >> 113   //  bochs_printf("Free ignored (not implemented yet)\n");
                                                   >> 114 }
                                                   >> 115 
                                                   >> 116 
                                                   >> 117 int open(const char *pathname, int flags, /* mode_t mode */...)
                                                   >> 118 {
                                                   >> 119   va_list ap;
                                                   >> 120   unsigned int mode = 0;
                                                   >> 121  
                                                   >> 122   va_start(ap, flags);
                                                   >> 123   if (flags & O_CREAT)
                                                   >> 124     mode = va_arg(ap, unsigned int);
                                                   >> 125   va_end(ap);
                                                   >> 126  
                                                   >> 127   return _sos_open(pathname, flags, mode);
                                                   >> 128 }
                                                   >> 129 
                                                   >> 130 
                                                   >> 131 int read(int fd, char * buf, size_t len)
                                                   >> 132 {
                                                   >> 133   int retval = _sos_read(fd, buf, & len);
                                                   >> 134   if (retval < 0)
                                                   >> 135     return retval;
                                                   >> 136   return len;
                                                   >> 137 }
                                                   >> 138 
                                                   >> 139 
                                                   >> 140 int write(int fd, const char * buf, size_t len)
                                                   >> 141 {
                                                   >> 142   int retval = _sos_write(fd, buf, & len);
                                                   >> 143   if (retval < 0)
                                                   >> 144     return retval;
                                                   >> 145   return len;
                                                   >> 146 }
                                                   >> 147 
                                                   >> 148 
                                                   >> 149 off_t lseek(int fd, off_t offset, int whence)
                                                   >> 150 {
                                                   >> 151   loff_t result = offset;
                                                   >> 152   int retval = _sos_seek64(fd, & result, whence);
                                                   >> 153   if (retval < 0)
                                                   >> 154     return retval;
                                                   >> 155   return result;
                                                   >> 156 }
                                                   >> 157 
                                                   >> 158 
                                                   >> 159 loff_t lseek64(int fd, loff_t offset, int whence)
                                                   >> 160 {
                                                   >> 161   loff_t result = offset;
                                                   >> 162   int retval = _sos_seek64(fd, & result, whence);
                                                   >> 163   if (retval < 0)
                                                   >> 164     return retval;
                                                   >> 165   return result;
                                                   >> 166 }
                                                   >> 167 
                                                   >> 168 
                                                   >> 169 void * mmap(void *start, size_t length, int prot , int flags,
                                                   >> 170             int fd, loff_t offset)
                                                   >> 171 {
                                                   >> 172   /* At kernel side, offset is considered positive */
                                                   >> 173   if (offset < 0)
                                                   >> 174     return NULL;
                                                   >> 175 
                                                   >> 176   if (0 != _sos_fmmap(& start, length, prot, flags, fd, offset))
                                                   >> 177     return NULL;
                                                   >> 178 
                                                   >> 179   return start;
                                                   >> 180 }
                                                   >> 181 
                                                   >> 182 
                                                   >> 183 int ftruncate(int fd, off_t length)
                                                   >> 184 {
                                                   >> 185   return _sos_ftruncate64(fd, length);
                                                   >> 186 }
                                                   >> 187 
                                                   >> 188 
                                                   >> 189 struct sos_DIR_struct
                                                   >> 190 {
                                                   >> 191   int fd;
                                                   >> 192   struct dirent dirent;
                                                   >> 193 };
                                                   >> 194 
                                                   >> 195 
                                                   >> 196 DIR *opendir(const char *name)
                                                   >> 197 {
                                                   >> 198   DIR * result = malloc(sizeof(DIR));
                                                   >> 199   if (! result)
                                                   >> 200     return NULL;
                                                   >> 201 
                                                   >> 202   result->fd = _sos_open(name, O_DIRECTORY | O_RDONLY, 0);
                                                   >> 203   return result;
                                                   >> 204 }
                                                   >> 205 
                                                   >> 206 
                                                   >> 207 int dirfd(const DIR * dir)
                                                   >> 208 {
                                                   >> 209   if (dir)
                                                   >> 210     return dir->fd;
                                                   >> 211   return -1;
                                                   >> 212 }
                                                   >> 213 
                                                   >> 214 
                                                   >> 215 struct dirent *readdir(DIR *dir)
                                                   >> 216 {
                                                   >> 217   int retval = _sos_readdir(dir->fd, & dir->dirent);
                                                   >> 218   if (retval < 0)
                                                   >> 219     return NULL;
                                                   >> 220   return & dir->dirent;
                                                   >> 221 }
                                                   >> 222 
                                                   >> 223 
                                                   >> 224 int closedir(DIR *dir)
                                                   >> 225 {
                                                   >> 226   close(dir->fd);
                                                   >> 227   free(dir);
                                                   >> 228   return 0;
                                                   >> 229 }
                                                   >> 230 
                                                   >> 231 
                                                   >> 232 int stat(const char *file_name, struct stat *buf)
                                                   >> 233 {
                                                   >> 234   return _sos_stat(file_name, TRUE, buf);
                                                   >> 235 }
                                                   >> 236 
                                                   >> 237 
                                                   >> 238 int lstat(const char *file_name, struct stat *buf)
                                                   >> 239 {
                                                   >> 240   return _sos_stat(file_name, FALSE, buf);
106 }                                                 241 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]