]> git.wh0rd.org - fontconfig.git/commitdiff
Add *NeededBytesAlign(), which overestimates the padding which is later
authorPatrick Lam <plam@MIT.EDU>
Wed, 16 Nov 2005 15:55:17 +0000 (15:55 +0000)
committerPatrick Lam <plam@MIT.EDU>
Wed, 16 Nov 2005 15:55:17 +0000 (15:55 +0000)
    added by the new ALIGN macro. Fix alignment problems on ia64 and s390
    by bumping up block_ptr appropriately. (Earlier version by Andreas
    Schwab).
Use sysconf to determine proper PAGESIZE value; this appears to be
    POSIX-compliant. (reported by Andreas Schwab)
reviewed by: plam

ChangeLog
src/fccache.c
src/fccharset.c
src/fcfs.c
src/fcint.h
src/fclang.c
src/fcname.c
src/fcpat.c

index e1e33ef118fea372d681e6c8a7bf4a268d8bd782..f2b0ec28b9afdde79717fffaae38304f89ed2f25 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2005-11-16  Patrick Lam  <plam@mit.edu>
+       * src/fccache.c (FcDirCacheProduce):
+       * src/fccharset.c (FcCharSetDistributeBytes):
+       * src/fcfs.c (FcFontSetDistributeBytes):
+       * src/fcint.h:
+       * src/fclang.c (FcLangSetDistributeBytes):
+       * src/fcname.c (FcObjectDistributeBytes):
+       * src/fcpat.c (FcPatternNeededBytes, FcValueListNeededBytes,
+               FcStrNeededBytes):
+       
+       Add *NeededBytesAlign(), which overestimates the padding which is
+       later added by the new ALIGN macro.  Fix alignment problems on
+       ia64 and s390 by bumping up block_ptr appropriately.  (Earlier
+       version by Andreas Schwab).
+
+2005-11-16  Stephan Kulow  <coolo@kde.org>
+       reviewed by: plam
+       
+       * src/fccache.c:
+
+       Use sysconf to determine proper PAGESIZE value; this
+       appears to be POSIX-compliant. (reported by Andreas Schwab)
+
 2005-11-04  Patrick Lam  <plam@mit.edu>
        * fc-lang/fc-lang.c:
        * src/fccharset.c:
index 69bb3853680c00d184b53f5e1de70f0864371ff9..af8a318c43e4f5743b693b548f9f16bbc458e9fe 100644 (file)
 #include <sys/types.h>
 #include <unistd.h>
 #include "fcint.h"
+#include <unistd.h>
 
 #define ENDIAN_TEST 0x12345678
-#define MACHINE_SIGNATURE_SIZE 9 + 5*19 + 1
+#define MACHINE_SIGNATURE_SIZE 9 + 5*20 + 1
 
 static off_t
 FcCacheSkipToArch (int fd, const char * arch);
@@ -417,7 +418,6 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
     return FcFalse;
 }
 
-#define PAGESIZE 8192
 /* 
  * Find the next presumably-mmapable offset after the supplied file
  * position.
@@ -425,10 +425,13 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
 static int
 FcCacheNextOffset(off_t w)
 {
-    if (w % PAGESIZE == 0) 
+    static long pagesize = -1;
+    if (pagesize == -1)
+       pagesize = sysconf(_SC_PAGESIZE);
+    if (w % pagesize == 0) 
        return w;
     else
-       return ((w / PAGESIZE)+1)*PAGESIZE;
+       return ((w / pagesize)+1)*pagesize;
 }
 
 /* return the address of the segment for the provided arch,
@@ -801,7 +804,8 @@ FcDirCacheProduce (FcFontSet *set, FcCache *metadata)
 
     memset (metadata, 0, sizeof(FcCache));
     FcFontSetNewBank();
-    metadata->count = FcFontSetNeededBytes (set);
+    metadata->count = FcFontSetNeededBytes (set) + 
+       FcFontSetNeededBytesAlign ();
     metadata->magic = FC_CACHE_MAGIC;
     metadata->bank = bank;
 
@@ -943,12 +947,12 @@ static char *
 FcCacheMachineSignature ()
 {
     static char buf[MACHINE_SIGNATURE_SIZE];
-    int magic = ENDIAN_TEST;
+    int32_t magic = ENDIAN_TEST;
     char * m = (char *)&magic;
 
     sprintf (buf, "%2x%2x%2x%2x "
             "%4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x %4x "
-            "%4x %4x %4x %4x %4x %4x %4x\n", 
+            "%4x %4x %4x %4x %4x %4x %4x %4x\n", 
             m[0], m[1], m[2], m[3],
             (unsigned int)sizeof (char),
             (unsigned int)sizeof (char *),
@@ -968,7 +972,8 @@ FcCacheMachineSignature ()
             (unsigned int)sizeof (FcChar16),
             (unsigned int)sizeof (FcCharLeaf),
             (unsigned int)sizeof (FcChar32),
-            (unsigned int)sizeof (FcCache));
+            (unsigned int)sizeof (FcCache),
+            (unsigned int)sysconf(_SC_PAGESIZE));
 
     return buf;
 }
index 9fa0e6b7b47db9adb6d3c71bdf774faafcdae765..790d78f3c619d1fab28096594f839a21413a5f9f 100644 (file)
@@ -1331,6 +1331,13 @@ FcCharSetNeededBytes (const FcCharSet *c)
        sizeof (FcChar16) * c->num;     /* number */
 }
 
