From 9aa6fa6bf0a727c1efc3315456cc4eaba9157af8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Mon, 19 May 1997 16:33:22 -0700
Subject: [PATCH] memory.{c,h} renamed to pike_memory.{c,h}

Rev: .cvsignore:1.12
Rev: src/Makefile.in:1.31
Rev: src/array.c:1.10
Rev: src/builtin_functions.c:1.37
Rev: src/constants.c:1.11
Rev: src/docode.c:1.17
Rev: src/dynamic_buffer.c:1.4
Rev: src/gc.c:1.9
Rev: src/gc.h:1.8
Rev: src/global.h:1.8
Rev: src/las.c:1.33
Rev: src/lex.c:1.24
Rev: src/mapping.c:1.20
Rev: src/modules/Image/quant.c:1.9
Rev: src/modules/Regexp/pike_regexp.c:1.4
Rev: src/modules/call_out/call_out.c:1.12
Rev: src/modules/sprintf/sprintf.c:1.11
Rev: src/multiset.c:1.6
Rev: src/object.c:1.18
Rev: src/opcodes.c:1.7
Rev: src/operators.c:1.15
Rev: src/peep.c:1.12
Rev: src/pike_macros.h:1.2
Rev: src/pike_memory.c:1.1
Rev: src/pike_memory.h:1.1
Rev: src/pike_types.c:1.22
Rev: src/stralloc.c:1.15
---
 .cvsignore                       |   1 +
 .gitignore                       |   1 +
 src/Makefile.in                  |   2 +-
 src/array.c                      |   2 +-
 src/builtin_functions.c          |   4 +-
 src/constants.c                  |   2 +-
 src/docode.c                     |   4 +-
 src/dynamic_buffer.c             |   2 +-
 src/gc.c                         |   1 +
 src/gc.h                         |   1 +
 src/global.h                     |  52 ++---
 src/las.c                        |   4 +-
 src/lex.c                        |   4 +-
 src/mapping.c                    |   4 +-
 src/modules/Image/quant.c        |   3 +-
 src/modules/Regexp/pike_regexp.c |   2 +-
 src/modules/call_out/call_out.c  |   4 +-
 src/modules/sprintf/sprintf.c    |   4 +-
 src/multiset.c                   |   2 +-
 src/object.c                     |   4 +-
 src/opcodes.c                    |   2 +-
 src/operators.c                  |   4 +-
 src/peep.c                       |   1 +
 src/pike_macros.h                |   2 +-
 src/pike_memory.c                | 367 +++++++++++++++++++++++++++++++
 src/pike_memory.h                |  61 +++++
 src/pike_types.c                 |   4 +-
 src/stralloc.c                   |   2 +-
 28 files changed, 491 insertions(+), 55 deletions(-)
 create mode 100644 src/pike_memory.c
 create mode 100644 src/pike_memory.h

diff --git a/.cvsignore b/.cvsignore
index bbef49da9c..0136d70c2d 100644
--- a/.cvsignore
+++ b/.cvsignore
@@ -2,6 +2,7 @@ Pike-v0.3.tar.gz
 Pike-v0.3.tar.hz
 Pike-v0.4.tar.gz
 Pike-v0.4.tar.hz
+Pike-v0.5b7.tar.gz
 Pike_v0.1.tar.gz
 Pike_v0.2.tar.gz
 Pike_v0.3.tar.gz
diff --git a/.gitignore b/.gitignore
index 18f1f53671..e1eba67679 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,6 +34,7 @@ core
 /Pike-v0.3.tar.hz
 /Pike-v0.4.tar.gz
 /Pike-v0.4.tar.hz
+/Pike-v0.5b7.tar.gz
 /Pike_v0.1.tar.gz
 /Pike_v0.2.tar.gz
 /Pike_v0.3.tar.gz
diff --git a/src/Makefile.in b/src/Makefile.in
index fb7864d354..804f48517a 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -88,7 +88,7 @@ OBJ= \
  pike_types.o \
  main.o \
  mapping.o \
