OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [common/] [v2_0/] [doc/] [libc.sgml] - Blame information for rev 545

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
2
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
30
31
 
32
33
The ISO Standard C and Math Libraries
34
35
<!-- <xref> -->C and math library overview
36
37
eCos provides compatibility with the
38
        ISO 9899:1990 specification for the standard C library, which
39
        is essentially the same as the better-known ANSI C3.159-1989
40
        specification (C-89).
41
There are three aspects of this compatibility supplied by eCos.
42
First there is a C library which
43
implements the functions defined by the ISO standard, except for the
44
mathematical functions. This is provided by the eCos C library
45
packages. 
46
Then eCos provides a math
47
library, which implements the mathematical functions from the ISO
48
C library. This distinction between C and math libraries is frequently
49
drawn — most standard C library implementations provide
50
separate linkable files for the two, and the math library contains
51
all the functions from the math.h header
52
file.
53
There is a third element to the ISO C library, which is the
54
environment in which applications run when they use the standard
55
C library. This environment is set up by the C library startup procedure
56
()
57
and it provides (among other things) a main() entry
58
point function, an exit() function that
59
does the cleanup required by the standard (including handlers registered
60
using the atexit() function), and an environment
61
that can be read with getenv(). 
62
The description in this manual focuses on the eCos-specific
63
aspects of the C library (mostly related to eCos's
64
configurability) as well as mentioning the omissions from the standard
65
in this release. We do not attempt to define the semantics of each
66
function, since that information can be found in the ISO, ANSI,
67
POSIX and IEEE standards, and the many good books that have been
68
written about the standard C library, that cover usage of these
69
functions in a more general and useful way.
70
71
Included non-ISO functions
72
The following functions from the POSIX specification
73
are included for convenience: 
74
        extern char **environ variable
75
(for setting up the environment for use with getenv())
76
        _exit() 
77
        strtok_r() 
78
        rand_r() 
79
        asctime_r() 
80
        ctime_r() 
81
        localtime_r() 
82
        gmtime_r() 
83
eCos provides the following additional
84
implementation-specific functions within the standard C library
85
to adjust the date and time settings:
86
void cyg_libc_time_setdst(
87
  cyg_libc_time_dst state
88
);
89
This function sets the state of Daylight Savings Time. The
90
values for state are:
91
CYG_LIBC_TIME_DSTNA   unknown
92
CYG_LIBC_TIME_DSTOFF  off
93
CYG_LIBC_TIME_DSTON   on
94
 
95
void cyg_libc_time_setzoneoffsets(
96
  time_t stdoffset, time_t dstoffset
97
);
98
This function sets the offsets from UTC used when Daylight
99
Savings Time is enabled or disabled. The offsets are in time_t’s,
100
which are seconds in the current inplementation.
101
Cyg_libc_time_dst cyg_libc_time_getzoneoffsets(
102
  time_t *stdoffset, time_t *dstoffset
103
);
104
This function retrieves the current setting for Daylight Savings
105
Time along with the offsets used for both STD and DST. The offsets
106
are both in time_t’s, which are seconds in the
107
current implementation.
108
cyg_bool cyg_libc_time_settime(
109
  time_t utctime
110
);
111
This function sets the current time for the system The time
112
is specified as a time_t in UTC.
113
It returns non-zero on error.
114
115
116
Math library compatibility modes
117
This math library is capable of being operated in several
118
different compatibility modes. These options deal solely with how
119
errors are handled. 
120
There are 4 compatibility modes: ANSI/POSIX 1003.1;
121
IEEE-754; X/Open Portability Guide issue 3 (XPG3); and
122
System V Interface Definition Edition 3. 
123
In IEEE mode, the matherr() function
124
(see below) is never called, no warning messages are printed on
125
the stderr output stream, and errno is never set. 
126
In ANSI/POSIX mode, errno is set correctly,
127
but matherr() is never called and no warning messages
128
are printed on the stderr output stream. 
129
In X/Open mode, errno is set correctly,
130
matherr() is called, but no warning messages are printed
131
on the stderr output stream. 
132
In SVID mode, functions which overflow return
133
a value HUGE (defined in math.h), which is the maximum
134
single precision floating point value (as opposed to
135
HUGE_VAL which is meant to stand for infinity). errno is
136
set correctly and matherr() is called. If
137
matherr() returns 0, warning messages are printed on
138
the stderr output stream for some errors. 
139
The mode can be compiled-in as IEEE-only, or any one of the
140
above methods settable at run-time. 
141
142
This math library assumes that the hardware (or software floating
143
point emulation) supports IEEE-754 style arithmetic, 32-bit 2's
144
complement integer arithmetic, doubles are in 64-bit IEEE-754 format.
145
146
147
<!-- <index></index> -->        matherr()
148
As mentioned above, in X/Open or SVID modes, the user
149
            can supply a function matherr() of