+int
+FcCharSetNeededBytesAlign (void)
+{
+    return __alignof__ (FcCharSet) + __alignof__ (int) + 
+       __alignof__ (FcCharLeaf) + __alignof__ (FcChar16);
+}
+
 static FcBool
 FcCharSetEnsureBank (int bi)
 {
@@ -1373,15 +1380,19 @@ FcCharSetDistributeBytes (FcCache * metadata, void * block_ptr)
        return 0;
 
     charsets[bi] = (FcCharSet *)block_ptr;
+    block_ptr = ALIGN (block_ptr, FcCharSet);
     block_ptr = (void *)((char *)block_ptr + 
                     (sizeof (FcCharSet) * charset_count));
     numbers[bi] = (FcChar16 *)block_ptr;
+    block_ptr = ALIGN (block_ptr, FcChar16);
     block_ptr = (void *)((char *)block_ptr + 
                     (sizeof(FcChar16) * charset_numbers_count));
     leaves[bi] = (FcCharLeaf *)block_ptr;
+    block_ptr = ALIGN (block_ptr, FcCharLeaf);
     block_ptr = (void *)((char *)block_ptr +
                     (sizeof(FcCharLeaf) * charset_leaf_count));
     leaf_idx[bi] = (int *)block_ptr;
+    block_ptr = ALIGN (block_ptr, int);
     block_ptr = (void *)((char *)block_ptr +
                     (sizeof(int) * charset_leaf_idx_count));
 
index 3ba567479d1c225291f041670c14e7ae0d5269e3..0788a6f628769e7eea06aaf5e6a577457b2b929e 100644 (file)
@@ -108,9 +108,19 @@ FcFontSetNeededBytes (FcFontSet *s)
        return 0;
 }
 
