a451ca45c7e68efbf6cf31eb6765c3b316367ca6
[fw/openocd] / src / helper / replacements.c
1 /***************************************************************************
2  *   Copyright (C) 2006 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                      *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34 /* 
35  * clear_malloc
36  *
37  * will alloc memory and clear it
38  */
39 void *clear_malloc(size_t size)
40 {
41         void *t = malloc(size);
42         if (t!=NULL)
43         {
44                 memset(t, 0x00, size);
45         }
46         return t;
47 }
48
49 void *fill_malloc(size_t size)
50 {
51         void *t = malloc(size);
52         if (t!=NULL)
53         {
54                 /* We want to initialize memory to some known bad state.  */
55                 /* 0 and 0xff yields 0 and -1 as integers, which often          */
56                 /* have meaningful values. 0x5555... is not often a valid       */
57                 /* integer and is quite easily spotted in the debugger          */
58                 /* also it is almost certainly an invalid address                                       */
59                 memset(t, 0x55, size);
60         }
61         return t;
62 }
63
64 #include "replacements.h"
65
66 #include <stdio.h>
67
68 /* replacements for gettimeofday */
69 #ifndef HAVE_GETTIMEOFDAY
70
71 /* Windows */
72 #ifdef _WIN32
73
74 #ifndef __GNUC__
75 #define EPOCHFILETIME (116444736000000000i64)
76 #else
77 #define EPOCHFILETIME (116444736000000000LL)
78 #endif
79
80 int gettimeofday(struct timeval *tv, struct timezone *tz)
81 {
82         FILETIME        ft;
83         LARGE_INTEGER   li;
84         __int64         t;
85         static int      tzflag;
86
87         if (tv)
88         {
89                 GetSystemTimeAsFileTime(&ft);
90                 li.LowPart  = ft.dwLowDateTime;
91                 li.HighPart = ft.dwHighDateTime;
92                 t  = li.QuadPart;                   /* In 100-nanosecond intervals */
93                 t -= EPOCHFILETIME;                 /* Offset to the Epoch time */
94                 t /= 10;                            /* In microseconds */
95                 tv->tv_sec  = (long)(t / 1000000);
96                 tv->tv_usec = (long)(t % 1000000);
97         }
98
99         if (tz)
100         {
101                 if (!tzflag)
102                 {
103                         _tzset();
104                         tzflag++;
105                 }
106                 tz->tz_minuteswest = _timezone / 60;
107                 tz->tz_dsttime = _daylight;
108         }
109
110         return 0;
111 }
112 #endif /* _WIN32 */
113
114 #endif /* HAVE_GETTIMEOFDAY */
115
116 #ifndef HAVE_STRNLEN
117 size_t strnlen(const char *s, size_t maxlen)
118 {
119         const char *end= (const char *)memchr(s, '\0', maxlen);
120         return end ? (size_t) (end - s) : maxlen;
121 }
122 #endif
123
124 #ifndef HAVE_STRNDUP
125 char* strndup(const char *s, size_t n)
126 {
127         size_t len = strnlen (s, n);
128         char *new = (char *) malloc (len + 1);
129
130         if (new == NULL)
131                 return NULL;
132
133         new[len] = '\0';
134         return (char *) memcpy (new, s, len);
135 }
136 #endif