1999-08-26 09:30:50 +00:00
|
|
|
/* Machine-independent I/O routines for gcov.
|
2002-02-01 18:16:02 +00:00
|
|
|
Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
|
1999-08-26 09:30:50 +00:00
|
|
|
Contributed by Bob Manson <manson@cygnus.com>.
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
This file is part of GCC.
|
1999-08-26 09:30:50 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
GCC is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 2, or (at your option) any later
|
|
|
|
version.
|
1999-08-26 09:30:50 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2002-02-01 18:16:02 +00:00
|
|
|
along with GCC; see the file COPYING. If not, write to the Free
|
|
|
|
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA. */
|
1999-08-26 09:30:50 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
#ifndef GCC_GCOV_IO_H
|
|
|
|
#define GCC_GCOV_IO_H
|
1999-08-26 09:30:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
static int __fetch_long PARAMS ((long *, char *, size_t)) ATTRIBUTE_UNUSED;
|
|
|
|
static int __read_long PARAMS ((long *, FILE *, size_t)) ATTRIBUTE_UNUSED;
|
|
|
|
static int __write_long PARAMS ((long, FILE *, size_t)) ATTRIBUTE_UNUSED;
|
|
|
|
static int __fetch_gcov_type PARAMS ((gcov_type *, char *, size_t)) ATTRIBUTE_UNUSED;
|
|
|
|
static int __store_gcov_type PARAMS ((gcov_type, char *, size_t)) ATTRIBUTE_UNUSED;
|
|
|
|
static int __read_gcov_type PARAMS ((gcov_type *, FILE *, size_t)) ATTRIBUTE_UNUSED;
|
|
|
|
static int __write_gcov_type PARAMS ((gcov_type, FILE *, size_t)) ATTRIBUTE_UNUSED;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
/* These routines only work for signed values. */
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
/* Store a portable representation of VALUE in DEST using BYTES*8-1 bits.
|
|
|
|
Return a non-zero value if VALUE requires more than BYTES*8-1 bits
|
2002-02-01 18:16:02 +00:00
|
|
|
to store. */
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
static int
|
2002-02-01 18:16:02 +00:00
|
|
|
__store_gcov_type (value, dest, bytes)
|
|
|
|
gcov_type value;
|
1999-08-26 09:30:50 +00:00
|
|
|
char *dest;
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
int upper_bit = (value < 0 ? 128 : 0);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
gcov_type oldvalue = value;
|
1999-08-26 09:30:50 +00:00
|
|
|
value = -value;
|
|
|
|
if (oldvalue != -value)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0 ; i < (sizeof (value) < bytes ? sizeof (value) : bytes) ; i++) {
|
|
|
|
dest[i] = value & (i == (bytes - 1) ? 127 : 255);
|
|
|
|
value = value / 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value && value != -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for(; i < bytes ; i++)
|
|
|
|
dest[i] = 0;
|
|
|
|
dest[bytes - 1] |= upper_bit;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve a quantity containing BYTES*8-1 bits from SOURCE and store
|
|
|
|
the result in DEST. Returns a non-zero value if the value in SOURCE
|
2002-02-01 18:16:02 +00:00
|
|
|
will not fit in DEST. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
__fetch_gcov_type (dest, source, bytes)
|
|
|
|
gcov_type *dest;
|
|
|
|
char *source;
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
gcov_type value = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
|
|
|
|
if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (; i >= 0; i--)
|
|
|
|
value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));
|
|
|
|
|
|
|
|
if ((source[bytes - 1] & 128) && (value > 0))
|
|
|
|
value = - value;
|
|
|
|
|
|
|
|
*dest = value;
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
__fetch_long (dest, source, bytes)
|
|
|
|
long *dest;
|
|
|
|
char *source;
|
1999-10-16 06:09:09 +00:00
|
|
|
size_t bytes;
|
1999-08-26 09:30:50 +00:00
|
|
|
{
|
|
|
|
long value = 0;
|
|
|
|
int i;
|
|
|
|
|
1999-10-16 06:09:09 +00:00
|
|
|
for (i = bytes - 1; (size_t) i > (sizeof (*dest) - 1); i--)
|
|
|
|
if (source[i] & ((size_t) i == (bytes - 1) ? 127 : 255 ))
|
1999-08-26 09:30:50 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (; i >= 0; i--)
|
1999-10-16 06:09:09 +00:00
|
|
|
value = value * 256 + (source[i] & ((size_t)i == (bytes - 1) ? 127 : 255));
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
if ((source[bytes - 1] & 128) && (value > 0))
|
|
|
|
value = - value;
|
|
|
|
|
|
|
|
*dest = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a BYTES*8-bit quantity to FILE, portably. Returns a non-zero
|
|
|
|
value if the write fails, or if VALUE can't be stored in BYTES*8
|
|
|
|
bits.
|
|
|
|
|
|
|
|
Note that VALUE may not actually be large enough to hold BYTES*8
|
|
|
|
bits, but BYTES characters will be written anyway.
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
BYTES may be a maximum of 10. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
__write_gcov_type (value, file, bytes)
|
|
|
|
gcov_type value;
|
|
|
|
FILE *file;
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
char c[10];
|
|
|
|
|
|
|
|
if (bytes > 10 || __store_gcov_type (value, c, bytes))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return fwrite(c, 1, bytes, file) != bytes;
|
|
|
|
}
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
__write_long (value, file, bytes)
|
|
|
|
long value;
|
|
|
|
FILE *file;
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
char c[10];
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
if (bytes > 10 || __store_gcov_type ((gcov_type)value, c, bytes))
|
1999-08-26 09:30:50 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return fwrite(c, 1, bytes, file) != bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a quantity containing BYTES bytes from FILE, portably. Return
|
|
|
|
a non-zero value if the read fails or if the value will not fit
|
|
|
|
in DEST.
|
|
|
|
|
|
|
|
Note that DEST may not be large enough to hold all of the requested
|
|
|
|
data, but the function will read BYTES characters anyway.
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
BYTES may be a maximum of 10. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
__read_gcov_type (dest, file, bytes)
|
|
|
|
gcov_type *dest;
|
|
|
|
FILE *file;
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
char c[10];
|
|
|
|
|
|
|
|
if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return __fetch_gcov_type (dest, c, bytes);
|
|
|
|
}
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
__read_long (dest, file, bytes)
|
|
|
|
long *dest;
|
|
|
|
FILE *file;
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
char c[10];
|
|
|
|
|
|
|
|
if (bytes > 10 || fread(c, 1, bytes, file) != bytes)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return __fetch_long (dest, c, bytes);
|
|
|
|
}
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
#endif /* ! GCC_GCOV_IO_H */
|