- memory.o \
+ pike_memory.o \
  module.o \
  module_support.o \
  object.o \
diff --git a/src/array.c b/src/array.c
index 5ec1df37e6..756357d5f0 100644
--- a/src/array.c
+++ b/src/array.c
@@ -15,7 +15,7 @@
 #include "pike_types.h"
 #include "fsort.h"
 #include "builtin_functions.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "gc.h"
 #include "main.h"
 
diff --git a/src/builtin_functions.c b/src/builtin_functions.c
index 2279f3ee24..b5f30fe757 100644
--- a/src/builtin_functions.c
+++ b/src/builtin_functions.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: builtin_functions.c,v 1.36 1997/05/07 06:27:32 per Exp $");
+RCSID("$Id: builtin_functions.c,v 1.37 1997/05/19 23:31:00 hubbe Exp $");
 #include "interpret.h"
 #include "svalue.h"
 #include "pike_macros.h"
@@ -25,7 +25,7 @@ RCSID("$Id: builtin_functions.c,v 1.36 1997/05/07 06:27:32 per Exp $");
 #include "gc.h"
 #include "backend.h"
 #include "main.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "threads.h"
 #include "time_stuff.h"
 #include "version.h"
diff --git a/src/constants.c b/src/constants.c
index 49f697bcf9..d960b818f7 100644
--- a/src/constants.c
+++ b/src/constants.c
@@ -9,7 +9,7 @@
 #include "program.h"
 #include "pike_types.h"
 #include "stralloc.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "interpret.h"
 #include "mapping.h"
 #include "error.h"
diff --git a/src/docode.c b/src/docode.c
index c6a3bcb7a9..e84657b08c 100644
--- a/src/docode.c
+++ b/src/docode.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: docode.c,v 1.16 1997/04/17 22:47:06 hubbe Exp $");
+RCSID("$Id: docode.c,v 1.17 1997/05/19 23:31:00 hubbe Exp $");
 #include "las.h"
 #include "program.h"
 #include "language.h"
@@ -15,7 +15,7 @@ RCSID("$Id: docode.c,v 1.16 1997/04/17 22:47:06 hubbe Exp $");
 #include "array.h"
 #include "pike_macros.h"
 #include "error.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "svalue.h"
 #include "main.h"
 #include "lex.h"
diff --git a/src/dynamic_buffer.c b/src/dynamic_buffer.c
index aa83381152..aca18c7266 100644
--- a/src/dynamic_buffer.c
+++ b/src/dynamic_buffer.c
@@ -7,7 +7,7 @@
 #include "dynamic_buffer.h"
 #include "stralloc.h"
 #include "error.h"
-#include "memory.h"
+#include "pike_memory.h"
 
 static dynamic_buffer buff;
 
diff --git a/src/gc.c b/src/gc.c
index a3d1f008be..6b33fc6d70 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -18,6 +18,7 @@ struct callback *gc_evaluator_callback=0;
 #include "stralloc.h"
 #include "stuff.h"
 #include "error.h"
+#include "pike_memory.h"
 
 #include "gc.h"
 #include "main.h"
diff --git a/src/gc.h b/src/gc.h
index 74876eec46..e7ee576669 100644
--- a/src/gc.h
+++ b/src/gc.h
@@ -4,6 +4,7 @@
 #ifdef GC2
 
 #include "global.h"
+#include "callback.h"
 
 extern INT32 num_objects;
 extern INT32 num_allocs;
diff --git a/src/global.h b/src/global.h
index 21585e30df..a56bf86208 100644
--- a/src/global.h
+++ b/src/global.h
@@ -84,35 +84,23 @@ char *alloca ();
 #undef HAVE_STRING_H
 #endif
 
-#ifdef HAVE_MEMORY_H
-#include <memory.h>
-#undef HAVE_MEMORY_H
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#undef HAVE_LIMITS_H
 #endif
 
