src/SDL_fatal.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jun 2009 04:37:27 +0000
changeset 3162 dc1eb82ffdaa
parent 2984 0b160c970b7e
child 3644 09ce26cedfd2
permissions -rw-r--r--
Von: Thomas Zimmermann
Betreff: [SDL] [PATCH] Make static variables const
Datum: Tue, 19 May 2009 19:45:37 +0200

Hi,

this is a set of simple changes which make some of SDL's internal static
arrays constant. The purpose is to shrink the number of write-able
static bytes and thus increase the number of memory pages shared between
SDL applications.

The patch set is against trunk@4513. Each of the attached patch files is
specific to a sub-system. The set is completed by a second mail, because
of the list's 40 KiB limit.

The files readelf-r4513.txt and readelf-const-patch.txt where made by
calling 'readelf -S libSDL.so'. They show the difference in ELF sections
without and with the patch. Some numbers measured on my x86-64:

Before

[13] .rodata PROGBITS 00000000000eaaa0 000eaaa0
0000000000008170 0000000000000000 A 0 0 32
[19] .data.rel.ro PROGBITS 00000000003045e0 001045e0
00000000000023d0 0000000000000000 WA 0 0 32
[23] .data PROGBITS 00000000003076e0 001076e0
0000000000004988 0000000000000000 WA 0 0 32

After

[13] .rodata PROGBITS 00000000000eaaa0 000eaaa0
0000000000009a50 0000000000000000 A 0 0 32
[19] .data.rel.ro PROGBITS 0000000000306040 00106040
0000000000002608 0000000000000000 WA 0 0 32
[23] .data PROGBITS 0000000000309360 00109360
0000000000002e88 0000000000000000 WA 0 0 32

The size of the write-able data section decreased considerably. Some
entries became const-after-relocation, while most of its content went
straight into the read-only data section.

Best regards, Thomas
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* General fatal signal handling code for SDL */
    25 
    26 #ifdef HAVE_SIGNAL_H
    27 
    28 #include <signal.h>
    29 
    30 #include "SDL_stdinc.h"
    31 #include "SDL_fatal.h"
    32 
    33 /* This installs some signal handlers for the more common fatal signals,
    34    so that if the programmer is lazy, the app doesn't die so horribly if
    35    the program crashes.
    36 */
    37 
    38 static void
    39 SDL_Parachute(int sig)
    40 {
    41     signal(sig, SIG_DFL);
    42     SDL_Quit();
    43     raise(sig);
    44 }
    45 
    46 static const int SDL_fatal_signals[] = {
    47     SIGSEGV,
    48 #ifdef SIGBUS
    49     SIGBUS,
    50 #endif
    51 #ifdef SIGFPE
    52     SIGFPE,
    53 #endif
    54 #ifdef SIGQUIT
    55     SIGQUIT,
    56 #endif
    57     0
    58 };
    59 
    60 void
    61 SDL_InstallParachute(void)
    62 {
    63     /* Set a handler for any fatal signal not already handled */
    64     int i;
    65 #ifdef HAVE_SIGACTION
    66     struct sigaction action;
    67 
    68     for (i = 0; SDL_fatal_signals[i]; ++i) {
    69         sigaction(SDL_fatal_signals[i], NULL, &action);
    70         if (action.sa_handler == SIG_DFL) {
    71             action.sa_handler = SDL_Parachute;
    72             sigaction(SDL_fatal_signals[i], &action, NULL);
    73         }
    74     }
    75 #ifdef SIGALRM
    76     /* Set SIGALRM to be ignored -- necessary on Solaris */
    77     sigaction(SIGALRM, NULL, &action);
    78     if (action.sa_handler == SIG_DFL) {
    79         action.sa_handler = SIG_IGN;
    80         sigaction(SIGALRM, &action, NULL);
    81     }
    82 #endif
    83 #else
    84     void (*ohandler) (int);
    85 
    86     for (i = 0; SDL_fatal_signals[i]; ++i) {
    87         ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
    88         if (ohandler != SIG_DFL) {
    89             signal(SDL_fatal_signals[i], ohandler);
    90         }
    91     }
    92 #endif /* HAVE_SIGACTION */
    93     return;
    94 }
    95 
    96 void
    97 SDL_UninstallParachute(void)
    98 {
    99     /* Remove a handler for any fatal signal handled */
   100     int i;
   101 #ifdef HAVE_SIGACTION
   102     struct sigaction action;
   103 
   104     for (i = 0; SDL_fatal_signals[i]; ++i) {
   105         sigaction(SDL_fatal_signals[i], NULL, &action);
   106         if (action.sa_handler == SDL_Parachute) {
   107             action.sa_handler = SIG_DFL;
   108             sigaction(SDL_fatal_signals[i], &action, NULL);
   109         }
   110     }
   111 #else
   112     void (*ohandler) (int);
   113 
   114     for (i = 0; SDL_fatal_signals[i]; ++i) {
   115         ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
   116         if (ohandler != SDL_Parachute) {
   117             signal(SDL_fatal_signals[i], ohandler);
   118         }
   119     }
   120 #endif /* HAVE_SIGACTION */
   121 }
   122 
   123 #else
   124 
   125 /* No signals on this platform, nothing to do.. */
   126 
   127 void
   128 SDL_InstallParachute(void)
   129 {
   130     return;
   131 }
   132 
   133 void
   134 SDL_UninstallParachute(void)
   135 {
   136     return;
   137 }
   138 
   139 #endif /* HAVE_SIGNAL_H */
   140 /* vi: set ts=4 sw=4 expandtab: */