Commits

Shlomi Fish  committed 2a2b959

Associated the memory tracking with the soft_thread struct.

  • Participants
  • Parent commits fb211df
  • Branches make-code-suitable-for-fc-solve

Comments (0)

Files changed (7)

File patsolve/pat.c

 
 static int get_possible_moves(fc_solve_soft_thread_t * soft_thread, int *, int *);
 static void mark_irreversible(fc_solve_soft_thread_t * soft_thread, int n);
-static void win(POSITION *pos);
+static void win(fc_solve_soft_thread_t * soft_thread, POSITION *pos);
 static INLINE int get_pilenum(fc_solve_soft_thread_t * soft_thread, int w);
 
 /* Hash a pile. */
 
 			/* Report the win. */
 
-			win(pos);
+			win(soft_thread, pos);
 
 			if (Noexit) {
 				Numsol++;
 	do the recursive solve() on them, but only after queueing the other
 	moves. */
 
-	mp = mp0 = new_array(MOVE, n);
+	mp = mp0 = new_array(soft_thread, MOVE, n);
 	if (mp == NULL) {
 		return NULL;
 	}
 		p = (u_char *)soft_thread->Freepos;
 		soft_thread->Freepos = soft_thread->Freepos->queue;
 	} else {
-		p = new_from_block(Posbytes);
+		p = new_from_block(soft_thread, Posbytes);
 		if (p == NULL) {
 			return NULL;
 		}
 	/* Allocate space and store the pile numbers.  The tree node
 	will get filled in later, by insert_node(). */
 
-	p = new_from_block(Treebytes);
+	p = new_from_block(soft_thread, Treebytes);
 	if (p == NULL) {
 		return NULL;
 	}
 
 /* Win.  Print out the move stack. */
 
-static void win(POSITION *pos)
+static void win(fc_solve_soft_thread_t * soft_thread, POSITION *pos)
 {
 	int i, nmoves;
 	FILE *out;
 		i++;
 	}
 	nmoves = i;
-	mpp0 = new_array(MOVE *, nmoves);
+	mpp0 = new_array(soft_thread, MOVE *, nmoves);
 	if (mpp0 == NULL) {
 		return; /* how sad, so close... */
 	}
 		}
 	}
 	fclose(out);
-	free_array(mpp0, MOVE *, nmoves);
+	free_array(soft_thread, mpp0, MOVE *, nmoves);
 
 	if (!Quiet) {
 		printf("A winner.\n");
 			fc_solve_msg("Ran out of pile numbers!");
 			return -1;
 		}
-		l = new(BUCKETLIST);
+		l = new(soft_thread, BUCKETLIST);
 		if (l == NULL) {
 			return -1;
 		}
-		l->pile = new_array(u_char, soft_thread->Wlen[w] + 1);
+		l->pile = new_array(soft_thread, u_char, soft_thread->Wlen[w] + 1);
 		if (l->pile == NULL) {
-			free_ptr(l, BUCKETLIST);
+			free_ptr(soft_thread, l, BUCKETLIST);
 			return -1;
 		}
 
 	return l->pilenum;
 }
 
-void free_buckets(void)
+void free_buckets(fc_solve_soft_thread_t * soft_thread)
 {
 	int i, j;
 	BUCKETLIST *l, *n;
 		while (l) {
 			n = l->next;
 			j = strlen((const char *)l->pile);    /* @@@ use block? */
-			free_array(l->pile, u_char, j + 1);
-			free_ptr(l, BUCKETLIST);
+			free_array(soft_thread, l->pile, u_char, j + 1);
+			free_ptr(soft_thread, l, BUCKETLIST);
 			l = n;
 		}
 	}

File patsolve/pat.h

  */
 /* Solve Freecell and Seahaven type patience (solitaire) games. */
 
+#ifndef FC_SOLVE_PATSOLVE_PAT_H
+#define FC_SOLVE_PATSOLVE_PAT_H
+
+#include <stdio.h>
+#include <sys/types.h>
 #include "util.h"
-#include <sys/types.h>
 #include "config.h"
 #include "tree.h"
 #include "param.h"
 
 /* Statistics. */
 
-extern long Mem_remain;
-
 #define NQUEUES 100
 
 struct fc_solve_soft_thread_struct
 {
+    long Mem_remain;
     POSITION *Qhead[NQUEUES]; /* separate queue for each priority */
     POSITION *Qtail[NQUEUES]; /* positions are added here */
     int Maxq;
 extern void unpack_position(fc_solve_soft_thread_t * soft_thread, POSITION *);
 extern TREE *pack_position(fc_solve_soft_thread_t * soft_thread);
 extern void init_buckets(fc_solve_soft_thread_t * soft_thread);
-extern void init_clusters(void);
-extern u_char *new_from_block(size_t);
+extern void init_clusters(fc_solve_soft_thread_t * soft_thread);
+extern u_char *new_from_block(fc_solve_soft_thread_t * soft_thread, size_t);
 extern void pilesort(fc_solve_soft_thread_t * soft_thread);
 extern void msdeal(fc_solve_soft_thread_t * soft_thread, u_int64_t);
+
+/* A function and some macros for allocating memory. */
+
+extern void *new_(fc_solve_soft_thread_t * soft_thread, size_t s);
+
+#define new(soft_thread, type) (type *)new_(soft_thread, sizeof(type))
+#define free_ptr(soft_thread, ptr, type) free(ptr); (soft_thread)->Mem_remain += sizeof(type)
+
+#define new_array(soft_thread, type, size) (type *)new_(soft_thread, (size) * sizeof(type))
+#define free_array(soft_thread, ptr, type, size) free(ptr); \
+				    (soft_thread)->Mem_remain += (size) * sizeof(type)
+
+extern void free_buckets(fc_solve_soft_thread_t * soft_thread);
+extern void free_clusters(fc_solve_soft_thread_t * soft_thread);
+extern void free_blocks(fc_solve_soft_thread_t * soft_thread);
+
+#endif /* #ifndef FC_SOLVE_PATSOLVE_PAT_H */

File patsolve/patmain.c

 int Status;             /* win, lose, or fail */
 int Quiet = FALSE;      /* print entertaining messages, else exit(Status); */
 
-long Mem_remain = 50 * 1000 * 1000;
 #if DEBUG
 long Init_mem_remain;
 #endif
 
     soft_thread = &soft_thread_struct;
 
+    soft_thread->Mem_remain = (50 * 1000 * 1000);
     soft_thread->Freepos = NULL;
     /* Default variation. */
     soft_thread_struct.Same_suit = SAME_SUIT;
 				break;
 
 			case 'M':
-				Mem_remain = atol(curr_arg) * 1000000;
+				soft_thread->Mem_remain = atol(curr_arg) * 1000000;
 				curr_arg = NULL;
 				break;
 
 	if (Stack && Noexit) {
 		fatalerr("-S and -E may not be used together.");
 	}
-	if (Mem_remain < BLOCKSIZE * 2) {
+	if (soft_thread->Mem_remain < BLOCKSIZE * 2) {
 		fatalerr("-M too small.");
 	}
 	if (soft_thread->Nwpiles > MAXWPILES) {
 	/* Initialize the hash tables. */
 
 	init_buckets(soft_thread);
-	init_clusters();
+	init_clusters(soft_thread);
 
 	/* Reset stats. */
 
 #endif
 	}
 	if (!Interactive) {
-		free_buckets();
-		free_clusters();
-		free_blocks();
+		free_buckets(soft_thread);
+		free_clusters(soft_thread);
+		free_blocks(soft_thread);
 		soft_thread->Freepos = NULL;
 	}
 #if DEBUG

File patsolve/patsolve.c

 			q = TRUE;
 		}
 	}
-	free_array(mp0, MOVE, nmoves);
+	free_array(soft_thread, mp0, MOVE, nmoves);
 
 	/* Return true if this position needs to be kept around. */
 

File patsolve/tree.c

 TREELIST *Treelist[NBUCKETS];
 
 static int insert_node(TREE *n, int d, TREE **tree, TREE **node);
-static TREELIST *cluster_tree(int cluster);
+static TREELIST *cluster_tree(fc_solve_soft_thread_t * soft_thread, int cluster);
 static void give_back_block(u_char *p);
-static BLOCK *new_block(void);
+static BLOCK *new_block(fc_solve_soft_thread_t * soft_thread);
 
 int Pilebytes;
 
 
 	/* Get the tree for this cluster. */
 
-	tl = cluster_tree(k);
+	tl = cluster_tree(soft_thread, k);
 	if (tl == NULL) {
 		return ERR;
 	}
 
 /* Clusters are also stored in a hashed array. */
 
-void init_clusters(void)
+void init_clusters(fc_solve_soft_thread_t * soft_thread)
 {
 	memset(Treelist, 0, sizeof(Treelist));
-	Block = new_block();                    /* @@@ */
+	Block = new_block(soft_thread);                    /* @@@ */
 }
 
-static TREELIST *cluster_tree(int cluster)
+static TREELIST *cluster_tree(fc_solve_soft_thread_t * soft_thread, int cluster)
 {
 	int bucket;
 	TREELIST *tl, *last;
 	/* If we didn't find it, make a new one and add it to the list. */
 
 	if (tl == NULL) {
-		tl = new(TREELIST);
+		tl = new(soft_thread, TREELIST);
 		if (tl == NULL) {
 			return NULL;
 		}
 
 /* Block storage.  Reduces overhead, and can be freed quickly. */
 
-static BLOCK *new_block(void)
+static BLOCK *new_block(fc_solve_soft_thread_t * soft_thread)
 {
 	BLOCK *b;
 
-	b = new(BLOCK);
+	b = new(soft_thread, BLOCK);
 	if (b == NULL) {
 		return NULL;
 	}
-	b->block = new_array(u_char, BLOCKSIZE);
+	b->block = new_array(soft_thread, u_char, BLOCKSIZE);
 	if (b->block == NULL) {
-		free_ptr(b, BLOCK);
+		free_ptr(soft_thread, b, BLOCK);
 		return NULL;
 	}
 	b->ptr = b->block;
 
 /* Like new(), only from the current block.  Make a new block if necessary. */
 
-u_char *new_from_block(size_t s)
+u_char *new_from_block(fc_solve_soft_thread_t * soft_thread, size_t s)
 {
 	u_char *p;
 	BLOCK *b;
 
 	b = Block;
 	if (s > b->remain) {
-		b = new_block();
+		b = new_block(soft_thread);
 		if (b == NULL) {
 			return NULL;
 		}
 	b->remain += s;
 }
 
-void free_blocks(void)
+void free_blocks(fc_solve_soft_thread_t * soft_thread)
 {
 	BLOCK *b, *next;
 
 	b = Block;
 	while (b) {
 		next = b->next;
-		free_array(b->block, u_char, BLOCKSIZE);
-		free_ptr(b, BLOCK);
+		free_array(soft_thread, b->block, u_char, BLOCKSIZE);
+		free_ptr(soft_thread, b, BLOCK);
 		b = next;
 	}
 }
 
-void free_clusters(void)
+void free_clusters(fc_solve_soft_thread_t * soft_thread)
 {
 	int i;
 	TREELIST *l, *n;
 		l = Treelist[i];
 		while (l) {
 			n = l->next;
-			free_ptr(l, TREELIST);
+			free_ptr(soft_thread, l, TREELIST);
 			l = n;
 		}
 	}

File patsolve/util.c

 
 /* Allocate some space and return a pointer to it.  See new() in util.h. */
 
-void *new_(size_t s)
+void *new_(fc_solve_soft_thread_t * soft_thread, size_t s)
 {
 	void *x;
 
-	if (s > Mem_remain) {
+	if (s > soft_thread->Mem_remain) {
 #if 0
 		POSITION *pos;
 
 			free_array(Freepos, u_char, sizeof(POSITION) + Ntpiles);
 			Freepos = pos;
 		}
-		if (s > Mem_remain) {
+		if (s > soft_thread->Mem_remain) {
 			Status = FAIL;
 			return NULL;
 		}
 		return NULL;
 	}
 
-	Mem_remain -= s;
+	soft_thread->Mem_remain -= s;
 	return x;
 }

File patsolve/util.h

 #include <string.h>
 #include <math.h>
 
+#include "pat.h"
+
 #ifndef TRUE
 #define TRUE 1
 #define FALSE 0
 #ifdef  __cplusplus
 extern "C" {
 #endif
-
-/* A function and some macros for allocating memory. */
-
-extern void *new_(size_t s);
-extern long Mem_remain;
-
-#define new(type) (type *)new_(sizeof(type))
-#define free_ptr(ptr, type) free(ptr); Mem_remain += sizeof(type)
-
-#define new_array(type, size) (type *)new_((size) * sizeof(type))
-#define free_array(ptr, type, size) free(ptr); \
-				    Mem_remain += (size) * sizeof(type)
-
-extern void free_buckets(void);
-extern void free_clusters(void);
-extern void free_blocks(void);
-
 /* Error messages. */
 extern void fc_solve_msg(char *msg, ...);