diff --git a/src/cmpt.c b/src/cmpt.c
index 499a0fe3..6726b7fc 100644
--- a/src/cmpt.c
+++ b/src/cmpt.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/connect.c b/src/connect.c
index 2626ef32..361eb56b 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/convert.c b/src/convert.c
index 69d713f8..9f60ae3a 100644
--- a/src/convert.c
+++ b/src/convert.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/cookies.c b/src/cookies.c
index bdc32745..1258472a 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -43,6 +43,8 @@ as that of the covered work.  */
    sites that do send Set-Cookie2 also emit Set-Cookie for
    compatibility.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/ftp-basic.c b/src/ftp-basic.c
index 38f0069f..9145fe7d 100644
--- a/src/ftp-basic.c
+++ b/src/ftp-basic.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <assert.h>
diff --git a/src/ftp-ls.c b/src/ftp-ls.c
index e050c5a2..67e9bc51 100644
--- a/src/ftp-ls.c
+++ b/src/ftp-ls.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/ftp-opie.c b/src/ftp-opie.c
index 546e2105..5d740a1f 100644
--- a/src/ftp-opie.c
+++ b/src/ftp-opie.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/ftp.c b/src/ftp.c
index 645e565d..134c925a 100644
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/gen-md5.c b/src/gen-md5.c
index 833f3f28..d4d5f5c2 100644
--- a/src/gen-md5.c
+++ b/src/gen-md5.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include "gen-md5.h"
diff --git a/src/gnutls.c b/src/gnutls.c
index 57d4ad5c..258c0156 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -27,6 +27,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <assert.h>
diff --git a/src/hash.c b/src/hash.c
index 4697b779..f8b3c11f 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -31,6 +31,8 @@ as that of the covered work.  */
 /* With -DSTANDALONE, this file can be compiled outside Wget source
    tree.  To test, also use -DTEST.  */
 
+#define USE_GNULIB_ALLOC
+
 #ifndef STANDALONE
 # include "wget.h"
 #endif
diff --git a/src/host.c b/src/host.c
index 5a530221..1b418ce3 100644
--- a/src/host.c
+++ b/src/host.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/html-parse.c b/src/html-parse.c
index ade82f2b..623b7fd8 100644
--- a/src/html-parse.c
+++ b/src/html-parse.c
@@ -89,6 +89,8 @@ as that of the covered work.  */
 /* To test as standalone, compile with `-DSTANDALONE -I.'.  You'll
    still need Wget headers to compile.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #ifdef STANDALONE
@@ -100,6 +102,7 @@ as that of the covered work.  */
 #include <string.h>
 #include <assert.h>
 
+#include "utils.h"
 #include "html-parse.h"
 
 #ifdef STANDALONE
diff --git a/src/html-url.c b/src/html-url.c
index e9f2773a..717fe8c4 100644
--- a/src/html-url.c
+++ b/src/html-url.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/http-ntlm.c b/src/http-ntlm.c
index def832be..321d70d2 100644
--- a/src/http-ntlm.c
+++ b/src/http-ntlm.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 /* NTLM details:
diff --git a/src/http.c b/src/http.c
index c1049d8c..84bbe2e0 100644
--- a/src/http.c
+++ b/src/http.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/init.c b/src/init.c
index e079df95..1a5171ce 100644
--- a/src/init.c
+++ b/src/init.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/log.c b/src/log.c
index f7b3dca8..077ebeb8 100644
--- a/src/log.c
+++ b/src/log.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/main.c b/src/main.c
index b1e99695..04ff76f3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/mswindows.c b/src/mswindows.c
index cdfea2d2..c64da026 100644
--- a/src/mswindows.c
+++ b/src/mswindows.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #define INHIBIT_WRAP /* avoid wrapping of socket, bind, ... */
 
 #include "wget.h"