150
            the form:
151
int matherr( struct exception *e )
152
153
where struct exception is defined as:
154
struct exception {
155
 int type;
156
 char *name;
157
 double arg1, arg2, retval;
158
}; 
159
type is the exception type and is one of:
160
161
162
DOMAIN  
163
164
 
165
argument domain exception
166
167
168
169
SING    
170
171
 
172
argument singularity
173
174
175
176
OVERFLOW        
177
178
 
179
overflow range exception
180
181
182
183
UNDERFLOW       
184
185
 
186
underflow range exception
187
188
189
190
TLOSS   
191
192
 
193
total loss of significance
194
195
196
197
PLOSS   
198
199
 
200
partial loss of significance
201
202
203
204
name is a string containing the name of the
205
function
206
arg1 and arg2 are the
207
arguments passed to the function
208
retval is the default value that will be returned
209
by the function, and can be changed by matherr()
210
211
matherr must have “C” linkage, not “C++” linkage.
212
213
If matherr returns zero, or the user doesn't supply
214
their own matherr, then the following usually happens
215
in SVID mode:
216
          
217
Behavior of math exception handling
218
219
220
221
  Type
222
  Behavior
223
224
225
226
227
DOMAIN0.0 returned,
228
errno=EDOM, and a message printed on stderr
229
230
SINGHUGE of appropriate
231
sign is returned, errno=EDOM, and a message is printed
232
on stderr
233
234
OVERFLOWHUGE of
235
appropriate sign is returned, and errno=ERANGE
236
237
UNDERFLOW0.0 is
238
returned and errno=ERANGE
239
240
TLOSS0.0 is returned,
241
errno=ERANGE, and a message is printed on stderr
242
243
PLOSSThe current
244
implementation doesn't return this type
245
246
247
248
249
X/Open mode is similar except that the message is
250
not printed on stderr and HUGE_VAL is used in place of
251
HUGE
252
253
254
Thread-safety and re-entrancy
255
With the appropriate configuration options set below, the
256
math library is fully thread-safe if:
257
258
259
Depending on the compatibility mode, the
260
                setting of the errno variable from the C library is
261
                thread-safe
262
263
264
Depending on the compatibility mode, sending error messages
265
to the stderr output stream using the C library
266
fputs()
267
 function is thread-safe 
268
269
270
Depending on the compatibility mode, the user-supplied
271
matherr()
272
 function and anything it depends on are thread-safe 
273
274
275
In addition, with the exception of the gamma*() and lgamma*() functions,
276
the math library is reentrant (and thus safe to use from interrupt handlers)
277
if the Math library is always in IEEE mode.
278
279
280
281
Some implementation details
282
Here are some details about the implementation
283
which might be interesting, although they do not affect the ISO-defined
284
semantics of the library. 
285
286
287
It is possible to configure
288
eCos
289
 to have the standard C library without the kernel. You might want
