Changed the GDI driver interface to pass an opaque PHYSDEV pointer
[wine] / tools / winebuild / utils.c
1 /*
2  * Small utility functions for winebuild
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "build.h"
30
31 void *xmalloc (size_t size)
32 {
33     void *res;
34
35     res = malloc (size ? size : 1);
36     if (res == NULL)
37     {
38         fprintf (stderr, "Virtual memory exhausted.\n");
39         exit (1);
40     }
41     return res;
42 }
43
44 void *xrealloc (void *ptr, size_t size)
45 {
46     void *res = realloc (ptr, size);
47     if (res == NULL)
48     {
49         fprintf (stderr, "Virtual memory exhausted.\n");
50         exit (1);
51     }
52     return res;
53 }
54
55 char *xstrdup( const char *str )
56 {
57     char *res = strdup( str );
58     if (!res)
59     {
60         fprintf (stderr, "Virtual memory exhausted.\n");
61         exit (1);
62     }
63     return res;
64 }
65
66 char *strupper(char *s)
67 {
68     char *p;
69     for (p = s; *p; p++) *p = toupper(*p);
70     return s;
71 }
72
73 void fatal_error( const char *msg, ... )
74 {
75     va_list valist;
76     va_start( valist, msg );
77     if (input_file_name)
78     {
79         fprintf( stderr, "%s:", input_file_name );
80         if (current_line)
81             fprintf( stderr, "%d:", current_line );
82         fputc( ' ', stderr );
83     }
84     vfprintf( stderr, msg, valist );
85     va_end( valist );
86     exit(1);
87 }
88
89 void fatal_perror( const char *msg, ... )
90 {
91     va_list valist;
92     va_start( valist, msg );
93     if (input_file_name)
94     {
95         fprintf( stderr, "%s:", input_file_name );
96         if (current_line)
97             fprintf( stderr, "%d:", current_line );
98         fputc( ' ', stderr );
99     }
100     vfprintf( stderr, msg, valist );
101     perror( " " );
102     va_end( valist );
103     exit(1);
104 }
105
106 void warning( const char *msg, ... )
107 {
108     va_list valist;
109
110     if (!display_warnings) return;
111     va_start( valist, msg );
112     if (input_file_name)
113     {
114         fprintf( stderr, "%s:", input_file_name );
115         if (current_line)
116             fprintf( stderr, "%d:", current_line );
117         fputc( ' ', stderr );
118     }
119     fprintf( stderr, "warning: " );
120     vfprintf( stderr, msg, valist );
121     va_end( valist );
122 }
123
124 /* output a standard header for generated files */
125 void output_standard_file_header( FILE *outfile )
126 {
127     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
128              input_file_name );
129     fprintf( outfile,
130              "/* This file can be copied, modified and distributed without restriction. */\n\n" );
131 }
132
133 /* dump a byte stream into the assembly code */
134 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
135                  const char *label, int constant )
136 {
137     int i;
138
139     fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
140              constant ? "const " : "", label, len );
141     for (i = 0; i < len; i++)
142     {
143         if (!(i & 7)) fprintf( outfile, "\n  " );
144         fprintf( outfile, "0x%02x", *data++ );
145         if (i < len - 1) fprintf( outfile, "," );
146     }
147     fprintf( outfile, "\n};\n" );
148 }
149
150 /*****************************************************************
151  *  Function:    get_alignment
152  *
153  *  Description:
154  *    According to the info page for gas, the .align directive behaves
155  * differently on different systems.  On some architectures, the
156  * argument of a .align directive is the number of bytes to pad to, so
157  * to align on an 8-byte boundary you'd say
158  *     .align 8
159  * On other systems, the argument is "the number of low-order zero bits
160  * that the location counter must have after advancement."  So to
161  * align on an 8-byte boundary you'd say
162  *     .align 3
163  *
164  * The reason gas is written this way is that it's trying to mimick
165  * native assemblers for the various architectures it runs on.  gas
166  * provides other directives that work consistantly across
167  * architectures, but of course we want to work on all arches with or
168  * without gas.  Hence this function.
169  * 
170  *
171  *  Parameters:
172  *                alignBoundary  --  the number of bytes to align to.
173  *                                   If we're on an architecture where 
174  *                                   the assembler requires a 'number
175  *                                   of low-order zero bits' as a
176  *                                   .align argument, then this number
177  *                                   must be a power of 2.  
178  *
179  */
180 int get_alignment(int alignBoundary)
181 {
182 #ifdef __PPC__
183
184     int n = 0;
185
186     switch(alignBoundary)
187     {
188     case 2:
189         n = 1;
190         break;
191     case 4:
192         n = 2;
193         break;
194     case 8:
195         n = 3;
196         break;
197     case 16:
198         n = 4;
199         break;
200     case 32:
201         n = 5;
202         break;
203     case 64:
204         n = 6;
205         break;
206     case 128:
207         n = 7;
208         break;
209     case 256:
210         n = 8;
211         break;
212     case 512:
213         n = 9;
214         break;
215     case 1024:
216         n = 10;
217         break;
218     case 2048:
219         n = 11;
220         break;
221     case 4096:
222         n = 12;
223         break;
224     case 8192:
225         n = 13;
226         break;
227     case 16384:
228         n = 14;
229         break;
230     case 32768:
231         n = 15;
232         break;
233     case 65536:
234         n = 16;
235         break;
236     default:
237         fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
238                     alignBoundary);
239     }
240     return n;
241
242 #elif defined(__i386__) || defined(__sparc__)
243
244     return alignBoundary;
245
246 #else
247 #error "How does the '.align' assembler directive work on your architecture?"
248 #endif
249 }