+/* Returns an overestimate of the number of bytes that
+ * might later get eaten up by padding in the ALIGN macro. */
+int
+FcFontSetNeededBytesAlign (void)
+{
+    return __alignof__(int) + 
+       FcPatternNeededBytesAlign () + FcObjectNeededBytesAlign ();
+}
+
 void *
 FcFontSetDistributeBytes (FcCache * metadata, void * block_ptr)
 {
+    block_ptr = ALIGN (block_ptr, int);
     fcfs_pat_count = (int *)block_ptr;
     block_ptr = (int *)block_ptr + 1;
     // we don't consume any bytes for the fontset itself,
index 00ded265fe07316ec56ca270f3fa83b2ff787d10..4dbb7b4b0abb06ca9cba92c57433a30f652ffa6d 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <string.h>
 #include <ctype.h>
 #include <errno.h>
@@ -415,6 +416,8 @@ typedef struct _FcFileTime {
 
 typedef struct _FcCharMap FcCharMap;
 
+#define ALIGN(v,type) ((__typeof__(v))(((uintptr_t)(v) + __alignof__(type) - 1) & ~(__alignof__(type) - 1)))
+
 /* fcblanks.c */
 
 /* fccache.c */
@@ -550,6 +553,9 @@ FcCharSetNewBank (void);
 int
 FcCharSetNeededBytes (const FcCharSet *c);
 
+int
+FcCharSetNeededBytesAlign (void);
+
 void *
 FcCharSetDistributeBytes (FcCache * metadata,
                          void * block_ptr);
@@ -646,6 +652,9 @@ FcFontSetNewBank (void);
 int
 FcFontSetNeededBytes (FcFontSet *s);
 
+int
+FcFontSetNeededBytesAlign (void);
+
 void *
 FcFontSetDistributeBytes (FcCache * metadata, void * block_ptr);
 
@@ -741,6 +750,9 @@ FcLangSetNewBank (void);
 int
 FcLangSetNeededBytes (const FcLangSet *l);
 
+int
+FcLangSetNeededBytesAlign (void);
+
 void *
 FcLangSetDistributeBytes (FcCache * metadata,
                          void * block_ptr);
@@ -774,6 +786,9 @@ FcObjectToPtr (const char * si);
 int
 FcObjectNeededBytes (void);
 
+int
+FcObjectNeededBytesAlign (void);
+
 void *
 FcObjectUnserialize (FcCache metadata, void *block_ptr);
 
@@ -837,6 +852,9 @@ FcPatternNewBank (void);
 int
 FcPatternNeededBytes (FcPattern *p);
 
+int
+FcPatternNeededBytesAlign (void);
+
 void *
 FcPatternDistributeBytes (FcCache * metadata, void * block_ptr);
 
index b1c7c3ff80b294e31ac08f5288924509c7365329..5518821d003c310e441d98b04a94508d674a3bd4 100644 (file)
@@ -721,6 +721,12 @@ FcLangSetNeededBytes (const FcLangSet *l)
     return sizeof (FcLangSet);
 }
 
+int
+FcLangSetNeededBytesAlign (void)
+{
+    return __alignof__ (FcLangSet);
+}
+
 static FcBool
 FcLangSetEnsureBank (int bi)
 {
@@ -749,6 +755,7 @@ FcLangSetDistributeBytes (FcCache * metadata, void * block_ptr)
     if (!FcLangSetEnsureBank(bi))
        return 0;
 
+    block_ptr = ALIGN(block_ptr, FcLangSet);
     langsets[bi] = block_ptr;
     block_ptr = (void *)((char *)block_ptr +
                         langset_count * sizeof(FcLangSet));
@@ -778,6 +785,7 @@ FcLangSetUnserialize (FcCache metadata, void *block_ptr)
        return 0;
 
     FcMemAlloc (FC_MEM_LANGSET, metadata.langset_count * sizeof(FcLangSet));
+    block_ptr = ALIGN(block_ptr, FcLangSet);
     langsets[bi] = (FcLangSet *)block_ptr;
     block_ptr = (void *)((char *)block_ptr +
                         metadata.langset_count * sizeof(FcLangSet));
index 0767e114d2fe14b1d3440bb4a9131dda7559fc6a..6ca4f1ad30b3e497ba188189043cf2ad9d2145ac 100644 (file)
@@ -332,12 +332,20 @@ FcObjectNeededBytes ()
     return num + sizeof(int);
 }
 
+int
+FcObjectNeededBytesAlign (void)
+{
+    return __alignof__ (int) + __alignof__ (char);
+}
+
 void *
 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
 {
     *(int *)block_ptr = biggest_known_ntypes;
+    block_ptr = ALIGN (block_ptr, int);
     block_ptr = (int *) block_ptr + 1;
     biggest_ptr = block_ptr;
+    block_ptr = ALIGN (block_ptr, char);
     block_ptr = (char *) block_ptr + biggest_known_count;
     return block_ptr;
 }
index ca75f904efbca9816691e24f11a9a26f6118514a..bb922fef3914059dc53698ccd851a305b83a426e 100644 (file)
@@ -1430,6 +1430,8 @@ static void
 FcStrNewBank (void);
 static int
 FcStrNeededBytes (const FcChar8 * s);
+static int
+FcStrNeededBytesAlign (void);
 static void *
 FcStrDistributeBytes (FcCache * metadata, void * block_ptr);
 static const FcChar8 *
@@ -1441,6 +1443,8 @@ static void
 FcValueListNewBank (void);
 static int
 FcValueListNeededBytes (FcValueList * vl);
+static int
+FcValueListNeededBytesAlign (void);
 static void *
 FcValueListDistributeBytes (FcCache * metadata, void *block_ptr);
 static FcValueListPtr
@@ -1479,6 +1483,13 @@ FcPatternNeededBytes (FcPattern * p)
     return cum + sizeof (FcPattern) + sizeof(FcPatternElt)*p->num;
 }
 