290
to do this to use less memory. But if you disable the kernel, you
291
will be unable to use memory allocation, thread-safety and certain
292
stdio functions such as input. Other C library functionality is
293
unaffected.
294
295
296
The opaque type returned by
297
clock()
298
 is called clock_t, and is implemented as a 64 bit integer.
299
The value returned by
300
clock()
301
 is only correct if the kernel is configured with real-time clock
302
support, as determined by the CYGVAR_KERNEL_COUNTERS_CLOCK
303
configuration option in
304
kernel.h
305
.
306
307
308
The FILE type is not implemented as a structure, but rather
309
as a CYG_ADDRESS. 
310
311
312
The GNU C compiler will place its own built-in implementations
313
instead of some C library functions. This can be turned off with
314
the -fno-builtin option. The functions affected
315
by this are
316
abs()
317
,
318
cos()
319
,
320
fabs()
321
,
322
labs()
323
,
324
memcmp()
325
,
326
memcpy()
327
,
328
sin()
329
,
330
sqrt()
331
,
332
strcmp()
333
,
334
strcpy()
335
, and
336
strlen()
337
.
338
339
340
For faster execution speed you should avoid this option
341
and let the compiler use its built-ins. This can be turned off by
342
invoking
343
GCC
344
 with the -fno-builtin option. 
345
346
347
memcpy()
348
 and
349
memset()
350
 are located in the infrastructure package, not in the C library
351
package. This is because the compiler calls these functions, and
352
the kernel needs to resolve them even if the C library is not configured. 
353
354
355
Error codes such as EDOM and ERANGE, as well as
356
strerror()
357
, are implemented in the error package. The
358
error package is separate from the rest of the C and math libraries
359
so that the rest of
360
eCos
361
 can use these error handling facilities even if the C library is
362
not configured. 
363
364
365
When
366
free()
367
 is invoked, heap memory will normally be coalesced. If the CYGSEM_KERNEL_MEMORY_COALESCE
368
configuration parameter is not set, memory will not be coalesced,
369
which might cause programs to fail. 
370
371
372
Signals, as implemented by
373
<signal.h>, are guaranteed to work
374
correctly if raised using the
375
raise()
376
 function from a normal working program context. Using signals from
377
within an ISR or DSR context is not expected to work. Also, it is
378
not guaranteed that if CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
379
is set, that handling a signal using
380
signal()
381
 will necessarily catch that form of exception. For example, it
382
may be expected that a divide-by-zero error would be caught by handling
383
SIGFPE. However it depends on the underlying HAL implementation to implement
384
the required hardware exception. And indeed the hardware itself
385
may not be capable of detecting these exceptions so it may not be
386
possible for the HAL implementer to do this in any case. Despite
387
this lack of guarantees in this respect, the signals implementation
388
is still ISO C compliant since ISO C does not offer any such guarantees
389
either. 
390
391
392
The
393
getenv()
394
 function is implemented (unless the CYGPKG_LIBC_ENVIRONMENT configuration
395
option is turned off), but there is no shell or
396
putenv()
397
 function to set the environment dynamically. The environment is
398
set in a global variable environ, declared as:
399
extern char **environ; // Standard environment definition
400
The environment can be statically initialized at startup time
401
using the CYGDAT_LIBC_DEFAULT_ENVIRONMENT
402
option. If so, remember that the final entry of the array initializer
403
must be NULL. 
404
405
406
Here is a minimal eCos program which
407
demonstrates the use of environments (see also the test case in language/c/libc/current/tests/stdlib/getenv.c): 
408
#include <stdio.h>
409
#include <stdlib.h> // Main header for stdlib functions
410
 
411
extern char **environ; // Standard environment definition
412
 
