1112 lines · 38.9 KB · C
RawDownload
/* ------------------------------------------------------------------------- *//*   "memory" : Memory management and ICL memory setting commands            *//*              (For "memoryerror", see "errors.c")                          *//*                                                                           *//*   Part of Inform 6.33                                                     *//*   copyright (c) Graham Nelson 1993 - 2014                                 *//*                                                                           *//* ------------------------------------------------------------------------- */ #include "header.h" int32 malloced_bytes=0;                /* Total amount of memory allocated   */ #ifdef PC_QUICKC extern void *my_malloc(int32 size, char *whatfor){   char _huge *c;    if (memout_switch)        printf("Allocating %ld bytes for %s\n",size,whatfor);    if (size==0) return(NULL);    c=(char _huge *)halloc(size,1); malloced_bytes+=size;    if (c==0) memory_out_error(size, 1, whatfor);    return(c);} extern void my_realloc(void *pointer, int32 oldsize, int32 size,     char *whatfor){   char _huge *c;    if (size==0) {        my_free(pointer, whatfor);        return;    }    c=halloc(size,1); malloced_bytes+=size;    if (c==0) memory_out_error(size, 1, whatfor);    if (memout_switch)        printf("Increasing allocation to %ld bytes for %s was (%08lx) \now (%08lx)\n",            (long int) size,whatfor,(long int) (*(int **)pointer),             (long int) c);    memcpy(c, *(int **)pointer, MIN(oldsize, size));    hfree(*(int **)pointer);    *(int **)pointer = c;} extern void *my_calloc(int32 size, int32 howmany, char *whatfor){   void _huge *c;    if (memout_switch)        printf("Allocating %d bytes: array (%ld entries size %ld) for %s\n",            size*howmany,howmany,size,whatfor);    if ((size*howmany) == 0) return(NULL);    c=(void _huge *)halloc(howmany*size,1); malloced_bytes+=size*howmany;    if (c==0) memory_out_error(size, howmany, whatfor);    return(c);} extern void my_recalloc(void *pointer, int32 size, int32 oldhowmany,     int32 howmany, char *whatfor){   void _huge *c;    if (size*howmany==0) {        my_free(pointer, whatfor);        return;    }    c=(void _huge *)halloc(size*howmany,1); malloced_bytes+=size*howmany;    if (c==0) memory_out_error(size, howmany, whatfor);    if (memout_switch)        printf("Increasing allocation to %ld bytes: array (%ld entries size %ld) \for %s was (%08lx) now (%08lx)\n",            ((long int)size) * ((long int)howmany),            (long int)howmany,(long int)size,whatfor,            (long int) *(int **)pointer, (long int) c);    memcpy(c, *(int **)pointer, MIN(size*oldhowmany, size*howmany));    hfree(*(int **)pointer);    *(int **)pointer = c;} #else extern void *my_malloc(int32 size, char *whatfor){   char *c;    if (size==0) return(NULL);    c=malloc((size_t) size); malloced_bytes+=size;    if (c==0) memory_out_error(size, 1, whatfor);    if (memout_switch)        printf("Allocating %ld bytes for %s at (%08lx)\n",            (long int) size,whatfor,(long int) c);    return(c);} extern void my_realloc(void *pointer, int32 oldsize, int32 size,     char *whatfor){   void *c;    if (size==0) {        my_free(pointer, whatfor);        return;    }    c=realloc(*(int **)pointer, (size_t) size); malloced_bytes+=size;    if (c==0) memory_out_error(size, 1, whatfor);    if (memout_switch)        printf("Increasing allocation to %ld bytes for %s was (%08lx) \now (%08lx)\n",            (long int) size,whatfor,(long int) (*(int **)pointer),             (long int) c);    *(int **)pointer = c;} extern void *my_calloc(int32 size, int32 howmany, char *whatfor){   void *c;    if (size*howmany==0) return(NULL);    c=calloc(howmany,(size_t) size); malloced_bytes+=size*howmany;    if (c==0) memory_out_error(size, howmany, whatfor);    if (memout_switch)        printf("Allocating %ld bytes: array (%ld entries size %ld) \for %s at (%08lx)\n",            ((long int)size) * ((long int)howmany),            (long int)howmany,(long int)size,whatfor,            (long int) c);    return(c);} extern void my_recalloc(void *pointer, int32 size, int32 oldhowmany,     int32 howmany, char *whatfor){   void *c;    if (size*howmany==0) {        my_free(pointer, whatfor);        return;    }    c=realloc(*(int **)pointer, (size_t)size*(size_t)howmany);     malloced_bytes+=size*howmany;    if (c==0) memory_out_error(size, howmany, whatfor);    if (memout_switch)        printf("Increasing allocation to %ld bytes: array (%ld entries size %ld) \for %s was (%08lx) now (%08lx)\n",            ((long int)size) * ((long int)howmany),            (long int)howmany,(long int)size,whatfor,            (long int) *(int **)pointer, (long int) c);    *(int **)pointer = c;} #endif extern void my_free(void *pointer, char *whatitwas){    if (*(int **)pointer != NULL)    {   if (memout_switch)            printf("Freeing memory for %s at (%08lx)\n",                whatitwas, (long int) (*(int **)pointer));#ifdef PC_QUICKC        hfree(*(int **)pointer);#else        free(*(int **)pointer);#endif        *(int **)pointer = NULL;    }} /* ------------------------------------------------------------------------- *//*   Extensible blocks of memory, providing a kind of RAM disc as an         *//*   alternative to the temporary files option                               *//* ------------------------------------------------------------------------- */ static char chunk_name_buffer[60];static char *chunk_name(memory_block *MB, int no){   char *p = "(unknown)";    if (MB == &static_strings_area) p = "static strings area";    if (MB == &zcode_area)          p = "Z-code area";    if (MB == &link_data_area)      p = "link data area";    if (MB == &zcode_backpatch_table) p = "Z-code backpatch table";    if (MB == &zmachine_backpatch_table) p = "Z-machine backpatch table";    sprintf(chunk_name_buffer, "%s chunk %d", p, no);    return(chunk_name_buffer);} extern void initialise_memory_block(memory_block *MB){   int i;    MB->chunks = 0;    for (i=0; i<72; i++) MB->chunk[i] = NULL;    MB->extent_of_last = 0;    MB->write_pos = 0;} extern void deallocate_memory_block(memory_block *MB){   int i;    for (i=0; i<72; i++)        if (MB->chunk[i] != NULL)            my_free(&(MB->chunk[i]), chunk_name(MB, i));    MB->chunks = 0;    MB->extent_of_last = 0;} extern int read_byte_from_memory_block(memory_block *MB, int32 index){   uchar *p;    p = MB->chunk[index/ALLOC_CHUNK_SIZE];    if (p == NULL)    {   compiler_error_named("memory: read from unwritten byte in",            chunk_name(MB, index/ALLOC_CHUNK_SIZE));        return 0;    }    return p[index % ALLOC_CHUNK_SIZE];} extern void write_byte_to_memory_block(memory_block *MB, int32 index, int value){   uchar *p; int ch = index/ALLOC_CHUNK_SIZE;    if (ch < 0)    {   compiler_error_named("memory: negative index to", chunk_name(MB, 0));        return;    }    if (ch >= 72) memoryerror("ALLOC_CHUNK_SIZE", ALLOC_CHUNK_SIZE);     if (MB->chunk[ch] == NULL)    {   int i;        MB->chunk[ch] = my_malloc(ALLOC_CHUNK_SIZE, chunk_name(MB, ch));        p = MB->chunk[ch];        for (i=0; i<ALLOC_CHUNK_SIZE; i++) p[i] = 255;    }     p = MB->chunk[ch];    p[index % ALLOC_CHUNK_SIZE] = value;} /* ------------------------------------------------------------------------- *//*   Where the memory settings are declared as variables                     *//* ------------------------------------------------------------------------- */ int MAX_QTEXT_SIZE;int MAX_SYMBOLS;int SYMBOLS_CHUNK_SIZE;int HASH_TAB_SIZE;int MAX_OBJECTS;int MAX_ARRAYS;int MAX_ACTIONS;int MAX_ADJECTIVES;int MAX_DICT_ENTRIES;int MAX_STATIC_DATA;int MAX_PROP_TABLE_SIZE;int MAX_ABBREVS;int MAX_EXPRESSION_NODES;int MAX_VERBS;int MAX_VERBSPACE;int MAX_LABELS;int MAX_LINESPACE;int32 MAX_STATIC_STRINGS;int32 MAX_ZCODE_SIZE;int MAX_LOW_STRINGS;int32 MAX_TRANSCRIPT_SIZE;int MAX_CLASSES;int32 MAX_LINK_DATA_SIZE;int MAX_INCLUSION_DEPTH;int MAX_SOURCE_FILES;int32 MAX_INDIV_PROP_TABLE_SIZE;int32 MAX_OBJ_PROP_TABLE_SIZE;int MAX_OBJ_PROP_COUNT;int MAX_LOCAL_VARIABLES;int MAX_GLOBAL_VARIABLES;int DICT_WORD_SIZE; /* number of characters in a dict word */int DICT_CHAR_SIZE; /* (glulx) 1 for one-byte chars, 4 for Unicode chars */int DICT_WORD_BYTES; /* DICT_WORD_SIZE*DICT_CHAR_SIZE */int ZCODE_HEADER_EXT_WORDS; /* (zcode 1.0) requested header extension size */int ZCODE_HEADER_FLAGS_3; /* (zcode 1.1) value to place in Flags 3 word */int NUM_ATTR_BYTES;int GLULX_OBJECT_EXT_BYTES; /* (glulx) extra bytes for each object record */int32 MAX_NUM_STATIC_STRINGS;int32 MAX_UNICODE_CHARS;int32 MAX_STACK_SIZE;int32 MEMORY_MAP_EXTENSION;int ALLOC_CHUNK_SIZE;int WARN_UNUSED_ROUTINES; /* 0: no, 1: yes except in system files, 2: yes always */int OMIT_UNUSED_ROUTINES; /* 0: no, 1: yes */ /* The way memory sizes are set causes great nuisance for those parameters   which have different defaults under Z-code and Glulx. We have to get   the defaults right whether the user sets "-G $HUGE" or "$HUGE -G".    And an explicit value set by the user should override both defaults. */static int32 MAX_ZCODE_SIZE_z, MAX_ZCODE_SIZE_g;static int MAX_PROP_TABLE_SIZE_z, MAX_PROP_TABLE_SIZE_g;static int MAX_GLOBAL_VARIABLES_z, MAX_GLOBAL_VARIABLES_g;static int MAX_LOCAL_VARIABLES_z, MAX_LOCAL_VARIABLES_g;static int DICT_WORD_SIZE_z, DICT_WORD_SIZE_g;static int NUM_ATTR_BYTES_z, NUM_ATTR_BYTES_g;static int ALLOC_CHUNK_SIZE_z, ALLOC_CHUNK_SIZE_g; /* ------------------------------------------------------------------------- *//*   Memory control from the command line                                    *//* ------------------------------------------------------------------------- */ static void list_memory_sizes(void){   printf("+--------------------------------------+\n");    printf("|  %25s = %-7s |\n","Memory setting","Value");    printf("+--------------------------------------+\n");    printf("|  %25s = %-7d |\n","MAX_ABBREVS",MAX_ABBREVS);    printf("|  %25s = %-7d |\n","MAX_ACTIONS",MAX_ACTIONS);    printf("|  %25s = %-7d |\n","MAX_ADJECTIVES",MAX_ADJECTIVES);    printf("|  %25s = %-7d |\n","ALLOC_CHUNK_SIZE",ALLOC_CHUNK_SIZE);    printf("|  %25s = %-7d |\n","MAX_ARRAYS",MAX_ARRAYS);    printf("|  %25s = %-7d |\n","NUM_ATTR_BYTES",NUM_ATTR_BYTES);    printf("|  %25s = %-7d |\n","MAX_CLASSES",MAX_CLASSES);    printf("|  %25s = %-7d |\n","MAX_DICT_ENTRIES",MAX_DICT_ENTRIES);    printf("|  %25s = %-7d |\n","DICT_WORD_SIZE",DICT_WORD_SIZE);    if (glulx_mode)      printf("|  %25s = %-7d |\n","DICT_CHAR_SIZE",DICT_CHAR_SIZE);    printf("|  %25s = %-7d |\n","MAX_EXPRESSION_NODES",MAX_EXPRESSION_NODES);    printf("|  %25s = %-7d |\n","MAX_GLOBAL_VARIABLES",MAX_GLOBAL_VARIABLES);    printf("|  %25s = %-7d |\n","HASH_TAB_SIZE",HASH_TAB_SIZE);    if (!glulx_mode)      printf("|  %25s = %-7d |\n","ZCODE_HEADER_EXT_WORDS",ZCODE_HEADER_EXT_WORDS);    if (!glulx_mode)      printf("|  %25s = %-7d |\n","ZCODE_HEADER_FLAGS_3",ZCODE_HEADER_FLAGS_3);    printf("|  %25s = %-7d |\n","MAX_INCLUSION_DEPTH",MAX_INCLUSION_DEPTH);    printf("|  %25s = %-7d |\n","MAX_INDIV_PROP_TABLE_SIZE",        MAX_INDIV_PROP_TABLE_SIZE);    printf("|  %25s = %-7d |\n","MAX_LABELS",MAX_LABELS);    printf("|  %25s = %-7d |\n","MAX_LINESPACE",MAX_LINESPACE);    printf("|  %25s = %-7d |\n","MAX_LINK_DATA_SIZE",MAX_LINK_DATA_SIZE);    if (glulx_mode)      printf("|  %25s = %-7d |\n","MAX_LOCAL_VARIABLES",MAX_LOCAL_VARIABLES);    printf("|  %25s = %-7d |\n","MAX_LOW_STRINGS",MAX_LOW_STRINGS);    if (glulx_mode)      printf("|  %25s = %-7d |\n","MEMORY_MAP_EXTENSION",        MEMORY_MAP_EXTENSION);    if (glulx_mode)      printf("|  %25s = %-7d |\n","MAX_NUM_STATIC_STRINGS",        MAX_NUM_STATIC_STRINGS);    printf("|  %25s = %-7d |\n","MAX_OBJECTS",MAX_OBJECTS);    if (glulx_mode)      printf("|  %25s = %-7d |\n","GLULX_OBJECT_EXT_BYTES",        GLULX_OBJECT_EXT_BYTES);    if (glulx_mode)      printf("|  %25s = %-7d |\n","MAX_OBJ_PROP_COUNT",        MAX_OBJ_PROP_COUNT);    if (glulx_mode)      printf("|  %25s = %-7d |\n","MAX_OBJ_PROP_TABLE_SIZE",        MAX_OBJ_PROP_TABLE_SIZE);    printf("|  %25s = %-7d |\n","MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);    printf("|  %25s = %-7d |\n","MAX_QTEXT_SIZE",MAX_QTEXT_SIZE);    printf("|  %25s = %-7d |\n","MAX_SOURCE_FILES",MAX_SOURCE_FILES);    if (glulx_mode)      printf("|  %25s = %-7ld |\n","MAX_STACK_SIZE",           (long int) MAX_STACK_SIZE);    printf("|  %25s = %-7d |\n","MAX_STATIC_DATA",MAX_STATIC_DATA);    printf("|  %25s = %-7ld |\n","MAX_STATIC_STRINGS",           (long int) MAX_STATIC_STRINGS);    printf("|  %25s = %-7d |\n","MAX_SYMBOLS",MAX_SYMBOLS);    printf("|  %25s = %-7d |\n","SYMBOLS_CHUNK_SIZE",SYMBOLS_CHUNK_SIZE);    printf("|  %25s = %-7ld |\n","MAX_TRANSCRIPT_SIZE",           (long int) MAX_TRANSCRIPT_SIZE);    if (glulx_mode)      printf("|  %25s = %-7ld |\n","MAX_UNICODE_CHARS",           (long int) MAX_UNICODE_CHARS);    printf("|  %25s = %-7d |\n","WARN_UNUSED_ROUTINES",WARN_UNUSED_ROUTINES);    printf("|  %25s = %-7d |\n","OMIT_UNUSED_ROUTINES",OMIT_UNUSED_ROUTINES);    printf("|  %25s = %-7d |\n","MAX_VERBS",MAX_VERBS);    printf("|  %25s = %-7d |\n","MAX_VERBSPACE",MAX_VERBSPACE);    printf("|  %25s = %-7ld |\n","MAX_ZCODE_SIZE",           (long int) MAX_ZCODE_SIZE);    printf("+--------------------------------------+\n");} extern void set_memory_sizes(int size_flag){    if (size_flag == HUGE_SIZE)    {        MAX_QTEXT_SIZE  = 4000;        MAX_SYMBOLS     = 10000;         SYMBOLS_CHUNK_SIZE = 5000;        HASH_TAB_SIZE      = 512;         MAX_OBJECTS = 640;         MAX_ACTIONS      = 200;        MAX_ADJECTIVES   = 50;        MAX_DICT_ENTRIES = 2000;        MAX_STATIC_DATA  = 10000;         MAX_PROP_TABLE_SIZE_z = 30000;        MAX_PROP_TABLE_SIZE_g = 60000;         MAX_ABBREVS = 64;         MAX_EXPRESSION_NODES = 100;        MAX_VERBS = 200;        MAX_VERBSPACE = 4096;        MAX_LABELS = 1000;        MAX_LINESPACE = 16000;         MAX_STATIC_STRINGS = 8000;        MAX_ZCODE_SIZE_z = 20000;        MAX_ZCODE_SIZE_g = 40000;        MAX_LINK_DATA_SIZE = 2000;         MAX_LOW_STRINGS = 2048;         MAX_TRANSCRIPT_SIZE = 200000;        MAX_NUM_STATIC_STRINGS = 20000;         MAX_CLASSES = 64;         MAX_OBJ_PROP_COUNT = 128;        MAX_OBJ_PROP_TABLE_SIZE = 4096;         MAX_INDIV_PROP_TABLE_SIZE = 15000;        MAX_ARRAYS = 128;         MAX_GLOBAL_VARIABLES_z = 240;        MAX_GLOBAL_VARIABLES_g = 512;                ALLOC_CHUNK_SIZE_z = 8192;        ALLOC_CHUNK_SIZE_g = 32768;    }    if (size_flag == LARGE_SIZE)    {        MAX_QTEXT_SIZE  = 4000;        MAX_SYMBOLS     = 6400;         SYMBOLS_CHUNK_SIZE = 5000;        HASH_TAB_SIZE      = 512;         MAX_OBJECTS = 512;         MAX_ACTIONS      = 200;        MAX_ADJECTIVES   = 50;        MAX_DICT_ENTRIES = 1300;        MAX_STATIC_DATA  = 10000;         MAX_PROP_TABLE_SIZE_z = 15000;        MAX_PROP_TABLE_SIZE_g = 30000;         MAX_ABBREVS = 64;         MAX_EXPRESSION_NODES = 100;        MAX_VERBS = 140;        MAX_VERBSPACE = 4096;        MAX_LINESPACE = 10000;         MAX_LABELS = 1000;        MAX_STATIC_STRINGS = 8000;        MAX_ZCODE_SIZE_z = 20000;        MAX_ZCODE_SIZE_g = 40000;        MAX_LINK_DATA_SIZE = 2000;         MAX_LOW_STRINGS = 2048;         MAX_TRANSCRIPT_SIZE = 200000;        MAX_NUM_STATIC_STRINGS = 20000;         MAX_CLASSES = 64;         MAX_OBJ_PROP_COUNT = 64;        MAX_OBJ_PROP_TABLE_SIZE = 2048;         MAX_INDIV_PROP_TABLE_SIZE = 10000;        MAX_ARRAYS = 128;         MAX_GLOBAL_VARIABLES_z = 240;        MAX_GLOBAL_VARIABLES_g = 512;                ALLOC_CHUNK_SIZE_z = 8192;        ALLOC_CHUNK_SIZE_g = 16384;    }    if (size_flag == SMALL_SIZE)    {        MAX_QTEXT_SIZE  = 4000;        MAX_SYMBOLS     = 3000;         SYMBOLS_CHUNK_SIZE = 2500;        HASH_TAB_SIZE      = 512;         MAX_OBJECTS = 300;         MAX_ACTIONS      = 200;        MAX_ADJECTIVES   = 50;        MAX_DICT_ENTRIES = 700;        MAX_STATIC_DATA  = 10000;         MAX_PROP_TABLE_SIZE_z = 8000;        MAX_PROP_TABLE_SIZE_g = 16000;         MAX_ABBREVS = 64;         MAX_EXPRESSION_NODES = 40;        MAX_VERBS = 110;        MAX_VERBSPACE = 2048;        MAX_LINESPACE = 10000;        MAX_LABELS = 1000;         MAX_STATIC_STRINGS = 8000;        MAX_ZCODE_SIZE_z = 10000;        MAX_ZCODE_SIZE_g = 20000;        MAX_LINK_DATA_SIZE = 1000;         MAX_LOW_STRINGS = 1024;         MAX_TRANSCRIPT_SIZE = 100000;        MAX_NUM_STATIC_STRINGS = 10000;         MAX_CLASSES = 32;         MAX_OBJ_PROP_COUNT = 64;        MAX_OBJ_PROP_TABLE_SIZE = 1024;         MAX_INDIV_PROP_TABLE_SIZE = 5000;

Lines 1–500 of 1112.