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