-#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
-#define RCSID(X) \
- static char *rcsid __attribute__ ((unused)) =X
-#elif __GNUC__ == 2
-#define RCSID(X) \
- static char *rcsid = X; \
- static void *use_rcsid=(&use_rcsid, (void *)&rcsid)
-#else
-#define RCSID(X) \
- static char *rcsid = X
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#undef HAVE_SYS_TYPES_H
 #endif
 
-#if defined(__GNUC__) && !defined(DEBUG) && !defined(lint)
-#define INLINE inline
-#else
-#define INLINE
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#undef HAVE_MEMORY_H
 #endif
 
-/* we here define a few types with more defined values */
 
-#ifdef HAVE_LIMITS_H
-#include <limits.h>
-#undef HAVE_LIMITS_H
-#endif
+/* we here define a few types with more defined values */
 
 #define INT64 long long
 
@@ -137,11 +125,25 @@ char *alloca ();
 #define FLOAT_TYPE float
 
 
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#undef HAVE_SYS_TYPES_H
+
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
+#define RCSID(X) \
+ static char *rcsid __attribute__ ((unused)) =X
+#elif __GNUC__ == 2
+#define RCSID(X) \
+ static char *rcsid = X; \
+ static void *use_rcsid=(&use_rcsid, (void *)&rcsid)
+#else
+#define RCSID(X) \
+ static char *rcsid = X
 #endif
 
+#if defined(__GNUC__) && !defined(DEBUG) && !defined(lint)
+#define INLINE inline
+#else
+#define INLINE
+#endif
 
 #include "port.h"
 
diff --git a/src/las.c b/src/las.c
index 23c9066cee..f71e13c3b4 100644
--- a/src/las.c
+++ b/src/las.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: las.c,v 1.32 1997/04/18 01:17:40 hubbe Exp $");
+RCSID("$Id: las.c,v 1.33 1997/05/19 23:31:01 hubbe Exp $");
 
 #include "language.h"
 #include "interpret.h"
@@ -21,7 +21,7 @@ RCSID("$Id: las.c,v 1.32 1997/04/18 01:17:40 hubbe Exp $");
 #include "error.h"
 #include "docode.h"
 #include "main.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "operators.h"
 #include "callback.h"
 #include "pike_macros.h"
diff --git a/src/lex.c b/src/lex.c
index d5dca0f5ff..18c829778a 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: lex.c,v 1.23 1997/05/19 09:32:39 hubbe Exp $");
+RCSID("$Id: lex.c,v 1.24 1997/05/19 23:31:02 hubbe Exp $");
 #include "language.h"
 #include "array.h"
 #include "lex.h"
@@ -13,7 +13,7 @@ RCSID("$Id: lex.c,v 1.23 1997/05/19 09:32:39 hubbe Exp $");
 #include "constants.h"
 #include "hashtable.h"
 #include "stuff.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "interpret.h"
 #include "error.h"
 #include "object.h"
diff --git a/src/mapping.c b/src/mapping.c
index 4e23c21d60..6be1344b81 100644
--- a/src/mapping.c
+++ b/src/mapping.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: mapping.c,v 1.19 1997/05/19 22:48:08 hubbe Exp $");
+RCSID("$Id: mapping.c,v 1.20 1997/05/19 23:31:02 hubbe Exp $");
 #include "main.h"
 #include "object.h"
 #include "mapping.h"
@@ -13,7 +13,7 @@ RCSID("$Id: mapping.c,v 1.19 1997/05/19 22:48:08 hubbe Exp $");
 #include "pike_macros.h"
 #include "language.h"
 #include "error.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
 #include "las.h"
diff --git a/src/modules/Image/quant.c b/src/modules/Image/quant.c
index 182dd345e2..ef871d8703 100644
--- a/src/modules/Image/quant.c
+++ b/src/modules/Image/quant.c
@@ -1,5 +1,5 @@
 #include <config.h>
-/* $Id: quant.c,v 1.8 1997/05/19 22:50:27 hubbe Exp $ */
+/* $Id: quant.c,v 1.9 1997/05/19 23:32:02 hubbe Exp $ */
 
 /*
 
@@ -21,6 +21,7 @@ David K
 
 #include "error.h"
 #include "global.h"
+#include "pike_memory.h"
 #include "array.h"
 
 #include "threads.h"
diff --git a/src/modules/Regexp/pike_regexp.c b/src/modules/Regexp/pike_regexp.c
index a0ff79a859..6bb86144e6 100644
--- a/src/modules/Regexp/pike_regexp.c
+++ b/src/modules/Regexp/pike_regexp.c
@@ -61,7 +61,7 @@
 #include "global.h"
 #include <ctype.h>
 #include "pike_regexp.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "error.h"
 
 /*
diff --git a/src/modules/call_out/call_out.c b/src/modules/call_out/call_out.c
index 4b58191748..74fc1a8cdb 100644
--- a/src/modules/call_out/call_out.c
+++ b/src/modules/call_out/call_out.c
@@ -4,14 +4,14 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: call_out.c,v 1.11 1997/05/18 22:23:29 grubba Exp $");
+RCSID("$Id: call_out.c,v 1.12 1997/05/19 23:32:56 hubbe Exp $");
 #include "array.h"
 #include "dynamic_buffer.h"
 #include "object.h"
 #include "interpret.h"
 #include "error.h"
 #include "builtin_functions.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "main.h"
 #include "backend.h"
 #include "time_stuff.h"
diff --git a/src/modules/sprintf/sprintf.c b/src/modules/sprintf/sprintf.c
index 4f13157115..5bca763687 100644
--- a/src/modules/sprintf/sprintf.c
+++ b/src/modules/sprintf/sprintf.c
@@ -96,7 +96,7 @@
 */
 
 #include "global.h"
-RCSID("$Id: sprintf.c,v 1.10 1997/04/16 03:13:41 hubbe Exp $");
+RCSID("$Id: sprintf.c,v 1.11 1997/05/19 23:33:22 hubbe Exp $");
 #include "error.h"
 #include "array.h"
 #include "svalue.h"
@@ -105,7 +105,7 @@ RCSID("$Id: sprintf.c,v 1.10 1997/04/16 03:13:41 hubbe Exp $");
 #include "pike_types.h"
 #include "constants.h"
 #include "interpret.h"
-#include "memory.h"
+#include "pike_memory.h"
 
 #define FORMAT_INFO_STACK_SIZE 200
 #define RETURN_SHARED_STRING
diff --git a/src/multiset.c b/src/multiset.c
index c7a8f6dd60..fcc31acb2f 100644
--- a/src/multiset.c
+++ b/src/multiset.c
@@ -8,7 +8,7 @@
 #include "multiset.h"
 #include "svalue.h"
 #include "pike_macros.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "error.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
diff --git a/src/object.c b/src/object.c
index 93cd6cac8c..aef6fb78cc 100644
--- a/src/object.c
+++ b/src/object.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: object.c,v 1.17 1997/04/16 03:09:14 hubbe Exp $");
+RCSID("$Id: object.c,v 1.18 1997/05/19 23:31:04 hubbe Exp $");
 #include "object.h"
 #include "dynamic_buffer.h"
 #include "interpret.h"
@@ -12,7 +12,7 @@ RCSID("$Id: object.c,v 1.17 1997/04/16 03:09:14 hubbe Exp $");
 #include "stralloc.h"
 #include "svalue.h"
 #include "pike_macros.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "error.h"
 #include "main.h"
 #include "array.h"
diff --git a/src/opcodes.c b/src/opcodes.c
index 7169888891..254a8c8551 100644
--- a/src/opcodes.c
+++ b/src/opcodes.c
@@ -16,7 +16,7 @@
 #include "object.h"
 #include "error.h"
 #include "pike_types.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "fd_control.h"
 
 void index_no_free(struct svalue *to,struct svalue *what,struct svalue *ind)
