Import upstream 2.3.14
[packages/xinetd.git] / libs / src / sio / impl.h
1 /*
2  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
3  * All rights reserved.  The file named COPYRIGHT specifies the terms 
4  * and conditions for redistribution.
5  */
6
7 /*
8  * $Id: impl.h,v 1.2 2003/03/09 19:29:21 steveg Exp $
9  */
10
11 #ifndef SIO_BUFFER_SIZE
12
13 #include "sioconf.h"
14 #include "sio.h"
15
16 #ifdef HAVE_MMAP
17 #include <sys/types.h>
18
19
20 /*
21  * A struct map_unit describes a memory mapped area of a file.
22  *
23  * addr is the address where the file is mapped. If addr is NULL
24  * the other fields are meaningless.
25  * valid_bytes indicates how many bytes are _valid_ in that unit
26  * mapped_bytes of a unit is how many bytes are mapped in that
27  * unit ( valid <= mapped ).
28  * Normally mapped_bytes will be equal to valid_bytes until
29  * we reach the end of the file. Then if the file size is not a multiple
30  * of the unit size, only the rest of the file will be mapped at the
31  * unit, leaving part of what was mapped at the unit before still
32  * visible (this happens because I chose not to unmap a unit before
33  * remapping it). In that case valid_bytes shows the size of the "new"
34  * mapping and mapped_bytes shows how many bytes are really mapped.
35  * mapped_bytes is used in Sdone() to unmap the units.
36  */
37 struct map_unit
38 {
39         caddr_t addr ;
40         size_t valid_bytes ;
41         size_t mapped_bytes ;
42 } ;
43
44
45 /*
46  * Meaning of fields used when memory mapping:
47  *
48  *    file_offset:      it is the offset in the file where the next
49  *                      mapping should be done
50  *
51  *    file_size:        size of the file (obtained from stat(2))
52  */
53 struct mmap_descriptor
54 {
55    off_t file_offset ;
56    off_t file_size ;
57         struct map_unit first_unit ;
58         struct map_unit second_unit ;
59 } ;
60
61 typedef struct mmap_descriptor mapd_s ;
62
63 #endif /* HAVE_MMAP */
64
65 typedef enum { FAILURE, SUCCESS } sio_status_e ;
66
67 /*
68  * Descriptor management: convert a descriptor pointer to an input or
69  * output descriptor pointer
70  */
71 #define IDP( dp )                                               (&(dp)->descriptor.input_descriptor)
72 #define ODP( dp )                                               (&(dp)->descriptor.output_descriptor)
73
74 #define DESCRIPTOR_INITIALIZED( dp )    ((dp)->initialized)
75
76 /*
77  * Internal constants
78  */
79 #define SIO_BUFFER_SIZE         8192
80 #define SIO_NO_TIED_FD                          (-1)
81
82 typedef enum { NO = 0, YES = 1 } boolean_e ;
83
84 #ifndef FALSE
85 #define FALSE                   0
86 #define TRUE                    1
87 #endif
88
89 #ifndef NULL
90 #define NULL                    0
91 #endif
92
93 #ifdef MIN
94 #undef MIN
95 #endif
96 #define MIN( a, b )                                     ( (a) < (b) ? (a) : (b) )
97
98 #define NUL                                                             '\0'
99
100 #ifdef DEBUG
101
102 static char *itoa( num )
103         unsigned num ;
104 {
105 #define NUMBUF_SIZE             15
106         static char numbuf[ NUMBUF_SIZE ] ;
107         char *p = &numbuf[ NUMBUF_SIZE ] ;
108
109         *--p = '\0' ;
110         do
111         {
112                 *--p = num % 10 + '0' ;
113                 num /= 10 ;
114         }
115         while ( num ) ;
116         return( p ) ;
117 }
118
119 #       define ASSERT( expr )                                                                                                           \
120                 if ( ! (expr) )                                                                                                                 \
121                 {                                                                                                                                                               \
122                         char *s1 = "SIO assertion << expr >> failed: File: " ;  \
123                         char *s2 = __FILE__ ;                                                                                           \
124                         char *s3 = ", line: " ;                                                                                         \
125                         char *s4 = itoa( __LINE__ ) ;                                                                           \
126                         char *s5 = "\n" ;                                                                                                               \
127                         (void) write( 2, s1, strlen( s1 ) ) ;                                                   \
128                         (void) write( 2, s2, strlen( s2 ) ) ;                                                   \
129                         (void) write( 2, s3, strlen( s3 ) ) ;                                                   \
130                         (void) write( 2, s4, strlen( s4 ) ) ;                                                   \
131                         (void) write( 2, s5, strlen( s5 ) ) ;                                                   \
132                         exit ( 1 ) ;                                                                                                                    \
133                 }
134 #else
135 #       define ASSERT( expr )
136 #endif
137
138
139 #include <errno.h>
140
141 /*
142  * Internal functions that are visible
143  */
144 int __sio_writef( __sio_od_t *odp, int fd ) ;
145 int __sio_extend_buffer( __sio_id_t *idp, int fd, int b_left ) ;
146 int __sio_init( __sio_descriptor_t *dp, int fd, enum __sio_stream stream_type );
147 int __sio_more( __sio_id_t *idp, int fd ) ;
148 sio_status_e __sio_switch( __sio_id_t *idp, int fd ) ;
149
150
151 #ifdef HAVE_MEMCPY
152 #include <memory.h>
153 #define sio_memcopy( from, to, nbytes ) (void) memcpy( to, from, nbytes )
154 #define sio_memscan( from, nbytes, ch )   memchr( from, ch, nbytes )
155 #else
156 #define sio_memcopy( from, to, nbytes )      (void) bcopy( from, to, nbytes )
157 #endif
158
159 #ifndef sio_memcopy
160 #define sio_memcopy             __sio_memcopy
161 #define NEED_MEMCOPY
162 void __sio_memcopy() ;
163 #endif
164
165 #ifndef sio_memscan
166 char *sio_memscan() ;
167 #endif
168
169 #endif /* SIO_BUFFER_SIZE */
170