1 /* Machine-independent I/O routines for gcov.
2 Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include <sys/types.h>
27 static int __fetch_long
PARAMS ((long *, char *, size_t)) ATTRIBUTE_UNUSED
;
28 static int __read_long
PARAMS ((long *, FILE *, size_t)) ATTRIBUTE_UNUSED
;
29 static int __write_long
PARAMS ((long, FILE *, size_t)) ATTRIBUTE_UNUSED
;
30 static int __fetch_gcov_type
PARAMS ((gcov_type
*, char *, size_t)) ATTRIBUTE_UNUSED
;
31 static int __store_gcov_type
PARAMS ((gcov_type
, char *, size_t)) ATTRIBUTE_UNUSED
;
32 static int __read_gcov_type
PARAMS ((gcov_type
*, FILE *, size_t)) ATTRIBUTE_UNUSED
;
33 static int __write_gcov_type
PARAMS ((gcov_type
, FILE *, size_t)) ATTRIBUTE_UNUSED
;
35 /* These routines only work for signed values. */
37 /* Store a portable representation of VALUE in DEST using BYTES*8-1 bits.
38 Return a non-zero value if VALUE requires more than BYTES*8-1 bits
42 __store_gcov_type (value
, dest
, bytes
)
47 int upper_bit
= (value
< 0 ? 128 : 0);
52 gcov_type oldvalue
= value
;
54 if (oldvalue
!= -value
)
58 for(i
= 0 ; i
< (sizeof (value
) < bytes
? sizeof (value
) : bytes
) ; i
++) {
59 dest
[i
] = value
& (i
== (bytes
- 1) ? 127 : 255);
63 if (value
&& value
!= -1)
66 for(; i
< bytes
; i
++)
68 dest
[bytes
- 1] |= upper_bit
;
72 /* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store
73 the result in DEST. Returns a non-zero value if the value in SOURCE
74 will not fit in DEST. */
77 __fetch_gcov_type (dest
, source
, bytes
)
85 for (i
= bytes
- 1; (size_t) i
> (sizeof (*dest
) - 1); i
--)
86 if (source
[i
] & ((size_t) i
== (bytes
- 1) ? 127 : 255 ))
90 value
= value
* 256 + (source
[i
] & ((size_t)i
== (bytes
- 1) ? 127 : 255));
92 if ((source
[bytes
- 1] & 128) && (value
> 0))
100 __fetch_long (dest
, source
, bytes
)
108 for (i
= bytes
- 1; (size_t) i
> (sizeof (*dest
) - 1); i
--)
109 if (source
[i
] & ((size_t) i
== (bytes
- 1) ? 127 : 255 ))
113 value
= value
* 256 + (source
[i
] & ((size_t)i
== (bytes
- 1) ? 127 : 255));
115 if ((source
[bytes
- 1] & 128) && (value
> 0))
122 /* Write a BYTES*8-bit quantity to FILE, portably. Returns a non-zero
123 value if the write fails, or if VALUE can't be stored in BYTES*8
126 Note that VALUE may not actually be large enough to hold BYTES*8
127 bits, but BYTES characters will be written anyway.
129 BYTES may be a maximum of 10. */
132 __write_gcov_type (value
, file
, bytes
)
139 if (bytes
> 10 || __store_gcov_type (value
, c
, bytes
))
142 return fwrite(c
, 1, bytes
, file
) != bytes
;
146 __write_long (value
, file
, bytes
)
153 if (bytes
> 10 || __store_gcov_type ((gcov_type
)value
, c
, bytes
))
156 return fwrite(c
, 1, bytes
, file
) != bytes
;
159 /* Read a quantity containing BYTES bytes from FILE, portably. Return
160 a non-zero value if the read fails or if the value will not fit
163 Note that DEST may not be large enough to hold all of the requested
164 data, but the function will read BYTES characters anyway.
166 BYTES may be a maximum of 10. */
169 __read_gcov_type (dest
, file
, bytes
)
176 if (bytes
> 10 || fread(c
, 1, bytes
, file
) != bytes
)
179 return __fetch_gcov_type (dest
, c
, bytes
);
183 __read_long (dest
, file
, bytes
)
190 if (bytes
> 10 || fread(c
, 1, bytes
, file
) != bytes
)
193 return __fetch_long (dest
, c
, bytes
);
196 #endif /* ! GCC_GCOV_IO_H */