diff --git a/src/operators.c b/src/operators.c
index 988f760516..2fea7f42c4 100644
--- a/src/operators.c
+++ b/src/operators.c
@@ -5,7 +5,7 @@
 \*/
 #include <math.h>
 #include "global.h"
-RCSID("$Id: operators.c,v 1.14 1997/04/28 23:48:42 hubbe Exp $");
+RCSID("$Id: operators.c,v 1.15 1997/05/19 23:31:04 hubbe Exp $");
 #include "interpret.h"
 #include "svalue.h"
 #include "multiset.h"
@@ -15,7 +15,7 @@ RCSID("$Id: operators.c,v 1.14 1997/04/28 23:48:42 hubbe Exp $");
 #include "opcodes.h"
 #include "operators.h"
 #include "language.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "error.h"
 #include "docode.h"
 #include "constants.h"
diff --git a/src/peep.c b/src/peep.c
index 1552fe2dc6..fe70d6329a 100644
--- a/src/peep.c
+++ b/src/peep.c
@@ -8,6 +8,7 @@
 #include "main.h"
 #include "error.h"
 #include "lex.h"
+#include "pike_memory.h"
 #include "peep.h"
 
 struct p_instr_s
diff --git a/src/pike_macros.h b/src/pike_macros.h
index ff402f5bfe..812a50fc11 100644
--- a/src/pike_macros.h
+++ b/src/pike_macros.h
@@ -7,7 +7,7 @@
 #define MACROS_H
 
 #include <sys/param.h>
-#include "memory.h"
+#include "pike_memory.h"
 
 #define BASEOF(ptr, str_type, field)  \
 ((struct str_type *)((char*)ptr - (char*)& (((struct str_type *)0)->field)))
