1 /***************************************************************************
2 * Copyright (C) 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
30 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
40 * will alloc memory and clear it
42 void *clear_malloc(size_t size)
44 void *t = malloc(size);
47 memset(t, 0x00, size);
52 void *fill_malloc(size_t size)
54 void *t = malloc(size);
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);
67 #include "replacements.h"
71 /* replacements for gettimeofday */
72 #ifndef HAVE_GETTIMEOFDAY
78 #define EPOCHFILETIME (116444736000000000i64)
80 #define EPOCHFILETIME (116444736000000000LL)
83 int gettimeofday(struct timeval *tv, struct timezone *tz)
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);
109 tz->tz_minuteswest = _timezone / 60;
110 tz->tz_dsttime = _daylight;
117 #endif /* HAVE_GETTIMEOFDAY */
120 size_t strnlen(const char *s, size_t maxlen)
122 const char *end= (const char *)memchr(s, '\0', maxlen);
123 return end ? (size_t) (end - s) : maxlen;
128 char* strndup(const char *s, size_t n)
130 size_t len = strnlen (s, n);
131 char *new = (char *) malloc (len + 1);
137 return (char *) memcpy (new, s, len);