Sindbad~EG File Manager
/* M. Beeson, for Mathpert */
/* allocate_doc_data and release_doc_data and init_papyrus */
/*
6.10.94 original date
1.17.99 last modified
1.13.00 made workspace 256K instead of 208K
10.12.00 made maxvariables and maxdefns 64 instead of 48
9.5.04 made maxvariables and maxdefns 32, since dependencies are stored in a 32-bit word.
Even if we make more than 32 variables, the dependencies will not work correctly.
Of course, we could use a long long instead of a long, but 32 variables is enough!
7.8.05 Doubled the heapsize to one megabyte.
1.18.06 deleted "" and replaced GlobalAllocate by malloc and GlobalFreePointer by free,
and GlobalReallocate by realloc
9.2.06 doubled maxworkspace to 128K = 512Kbytes of memory
4.19.13 changed GetWindowLong and SetWindowLong to GetWindowLongPtr and SetWindowLongPtr, doubling offsets.
6.7.13 made the hwnd member of docstruct a ptrdiff_t instead of an unsigned long
6.18.13 changed release_docdata because brotherdoc is now a PDOCDATA
12.5.15 changed << 4 to *sizeof(unit) where heapsize_in_bytes is set.
10.22.24 made allocate_doc_data allocate papyrus
*/
#include <string.h>
#include <stdio.h> /* FILE, mentioned by wfile.h */
#include <stdlib.h> /* memset */
#include <assert.h>
#include <stddef.h>
#include "globals.h"
//#include "heap.h"
#include "heaps.h"
#include "graphstr.h"
#include "mpdoc.h"
#include "display.h"
#include "probtype.h"
#include "ssolve.h"
#include "activate.h"
#include "induct.h" /* reset_induction */
#include "automode.h" /* set_control_flags */
#include "cflags.h" /* set_problemtype */
#include "docdata.h"
#include "mpmem.h" /* permalloc */
static int nextdocnumber = 1; // they start with 1 so as to be positive
// this just serves as an identifier for a document's heap.
// The only point is it should be unique. So the numbering only
// starts over when the Engine is restarted.
/*___________________________________________________________________*/
int allocate_doc_data(PSYMBOLDATA pdata)
/* initialize the data required for a document */
/* Do NOT assume the document is active */
/* When this is called, mainchoice and problemtype have
already been initialized, and must get stored in the
document data structure. */
/* allocate the document's heap */
/* allocate the following fields of the document structure:
varlist, varinfo, model, assumptions, defns, parameters,
history, permspace, permhistory,papyrus.
Memory for these structures is
allocated on the Windows global heap, where it can be
realloc'd if necessary so that in essence these arrays have
no fixed dimension.
Does not allocate 'graphs' (which is an array of fixed
dimension MAXGRAPHS) and does not allocate the graph
structures in that array either (setupdata does that).
Return 0 for success,
2 for not enough memory to allocate the heap or other structures
*/
{ int err;
unsigned long heapsize_in_bytes;
pdata->docnumber = nextdocnumber;
++nextdocnumber;
pdata->DocVarData.maxvariables = 32;
pdata->DocVarData.nvariables = 0;
pdata->DocVarData.eigenvariable = 0;
pdata->DocVarData.maxparameters = MAXPARAMS;
pdata->DocVarData.nparameters = 0;
pdata->DocVarData.maxdefns = 32;
pdata->DocVarData.nextdefn = 0;
pdata->DocProverData.solver = ssolve;
pdata->DocProverData.maxassumptions = 64;
pdata->DocProverData.nextassumption = 0;
pdata->DocProverData.maxtheorems = 10;
pdata->DocProverData.nexttheorem = 0;
pdata->DocProverData.maxhistory = 512;
pdata->DocProverData.maxworkspace = 0x20000UL; /* 128 K, but in longs, so = 512K bytes */
pdata->DocProverData.nextworkspace = 0;
pdata->ngraphs = 0; /* in the the case of a graph document,
setupdata will change this. */
pdata->heapsize = (1 << 15); /* in units, yielding a one-megabyte = 1L << 20 heap */
heapsize_in_bytes = (((unsigned long) (pdata->heapsize)) * sizeof(heapunit));
pdata->heap = (heapunit *) calloc(1,heapsize_in_bytes);
if(pdata->heap==NULL)
{ pdata->heapsize =0;
printf("calloc failed in allocate_doc_data\n");
return 2;
}
err = create_heap(pdata->docnumber,pdata->heap,pdata->heapsize);
if(err)
return err;
pdata->DocVarData.varlist = (term *) calloc(1,pdata->DocVarData.maxvariables * sizeof(term));
if(pdata->DocVarData.varlist == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocVarData.varinfo = (varinf *) calloc(1,pdata->DocVarData.maxvariables * sizeof(varinf));
if(pdata->DocVarData.varinfo == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocVarData.parameters = (parameter *) calloc(1,pdata->DocVarData.maxparameters * sizeof(parameter));
if(pdata->DocVarData.parameters == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocVarData.defns = (defn *) calloc(1,pdata->DocVarData.maxdefns * sizeof(defn));
if(pdata->DocVarData.defns == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocProverData.assumptions = (assumption **) calloc(1,pdata->DocProverData.maxassumptions * sizeof(assumption *));
if(pdata->DocProverData.assumptions == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocProverData.theorems = (term *) calloc(1,pdata->DocProverData.maxtheorems * sizeof(term));
if(pdata->DocProverData.theorems == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocProverData.history = (term *) calloc(1,pdata->DocProverData.maxhistory * sizeof(term));
if(pdata->DocProverData.history == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocProverData.workspace = (unsigned long *) calloc(1,sizeof(unsigned long) * pdata->DocProverData.maxworkspace);
if(pdata->DocProverData.workspace == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocProverData.permhistory = (unsigned long *) calloc(1,pdata->DocProverData.maxhistory*sizeof(unsigned long));
if(pdata->DocProverData.permhistory == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocControlData.opseq = (operation *) calloc(1,pdata->DocProverData.maxhistory* sizeof(operation));
if(pdata->DocControlData.opseq == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->DocControlData.nlinedata = pdata->DocProverData.maxhistory + 5;
pdata->DocControlData.linedatahistory = (linedata *) calloc(1,pdata->DocControlData.nlinedata * sizeof(linedata));
if(pdata->DocControlData.linedatahistory == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
pdata->papyrus = (Papyrus *) calloc(1,sizeof(Papyrus));
if(pdata->papyrus == NULL)
{ printf("calloc failed in allocate_doc_data\n");
return 2;
}
return 0;
}
/*___________________________________________________________________*/
void zero_doc_data(PSYMBOLDATA pdata)
/* initialize the data allocated by allocate_doc_data
to zero again. Called by reset_document, which is called
when Next is pushed or chosen from a menu. Does not
reset the document heap to zero contents; does reset
the document user model to zero contents, which is
needed to kill old inhibitions that may be left over.
*/
{ memset(pdata->DocVarData.varlist, 0, pdata->DocVarData.maxvariables * sizeof(term));
memset(pdata->DocVarData.varinfo,0,pdata->DocVarData.maxvariables * sizeof(varinf));
memset(pdata->DocVarData.parameters,0,pdata->DocVarData.maxparameters * sizeof(parameter));
memset(pdata->DocVarData.defns,0,pdata->DocVarData.maxdefns * sizeof(defn));
memset(pdata->DocProverData.assumptions,0,pdata->DocProverData.maxassumptions * sizeof(assumption *));
memset(pdata->DocProverData.theorems,0,pdata->DocProverData.maxtheorems * sizeof(term));
memset(pdata->DocProverData.history,0,pdata->DocProverData.maxhistory * sizeof(term *));
memset(pdata->DocProverData.workspace,0, sizeof(unsigned long) * pdata->DocProverData.maxworkspace);
memset(pdata->DocProverData.permhistory,0,pdata->DocProverData.maxhistory*sizeof(unsigned long));
memset(pdata->DocControlData.opseq,0,pdata->DocProverData.maxhistory* sizeof(operation));
memset(pdata->DocControlData.linedatahistory,0,pdata->DocControlData.nlinedata* sizeof(linedata));
memset(pdata->DocControlData.model,0, HASHPRIME *sizeof(hashbucket));
}
/*___________________________________________________________________*/
void release_doc_data(PSYMBOLDATA pdata)
/* free all the space allocated by allocate_doc_data.
In the case of a graph document, setupdata allocates some space
but only on the document heap, and the document heap is here
given back to the operating system anyway, so there's no need
to worry about calling free2 here to undo setupdata.
This must not be called while pdata is the active document.
*/
{
free(pdata->heap);
remove_heap(pdata->docnumber);
free(pdata->DocVarData.varlist);
free(pdata->DocVarData.varinfo);
free(pdata->DocProverData.assumptions);
free(pdata->DocProverData.theorems);
free(pdata->DocProverData.history);
free(pdata->DocProverData.workspace);
free(pdata->DocVarData.parameters);
free(pdata->DocVarData.defns);
free(pdata->DocProverData.permhistory);
free(pdata->DocControlData.opseq);
free(pdata->DocControlData.linedatahistory);
}
/*_____________________________________________________________*/
void store_reason(PDOCDATA p, int n, const char *reason)
/* put the reason in the docdata at line n */
/* Also store the current eigenvariable in linedatahistory */
{ if((unsigned)(n + 5) >= p->DocControlData.nlinedata)
{ int mm = p->DocControlData.nlinedata + 100;
p->DocControlData.linedatahistory =
realloc(p->DocControlData.linedatahistory, mm * sizeof(linedata));
if(p->DocControlData.linedatahistory == NULL)
nospace();
p->DocControlData.nlinedata = (short)(p->DocControlData.nlinedata + 100);
/* use of += provokes a 32-bit warning message */
}
p->DocControlData.linedatahistory[n].reason = (const char *) permalloc((int) strlen((char *)reason)+1);
if(p->DocControlData.linedatahistory[n].reason == NULL)
nospace();
strcpy((char *)p->DocControlData.linedatahistory[n].reason,(char *)reason);
p->DocControlData.linedatahistory[n].eigenindex = (unsigned short) p->DocVarData.eigenvariable;
p->DocProverData.permhistory[0] = p->DocProverData.nextworkspace;
}
/*_____________________________________________________________*/
const char * get_reason(PDOCDATA p, int n)
/* get the reason for line n of the solution */
{ return p->DocControlData.linedatahistory[n].reason;
}
/*_____________________________________________________________*/
void store_comment(PDOCDATA p, int n, unsigned char *comment)
/* put the comment in the docdata at line n */
{ if((unsigned)(n + 5) >= p->DocControlData.nlinedata)
{ int mm = p->DocControlData.nlinedata + 100;
p->DocControlData.linedatahistory =
realloc(p->DocControlData.linedatahistory, mm * sizeof(linedata));
if(p->DocControlData.linedatahistory == NULL)
nospace();
p->DocControlData.nlinedata =(short)(p->DocControlData.nlinedata + 100);
/*use of += provokes a 32-bit warning message because the cast isn't there */
}
if(comment == NULL)
{ p->DocControlData.linedatahistory[n].comment = NULL;
return;
}
p->DocControlData.linedatahistory[n].comment = (unsigned char *) permalloc((int) strlen((char *)comment)+1);
if(p->DocControlData.linedatahistory[n].comment == NULL)
nospace();
strcpy((char *)p->DocControlData.linedatahistory[n].comment,(char *)comment);
}
/*_____________________________________________________________*/
unsigned char * get_comment(PDOCDATA p, int n)
/* get the comment for line n of the solution;
it can be NULL. Only 'permanent' comments are
entered in the docdata to be found here.
*/
{ return p->DocControlData.linedatahistory[n].comment;
}
/*_____________________________________________________________*/
unsigned char controlflags(PDOCDATA p, int n)
/* get the controlflags for line n of the solution.
*/
{ return p->DocControlData.linedatahistory[n].controlflags;
}
/*_____________________________________________________________*/
static int VersionNumber; /* 101 for version 1.01, etc. */
int GetVersionNumber(void)
{ return VersionNumber;
}
void SetVersionNumber(int n)
{ VersionNumber = n;
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists