Commits

Shlomi Fish committed a12dfaa

Convert libfreecell-solver to meta_alloc.[ch].

This way it should recycle more memory and as a result conserve it.

Comments (0)

Files changed (8)

fc-solve/source/CMakeLists.txt

 )
 
 SET (FREECELL_SOLVER_LIB_MODULES
-    alloc.c
     app_str.c
     card.c
     check_and_add_state.c
     freecell.c
     instance.c
     lib.c
+    meta_alloc.c
     move.c
     move_funcs_maps.c
     move_funcs_order.c

fc-solve/source/fcs_hash.c

 
 #include "fcs_hash.h"
 
-#include "alloc.h"
+#include "meta_alloc.h"
 
 #include "inline.h"
 
 
 
 void fc_solve_hash_init(
+    fcs_meta_compact_allocator_t * meta_alloc,
     fc_solve_hash_t * hash,
 #ifdef FCS_INLINED_HASH_COMPARISON
     enum FCS_INLINED_HASH_DATA_TYPE hash_type
 #endif
 #endif
 
-    fc_solve_compact_allocator_init(&(hash->allocator));
+    fc_solve_compact_allocator_init(&(hash->allocator), meta_alloc);
 
     return;
 }

fc-solve/source/fcs_hash.h

 
 #include "config.h"
 
-#include "alloc.h"
+#include "meta_alloc.h"
 
 #include "inline.h"
 #include "bool.h"
 
 extern void
 fc_solve_hash_init(
+    fcs_meta_compact_allocator_t * meta_alloc,
     fc_solve_hash_t * hash,
 #ifdef FCS_INLINED_HASH_COMPARISON
     enum FCS_INLINED_HASH_DATA_TYPE hash_type

fc-solve/source/instance.c

     hard_thread->prelude_idx = 0;
 
     fc_solve_reset_hard_thread(hard_thread);
-    fc_solve_compact_allocator_init(&(hard_thread->allocator));
+    fc_solve_compact_allocator_init(&(hard_thread->allocator), instance->meta_alloc);
 
     fcs_move_stack_init(hard_thread->reusable_move_stack);
 }
     Afterwards fc_solve_init_instance() should be called in order
     to really prepare it for solving.
   */
-fc_solve_instance_t * fc_solve_alloc_instance(void)
+fc_solve_instance_t * fc_solve_alloc_instance(fcs_meta_compact_allocator_t * meta_alloc)
 {
     fc_solve_instance_t * instance;
 
     instance = SMALLOC1(instance);
 
+    instance->meta_alloc = meta_alloc;
+
 #if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INDIRECT)
     instance->num_indirect_prev_states = 0;
 #endif
     );
 #elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INTERNAL_HASH)
     fc_solve_hash_init(
+        instance->meta_alloc,
         &(instance->hash),
 #ifdef FCS_INLINED_HASH_COMPARISON
         FCS_INLINED_HASH__STATES
        collection */
 #if FCS_STACK_STORAGE == FCS_STACK_STORAGE_INTERNAL_HASH
     fc_solve_hash_init(
+        instance->meta_alloc,
         &(instance->stacks_hash ),
 #ifdef FCS_INLINED_HASH_COMPARISON
         FCS_INLINED_HASH__COLUMNS

fc-solve/source/instance.h

 
 #include "pqueue.h"
 
-#include "alloc.h"
+#include "meta_alloc.h"
 
 /*
  * This is a linked list item that is used to implement a queue for the BFS
      * */
     fcs_move_stack_t solution_moves;
 
+    /*
+     * The meta allocator - see meta_alloc.h.
+     * */
+    fcs_meta_compact_allocator_t * meta_alloc;
 };
 
 typedef struct fc_solve_instance_struct fc_solve_instance_t;
 #define FCS_BEFS_WEIGHT_DEPTH 4
 #define FCS_BEFS_WEIGHT_NUM_CARDS_NOT_ON_PARENTS 5
 
-fc_solve_instance_t * fc_solve_alloc_instance(void);
+fc_solve_instance_t * fc_solve_alloc_instance(fcs_meta_compact_allocator_t * meta_alloc);
 
 extern void fc_solve_init_instance(
     fc_solve_instance_t * instance
 
     free(hard_thread->soft_threads);
 
-    if (likely(hard_thread->allocator.packs))
-    {
-        fc_solve_compact_allocator_finish(&(hard_thread->allocator));
-        hard_thread->allocator.packs = NULL;
-    }
+    fc_solve_compact_allocator_finish(&(hard_thread->allocator));
 }
 /*
     This function is the last function that should be called in the

fc-solve/source/kaz_tree.h

 #include "sfx.h"
 #endif
 
-#include "alloc.h"
+#include "meta_alloc.h"
 /*
  * Blurb for inclusion into C++ translation units
  */

fc-solve/source/lib.c

 #endif
 
     char * error_string;
+
+    fcs_meta_compact_allocator_t meta_alloc;
 } fcs_user_t;
 
 
     fcs_duplicate_preset(user->common_preset, *freecell_preset);
 #endif
 
+    fc_solve_meta_compact_allocator_init(&(user->meta_alloc));
+
     user->instances_list = NULL;
     user->num_instances = 0;
     user->long_iter_handler = NULL;
         free(user->error_string);
         user->error_string = NULL;
     }
+
+    fc_solve_meta_compact_allocator_finish(&(user->meta_alloc));
 }
 
 void DLLEXPORT freecell_solver_user_free(
     instance_item->limit = flare->limit = -1;
 
     user->active_flare = flare;
-    flare->obj = fc_solve_alloc_instance();
+    flare->obj = fc_solve_alloc_instance(&(user->meta_alloc));
 
     /*
      * Switch the soft_thread variable so it won't refer to the old

fc-solve/source/scans.c

 #include "state.h"
 #include "card.h"
 #include "scans.h"
-#include "alloc.h"
+#include "meta_alloc.h"
 #include "check_and_add_state.h"
 #include "move_stack_compact_alloc.h"
 #include "freecell.h"