413
int
414
main( int argc, char *argv[] )
415
{
416
 char *str;
417
 char *env[] = { "PATH=/usr/local/bin:/usr/bin",
418
 "HOME=/home/fred",
419
 "TEST=1234=5678",
420
 "home=hatstand",
421
 NULL };
422
 
423
 printf("Display the current PATH environment variable\n");
424
 
425
 environ = (char **)&env;
426
 
427
 str = getenv("PATH");
428
 
429
 if (str==NULL) {
430
  printf("The current PATH is unset\n");
431
 } else {
432
  printf("The current PATH is \"%s\"\n", str);
433
 }
434
 return 0;
435
} 
436
437
438
<!-- <index></index> -->Thread safety
439
The ISO C library has configuration options that control thread
440
safety, i.e. working behavior if multiple threads call the same
441
function at the same time.
442
The following functionality has to be configured correctly,
443
or used carefully in a multi-threaded environment:
444
445
446
mblen()
447
448
449
mbtowc()
450
451
452
wctomb()
453
454
455
456
printf()
457
 (and all standard I/O functions except for
458
sprintf()
459
 and
460
sscanf()
461
462
463
strtok()
464
465
466
rand()
467
 and
468
srand()
469
470
471
signal()
472
 and
473
raise()
474
475
476
asctime()
477
,
478
ctime()
479
,
480
gmtime()
481
, and
482
localtime()
483
484
485
the
486
errno
487
 variable
488
489
490
the
491
environ
492
 variable
493
494
495
date and time settings
496
497
498
In some cases, to make eCos development
499
easier, functions are provided (as specified by POSIX 1003.1) that define
500
re-entrant alternatives, i.e. rand_r(), strtok_r(), asctime_r(), ctime_r(), gmtime_r(),
501
and localtime_r(). In other cases,
502
configuration options are provided that control either locking of functions
503
or their shared data, such as with standard I/O streams,
504
or by using per-thread data, such as with the errno variable.
505
In some other cases, like the setting of date and time, no
506
re-entrant or thread-safe alternative or configuration is provided
507
as it is simply not a worthwhile addition (date and time should
508
rarely need to be set.)
509
510
511
<!-- <index></index> --><!-- <xref> -->C library startup
512
The C library includes a function declared as:
513
void cyg_iso_c_start( void )
514
This function is used to start an environment in which an
515
ISO C style program can run in the most compatible way.
516
What this function does is to create a thread which will invoke main() — normally
517
considered a program's entry point. In particular, it can
518
supply arguments to main() using the CYGDAT_LIBC_ARGUMENTS
519
configuration option, and when returning from main(),
520
or calling exit(), pending stdio file output
521
is flushed and any functions registered with atexit() are
522
invoked. This is all compliant with the ISO C standard in this respect. 
523
This thread starts execution when the eCos scheduler
524
is started. If the eCos kernel package is not
525
available (and hence there is no scheduler), then cyg_iso_c_start() will
526
invoke the main() function directly, i.e.
527
it will not return until the main() function
528
returns. 
529
The main() function should be defined
530
as the following, and if defined in a C++ file,
531
should have “C” linkage: 
532
extern int main(
533
  int argc,
534
  char *argv[] )
535
The thread that is started by cyg_iso_c_start() can
536
be manipulated directly, if you wish. For example you can suspend
537
it. The kernel C API needs a handle to do this, which is available
538
by including the following in your source code.
539
extern cyg_handle_t cyg_libc_main_thread;
540
Then for example, you can suspend the thread with the line:
541
cyg_thread_suspend( cyg_libc_main_thread );
542
If you call cyg_iso_c_start() and
543
do not provide your own main() function,
544
the system will provide a main() for you
545
which will simply return immediately.
546
In the default configuration, cyg_iso_c_start() is
547
invoked automatically by the cyg_package_start() function
548
in the infrastructure configuration. This means that in the simplest
549
case, your program can indeed consist of simply:
550
int main( int argc, char *argv[] )
551
{
552
 printf("Hello eCos\n");
553
}
554
If you override cyg_package_start() or cyg_start(),
555
or disable the infrastructure configuration option CYGSEM_START_ISO_C_COMPATIBILITY
556
then you must ensure that you call cyg_iso_c_start() yourself
557
if you want to be able to have your program start at the entry point
558
of main() automatically.
559
560
561

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.