[前][次][番号順一覧][スレッド一覧]

ruby-changes:7717

From: nobu <ko1@a...>
Date: Mon, 8 Sep 2008 17:27:13 +0900 (JST)
Subject: [ruby-changes:7717] Ruby:r19238 (mvm): * st.c (garbage_collect): checks if memory can be reclaimed.

nobu	2008-09-08 17:26:54 +0900 (Mon, 08 Sep 2008)

  New Revision: 19238

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19238

  Log:
    * st.c (garbage_collect): checks if memory can be reclaimed.

  Modified files:
    branches/mvm/ChangeLog
    branches/mvm/st.c

Index: mvm/ChangeLog
===================================================================
--- mvm/ChangeLog	(revision 19237)
+++ mvm/ChangeLog	(revision 19238)
@@ -1,3 +1,7 @@
+Mon Sep  8 17:26:51 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* st.c (garbage_collect): checks if memory can be reclaimed.
+
 Mon Sep  8 07:09:42 2008  Tadayoshi Funaba  <tadf@d...>
 
 	* complex.c: some adjustments.
Index: mvm/st.c
===================================================================
--- mvm/st.c	(revision 19237)
+++ mvm/st.c	(revision 19238)
@@ -61,9 +61,9 @@
 static void rehash(st_table *);
 
 #ifdef RUBY
-#define malloc xmalloc
-#define calloc xcalloc
-#define free(x) xfree(x)
+#define garbage_collect() rb_garbage_collect()
+#else
+#define garbage_collect() 0
 #endif
 
 #define alloc(type) (type*)malloc((size_t)sizeof(type))
@@ -168,12 +168,22 @@
 
     size = new_size(size);	/* round up to prime number */
 
+  retry:
     tbl = alloc(st_table);
+    if (!tbl) {
+      nomem:
+	if (garbage_collect()) goto retry;
+	retry 0;
+    }
     tbl->type = type;
     tbl->num_entries = 0;
     tbl->entries_packed = type == &type_numhash && size/2 <= MAX_PACKED_NUMHASH;
     tbl->num_bins = size;
     tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*));
+    if (!tbl->bins) {
+	free(tbl);
+	goto nomem;
+    }
     tbl->head = 0;
 
     return tbl;
@@ -332,15 +342,17 @@
     }
 }
 
-#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
+#define ADD_DIRECT(table, key, value, hash_val, bin_pos, result)\
 do {\
     st_table_entry *entry, *head;\
+    result = 0;\
     if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\
 	rehash(table);\
         bin_pos = hash_val % table->num_bins;\
     }\
     \
     entry = alloc(st_table_entry);\
+    if (!entry) break;
     \
     entry->hash = hash_val;\
     entry->key = key;\
@@ -356,6 +368,7 @@
     }\
     table->bins[bin_pos] = entry;\
     table->num_entries++;\
+    result = 1;\
 } while (0)
 
 static void
@@ -403,7 +416,13 @@
     FIND_ENTRY(table, ptr, hash_val, bin_pos);
 
     if (ptr == 0) {
-	ADD_DIRECT(table, key, value, hash_val, bin_pos);
+	int result;
+      retry:
+	ADD_DIRECT(table, key, value, hash_val, bin_pos, result);
+	if (!result) {
+	    if (garbage_collect()) goto retry;
+	    return -1;
+	}
 	return 0;
     }
     else {
@@ -416,6 +435,7 @@
 st_add_direct(st_table *table, st_data_t key, st_data_t value)
 {
     unsigned int hash_val, bin_pos;
+    int result;
 
     if (table->entries_packed) {
         int i;
@@ -432,7 +452,11 @@
 
     hash_val = do_hash(key, table);
     bin_pos = hash_val % table->num_bins;
-    ADD_DIRECT(table, key, value, hash_val, bin_pos);
+  retry:
+    ADD_DIRECT(table, key, value, hash_val, bin_pos, result);
+    if (!result) {
+	if (garbage_collect()) goto retry;
+    }
 }
 
 static void
@@ -443,8 +467,14 @@
     unsigned int hash_val;
 
     new_num_bins = new_size(table->num_bins+1);
+  retry:
     new_bins = (st_table_entry**)
-	xrealloc(table->bins, new_num_bins * sizeof(st_table_entry*));
+	realloc(table->bins, new_num_bins * sizeof(st_table_entry*));
+    if (!new_bins) {
+	if (garbage_collect()) goto retry;
+	return;
+    }
+
     for (i = 0; i < new_num_bins; ++i) new_bins[i] = 0;
     table->num_bins = new_num_bins;
     table->bins = new_bins;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]