Ticket #87: 0003-Move-OS-2-module-init-from-cairo-os2-surface.c-to-ca.patch

File 0003-Move-OS-2-module-init-from-cairo-os2-surface.c-to-ca.patch, 8.2 KB (added by dmik, 8 years ago)
  • src/cairo-os2-surface.c

    From 8088aa86ba59fde3f77d9de43a7c28ae65701230 Mon Sep 17 00:00:00 2001
    From: Dave Yeo <dave.r.yeo@gmail.com>
    Date: Tue, 30 Aug 2011 21:15:05 -0700
    Subject: [PATCH 3/5] Move OS/2 module init from cairo-os2-surface.c to
     cairo-system.c.
    
    Signed-off-by: Dave Yeo <dave.r.yeo@gmail.com>
    ---
     src/cairo-os2-surface.c | 166 ------------------------------------------------
     src/cairo-system.c      | 129 +++++++++++++++++++++++++++++++++++++
     2 files changed, 129 insertions(+), 166 deletions(-)
    
    diff --git a/src/cairo-os2-surface.c b/src/cairo-os2-surface.c
    index ba46b22..74c0567 100644
    a b  
    4242#include "cairo-default-context-private.h"
    4343#include "cairo-error-private.h"
    4444
    45 #if CAIRO_HAS_FC_FONT
    46 #include <fontconfig/fontconfig.h>
    47 #endif
    48 
    49 #include <float.h>
    50 #ifdef BUILD_CAIRO_DLL
    51 # include "cairo-os2.h"
    52 # ifndef __WATCOMC__
    53 #  include <emx/startup.h>
    54 # endif
    55 #endif
    56 
    57 /*
    58  * Here comes the extra API for the OS/2 platform. Currently it consists
    59  * of two extra functions, the cairo_os2_init() and the
    60  * cairo_os2_fini(). Both of them are called automatically if
    61  * Cairo is compiled to be a DLL file, but you have to call them before
    62  * using the Cairo API if you link to Cairo statically!
    63  *
    64  * You'll also find the code in here which deals with DLL initialization
    65  * and termination, if the code is built to be a DLL.
    66  * (if BUILD_CAIRO_DLL is defined)
    67  */
    68 
    69 /* Initialization counter: */
    70 static int cairo_os2_initialization_count = 0;
    71 
    72 static inline void
    73 DisableFPUException (void)
    74 {
    75     unsigned short usCW;
    76 
    77     /* Some OS/2 PM API calls modify the FPU Control Word,
    78      * but forget to restore it.
    79      *
    80      * This can result in XCPT_FLOAT_INVALID_OPCODE exceptions,
    81      * so to be sure, we disable Invalid Opcode FPU exception
    82      * before using FPU stuffs.
    83      */
    84     usCW = _control87 (0, 0);
    85     usCW = usCW | EM_INVALID | 0x80;
    86     _control87 (usCW, MCW_EM | 0x80);
    87 }
    88 
    89 /**
    90  * cairo_os2_init:
    91  *
    92  * Initializes the Cairo library. This function is automatically called if
    93  * Cairo was compiled to be a DLL (however it's not a problem if it's called
    94  * multiple times). But if you link to Cairo statically, you have to call it
    95  * once to set up Cairo's internal structures and mutexes.
    96  *
    97  * Since: 1.4
    98  **/
    99 cairo_public void
    100 cairo_os2_init (void)
    101 {
    102     /* This may initialize some stuffs, like create mutex semaphores etc.. */
    103 
    104     cairo_os2_initialization_count++;
    105     if (cairo_os2_initialization_count > 1) return;
    106 
    107     DisableFPUException ();
    108 
    109 #if CAIRO_HAS_FC_FONT
    110     /* Initialize FontConfig */
    111     FcInit ();
    112 #endif
    113 
    114     CAIRO_MUTEX_INITIALIZE ();
    115 }
    116 
    117 /**
    118  * cairo_os2_fini:
    119  *
    120  * Uninitializes the Cairo library. This function is automatically called if
    121  * Cairo was compiled to be a DLL (however it's not a problem if it's called
    122  * multiple times). But if you link to Cairo statically, you have to call it
    123  * once to shut down Cairo, to let it free all the resources it has allocated.
    124  *
    125  * Since: 1.4
    126  **/
    127 cairo_public void
    128 cairo_os2_fini (void)
    129 {
    130     /* This has to uninitialize some stuffs, like destroy mutex semaphores etc.. */
    131 
    132     if (cairo_os2_initialization_count <= 0) return;
    133     cairo_os2_initialization_count--;
    134     if (cairo_os2_initialization_count > 0) return;
    135 
    136     DisableFPUException ();
    137 
    138     cairo_debug_reset_static_data ();
    139 
    140 #if CAIRO_HAS_FC_FONT
    141 # if HAVE_FCFINI
    142     /* Uninitialize FontConfig */
    143     FcFini ();
    144 # endif
    145 #endif
    146 
    147 #ifdef __WATCOMC__
    148     /* It can happen that the libraries we use have memory leaks,
    149      * so there are still memory chunks allocated at this point.
    150      * In these cases, Watcom might still have a bigger memory chunk,
    151      * called "the heap" allocated from the OS.
    152      * As we want to minimize the memory we lose from the point of
    153      * view of the OS, we call this function to shrink that heap
    154      * as much as possible.
    155      */
    156     _heapshrink ();
    157 #else
    158     /* GCC has a heapmin function that approximately corresponds to
    159      * what the Watcom function does
    160      */
    161     _heapmin ();
    162 #endif
    163 }
    164 
    16545/*
    16646 * This function calls the allocation function depending on which
    16747 * method was compiled into the library: it can be native allocation
    void _buffer_free (void *buffer) 
    21797#endif
    21898}
    21999
    220 /* XXX
    221  * The cairo_os2_ini() and cairo_os2_fini() functions should be removed and
    222  * the LibMain code moved to cairo-system.c.  It should also call
    223  * cairo_debug_reset_static_data() instead of duplicating its logic...
    224  */
    225 
    226 #ifdef BUILD_CAIRO_DLL
    227 /* The main DLL entry for DLL initialization and uninitialization */
    228 /* Only include this code if we're about to build a DLL.          */
    229 
    230 #ifdef __WATCOMC__
    231 unsigned _System
    232 LibMain (unsigned hmod,
    233          unsigned termination)
    234 #else
    235 unsigned long _System
    236 _DLL_InitTerm (unsigned long hModule,
    237                unsigned long termination)
    238 #endif
    239 {
    240     if (termination) {
    241         /* Unloading the DLL */
    242         cairo_os2_fini ();
    243 
    244 #ifndef __WATCOMC__
    245         /* Uninitialize RTL of GCC */
    246         __ctordtorTerm ();
    247         _CRT_term ();
    248 #endif
    249         return 1;
    250     } else {
    251         /* Loading the DLL */
    252 #ifndef __WATCOMC__
    253         /* Initialize RTL of GCC */
    254         if (_CRT_init () != 0)
    255             return 0;
    256         __ctordtorInit ();
    257 #endif
    258 
    259         cairo_os2_init ();
    260         return 1;
    261     }
    262 }
    263 
    264 #endif /* BUILD_CAIRO_DLL */
    265 
    266100/*
    267101 * The following part of the source file contains the code which might
    268102 * be called the "core" of the OS/2 backend support. This contains the
  • src/cairo-system.c

    diff --git a/src/cairo-system.c b/src/cairo-system.c
    index 1ff4d07..c3e2257 100644
    a b  
    3434 *      Owen Taylor <otaylor@redhat.com>
    3535 *      Stuart Parmenter <stuart@mozilla.com>
    3636 *      Vladimir Vukicevic <vladimir@pobox.com>
     37 *      Rich Walsh <rich@e-vertise.com>
    3738 */
    3839
    3940/* This file should include code that is system-specific, not
    DllMain (HINSTANCE hinstDLL, 
    9596#endif
    9697#endif
    9798
     99#ifdef __OS2__
     100
     101#include <float.h>
     102#if CAIRO_HAS_FC_FONT
     103#include <fontconfig/fontconfig.h>
     104#endif
     105
     106cairo_public void
     107cairo_os2_init (void);
     108cairo_public void
     109cairo_os2_fini (void);
     110
     111static int cairo_os2_init_count = 0;
     112
     113/**
     114 * DLL Initialization/Termination functions -
     115 * not used when Cairo is statically linked.
     116 *
     117 **/
     118
     119#ifdef BUILD_CAIRO_DLL
     120
     121#ifdef __WATCOMC__
     122unsigned _System
     123LibMain (unsigned hmod,
     124         unsigned termination)
     125{
     126    if (termination) {
     127        cairo_os2_fini ();
     128        return 1;
     129    }
     130
     131    cairo_os2_init ();
     132    return 1;
     133}
     134
     135#else
     136
     137#include <emx/startup.h>
     138
     139unsigned long _System
     140_DLL_InitTerm (unsigned long hmod,
     141               unsigned long termination)
     142{
     143    if (termination) {
     144        cairo_os2_fini ();
     145        __ctordtorTerm ();
     146        _CRT_term ();
     147        return 1;
     148    }
     149
     150    if (_CRT_init ())
     151        return 0;
     152    __ctordtorInit ();
     153
     154    cairo_os2_init ();
     155    return 1;
     156}
     157#endif /* __WATCOMC__ */
     158
     159#endif /* BUILD_CAIRO_DLL */
     160
     161/**
     162 * cairo_os2_init:
     163 * System-specific initialization.
     164 *
     165 * This is called automatically if Cairo is built as a DLL, but must be
     166 * explicitly invoked if Cairo is used as a statically linked library.
     167 *
     168 * Since: 1.4
     169 **/
     170
     171cairo_public void
     172cairo_os2_init (void)
     173{
     174    unsigned short usCW;
     175
     176    cairo_os2_init_count++;
     177    if (cairo_os2_init_count > 1)
     178        return;
     179
     180    /* Workaround a bug in some OS/2 PM API calls that
     181     * modify the FPU Control Word but fail to restore it.
     182     */
     183    usCW = _control87 (0, 0);
     184    usCW = usCW | EM_INVALID | 0x80;
     185    _control87 (usCW, MCW_EM | 0x80);
     186
     187#if CAIRO_HAS_FC_FONT
     188    FcInit ();
     189#endif
     190
     191    CAIRO_MUTEX_INITIALIZE ();
     192}
     193
     194
     195/**
     196 * cairo_os2_fini:
     197 * System-specific finalization.
     198 *
     199 * This is called automatically if Cairo is built as a DLL, but must be
     200 * explicitly invoked if Cairo is used as a statically linked library.
     201 *
     202 * Since: 1.4
     203 **/
     204
     205cairo_public void
     206cairo_os2_fini (void)
     207{
     208    if (!cairo_os2_init_count)
     209        return;
     210    cairo_os2_init_count--;
     211    if (cairo_os2_init_count)
     212        return;
     213
     214#if CAIRO_HAS_FC_FONT
     215#if HAVE_FCFINI
     216    FcFini ();
     217#endif
     218#endif
     219
     220    CAIRO_MUTEX_FINALIZE ();
     221
     222    cairo_debug_reset_static_data ();
     223}
     224
     225#endif /* __OS2__ */
     226