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