- added patch for faster start of OpenOCD
[fw/openocd] / src / helper / replacements.c
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <strings.h>
29 /* 
30  * clear_malloc
31  *
32  * will alloc memory and clear it
33  */
34 void *clear_malloc(size_t size)
35 {
36         void *t = malloc(size);
37         if (t!=NULL)
38         {
39                 memset(t, 0x00, size);
40         }
41         return t;
42 }
43
44 void *fill_malloc(size_t size)
45 {
46         void *t = malloc(size);
47         if (t!=NULL)
48         {
49                 /* We want to initialize memory to some known bad state.  */
50                 /* 0 and 0xff yields 0 and -1 as integers, which often          */
51                 /* have meaningful values. 0x5555... is not often a valid       */
52                 /* integer and is quite easily spotted in the debugger          */
53                 /* also it is almost certainly an invalid address                                       */
54                 memset(t, 0x55, size);
55         }
56         return t;
57 }
58
59 #include "replacements.h"
60
61 #include <stdio.h>
62
63 /* replacements for gettimeofday */
64 #ifndef HAVE_GETTIMEOFDAY
65
66 /* Windows */
67 #ifdef _WIN32
68
69 #ifndef __GNUC__
70 #define EPOCHFILETIME (116444736000000000i64)
71 #else
72 #define EPOCHFILETIME (116444736000000000LL)
73 #endif
74
75 int gettimeofday(struct timeval *tv, struct timezone *tz)
76 {
77         FILETIME        ft;
78         LARGE_INTEGER   li;
79         __int64         t;
80         static int      tzflag;
81
82         if (tv)
83         {
84                 GetSystemTimeAsFileTime(&ft);
85                 li.LowPart  = ft.dwLowDateTime;
86                 li.HighPart = ft.dwHighDateTime;
87                 t  = li.QuadPart;                   /* In 100-nanosecond intervals */
88                 t -= EPOCHFILETIME;                 /* Offset to the Epoch time */
89                 t /= 10;                            /* In microseconds */
90                 tv->tv_sec  = (long)(t / 1000000);
91                 tv->tv_usec = (long)(t % 1000000);
92         }
93
94         if (tz)
95         {
96                 if (!tzflag)
97                 {
98                         _tzset();
99                         tzflag++;
100                 }
101                 tz->tz_minuteswest = _timezone / 60;
102                 tz->tz_dsttime = _daylight;
103         }
104
105         return 0;
106 }
107 #endif /* _WIN32 */
108
109 #endif /* HAVE_GETTIMEOFDAY */
110
111 #ifndef HAVE_STRNLEN
112 size_t strnlen(const char *s, size_t maxlen)
113 {
114         const char *end= (const char *)memchr(s, '\0', maxlen);
115         return end ? (size_t) (end - s) : maxlen;
116 }
117 #endif
118
119 #ifndef HAVE_STRNDUP
120 char* strndup(const char *s, size_t n)
121 {
122         size_t len = strnlen (s, n);
123         char *new = (char *) malloc (len + 1);
124
125         if (new == NULL)
126                 return NULL;
127
128         new[len] = '\0';
129         return (char *) memcpy (new, s, len);
130 }
131 #endif