diff --git a/src/netrc.c b/src/netrc.c
index fea440d3..35163a61 100644
--- a/src/netrc.c
+++ b/src/netrc.c
@@ -30,6 +30,8 @@ as that of the covered work.  */
 /* This file used to be kept in synch with the code in Fetchmail, but
    the latter has diverged since.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/openssl.c b/src/openssl.c
index 03fb1810..d35aba32 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -29,6 +29,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <assert.h>
diff --git a/src/progress.c b/src/progress.c
index 8b9b1013..62e8d87d 100644
--- a/src/progress.c
+++ b/src/progress.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/ptimer.c b/src/ptimer.c
index 9edddd82..0f5b5db0 100644
--- a/src/ptimer.c
+++ b/src/ptimer.c
@@ -50,6 +50,8 @@ as that of the covered work.  */
      double secs = ptimer_measure ();
      printf ("The loop took %.2fs\n", secs);  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
@@ -71,6 +73,7 @@ as that of the covered work.  */
 # include <windows.h>
 #endif
 
+#include "utils.h"
 #include "ptimer.h"
 
 /* Depending on the OS, one and only one of PTIMER_POSIX,
diff --git a/src/recur.c b/src/recur.c
index d1d0f18d..a992230d 100644
--- a/src/recur.c
+++ b/src/recur.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/res.c b/src/res.c
index 8c35f0e1..94d9769d 100644
--- a/src/res.c
+++ b/src/res.c
@@ -67,6 +67,8 @@ as that of the covered work.  */
    res_match_path, res_register_specs, res_get_specs, and
    res_retrieve_file.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/retr.c b/src/retr.c
index 179430ac..60d4c629 100644
--- a/src/retr.c
+++ b/src/retr.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/snprintf.c b/src/snprintf.c
index f458981a..2a8fc6e6 100644
--- a/src/snprintf.c
+++ b/src/snprintf.c
@@ -87,6 +87,8 @@
  *
  **************************************************************/
 
+#define USE_GNULIB_ALLOC
+
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
diff --git a/src/spider.c b/src/spider.c
index 08fefc9f..70254093 100644
--- a/src/spider.c
+++ b/src/spider.c
@@ -27,6 +27,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/test.c b/src/test.c
index 04658bc7..d6a86592 100644
--- a/src/test.c
+++ b/src/test.c
@@ -27,6 +27,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include <stdio.h>
 
 #include "test.h"
diff --git a/src/url.c b/src/url.c
index a561725a..997b1058 100644
--- a/src/url.c
+++ b/src/url.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/utils.c b/src/utils.c
index f3b4a60c..d1366ef9 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -28,6 +28,8 @@ Corresponding Source for a non-source form of such a combination
 shall include the source code for the parts of OpenSSL used as well
 as that of the covered work.  */
 
+#define USE_GNULIB_ALLOC
+
 #include "wget.h"
 
 #include <stdio.h>
diff --git a/src/utils.h b/src/utils.h
index 7d41b314..bac112ed 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -31,6 +31,32 @@ as that of the covered work.  */
 #ifndef UTILS_H
 #define UTILS_H
 
+#ifdef USE_GNULIB_ALLOC
+
+/* Constant is using when we don`t know attempted size exactly */
+#define UNKNOWN_ATTEMPTED_SIZE -3
+
+/* Macros that interface to malloc, but know about type sizes, and
+   cast the result to the appropriate type.  The casts are not
+   necessary in standard C, but Wget performs them anyway for the sake
+   of pre-standard environments and possibly C++.  */
+
+#define xnew(type) (xmalloc (sizeof (type)))
+#define xnew0(type) (xcalloc (1, sizeof (type)))
+#define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
+#define xnew0_array(type, len) (xcalloc ((len), sizeof (type)))
+
+#define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
+
+#define xfree free
+/* Free P if it is non-NULL.  C requires free() to behaves this way by
+   default, but Wget's code is historically careful not to pass NULL
+   to free.  This allows us to assert p!=NULL in xfree to check
+   additional errors.  (But we currently don't do that!)  */
+#define xfree_null(p) if (!(p)) ; else xfree (p)
+
+#endif /* USE_GNULIB_ALLOC */
+
 struct hash_table;
 
 struct file_memory {