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 8) and /userland/libc.c (Article 7.5)


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"                            << 
022                                                   021 
023                                                   022 
024 void * fakemmap(void *start, size_t length, in !! 023 void * mmap(void *start, size_t length, int prot , int flags,
025                 const char *path, loff_t offse !! 024             const char *path, loff_t offset)
026 {                                                 025 {
027   /* At kernel side, offset is considered posi    026   /* At kernel side, offset is considered positive */
028   if (offset < 0)                                 027   if (offset < 0)
029     return NULL;                                  028     return NULL;
030                                                   029 
031   if (0 != _sos_fakemmap(& start, length, prot !! 030   if (0 != _sos_mmap(& start, length, prot, flags, path, offset))
032     return NULL;                                  031     return NULL;
033                                                   032 
034   return start;                                   033   return start;
035 }                                                 034 }
036                                                   035 
037                                                   036 
038 void * mremap(void * old_addr, size_t old_len,    037 void * mremap(void * old_addr, size_t old_len,
039               size_t new_len,                     038               size_t new_len,
040               unsigned long flags)                039               unsigned long flags)
041 {                                                 040 {
042   void * new_uaddr = old_addr;                    041   void * new_uaddr = old_addr;
043   if (0 != _sos_mresize(old_addr, old_len, & n    042   if (0 != _sos_mresize(old_addr, old_len, & new_uaddr, new_len, flags))
044     return NULL;                                  043     return NULL;
045                                                   044 
046   return new_uaddr;                               045   return new_uaddr;
047 }                                                 046 }
048                                                   047 
049                                                   048 
050 /**                                               049 /**
051  * The presence of this global variable withou    050  * The presence of this global variable without any protected access
052  * to it explains why the "brk/sbrk" functions    051  * to it explains why the "brk/sbrk" functions below are MT-unsafe !
053  */                                               052  */
054 static void * kernel_heap_top = NULL;             053 static void * kernel_heap_top = NULL;
055 int brk(void *end_data_seg)                       054 int brk(void *end_data_seg)
056 {                                                 055 {
057   if (! end_data_seg)                             056   if (! end_data_seg)
058     return -1;                                    057     return -1;
059                                                   058 
060   kernel_heap_top = _sos_brk(end_data_seg);       059   kernel_heap_top = _sos_brk(end_data_seg);
061   return 0;                                       060   return 0;
062 }                                                 061 }
063                                                   062 
064                                                   063 
065 void *sbrk(ptrdiff_t increment)                   064 void *sbrk(ptrdiff_t increment)
066 {                                                 065 {
067   if (! kernel_heap_top)                          066   if (! kernel_heap_top)
068     kernel_heap_top = _sos_brk(0);                067     kernel_heap_top = _sos_brk(0);
069                                                   068 
070   kernel_heap_top = _sos_brk(kernel_heap_top +    069   kernel_heap_top = _sos_brk(kernel_heap_top + increment);
071   return kernel_heap_top;                         070   return kernel_heap_top;
072 }                                                 071 }
073                                                   072 
074                                                   073 
075 void * calloc (size_t nmemb, size_t size)         074 void * calloc (size_t nmemb, size_t size)
076 {                                                 075 {
077   return malloc(nmemb * size);                    076   return malloc(nmemb * size);
078 }                                                 077 }
079                                                   078 
080                                                   079 
081 /**                                               080 /**
082  * The presence of this global variable withou    081  * The presence of this global variable without any protected access
083  * to it explains why the "malloc/calloc" func    082  * to it explains why the "malloc/calloc" functions below are
084  * MT-unsafe !                                    083  * MT-unsafe !
085  */                                               084  */
086 static void * malloc_heap_top = NULL;             085 static void * malloc_heap_top = NULL;
087 void * malloc (size_t size)                       086 void * malloc (size_t size)
088 {                                                 087 {
089   void * retval;                                  088   void * retval;
090                                                   089 
091   if (size <= 0)                                  090   if (size <= 0)
092     return NULL;                                  091     return NULL;
093                                                   092 
094   /* Align on a 4B boundary */                    093   /* Align on a 4B boundary */
095   size = ((size-1) & ~3) + 4;                     094   size = ((size-1) & ~3) + 4;
096                                                   095 
097   if (! kernel_heap_top)                          096   if (! kernel_heap_top)
098     kernel_heap_top = _sos_brk(0);                097     kernel_heap_top = _sos_brk(0);
099                                                   098 
100   if (! malloc_heap_top)                          099   if (! malloc_heap_top)
101     malloc_heap_top = kernel_heap_top;            100     malloc_heap_top = kernel_heap_top;
102                                                   101 
103   retval = malloc_heap_top;                       102   retval = malloc_heap_top;
104   malloc_heap_top += size;                        103   malloc_heap_top += size;
105                                                   104 
106   _sos_brk(malloc_heap_top);                   << 
107   return retval;                                  105   return retval;
108 }                                              << 
109                                                << 
110                                                << 
111 void free(void *ptr)                           << 
112 {                                              << 
113   //  bochs_printf("Free ignored (not implemen << 
114 }                                              << 
115                                                << 
116                                                << 
117 int open(const char *pathname, int flags, /* m << 
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, whenc << 
153   if (retval < 0)                              << 
154     return retval;                             << 
155   return result;                               << 
156 }                                              << 
157                                                << 
158                                                << 
159 loff_t lseek64(int fd, loff_t offset, int when << 
160 {                                              << 
161   loff_t result = offset;                      << 
162   int retval = _sos_seek64(fd, & result, whenc << 
163   if (retval < 0)                              << 
164     return retval;                             << 
165   return result;                               << 
166 }                                              << 
167                                                << 
168                                                << 
169 void * mmap(void *start, size_t length, int pr << 
170             int fd, loff_t offset)             << 
171 {                                              << 
172   /* At kernel side, offset is considered posi << 
173   if (offset < 0)                              << 
174     return NULL;                               << 
175                                                << 
176   if (0 != _sos_fmmap(& start, length, prot, f << 
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 << 
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->di << 
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 *b << 
233 {                                              << 
234   return _sos_stat(file_name, TRUE, buf);      << 
235 }                                              << 
236                                                << 
237                                                << 
238 int lstat(const char *file_name, struct stat * << 
239 {                                              << 
240   return _sos_stat(file_name, FALSE, buf);     << 
241 }                                                 106 }
                                                      

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