diff --git a/src/pike_memory.c b/src/pike_memory.c
new file mode 100644
index 0000000000..5a4ef44aa9
--- /dev/null
+++ b/src/pike_memory.c
@@ -0,0 +1,367 @@
+/*\
+||| This file a part of Pike, and is copyright by Fredrik Hubinette
+||| Pike is distributed as GPL (General Public License)
+||| See the files COPYING and DISCLAIMER for more information.
+\*/
+#include "global.h"
+#include "pike_memory.h"
+#include "error.h"
+#include "pike_macros.h"
+
+char *xalloc(SIZE_T size)
+{
+  char *ret;
+  if(!size) return 0;
+
+  ret=(char *)malloc(size);
+  if(ret) return ret;
+
+  error("Out of memory.\n");
+  return 0;
+}
+
+void swap(char *a, char *b, INT32 size)
+{
+  int tmp;
+  char tmpbuf[1024];
+  while(size)
+  {
+    tmp=MINIMUM((long)sizeof(tmpbuf), size);
+    MEMCPY(tmpbuf,a,tmp);
+    MEMCPY(b,a,tmp);
+    MEMCPY(b,tmpbuf,tmp);
+    size-=tmp;
+    a+=tmp;
+    b+=tmp;
+  }
+}
+
+void reverse(char *memory, INT32 nitems, INT32 size)
+{
+#define DOSIZE(X,Y)				\
+ case X:					\
+ {						\
+  struct Y { char tt[X]; };			\
+  struct Y tmp;					\
+  struct Y *start=(struct Y *) memory;		\
+  struct Y *end=start+nitems-1;			\
+  while(start<end){tmp=*start;*(start++)=*end;*(end--)=tmp;} \
+  break;					\
+ }
+
+  switch(size)
+  {
+    DOSIZE(1,TMP1)
+    DOSIZE(2,TMP2)
+    DOSIZE(4,TMP4)
+    DOSIZE(8,TMP8)
+  default:
+  {
+    char *start = (char *) memory;
+    char *end=start+(nitems-1)*size;
+    while(start<end)
+    {
+      swap(start,end,size);
+      start+=size;
+      end-=size;
+    }
+  }
+  }
+}
+
+/*
+ * This function may NOT change 'order'
+ * This function is hopefully fast enough...
+ */
+void reorder(char *memory, INT32 nitems, INT32 size,INT32 *order)
+{
+  INT32 e;
+  char *tmp;
+  tmp=xalloc(size * nitems);
+#undef DOSIZE
+#define DOSIZE(X,Y)				\
+ case X:					\
+ {						\
+  struct Y { char tt[X]; };			\
+  struct Y *from=(struct Y *) memory;		\
+  struct Y *to=(struct Y *) tmp;		\
+  for(e=0;e<nitems;e++) to[e]=from[order[e]];	\
+  break;					\
+ }
+  
+
+  switch(size)
+  {
+    DOSIZE(1,TMP1)
+    DOSIZE(2,TMP2)
+    DOSIZE(4,TMP4)
+    DOSIZE(8,TMP8)
+    DOSIZE(16,TMP16)
+
+  default:
+    for(e=0;e<nitems;e++) MEMCPY(tmp+e*size, memory+order[e]*size, size);
+  }
+
+  MEMCPY(memory, tmp, size * nitems);
+  free(tmp);
+}
+
+unsigned INT32 hashmem(const unsigned char *a,INT32 len,INT32 mlen)
+{
+  unsigned INT32 ret;
+
+  ret=9248339*len;
+  if(len<mlen) mlen=len;
+  switch(mlen&7)
+  {
+    case 7: ret^=*(a++);
+    case 6: ret^=(ret<<4)+*(a++);
+    case 5: ret^=(ret<<7)+*(a++);
+    case 4: ret^=(ret<<6)+*(a++);
+    case 3: ret^=(ret<<3)+*(a++);
+    case 2: ret^=(ret<<7)+*(a++);
+    case 1: ret^=(ret<<5)+*(a++);
+  }
+
+#ifdef HANDLES_UNALIGNED_MEMORY_ACCESS
+  {
+    unsigned int *b;
+    b=(unsigned int *)a;
+
+    for(mlen>>=3;--mlen>=0;)
+    {
+      ret^=(ret<<7)+*(b++);
+      ret^=(ret>>6)+*(b++);
+    }
+  }
+#else
+  for(mlen>>=3;--mlen>=0;)
+  {
+    ret^=(ret<<7)+((((((*(a++)<<3)+*(a++))<<4)+*(a++))<<5)+*(a++));
+    ret^=(ret>>6)+((((((*(a++)<<3)+*(a++))<<4)+*(a++))<<5)+*(a++));
+  }
+#endif
+  return ret;
+}
+
+unsigned INT32 hashstr(const unsigned char *str,INT32 maxn)
+{
+  unsigned INT32 ret,c;
+  
+  ret=str++[0];
+  for(; maxn>=0; maxn--)
+  {
+    c=str++[0];
+    if(!c) break;
+    ret ^= ( ret << 4 ) + c ;
+    ret &= 0x7fffffff;
+  }
+
+  return ret;
+}
+
+
+/*
+ * a quick memory search function.
+ * Written by Fredrik Hubinette (hubbe@lysator.liu.se)
+ */
+void init_memsearch(struct mem_searcher *s,
+		    char *needle,
+		    SIZE_T needlelen,
+		    SIZE_T max_haystacklen)
+{
+  s->needle=needle;
+  s->needlelen=needlelen;
+
+  switch(needlelen)
+  {
+  case 0: s->method=no_search; break;
+  case 1: s->method=use_memchr; break;
+  case 2:
+  case 3:
+  case 4:
+  case 5:
+  case 6: s->method=memchr_and_memcmp; break;
+  default:
+    if(max_haystacklen <= needlelen + 64)
+    {
+      s->method=memchr_and_memcmp;
+    }else{
+      INT32 tmp, h;
+      unsigned INT32 hsize, e, max;
+      unsigned char *q;
+      struct link *ptr;
+
+      hsize=52+(max_haystacklen >> 7)  - (needlelen >> 8);
+      max  =13+(max_haystacklen >> 4)  - (needlelen >> 5);
+
+      if(hsize > NELEM(s->set))
+      {
+	hsize=NELEM(s->set);
+      }else{
+	for(e=8;e<hsize;e+=e);
+	hsize=e;
+      }
+    
+      for(e=0;e<hsize;e++) s->set[e]=0;
+      hsize--;
+
+      if(max > needlelen) max=needlelen;
+      max=(max-sizeof(INT32)+1) & -sizeof(INT32);
+      if(max > MEMSEARCH_LINKS) max=MEMSEARCH_LINKS;
+
+      ptr=& s->links[0];
+
+      q=(unsigned char *)needle;
+
+#if BYTEORDER == 4321
+      for(tmp=e=0;e<sizeof(INT32)-1;e++)
+      {
+	tmp<<=8;
+	tmp|=*(q++);
+      }
+#endif
+
+      for(e=0;e<max;e++)
+      {
+#if BYTEORDER == 4321
+	tmp<<=8;
+	tmp|=*(q++);
+#else
+	tmp=EXTRACT_INT(q);
+	q++;
+#endif
+	h=tmp;
+	h+=h>>7;
+	h+=h>>17;
+	h&=hsize;
+
+	ptr->offset=e;
+	ptr->key=tmp;
+	ptr->next=s->set[h];
+	s->set[h]=ptr;
+	ptr++;
+      }
+      s->hsize=hsize;
+      s->max=max;
+      s->method=hubbe_search;
+    }
+  }
+}
+		    
+
+char *memory_search(struct mem_searcher *s,
+		    char *haystack,
+		    SIZE_T haystacklen)
+{
+  if(s->needlelen > haystacklen) return 0;
+
+  switch(s->method)
+  {
+  case no_search:
+    return haystack;
+
+  case use_memchr:
+    return MEMCHR(haystack,s->needle[0],haystacklen);
+
+  case memchr_and_memcmp:
+    {
+      char *end,c,*needle;
+      SIZE_T needlelen;
+      
+      needle=s->needle;
+      needlelen=s->needlelen;
+      
+      end=haystack + haystacklen - needlelen+1;
+      c=needle[0];
+      needle++;
+      needlelen--;
+      while((haystack=MEMCHR(haystack,c,end-haystack)))
+	if(!MEMCMP(++haystack,needle,needlelen))
+	  return haystack-1;
+
+      return 0;
+    }
+
+  case hubbe_search:
+    {
+      INT32 tmp, h;
+      char *q, *end;
+      register struct link *ptr;
+      
+      end=haystack+haystacklen;
+      q=haystack + s->max - sizeof(INT32);
+      q=(char *)( ((long)q) & -sizeof(INT32));
+      for(;q<=end-sizeof(INT32);q+=s->max)
+      {
+	h=tmp=*(INT32 *)q;
+	
+	h+=h>>7;
+	h+=h>>17;
+	h&=s->hsize;
+	
+	for(ptr=s->set[h];ptr;ptr=ptr->next)
+	{
+	  char *where;
+	  
+	  if(ptr->key != tmp) continue;
+	  
+	  where=q-ptr->offset;
+	  if(where<haystack) continue;
+	  if(where+s->needlelen>end) return 0;
+	  
+	  if(!MEMCMP(where,s->needle,s->needlelen))
+	    return where;
+	}
+      }
+    }
+  }
+  return 0;
+}
+
+char *my_memmem(char *needle,
+		SIZE_T needlelen,
+		char *haystack,
+		SIZE_T haystacklen)
+{
+  struct mem_searcher tmp;
+  init_memsearch(&tmp, needle, needlelen, haystacklen);
+  return memory_search(&tmp, haystack, haystacklen);
+}
+
+void memfill(char *to,
+	     INT32 tolen,
+	     char *from,
+	     INT32 fromlen,
+	     INT32 offset)
+{
+  if(fromlen==1)
+  {
+    MEMSET(to, *from, tolen);
+  }
+  else if(tolen>0)
+  {
+    INT32 tmp=MINIMUM(tolen, fromlen - offset);
+    MEMCPY(to, from + offset, tmp);
+    to+=tmp;
+    tolen-=tmp;
+
+    if(tolen > 0)
+    {
+      tmp=MINIMUM(tolen, fromlen);
+      MEMCPY(to, from, tmp);
+      from=to;
+      to+=tmp;
+      tolen-=tmp;
+      
+      while(tolen>0)
+      {
+	tmp=MINIMUM(tolen, fromlen);
+	MEMCPY(to, from, MINIMUM(tolen, fromlen));
+	fromlen+=tmp;
+	tolen-=tmp;
+	to+=tmp;
+      }
+    }
+  }
+}
diff --git a/src/pike_memory.h b/src/pike_memory.h
new file mode 100644
index 0000000000..e53a9fe6ef
--- /dev/null
+++ b/src/pike_memory.h
@@ -0,0 +1,61 @@
+/*\
+||| This file a part of Pike, and is copyright by Fredrik Hubinette
+||| Pike is distributed as GPL (General Public License)
+||| See the files COPYING and DISCLAIMER for more information.
+\*/
+#ifndef MEMORY_H
+#define MEMORY_H
+
+#include "global.h"
+
+#define MEMSEARCH_LINKS 512
+
+struct link
+{
+  struct link *next;
+  INT32 key, offset;
+};
+
+enum methods {
+  no_search,
+  use_memchr,
+  memchr_and_memcmp,
+  hubbe_search
+};
+
+struct mem_searcher
+{
+  enum methods method;
+  char *needle;
+  SIZE_T needlelen;
+  unsigned INT32 hsize, max;
+  struct link links[MEMSEARCH_LINKS];
+  struct link *set[MEMSEARCH_LINKS];
+};
+
+/* Prototypes begin here */
+char *xalloc(SIZE_T size);
+void swap(char *a, char *b, INT32 size);
+void reverse(char *memory, INT32 nitems, INT32 size);
+void reorder(char *memory, INT32 nitems, INT32 size,INT32 *order);
+unsigned INT32 hashmem(const unsigned char *a,INT32 len,INT32 mlen);
+unsigned INT32 hashstr(const unsigned char *str,INT32 maxn);
+void init_memsearch(struct mem_searcher *s,
+		    char *needle,
+		    SIZE_T needlelen,
+		    SIZE_T max_haystacklen);
+char *memory_search(struct mem_searcher *s,
+		    char *haystack,
+		    SIZE_T haystacklen);
+char *my_memmem(char *needle,
+		SIZE_T needlelen,
+		char *haystack,
+		SIZE_T haystacklen);
+void memfill(char *to,
+	     INT32 tolen,
+	     char *from,
+	     INT32 fromlen,
+	     INT32 offset);
+/* Prototypes end here */
+
+#endif
diff --git a/src/pike_types.c b/src/pike_types.c
index 92c102e6bf..18ac0ff8e4 100644
--- a/src/pike_types.c
+++ b/src/pike_types.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: pike_types.c,v 1.21 1997/04/16 03:09:15 hubbe Exp $");
+RCSID("$Id: pike_types.c,v 1.22 1997/05/19 23:31:06 hubbe Exp $");
 #include <ctype.h>
 #include "svalue.h"
 #include "pike_types.h"
@@ -20,7 +20,7 @@ RCSID("$Id: pike_types.c,v 1.21 1997/04/16 03:09:15 hubbe Exp $");
 #include "error.h"
 #include "las.h"
 #include "language.h"
-#include "memory.h"
+#include "pike_memory.h"
 
 int max_correct_args;
 
diff --git a/src/stralloc.c b/src/stralloc.c
index 4cbbeafb88..ba03109b9e 100644
--- a/src/stralloc.c
+++ b/src/stralloc.c
@@ -8,7 +8,7 @@
 #include "pike_macros.h"
 #include "dynamic_buffer.h"
 #include "pike_macros.h"
-#include "memory.h"
+#include "pike_memory.h"
 #include "error.h"
 #include "gc.h"
 
-- 
GitLab