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