fcfa8bc86a5d8cd22b47a2ae6767ac2f65515b0f
[fw/openocd] / src / helper / replacements.h
1 /***************************************************************************
2  *   Copyright (C) 2006 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifndef REPLACEMENTS_H
21 #define REPLACEMENTS_H
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "types.h"
28
29 /* include necessary headers for socket functionality */
30 #ifdef _WIN32
31 #include <winsock2.h>
32 #else
33 #include <sys/socket.h>
34 #include <sys/poll.h>
35 #include <netinet/in.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #endif
39
40 #ifdef HAVE_SYS_PARAM_H
41 #include <sys/param.h> /* for MIN/MAX macros */
42 #endif
43
44 /* MIN,MAX macros */
45 #ifndef MIN
46 #define MIN(a,b) (((a)<(b))?(a):(b))
47 #endif
48 #ifndef MAX
49 #define MAX(a,b) (((a)>(b))?(a):(b))
50 #endif
51                                                                                                                                  
52 /* gettimeofday() */
53 #ifndef HAVE_GETTIMEOFDAY
54
55 #ifndef _TIMEVAL_DEFINED
56 #define _TIMEVAL_DEFINED
57
58 struct timeval {
59         long tv_sec;
60         long tv_usec;
61 };
62 #endif /* _TIMEVAL_DEFINED */
63
64 struct timezone {
65     int tz_minuteswest;
66         int tz_dsttime;
67 };
68
69 extern int gettimeofday(struct timeval *tv, struct timezone *tz);
70 #endif
71
72 /**** clear_malloc & fill_malloc ****/
73 void *clear_malloc(size_t size);
74 void *fill_malloc(size_t size);
75
76 /*
77  * Now you have 3 ways for the malloc function:
78  *
79  * 1. Do not change anything, use the original malloc
80  *
81  * 2. Use the clear_malloc function instead of the original malloc.
82  *    In this case you must use the following define:
83  *    #define malloc((_a)) clear_malloc((_a))
84  *
85  * 3. Use the fill_malloc function instead of the original malloc.
86  *    In this case you must use the following define:
87  *    #define malloc((_a)) fill_malloc((_a))
88  *
89  * We have figured out that there could exist some malloc problems
90  * where variables are using without to be initialise. To find this
91  * places, use the fill_malloc function. With this function we want 
92  * to initialize memory to some known bad state. This is quite easily 
93  * spotted in the debugger and will trap to an invalid address. 
94  *
95  * clear_malloc can be used if you want to set not initialise 
96  * variable to 0.
97  *
98  * If you do not want to change the malloc function, to not use one of
99  * the following macros. Which is the default way.
100  */
101  
102 /* #define malloc(_a) clear_malloc(_a) */
103 /* #define malloc(_a) fill_malloc(_a) */
104
105 /* GNU extensions to the C library that may be missing on some systems */
106 #ifndef HAVE_STRNDUP
107 extern char* strndup(const char *s, size_t n);
108 #endif /* HAVE_STRNDUP */
109
110 #ifndef HAVE_STRNLEN
111 extern size_t strnlen(const char *s, size_t maxlen);
112 #endif /* HAVE_STRNLEN */
113
114 #ifndef HAVE_USLEEP
115 static __inline unsigned usleep(unsigned int usecs)
116 {
117 #ifdef _WIN32
118         Sleep((usecs/1000));
119         return 0;
120 #else
121 #error no usleep defined for your platform
122 #endif
123 }
124 #endif /* HAVE_USLEEP */
125
126 /* Windows specific */
127 #ifdef _WIN32
128
129 #define WIN32_LEAN_AND_MEAN
130 #include <windows.h>
131 #include <time.h>
132
133 #undef ERROR
134
135 #if IS_MINGW == 1
136 static __inline unsigned char inb(unsigned short int port)
137 {
138         unsigned char _v;
139         __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
140         return _v;
141 }
142
143 static __inline void outb(unsigned char value, unsigned short int port)
144 {
145         __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
146 }
147
148 #endif /* IS_MINGW */
149 #endif  /* _WIN32 */
150
151 /* generic socket functions for Windows and Posix */
152 static __inline int write_socket( int handle, const void *buffer, unsigned int count )
153 {
154 #ifdef _WIN32
155     return send(handle, buffer, count, 0);
156 #else
157     return write(handle, buffer, count);
158 #endif
159 }
160
161 static __inline int read_socket( int handle, void *buffer, unsigned int count )
162 {
163 #ifdef _WIN32
164     return recv(handle, buffer, count, 0);
165 #else
166     return read(handle, buffer, count);
167 #endif
168 }
169
170 static __inline int close_socket(int sock)
171 {
172 #ifdef _WIN32
173     return closesocket(sock);
174 #else
175     return close(sock);
176 #endif
177 }
178
179 static __inline void socket_nonblock(int fd)
180 {
181 #ifdef _WIN32
182         long nonblock = 1;
183         ioctlsocket(fd, FIONBIO, &nonblock );
184 #else
185         int oldopts = fcntl(fd, F_GETFL, 0);
186         fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
187 #endif
188 }
189
190 #ifndef HAVE_ELF_H
191
192 typedef struct
193 {
194         unsigned char   e_ident[16];    /* Magic number and other info */
195         u16     e_type;                 /* Object file type */
196         u16     e_machine;              /* Architecture */
197         u32     e_version;              /* Object file version */
198         u32 e_entry;            /* Entry point virtual address */
199         u32 e_phoff;            /* Program header table file offset */
200         u32     e_shoff;                /* Section header table file offset */
201         u32     e_flags;                /* Processor-specific flags */
202         u16     e_ehsize;               /* ELF header size in bytes */
203         u16     e_phentsize;            /* Program header table entry size */
204         u16     e_phnum;                /* Program header table entry count */
205         u16     e_shentsize;            /* Section header table entry size */
206         u16     e_shnum;                /* Section header table entry count */
207         u16     e_shstrndx;             /* Section header string table index */
208 } Elf32_Ehdr;
209
210 #define ELFMAG          "\177ELF"
211 #define SELFMAG         4
212
213 #define EI_CLASS        4               /* File class byte index */
214 #define ELFCLASS32      1               /* 32-bit objects */
215 #define ELFCLASS64      2               /* 64-bit objects */
216
217 #define EI_DATA         5               /* Data encoding byte index */
218 #define ELFDATA2LSB     1               /* 2's complement, little endian */
219 #define ELFDATA2MSB     2               /* 2's complement, big endian */
220
221 typedef struct
222 {
223         u32 p_type;                     /* Segment type */
224         u32 p_offset;           /* Segment file offset */
225         u32 p_vaddr;            /* Segment virtual address */
226         u32 p_paddr;            /* Segment physical address */
227         u32 p_filesz;           /* Segment size in file */
228         u32 p_memsz;            /* Segment size in memory */
229         u32 p_flags;            /* Segment flags */
230         u32 p_align;            /* Segment alignment */
231 } Elf32_Phdr;
232
233 #define PT_LOAD         1               /* Loadable program segment */
234
235 #endif /* HAVE_ELF_H */
236
237 #endif /* REPLACEMENTS_H */