+int
+FcPatternNeededBytesAlign (void)
+{
+    return __alignof__ (FcPattern) + __alignof__ (FcPatternElt) + 
+       FcValueListNeededBytesAlign ();
+}
+
 static FcBool
 FcPatternEnsureBank (int bi)
 {
@@ -1525,12 +1536,14 @@ FcPatternDistributeBytes (FcCache * metadata, void * block_ptr)
        return 0;
 
     fcpattern_ptr = 0;
+    block_ptr = ALIGN(block_ptr, FcPattern);
     fcpatterns[bi] = (FcPattern *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + 
                         (sizeof (FcPattern) * fcpattern_count));
     
     FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt) * fcpatternelt_count);
     fcpatternelt_ptr = 0;
+    block_ptr = ALIGN(block_ptr, FcPatternElt);
     fcpatternelts[bi] = (FcPatternElt *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + 
                         (sizeof (FcPatternElt) * fcpatternelt_count));
@@ -1603,12 +1616,14 @@ FcPatternUnserialize (FcCache metadata, void *block_ptr)
        return FcFalse;
 
     FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern) * metadata.pattern_count);
+    block_ptr = ALIGN(block_ptr, FcPattern);
     fcpatterns[bi] = (FcPattern *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + 
                         (sizeof (FcPattern) * metadata.pattern_count));
     
     FcMemAlloc (FC_MEM_PATELT, 
                sizeof (FcPatternElt) * metadata.patternelt_count);
+    block_ptr = ALIGN(block_ptr, FcPatternElt);
     fcpatternelts[bi] = (FcPatternElt *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + 
                         (sizeof (FcPatternElt) * metadata.patternelt_count));
@@ -1660,6 +1675,13 @@ FcValueListNeededBytes (FcValueList *p)
     return cum;
 }
 
+static int
+FcValueListNeededBytesAlign (void)
+{
+    return FcCharSetNeededBytesAlign() + FcLangSetNeededBytesAlign() + 
+       FcStrNeededBytesAlign() + __alignof__ (FcValueList);
+}
+
 static FcBool
 FcValueListEnsureBank (int bi)
 {
@@ -1694,6 +1716,7 @@ FcValueListDistributeBytes (FcCache * metadata, void *block_ptr)
 
     FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList) * fcvaluelist_count);
     fcvaluelist_ptr = 0;
+    block_ptr = ALIGN(block_ptr, FcValueList);
     fcvaluelists[bi] = (FcValueList *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + 
                         (sizeof (FcValueList) * fcvaluelist_count));
@@ -1774,6 +1797,7 @@ FcValueListUnserialize (FcCache metadata, void *block_ptr)
 
     FcMemAlloc (FC_MEM_VALLIST, 
                sizeof (FcValueList) * metadata.valuelist_count);
+    block_ptr = ALIGN(block_ptr, FcValueList);
     fcvaluelists[bi] = (FcValueList *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + 
                         (sizeof (FcValueList) * metadata.valuelist_count));
@@ -1850,6 +1874,11 @@ FcStrNeededBytes (const FcChar8 * s)
     b->next = 0;
     b->hash = hash;
     strcpy ((char *) (b + 1), (char *)s);
+
+    /* Yes, the following line is convoluted.  However, it is
+     * incorrect to replace the with a memset, because the C
+     * specification doesn't guarantee that the null pointer is
+     * the same as the zero bit pattern. */
     *(char **)((char *) (b + 1) + strlen((char *)s) + 1) = 0;
     *p = b;
 
@@ -1857,6 +1886,12 @@ FcStrNeededBytes (const FcChar8 * s)
     return strlen((char *)s) + 1;
 }
 
+static int
+FcStrNeededBytesAlign (void)
+{
+    return __alignof__ (char);
+}
+
 static FcBool
 FcStrEnsureBank (int bi)
 {
@@ -1887,6 +1922,7 @@ FcStrDistributeBytes (FcCache * metadata, void * block_ptr)
        return 0;
 
     FcMemAlloc (FC_MEM_STRING, sizeof (char) * fcstr_count);
+    block_ptr = ALIGN (block_ptr, FcChar8);
     static_strs[bi] = (FcChar8 *)block_ptr;
     block_ptr = (void *)((char *)block_ptr + (sizeof (char) * fcstr_count));
     metadata->str_count = fcstr_count;