Просмотр исходного кода

Use offsetof() instead of ad-hoc calculations

Guillem Jover лет назад: 18
Родитель
Сommit
411e1d5de6
4 измененных файлов с 18 добавлено и 7 удалено
  1. 10 0
      ChangeLog
  2. 4 0
      lib/dpkg.h
  3. 3 5
      lib/parsedump.h
  4. 1 2
      lib/tarfn.c

+ 10 - 0
ChangeLog

@@ -1,3 +1,13 @@
+2008-01-22  Guillem Jover  <guillem@debian.org>
+
+	* lib/dpkg.h [!offsetof] (offsetof): New compatibilty macro.
+	* lib/parsedump.h (PKGIFPOFF): Use offsetof instead of ad-hoc
+	calculation.
+	(FILEFOFF): Likewise.
+	(struct fieldinfo): Change integer member type to size_t.
+	* lib/tarfn.c (TarChecksumOffset): Use offsetof instead of ad-hoc
+	calculation.
+
 2008-01-22  Guillem Jover  <guillem@debian.org>
 
 	* configure.ac (AC_CHECK_HEADERS): Check for 'stddef.h'.

+ 4 - 0
lib/dpkg.h

@@ -414,6 +414,10 @@ void unsetenv(const char *x);
 
 /*** other compatibility functions ***/
 
+#ifndef offsetof
+#define offsetof(st, m) ((size_t)&((st *)NULL)->m)
+#endif
+
 #ifndef HAVE_STRTOUL
 #define strtoul strtol
 #endif

+ 3 - 5
lib/parsedump.h

@@ -25,12 +25,10 @@
 
 struct fieldinfo;
 
-#define PKGIFPOFF(f) ((char*)(&(((struct pkginfoperfile *)0x1000)->f)) - \
-                      (char*)(struct pkginfoperfile *)0x1000)
+#define PKGIFPOFF(f) (offsetof(struct pkginfoperfile, f))
 #define PKGPFIELD(pifp,of,type) (*(type*)((char*)(pifp)+(of)))
 
-#define FILEFOFF(f) ((char*)(&(((struct filedetails *)0x1000)->f)) - \
-                     (char*)(struct filedetails *)0x1000)
+#define FILEFOFF(f) (offsetof(struct filedetails, f))
 #define FILEFFIELD(filedetail,of,type) (*(type*)((char*)(filedetail)+(of)))
 
 typedef void freadfunction(struct pkginfo *pigp, struct pkginfoperfile *pifp,
@@ -56,7 +54,7 @@ struct fieldinfo {
   const char *name;
   freadfunction *rcall;
   fwritefunction *wcall;
-  unsigned int integer;
+  size_t integer;
 };
 
 void parseerr(FILE *file, const char *filename, int lno, FILE *warnto, int *warncount,

+ 1 - 2
lib/tarfn.c

@@ -34,8 +34,7 @@ struct TarHeader {
 };
 typedef struct TarHeader	TarHeader;
 
-static const unsigned int	TarChecksumOffset
-	= (unsigned int)&(((TarHeader *)NULL)->Checksum);
+static const size_t TarChecksumOffset = offsetof(TarHeader, Checksum);
 
 /* Octal-ASCII-to-long */
 static long