From 244ce6ed005589b28f315164ff73d4b416472ffe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Sun, 26 Jan 1997 17:11:27 -0800
Subject: [PATCH] global constants are now stored in mappings

Rev: src/constants.c:1.6
Rev: src/constants.h:1.3
---
 src/constants.c | 93 +++++++++++--------------------------------------
 src/constants.h | 10 +-----
 2 files changed, 21 insertions(+), 82 deletions(-)

diff --git a/src/constants.c b/src/constants.c
index d177bf3e3a..f9c15cc718 100644
--- a/src/constants.c
+++ b/src/constants.c
@@ -11,50 +11,38 @@
 #include "stralloc.h"
 #include "memory.h"
 #include "interpret.h"
+#include "mapping.h"
 
-static struct hash_table *efun_hash = 0;
 static INT32 num_callable=0;
+static struct mapping *builtin_constants = 0;
 
-struct efun *lookup_efun(struct pike_string *name)
+struct mapping *get_builtin_constants()
 {
-  struct hash_entry *h;
+  if(!builtin_constants)
+    builtin_constants=allocate_mapping(20);
 
-  if(!efun_hash) return 0;
-  h=hash_lookup(efun_hash, name);
-  if(!h) return 0;
-  return BASEOF(h, efun, link);
+  return builtin_constants;
 }
 
 void low_add_efun(struct pike_string *name, struct svalue *fun)
 {
-  struct efun *parent;
-  
-  parent=lookup_efun(name);
+  struct svalue s;
 
-  if(!parent)
-  {
-    if(!fun) return;
+  if(!builtin_constants)
+    builtin_constants=allocate_mapping(20);
 
-    parent=ALLOC_STRUCT(efun);
-    copy_shared_string(parent->link.s,name);
-    efun_hash=hash_insert(efun_hash, &parent->link);
-  }else{
-    free_svalue(& parent->function);
+  s.type=T_STRING;
+  s.subtype=0;
+  s.u.string=name;
 
-    /* Disable efun */
-    if(!fun)
-    {
-      efun_hash=hash_unlink(efun_hash, &parent->link);
-      free_string(parent->link.s);
-      free((char *)parent);
-      return;
-    }
+  if(fun)
+  {
+    mapping_insert(builtin_constants, &s, fun);
+  }else{
+    map_delete(builtin_constants, &s);
   }
-
-  assign_svalue_no_free(& parent->function, fun);
 }
 
-
 struct callable *make_callable(c_fun fun,
 			       char *name,
 			       char *type,
@@ -107,55 +95,14 @@ void add_efun(char *name, c_fun fun, char *type, INT16 flags)
   add_efun2(name,fun,type,flags,0,0);
 }
 
-static void push_efun_entry(struct hash_entry *h)
-{
-  struct efun *f;
-  check_stack(1);
-  f=BASEOF(h, efun, link);
-  push_string(f->link.s);
-  f->link.s->refs++;
-  copy_svalues_recursively_no_free(sp,& f->function,1,0);
-  sp++;
-}
-
-void push_all_efuns_on_stack()
-{
-  if(efun_hash)
-    map_hashtable(efun_hash,push_efun_entry);
-}
-
-static void free_one_hashtable_entry(struct hash_entry *h)
-{
-  struct efun *f;
-  f=BASEOF(h, efun, link);
-  free_svalue(& f->function);
-  free((char *)f);
-}
-
 void cleanup_added_efuns()
 {
-  if(efun_hash)
+  if(builtin_constants)
   {
-    free_hashtable(efun_hash,free_one_hashtable_entry);
-    efun_hash=0;
+    free_mapping(builtin_constants);
+    builtin_constants=0;
   }
-  
 }
-
-void count_memory_in_constants(INT32 *num_, INT32 *size_)
-{
-  INT32 size=0, num=0;
-  if(efun_hash)
-  {
-    size=sizeof(struct hash_table) +
-      efun_hash->mask*sizeof(struct hash_entry)+
-    efun_hash->entries*sizeof(struct efun);
-    num=efun_hash->entries;
-  }
-  *num_=num;
-  *size_=size;
-}
-
 void count_memory_in_callables(INT32 *num_, INT32 *size_)
 {
   *num_=num_callable;
diff --git a/src/constants.h b/src/constants.h
index b6e274c7f4..1c10731dfa 100644
--- a/src/constants.h
+++ b/src/constants.h
@@ -10,12 +10,6 @@
 #include "hashtable.h"
 #include "las.h" /* For OPT_SIDE_EFFECT etc. */
 
-struct efun
-{
-  struct svalue function;
-  struct hash_entry link;
-};
-
 typedef void (*c_fun)(INT32);
 typedef int (*docode_fun)(node *n);
 typedef node *(*optimize_fun)(node *n);
@@ -32,7 +26,7 @@ struct callable
 };
 
 /* Prototypes begin here */
-struct efun *lookup_efun(struct pike_string *name);
+struct mapping *get_builtin_constants();
 void low_add_efun(struct pike_string *name, struct svalue *fun);
 struct callable *make_callable(c_fun fun,
 			       char *name,
@@ -48,9 +42,7 @@ void add_efun2(char *name,
 	       optimize_fun optimize,
 	       docode_fun docode);
 void add_efun(char *name, c_fun fun, char *type, INT16 flags);
-void push_all_efuns_on_stack();
 void cleanup_added_efuns();
-void count_memory_in_constants(INT32 *num_, INT32 *size_);
 void count_memory_in_callables(INT32 *num_, INT32 *size_);
 /* Prototypes end here */
 
-- 
GitLab