diff options
author | Torbjörn Andersson | 2003-09-17 06:28:27 +0000 |
---|---|---|
committer | Torbjörn Andersson | 2003-09-17 06:28:27 +0000 |
commit | 2536823c23d5bc7cb5a376eefc3444f1b8d3b485 (patch) | |
tree | 69064e07215385d06348696488c34e7732036903 /sword2 | |
parent | dffa1cbc2f48e36376e016f23ce79dfed4d67a97 (diff) | |
download | scummvm-rg350-2536823c23d5bc7cb5a376eefc3444f1b8d3b485.tar.gz scummvm-rg350-2536823c23d5bc7cb5a376eefc3444f1b8d3b485.tar.bz2 scummvm-rg350-2536823c23d5bc7cb5a376eefc3444f1b8d3b485.zip |
cleanup
svn-id: r10267
Diffstat (limited to 'sword2')
-rw-r--r-- | sword2/layers.cpp | 291 | ||||
-rw-r--r-- | sword2/layers.h | 58 | ||||
-rw-r--r-- | sword2/mem_view.cpp | 236 | ||||
-rw-r--r-- | sword2/mem_view.h | 10 | ||||
-rw-r--r-- | sword2/memory.cpp | 737 | ||||
-rw-r--r-- | sword2/memory.h | 78 | ||||
-rw-r--r-- | sword2/sync.cpp | 198 | ||||
-rw-r--r-- | sword2/sync.h | 9 |
8 files changed, 763 insertions, 854 deletions
diff --git a/sword2/layers.cpp b/sword2/layers.cpp index 9dfa9ad121..187f78c480 100644 --- a/sword2/layers.cpp +++ b/sword2/layers.cpp @@ -17,55 +17,44 @@ * $Header$ */ -//------------------------------------------------------------------------------------ -//high level layer initialising +// high level layer initialising -//the system supports: -// 1 optional background parallax layer -// 1 not optional normal backdrop layer -// 3 normal sorted layers -// up to 2 foreground parallax layers - -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> +// the system supports: +// 1 optional background parallax layer +// 1 not optional normal backdrop layer +// 3 normal sorted layers +// up to 2 foreground parallax layers #include "stdafx.h" -#include "driver/driver96.h" #include "build_display.h" -#include "console.h" #include "debug.h" #include "header.h" #include "layers.h" -#include "memory.h" -#include "object.h" #include "protocol.h" #include "resman.h" #include "sound.h" // (James22july97) for Clear_fx_queue() called from FN_init_background() -//------------------------------------------------------------------------------------ +// this_screen describes the current back buffer and its in-game scroll +// positions, etc. +screen_info this_screen; -screen_info this_screen; //this_screen describes the current back buffer and its in-game scroll positions, etc. -//------------------------------------------------------------------------------------ -int32 FN_init_background(int32 *params) //Tony11Sept96 -{ -//param 0 res id of normal background layer - cannot be 0 -//param 1 1 yes 0 no for a new palette -//this screen defines the size of the back buffer +int32 FN_init_background(int32 *params) { // Tony11Sept96 + // param 0 res id of normal background layer - cannot be 0 + // param 1 1 yes 0 no for a new palette + // this screen defines the size of the back buffer _multiScreenHeader *screenLayerTable; // James 06feb97 - _screenHeader *screen_head; - _layerHeader *layer; - _spriteInfo spriteInfo; - uint32 j; - uint8 *file; - uint32 rv; - + _screenHeader *screen_head; + _layerHeader *layer; + _spriteInfo spriteInfo; + uint32 j; + uint8 *file; + uint32 rv; +#ifdef _SWORD2_DEBUG //-------------------------------------- // Write to walkthrough file (zebug0.txt) - #ifdef _SWORD2_DEBUG Zdebug(0,"====================================="); Zdebug(0,"CHANGED TO LOCATION \"%s\"", FetchObjectName(*params)); Zdebug(0,"====================================="); @@ -74,222 +63,192 @@ int32 FN_init_background(int32 *params) //Tony11Sept96 Zdebug("====================================="); Zdebug("CHANGED TO LOCATION \"%s\"", FetchObjectName(*params)); Zdebug("====================================="); - #endif //-------------------------------------- +#endif - Clear_fx_queue(); // stops all fx & clears the queue (James22july97) - + // stop all fx & clears the queue (James22july97) + Clear_fx_queue(); #ifdef _SWORD2_DEBUG Zdebug("FN_init_background(%d)", *params); - if (!*params) - { - Con_fatal_error("ERROR: FN_set_background cannot have 0 for background layer id! (%s line=%u)",__FILE__,__LINE__); + if (!*params) { + Con_fatal_error("ERROR: FN_set_background cannot have 0 for background layer id! (%s line=%u)", __FILE__, __LINE__); } -#endif // _SWORD2_DEBUG +#endif - - //------------------------------------------------------- // if the screen is still fading down then wait for black WaitForFade(); - //------------------------------------------------------- - - if (this_screen.mask_flag) // if last screen was using a shading mask (see below) (James 08apr97) - { + // if last screen was using a shading mask (see below) (James 08apr97) + if (this_screen.mask_flag) { rv = CloseLightMask(); - if (rv) ExitWithReport("Driver Error %.8x [%s line %u]", rv, __FILE__, __LINE__); } - //-------------------------------------------------------- // New stuff for faster screen drivers (James 06feb97) - if (this_screen.background_layer_id) // for drivers: close the previous screen if one is open + // for drivers: close the previous screen if one is open + if (this_screen.background_layer_id) CloseBackgroundLayer(); - //-------------------------------------------------------- - + this_screen.background_layer_id = *params; // set the res id + this_screen.new_palette = *(params + 1); // yes or no - palette is taken from layer file - this_screen.background_layer_id=*params; //set the res id - this_screen.new_palette = *(params+1); //yes or no - palette is taken from layer file + // ok, now read the resource and pull out all the normal sort layer + // info/and set them up at the beginning of the sort list - why do it + // each cycle - -//ok, now read the resource and pull out all the normal sort layer info -//and set them up at the beginning of the sort list - why do it each cycle - - - file = res_man.Res_open(this_screen.background_layer_id); //file points to 1st byte in the layer file + // file points to 1st byte in the layer file + file = res_man.Res_open(this_screen.background_layer_id); screen_head = FetchScreenHeader(file); - this_screen.number_of_layers= screen_head->noLayers; //set number of special sort layers + //set number of special sort layers + this_screen.number_of_layers = screen_head->noLayers; this_screen.screen_wide = screen_head->width; this_screen.screen_deep = screen_head->height; Zdebug("res test layers=%d width=%d depth=%d", screen_head->noLayers, screen_head->width, screen_head->height); - SetLocationMetrics(screen_head->width, screen_head->height); //initialise the driver back buffer - + //initialise the driver back buffer + SetLocationMetrics(screen_head->width, screen_head->height); - if (screen_head->noLayers) - for (j=0;j<screen_head->noLayers;j++) - { - layer=FetchLayerHeader(file,j); //get layer header for layer j + if (screen_head->noLayers) { + for (j = 0; j < screen_head->noLayers; j++) { + // get layer header for layer j + layer = FetchLayerHeader(file, j); -// add into the sort list + // add into the sort list - sort_list[j].sort_y = layer->y+layer->height; //need this for sorting - but leave the rest blank, we'll take from the header at print time - sort_list[j].layer_number=j+1; //signifies a layer + // need this for sorting - but leave the rest blank, + // we'll take from the header at print time + sort_list[j].sort_y = layer->y + layer->height; + // signifies a layer + sort_list[j].layer_number = j + 1; Zdebug("init layer %d", j); } + } + // using the screen size setup the scrolling variables + // if layer is larger than physical screen + if (screen_head->width > screenWide || screen_head->height > screenDeep) { + // switch on scrolling (2 means first time on screen) + this_screen.scroll_flag = 2; -//using the screen size setup the scrolling variables - - if( ((screen_head->width) > screenWide) || (screen_head->height>screenDeep) ) // if layer is larger than physical screen - { - this_screen.scroll_flag = 2; //switch on scrolling (2 means first time on screen) + // note, if we've already set the player up then we could do + // the initial scroll set here -// note, if we've already set the player up then we could do the initial scroll set here + // reset scroll offsets - this_screen.scroll_offset_x = 0; //reset scroll offsets + this_screen.scroll_offset_x = 0; this_screen.scroll_offset_y = 0; -// calc max allowed offsets (to prevent scrolling off edge) - MOVE TO NEW_SCREEN in GTM_CORE.C !! - this_screen.max_scroll_offset_x = screen_head->width-screenWide; // NB. min scroll offsets are both zero - this_screen.max_scroll_offset_y = screen_head->height-(screenDeep-(RDMENU_MENUDEEP*2)); // 'screenDeep' includes the menu's, so take away 80 pixels - } - else //layer fits on physical screen - scrolling not required - { - this_screen.scroll_flag = 0; //switch off scrolling - this_screen.scroll_offset_x = 0; //reset scroll offsets + // calc max allowed offsets (to prevent scrolling off edge) - + // MOVE TO NEW_SCREEN in GTM_CORE.C !! + // NB. min scroll offsets are both zero + this_screen.max_scroll_offset_x = screen_head->width-screenWide; + // 'screenDeep' includes the menu's, so take away 80 pixels + this_screen.max_scroll_offset_y = screen_head->height - (screenDeep - (RDMENU_MENUDEEP * 2)); + } else { + // layer fits on physical screen - scrolling not required + this_screen.scroll_flag = 0; // switch off scrolling + this_screen.scroll_offset_x = 0; // reset scroll offsets this_screen.scroll_offset_y = 0; } - ResetRenderEngine(); //no inter-cycle scrol between new screens (see setScrollTarget in build display) + // no inter-cycle scroll between new screens (see setScrollTarget in + // build display) + ResetRenderEngine(); // these are the physical screen coords where the system // will try to maintain George's actual feet coords - this_screen.feet_x=320; - this_screen.feet_y=340; + this_screen.feet_x = 320; + this_screen.feet_y = 340; - - //---------------------------------------------------- // shading mask screenLayerTable = (_multiScreenHeader *) ((uint8 *) file + sizeof(_standardHeader)); - if (screenLayerTable->maskOffset) - { - spriteInfo.x = 0; - spriteInfo.y = 0; - spriteInfo.w = screen_head->width; - spriteInfo.h = screen_head->height; - spriteInfo.scale = 0; - spriteInfo.scaledWidth = 0; - spriteInfo.scaledHeight = 0; - spriteInfo.type = 0; - spriteInfo.blend = 0; - spriteInfo.data = FetchShadingMask(file); - spriteInfo.colourTable = 0; - - rv = OpenLightMask( &spriteInfo ); + if (screenLayerTable->maskOffset) { + spriteInfo.x = 0; + spriteInfo.y = 0; + spriteInfo.w = screen_head->width; + spriteInfo.h = screen_head->height; + spriteInfo.scale = 0; + spriteInfo.scaledWidth = 0; + spriteInfo.scaledHeight = 0; + spriteInfo.type = 0; + spriteInfo.blend = 0; + spriteInfo.data = FetchShadingMask(file); + spriteInfo.colourTable = 0; + + rv = OpenLightMask(&spriteInfo); if (rv) ExitWithReport("Driver Error %.8x [%s line %u]", rv, __FILE__, __LINE__); - this_screen.mask_flag=1; // so we know to close it later! (see above) + // so we know to close it later! (see above) + this_screen.mask_flag = 1; + } else { + // no need to close a mask later + this_screen.mask_flag = 0; } - else - this_screen.mask_flag=0; // no need to close a mask later - - //---------------------------------------------------- - res_man.Res_close(this_screen.background_layer_id); //close the screen file + // close the screen file + res_man.Res_close(this_screen.background_layer_id); SetUpBackgroundLayers(); - Zdebug("end init"); - return(1); + return 1; } -//------------------------------------------------------------------------------------ + // called from FN_init_background & also from control panel -void SetUpBackgroundLayers(void) // James(13jun97) -{ +void SetUpBackgroundLayers(void) { // James(13jun97) _multiScreenHeader *screenLayerTable; // James 06feb97 - _screenHeader *screen_head; - uint8 *file; + _screenHeader *screen_head; + uint8 *file; + int i; - - if (this_screen.background_layer_id) // if we actually have a screen to initialise (in case called from control panel) - { - //------------------------------ + // if we actually have a screen to initialise (in case not called from + // control panel) + if (this_screen.background_layer_id) { // open resource & set pointers to headers + // file points to 1st byte in the layer file - file = res_man.Res_open(this_screen.background_layer_id); //file points to 1st byte in the layer file + file = res_man.Res_open(this_screen.background_layer_id); screen_head = FetchScreenHeader(file); screenLayerTable = (_multiScreenHeader *) ((uint8 *) file + sizeof(_standardHeader)); - //------------------------------ - // first background parallax - - if (screenLayerTable->bg_parallax[0]) - InitialiseBackgroundLayer(FetchBackgroundParallaxLayer(file,0)); - else - InitialiseBackgroundLayer(NULL); + // Background parallax layers - //------------------------------ - // second background parallax - - if (screenLayerTable->bg_parallax[1]) - InitialiseBackgroundLayer(FetchBackgroundParallaxLayer(file,1)); - else - InitialiseBackgroundLayer(NULL); + for (i = 0; i < 2; i++) { + if (screenLayerTable->bg_parallax[i]) + InitialiseBackgroundLayer(FetchBackgroundParallaxLayer(file, i)); + else + InitialiseBackgroundLayer(NULL); + } - //------------------------------ - // normal backround layer + // Normal backround layer InitialiseBackgroundLayer(FetchBackgroundLayer(file)); - //------------------------------ - // first foreground parallax + // Foreground parallax layers - if (screenLayerTable->fg_parallax[0]) - InitialiseBackgroundLayer(FetchForegroundParallaxLayer(file,0)); - else - InitialiseBackgroundLayer(NULL); - - //------------------------------ - // second foreground parallax - - if (screenLayerTable->fg_parallax[1]) - InitialiseBackgroundLayer(FetchForegroundParallaxLayer(file,1)); - else - InitialiseBackgroundLayer(NULL); - - //---------------------------------------------------- - - res_man.Res_close(this_screen.background_layer_id); //close the screen file + for (i = 0; i < 2; i++) { + if (screenLayerTable->fg_parallax[i]) + InitialiseBackgroundLayer(FetchForegroundParallaxLayer(file, i)); + else + InitialiseBackgroundLayer(NULL); + } - //---------------------------------------------------- - } - else // no current screen to initialise! (In case called from control panel) - { + // close the screen file + res_man.Res_close(this_screen.background_layer_id); } } - -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ diff --git a/sword2/layers.h b/sword2/layers.h index 8bc73b7dda..daf15926ce 100644 --- a/sword2/layers.h +++ b/sword2/layers.h @@ -20,42 +20,40 @@ #ifndef _LAYERS #define _LAYERS -//#include "src\driver96.h" #include "memory.h" - - - - - - - -typedef struct -{ - uint16 scroll_offset_x; // position x - uint16 scroll_offset_y; // position y - uint16 max_scroll_offset_x; // calc'ed in FN_init_background - uint16 max_scroll_offset_y; // - int16 player_feet_x; // feet coordinates to use - cant just fetch the player compact anymore - int16 player_feet_y; - int16 feet_x; // special offset-to-player position - tweek as desired - always set in screen manager object startup - int16 feet_y; - uint16 screen_wide; // size of background layer - hense size of back buffer itself (Paul actually malloc's it) - uint16 screen_deep; - uint32 background_layer_id; //id of the normal background layer - uint16 number_of_layers; // from the header of the main background layer - uint8 new_palette; // set to non zero to start the palette held within layer file fading up after a build_display - uint8 scroll_flag; // scroll mode 0 off 1 on - uint8 mask_flag; // using shading mask +typedef struct { + uint16 scroll_offset_x; // position x + uint16 scroll_offset_y; // position y + uint16 max_scroll_offset_x; // calc'ed in FN_init_background + uint16 max_scroll_offset_y; + // feet coordinates to use - cant just fetch the player compact anymore + int16 player_feet_x; + int16 player_feet_y; + // special offset-to-player position - tweek as desired - always set + // in screen manager object startup + int16 feet_x; + int16 feet_y; + // size of background layer - hence size of back buffer itself (Paul + // actually malloc's it) + uint16 screen_wide; + uint16 screen_deep; + uint32 background_layer_id; // id of the normal background layer + // from the header of the main background layer + uint16 number_of_layers; + // set to non zero to start the palette held within layer file fading + // up after a build_display + uint8 new_palette; + uint8 scroll_flag; // scroll mode 0 off 1 on + uint8 mask_flag; // using shading mask } screen_info; - -extern screen_info this_screen; - +extern screen_info this_screen; int32 FN_init_background(int32 *params); // Tony11Sept96 -void SetUpBackgroundLayers(void); // James(13jun97) called from control panel (as well as inside FN_init_background) - +// James(13jun97) +// called from control panel (as well as inside FN_init_background) +void SetUpBackgroundLayers(void); #endif diff --git a/sword2/mem_view.cpp b/sword2/mem_view.cpp index 3f50b04b9d..ceeee39aef 100644 --- a/sword2/mem_view.cpp +++ b/sword2/mem_view.cpp @@ -17,210 +17,159 @@ * $Header$ */ -//-------------------------------------------------------------------------------------- -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -//#include <windows.h> - #include "stdafx.h" -#include "driver/driver96.h" #include "build_display.h" #include "console.h" #include "debug.h" -#include "defs.h" #include "header.h" -#include "layers.h" #include "mem_view.h" #include "memory.h" #include "resman.h" #include "sword2.h" // (James11aug97) for CloseGame() -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- - -char buf[50]; //has to be global because a local in Fetch_mem_owner is destroyed on exit -//-------------------------------------------------------------------------------------- -void Console_mem_display(void) //Tony13Aug96 -{ -// - int pass,found_end,k,j,free=0; +// has to be global because a local in Fetch_mem_owner is destroyed on exit +char buf[50]; + +void Console_mem_display(void) { // Tony13Aug96 + int pass, found_end, k, j, free = 0; _standardHeader *file_header; - int scrolls=0; - char c; - - char inf[][20]= - { - {"M_null "}, - {"M_free "}, - {"M_locked"}, - {"M_float "} + int scrolls = 0; + char c; + + char inf[][20] = { + { "M_null " }, + { "M_free " }, + { "M_locked" }, + { "M_float " } }; - - j=base_mem_block; - do - { - - if (mem_list[j].uid<65536) - { + j = base_mem_block; + do { + if (mem_list[j].uid < 65536) { file_header = (_standardHeader*) res_man.Res_open(mem_list[j].uid); - res_man.Res_close(mem_list[j].uid); //close immediately so give a true count + // close immediately so give a true count + res_man.Res_close(mem_list[j].uid); Zdebug("view %d", mem_list[j].uid); + pass = 0; + found_end = 0; - pass=0; - found_end=0; - - for (k=0;k<30;k++) - { - if (file_header->name[k]==0) - { found_end=1; + for (k = 0; k < 30; k++) { + if (file_header->name[k] == 0) { + found_end = 1; break; } - if ( (file_header->name[k]<32)||(file_header->name[k]>'z')) - pass=1; - + if (file_header->name[k] < ' ' || file_header->name[k] > 'z') + pass = 1; } - if (file_header->name[0]==0) - pass=1; //also illegal - - - if ((!pass)&&(found_end)) //&&(file_header->fileType<10)) - Print_to_console("%d %s, size 0x%.5x (%dk %d%%), res %d %s %s, A%d, C%d", j, - inf[mem_list[j].state], - mem_list[j].size, mem_list[j].size/1024, (mem_list[j].size*100)/total_free_memory, mem_list[j].uid, + if (file_header->name[0] == 0) + pass = 1; // also illegal + if (!pass && found_end) { // && file_header->fileType < 10) + Print_to_console("%d %s, size 0x%.5x (%dk %d%%), res %d %s %s, A%d, C%d", + j, inf[mem_list[j].state], + mem_list[j].size, + mem_list[j].size / 1024, + (mem_list[j].size * 100) / total_free_memory, + mem_list[j].uid, res_man.Fetch_cluster(mem_list[j].uid), file_header->name, res_man.Fetch_age(mem_list[j].uid), res_man.Fetch_count(mem_list[j].uid)); - - else Print_to_console(" %d is an illegal resource", mem_list[j].uid); - + } else + Print_to_console(" %d is an illegal resource", mem_list[j].uid); + } else { + Print_to_console("%d %s, size 0x%.5x (%dk %d%%), %s", + j, inf[mem_list[j].state], mem_list[j].size, + mem_list[j].size / 1024, + (mem_list[j].size * 100) / total_free_memory, + Fetch_mem_owner(mem_list[j].uid)); } - else - Print_to_console("%d %s, size 0x%.5x (%dk %d%%), %s", j, - inf[mem_list[j].state], - mem_list[j].size, mem_list[j].size/1024, (mem_list[j].size*100)/total_free_memory, - Fetch_mem_owner(mem_list[j].uid) ); - - if (mem_list[j].state==MEM_free) - free+=mem_list[j].size; + if (mem_list[j].state == MEM_free) + free += mem_list[j].size; - j=mem_list[j].child; + j = mem_list[j].child; scrolls++; Build_display(); - if (scrolls==18) - { + if (scrolls==18) { Temp_print_to_console("- Press ESC to stop or any other key to continue"); Build_display(); - do - { - // Service windows + do { ServiceWindows(); - } - while(!KeyWaiting()); + } while(!KeyWaiting()); ReadKey(&c); //kill the key we just pressed - if (c==27) //ESC + if (c == 27) //ESC break; - Clear_console_line(); //clear the Press Esc message ready for the new line - scrolls=0; + // clear the Press Esc message ready for the new line + Clear_console_line(); + scrolls = 0; } - } - while (j!=-1); + } while (j != -1); Scroll_console(); - Print_to_console("(total memory block 0x%.8x %dk %dMB) %d / %d%% free", total_free_memory, - total_free_memory/1024, - total_free_memory/(1000*1024), - free, - (free*100)/total_free_memory); - - + Print_to_console("(total memory block 0x%.8x %dk %dMB) %d / %d%% free", + total_free_memory, total_free_memory / 1024, + total_free_memory / (1000 * 1024), free, + (free * 100) / total_free_memory); } -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- -const char *Fetch_mem_owner(uint32 uid) //Tony3June96 -{ - switch(uid) - { - case UID_memman: - return("MEMMAN"); - break; +const char *Fetch_mem_owner(uint32 uid) { //Tony3June96 + switch(uid) { + case UID_memman: + return "MEMMAN"; - case UID_font: - return("font"); - break; + case UID_font: + return "font"; - case UID_temp: - return("temp ram allocation"); - break; + case UID_temp: + return "temp ram allocation"; - case UID_decompression_buffer: - return("decompression buffer"); - break; + case UID_decompression_buffer: + return "decompression buffer"; - case UID_shrink_buffer: - return("shrink buffer"); - break; + case UID_shrink_buffer: + return "shrink buffer"; - case UID_con_sprite: - return("console sprite buffer"); - break; + case UID_con_sprite: + return "console sprite buffer"; - case UID_text_sprite: - return("text sprite"); - break; + case UID_text_sprite: + return "text sprite"; - case UID_walk_anim: - return("walk anim"); - break; + case UID_walk_anim: + return "walk anim"; - case UID_savegame_buffer: - return("savegame buffer"); - break; + case UID_savegame_buffer: + return "savegame buffer"; default: sprintf(buf, "<sob> %d?", uid); - return(buf); - break; + return buf; } } -//-------------------------------------------------------------------------------------- -void Create_mem_string( char *string ) // James (21oct96 updated 4dec96) -{ - int blockNo = base_mem_block; - int blocksUsed=0; - int mem_free=0; - int mem_locked=0; - int mem_floating=0; - int memUsed=0; - int percent; - - - - while (blockNo != -1) - { - switch (mem_list[blockNo].state) - { +void Create_mem_string(char *string) { // James (21oct96 updated 4dec96) + int blockNo = base_mem_block; + int blocksUsed = 0; + int mem_free = 0; + int mem_locked = 0; + int mem_floating = 0; + int memUsed = 0; + int percent; + + while (blockNo != -1) { + switch (mem_list[blockNo].state) { case MEM_free: mem_free++; break; @@ -242,7 +191,8 @@ void Create_mem_string( char *string ) // James (21oct96 updated 4dec96) percent = (memUsed * 100) / total_free_memory; - sprintf( string, "locked(%u)+float(%u)+free(%u) = %u/%u blocks (%u%% used)(cur %uk)", mem_locked, mem_floating, mem_free, blocksUsed, MAX_mem_blocks, percent, (res_man.Res_fetch_useage()/1024) ); + sprintf(string, + "locked(%u)+float(%u)+free(%u) = %u/%u blocks (%u%% used)(cur %uk)", + mem_locked, mem_floating, mem_free, blocksUsed, MAX_mem_blocks, + percent, (res_man.Res_fetch_useage() / 1024)); } -//-------------------------------------------------------------------------------------- -//-------------------------------------------------------------------------------------- diff --git a/sword2/mem_view.h b/sword2/mem_view.h index 84fa5667d4..f8047c7f78 100644 --- a/sword2/mem_view.h +++ b/sword2/mem_view.h @@ -20,12 +20,8 @@ #ifndef MEMVIEW_H #define MEMVIEW_H -//#include "src\driver96.h" - - -const char *Fetch_mem_owner(uint32 uid); -void Console_mem_display(void); // Tony (13Aug96) -void Create_mem_string( char *string ); // James (21oct96 updated 4dec96) - +const char *Fetch_mem_owner(uint32 uid); +void Console_mem_display(void); // Tony (13Aug96) +void Create_mem_string(char *string); // James (21oct96 updated 4dec96) #endif diff --git a/sword2/memory.cpp b/sword2/memory.cpp index 8ae7c77989..af581b2e77 100644 --- a/sword2/memory.cpp +++ b/sword2/memory.cpp @@ -17,412 +17,488 @@ * $Header$ */ -//memory manager - "remember, it's not good to leave memory locked for a moment longer than necessary" Tony -// "actually, in a sequential system theoretically you never need to lock any memory!" Chris ;) +// FIXME: We should investigate which problem all this memory managing stuff +// is trying to solve. I'm not convinced that it's really needed. + +// memory manager +// - "remember, it's not good to leave memory locked for a moment longer +// than necessary" Tony +// - "actually, in a sequential system theoretically you never need to lock +// any memory!" Chris ;) // -// This is a very simple implementation but I see little advantage to being any cleverer -// with the coding - i could have put the mem blocks before the defined blocks instead -// of in an array and then used pointers to child/parent blocks. But why bother? I've Kept it simple. -// When it needs updating or customising it will be accessable to anyone who looks at it. -// *doesn't have a purgeable/age consituant yet - if anyone wants this then I'll add it in. - - -// MemMan v1.1 +// This is a very simple implementation but I see little advantage to being +// any cleverer with the coding - i could have put the mem blocks before the +// defined blocks instead of in an array and then used pointers to +// child/parent blocks. But why bother? I've Kept it simple. When it needs +// updating or customising it will be accessable to anyone who looks at it. +// +// Doesn't have a purgeable/age consituant yet - if anyone wants this then +// I'll add it in. -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> +// MemMan v1.1 #include "stdafx.h" -#include "driver/driver96.h" -#include "console.h" #include "debug.h" #include "memory.h" #include "resman.h" +uint32 total_blocks; +uint32 total_free_memory; + +#define MEMORY_POOL (1024 * 12000) -uint32 total_blocks; -uint32 base_mem_block; -uint32 total_free_memory; -uint8 *free_memman; //address of init malloc to be freed later +// address of init malloc to be freed later +uint8 *free_memman; -//#define MEMDEBUG 1 +// list of defined memory handles - each representing a block of memory. +mem mem_list[MAX_mem_blocks]; -mem mem_list[MAX_mem_blocks]; //list of defined memory handles - each representing a block of memory. +uint32 base_mem_block; -int32 VirtualDefrag( uint32 size ); // Used to determine if the required size can be obtained if the defragger is allowed to run. -int32 suggestedStart = 0; // Start position of the Defragger as indicated by its sister VirtualDefrag. +// #define MEMDEBUG 1 -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -void Close_memory_manager(void) //Tony2Oct96 -{ +// Used to determine if the required size can be obtained if the defragger is +// allowed to run. + +int32 VirtualDefrag(uint32 size); + +// Start position of the Defragger as indicated by its sister VirtualDefrag. +int32 suggestedStart = 0; + +void Close_memory_manager(void) { // Tony2Oct96 free(free_memman); } -//------------------------------------------------------------------------------------ -void Init_memory_manager(void) //Tony9April96 -{ - uint32 j; - uint8 *memory_base; - total_free_memory=12000*1024; //12MB +void Init_memory_manager(void) { // Tony9April96 + uint32 j; + uint8 *memory_base; + total_free_memory = MEMORY_POOL; - //malloc memory and adjust for long boundaries - memory_base = (uint8 *) malloc(total_free_memory); + // malloc memory and adjust for long boundaries + memory_base = (uint8 *) malloc(total_free_memory); - if (!memory_base) //could not grab the memory - { + if (!memory_base) { //could not grab the memory Zdebug("couldn't malloc %d in Init_memory_manager", total_free_memory); - ExitWithReport("Init_memory_manager() couldn't malloc %d bytes [line=%d file=%s]",total_free_memory,__LINE__,__FILE__); + ExitWithReport("Init_memory_manager() couldn't malloc %d bytes [line=%d file=%s]", total_free_memory, __LINE__, __FILE__); } - free_memman = memory_base; //the original malloc address - -//force to long word boundary - memory_base+=3; - memory_base = (uint8 *)((uint32)memory_base & 0xfffffffc); // ** was (int)memory_base -// total_free_memory-=3; //play safe + // the original malloc address + free_memman = memory_base; +#if 0 + // FIXME: I don't think it's necessary to force alignment here, + // because memory_base is the address returned by malloc(), and + // according to my C book "every allocated region from malloc must + // be aligned for any type". + // force to long word boundary + memory_base += 3; + memory_base = (uint8 *) ((uint32) memory_base & 0xfffffffc); // ** was (int)memory_base + // total_free_memory -= 3; //play safe +#endif -//set all but first handle to unused - for (j=1;j<MAX_mem_blocks;j++) - mem_list[j].state=MEM_null; + // set all but first handle to unused + for (j = 1; j < MAX_mem_blocks; j++) + mem_list[j].state = MEM_null; - - total_blocks=1; //total used (free, locked or floating) + // total used (free, locked or floating) + total_blocks = 1; mem_list[0].ad = memory_base; - mem_list[0].state= MEM_free; - mem_list[0].age=0; - mem_list[0].size=total_free_memory; - mem_list[0].parent=-1; //we are base - for now - mem_list[0].child=-1; //we are the end as well - mem_list[0].uid=UID_memman; //init id - - base_mem_block=0; //for now + mem_list[0].state = MEM_free; + mem_list[0].age = 0; + mem_list[0].size = total_free_memory; + mem_list[0].parent = -1; // we are base - for now + mem_list[0].child = -1; // we are the end as well + mem_list[0].uid = UID_memman; // init id + + base_mem_block = 0; // for now } -//------------------------------------------------------------------------------------ -mem *Talloc(uint32 size, uint32 type, uint32 unique_id) //Tony10Apr96 -{ -//allocate a block of memory - locked or float - -// returns 0 if fails to allocate the memory -// or a pointer to a mem structure - int32 nu_block; - uint32 spawn=0; - uint32 slack; +// This is the low-level memory allocator +mem *Talloc(uint32 size, uint32 type, uint32 unique_id) { // Tony10Apr96 + // allocate a block of memory - locked or float + // returns 0 if fails to allocate the memory + // or a pointer to a mem structure + int32 nu_block; + uint32 spawn = 0; + uint32 slack; + // we must first round the size UP to a dword, so subsequent blocks + // will start dword alligned -//we must first round the size UP to a dword, so subsequent blocks will start dword alligned - size+=3; //move up - size &= 0xfffffffc; //and back down to boundary + size += 3; // move up + size &= 0xfffffffc; // and back down to boundary + // find a free block large enough + // the defragger returns when its made a big enough block. This is + // a good time to defrag as we're probably not doing anything super + // time-critical at the moment - -//find a free block large enough - if ( (nu_block = Defrag_mem(size))==-1) //the defragger returns when its made a big enough block. This is a good time to defrag as we're probably not - { //doing anything super time-critical at the moment - return(0); //error - couldn't find a big enough space + if ((nu_block = Defrag_mem(size)) == -1) { + // error - couldn't find a big enough space + return 0; } - - -//an exact fit? - if (mem_list[nu_block].size==size) //no new block is required as the fit is perfect - { - mem_list[nu_block].state=type; //locked or float - mem_list[nu_block].size=size; //set to the required size - mem_list[nu_block].uid=unique_id; //an identifier + // an exact fit? + if (mem_list[nu_block].size == size) { + // no new block is required as the fit is perfect + mem_list[nu_block].state = type; // locked or float + mem_list[nu_block].size = size; // set to the required size + mem_list[nu_block].uid = unique_id; // an identifier #ifdef MEMDEBUG Mem_debug(); -#endif //MEMDEBUG - return(&mem_list[nu_block]); +#endif + + return &mem_list[nu_block]; } + // nu_block is the free block to split, forming our locked/float block + // with a new free block in any remaining space -// nu_block is the free block to split, forming our locked/float block with a new free block in any remaining space + // If our child is free then is can expand downwards to eat up our + // chopped space this is good because it doesn't create an extra block + // so keeping the block count down. + // + // Why? Imagine you Talloc 1000k, then free it. Now keep allocating 10 + // bytes less and freeing again you end up with thousands of new free + // mini blocks. This way avoids that as the free child keeps growing + // downwards. + if (mem_list[nu_block].child != -1 && mem_list[mem_list[nu_block].child].state == MEM_free) { + // our child is free + // the spare memory is the blocks current size minus the + // amount we're taking -//if our child is free then is can expand downwards to eat up our chopped space -//this is good because it doesn't create an extra bloc so keeping the block count down -//why? -//imagine you Talloc 1000k, then free it. Now keep allocating 10 bytes less and freeing again -//you end up with thousands of new free mini blocks. this way avoids that as the free child keeps growing downwards - if ((mem_list[nu_block].child != -1) && (mem_list[mem_list[nu_block].child].state==MEM_free)) //our child is free - { - slack=mem_list[nu_block].size-size; //the spare memory is the blocks current size minus the amount we're taking + slack = mem_list[nu_block].size - size; - mem_list[nu_block].state=type; //locked or float - mem_list[nu_block].size=size; //set to the required size - mem_list[nu_block].uid=unique_id; //an identifier + mem_list[nu_block].state = type; // locked or float + mem_list[nu_block].size = size; // set to the required size + mem_list[nu_block].uid = unique_id; // an identifier - mem_list[mem_list[nu_block].child].ad = mem_list[nu_block].ad+size; //child starts after us - mem_list[mem_list[nu_block].child].size += slack; //childs size increases + // child starts after us + mem_list[mem_list[nu_block].child].ad = mem_list[nu_block].ad + size; + // child's size increases + mem_list[mem_list[nu_block].child].size += slack; - return(&mem_list[nu_block]); + return &mem_list[nu_block]; } + // otherwise we spawn a new block after us and before our child - our + // child being a proper block that we cannot change -// otherwise we spawn a new block after us and before our child - our child being a proper block that we cannot change + // we remain a child of our parent + // we spawn a new child and it inherits our current child -// we remain a child of our parent -// we spawn a new child and it inherits our current child + // find a NULL slot for a new block -//find a NULL slot for a new block - while((mem_list[spawn].state!=MEM_null)&&(spawn!=MAX_mem_blocks)) + while (mem_list[spawn].state != MEM_null && spawn!=MAX_mem_blocks) spawn++; - - if (spawn==MAX_mem_blocks) //run out of blocks - stop the program. this is a major blow up and we need to alert the developer - { - Mem_debug(); //Lets get a printout of this - ExitWithReport("ERROR: ran out of mem blocks in Talloc() [file=%s line=%u]",__FILE__,__LINE__); + if (spawn == MAX_mem_blocks) { + // run out of blocks - stop the program. this is a major blow + // up and we need to alert the developer + // Lets get a printout of this + Mem_debug(); + ExitWithReport("ERROR: ran out of mem blocks in Talloc() [file=%s line=%u]", __FILE__, __LINE__); } + mem_list[spawn].state = MEM_free; // new block is free + mem_list[spawn].uid = UID_memman; // a memman created bloc + // size of the existing parent free block minus the size of the new + // space Talloc'ed. - mem_list[spawn].state=MEM_free; //new block is free - mem_list[spawn].uid=UID_memman; //a memman created bloc - mem_list[spawn].size= mem_list[nu_block].size-size; //size of the existing parent free block minus the size of the new space Talloc'ed. - //IOW the remaining memory is given to the new free block - mem_list[spawn].ad = mem_list[nu_block].ad+size; //we start 1 byte after the newly allocated block - mem_list[spawn].parent=nu_block; //the spawned child gets it parent - the newly allocated block + mem_list[spawn].size = mem_list[nu_block].size - size; - mem_list[spawn].child=mem_list[nu_block].child; //the new child inherits the parents old child (we are its new child "Waaaa") + // IOW the remaining memory is given to the new free block + // we start 1 byte after the newly allocated block + mem_list[spawn].ad = mem_list[nu_block].ad + size; + // the spawned child gets it parent - the newly allocated block + mem_list[spawn].parent = nu_block; - if (mem_list[spawn].child!=-1) //is the spawn the end block? - mem_list[mem_list[spawn].child].parent= spawn; //the child of the new free-spawn needs to know its new parent + // the new child inherits the parents old child (we are its new + // child "Waaaa") + mem_list[spawn].child = mem_list[nu_block].child; + // is the spawn the end block? + if (mem_list[spawn].child != -1) { + // the child of the new free-spawn needs to know its new parent + mem_list[mem_list[spawn].child].parent = spawn; + } - mem_list[nu_block].state=type; //locked or float - mem_list[nu_block].size=size; //set to the required size - mem_list[nu_block].uid=unique_id; //an identifier - mem_list[nu_block].child=spawn; //the new blocks new child is the newly formed free block - + mem_list[nu_block].state = type; // locked or float + mem_list[nu_block].size = size; // set to the required size + mem_list[nu_block].uid = unique_id; // an identifier - total_blocks++; //we've brought a new block into the world. Ahhh! + // the new blocks new child is the newly formed free block + mem_list[nu_block].child = spawn; + //we've brought a new block into the world. Ahhh! + total_blocks++; #ifdef MEMDEBUG Mem_debug(); -#endif //MEMDEBUG +#endif - return(&mem_list[nu_block]); + return &mem_list[nu_block]; } -//------------------------------------------------------------------------------------ -void Free_mem(mem *block) //Tony10Apr96 -{ -//kill a block of memory - which was presumably floating or locked -//once you've done this the memory may be recycled - block->state=MEM_free; - block->uid=UID_memman; //belongs to the memory manager again +void Free_mem(mem *block) { // Tony10Apr96 + // kill a block of memory - which was presumably floating or locked + // once you've done this the memory may be recycled + + block->state = MEM_free; + block->uid = UID_memman; // belongs to the memory manager again #ifdef MEMDEBUG Mem_debug(); -#endif //MEMDEBUG +#endif } -//------------------------------------------------------------------------------------ -void Float_mem(mem *block) //Tony10Apr96 -{ -//set a block to float -//wont be trashed but will move around in memory - block->state=MEM_float; +void Float_mem(mem *block) { // Tony10Apr96 + // set a block to float + // wont be trashed but will move around in memory + + block->state = MEM_float; #ifdef MEMDEBUG Mem_debug(); -#endif //MEMDEBUG +#endif } -//------------------------------------------------------------------------------------ -void Lock_mem(mem *block) //Tony11Apr96 -{ -//set a block to lock -//wont be moved - don't lock memory for any longer than necessary unless you know the locked memory is at the bottom of the heap - block->state=MEM_locked; //can't move now - this block is now crying out to be floated or free'd again +void Lock_mem(mem *block) { // Tony11Apr96 + // set a block to lock + // wont be moved - don't lock memory for any longer than necessary + // unless you know the locked memory is at the bottom of the heap + + // can't move now - this block is now crying out to be floated or + // free'd again + + block->state = MEM_locked; #ifdef MEMDEBUG Mem_debug(); -#endif //MEMDEBUG +#endif } -//------------------------------------------------------------------------------------ -int32 Defrag_mem(uint32 req_size) //Tony10Apr96 -{ -//moves floating blocks down and/or merges free blocks until a large enough space is found -//or there is nothing left to do and a big enough block cannot be found -//we stop when we find/create a large enough block - this is enough defragging. - int32 cur_block; //block 0 remains the parent block - int32 original_parent,child, end_child; - uint32 j; - uint32 *a; - uint32 *b; +int32 Defrag_mem(uint32 req_size) { // Tony10Apr96 + // moves floating blocks down and/or merges free blocks until a large + // enough space is found or there is nothing left to do and a big + // enough block cannot be found we stop when we find/create a large + // enough block - this is enough defragging. + int32 cur_block; // block 0 remains the parent block + int32 original_parent,child, end_child; + uint32 j; + uint32 *a; + uint32 *b; -// cur_block=base_mem_block; //the mother of all parents + // cur_block = base_mem_block; //the mother of all parents cur_block = suggestedStart; - - do - { - if (mem_list[cur_block].state==MEM_free) //is current block a free block? - { - - if (mem_list[cur_block].size>=req_size) - { - return(cur_block); //this block is big enough - return its id + do { + // is current block a free block? + if (mem_list[cur_block].state == MEM_free) { + if (mem_list[cur_block].size >= req_size) { + // this block is big enough - return its id + return cur_block; } - - if (mem_list[cur_block].child==-1) //the child is the end block - stop if the next block along is the end block - return(-1); //no luck, couldn't find a big enough block - - -// current free block is too small, but if its child is *also* free then merge the two together - if (mem_list[mem_list[cur_block].child].state==MEM_free) - { -// ok, we nuke the child and inherit its child - - child=mem_list[cur_block].child; - - mem_list[cur_block].size+= mem_list[child].size; //our size grows by the size of our child - mem_list[cur_block].child = mem_list[child].child; //our new child is our old childs, child - - if (mem_list[child].child!=-1) //not if the chld we're nuking is the end child (it has no child) - mem_list[mem_list[child].child].parent=cur_block; //the (nuked) old childs childs parent is now us - - mem_list[child].state=MEM_null; //clean up the nuked child, so it can be used again - - total_blocks--; + // the child is the end block - stop if the next block + // along is the end block + if (mem_list[cur_block].child == -1) { + // no luck, couldn't find a big enough block + return -1; } + // current free block is too small, but if its child + // is *also* free then merge the two together -// current free block is too small, but if its child is a float then we move the floating memory block down and the free up -// but, parent/child relationships must be such that the memory is all continuous between blocks. ie. a childs memory always -// begins 1 byte after its parent finishes. However, the positions in the memory list may become truly random, but, any particular -// block of locked or floating memory must retain its position within the mem_list - the float stays a float because the handle/pointer has been passed back -// what this means is that when the physical memory of the foat moves down (and the free up) the child becomes the parent and the parent the child -// but, remember, the parent had a parent and the child another child - these swap over too as the parent/child swap takes place - phew. - else if (mem_list[mem_list[cur_block].child].state==MEM_float) - { - child=mem_list[cur_block].child; //our child is currently floating - - // memcpy(mem_list[cur_block].ad, mem_list[child].ad, mem_list[child].size); //move the higher float down over the free block - - - a=(uint32*) mem_list[cur_block].ad; - b=(uint32*) mem_list[child].ad; - - for (j=0;j<mem_list[child].size/4;j++) - *(a++)=*(b++); + if (mem_list[mem_list[cur_block].child].state == MEM_free) { + // ok, we nuke the child and inherit its child + child = mem_list[cur_block].child; + // our size grows by the size of our child + mem_list[cur_block].size += mem_list[child].size; -// both *ad's change - mem_list[child].ad = mem_list[cur_block].ad; //the float is now where the free was - mem_list[cur_block].ad += mem_list[child].size; //and the free goes up by the size of the float (which has come down) + // our new child is our old childs, child + mem_list[cur_block].child = mem_list[child].child; -// the status of the mem_list blocks must remain the same, so... - original_parent= mem_list[cur_block].parent; //our child gets this when we become its child and it our parent - mem_list[cur_block].parent=child; //the free's child becomes its parent - mem_list[cur_block].child= mem_list[child].child; //the new child inherits its previous childs child + // not if the chld we're nuking is the end + // child (it has no child) - end_child=mem_list[child].child; //save this - see next line + if (mem_list[child].child != -1) { + // the (nuked) old childs childs + // parent is now us + mem_list[mem_list[child].child].parent = cur_block; + } - mem_list[child].child=cur_block; //the floats parent becomes its child - mem_list[child].parent= original_parent; + // clean up the nuked child, so it can be used + // again + mem_list[child].state = MEM_null; - if (end_child!=-1) //if the child had a child - mem_list[end_child].parent=cur_block; //then its parent is now the new child - - if (original_parent==-1) //the base block was the true base parent - base_mem_block=child; //then the child that has moved down becomes the base block as it sits at the lowest possible memory location - else - mem_list[original_parent].child=child; //otherwise the parent of the current free block - that is now the child - gets a new child, - //that child being previously the child of the child of the original parent + total_blocks--; + } else if (mem_list[mem_list[cur_block].child].state == MEM_float) { + // current free block is too small, but if its + // child is a float then we move the floating + // memory block down and the free up but, + // parent/child relationships must be such + // that the memory is all continuous between + // blocks. ie. a childs memory always begins 1 + // byte after its parent finishes. However, the + // positions in the memory list may become + // truly random, but, any particular block of + // locked or floating memory must retain its + // position within the mem_list - the float + // stays a float because the handle/pointer + // has been passed back + // + // what this means is that when the physical + // memory of the foat moves down (and the free + // up) the child becomes the parent and the + // parent the child but, remember, the parent + // had a parent and the child another child - + // these swap over too as the parent/child swap + // takes place - phew. + + // our child is currently floating + child = mem_list[cur_block].child; + + // move the higher float down over the free + // block + // memcpy(mem_list[cur_block].ad, mem_list[child].ad, mem_list[child].size); + + a = (uint32*) mem_list[cur_block].ad; + b = (uint32*) mem_list[child].ad; + + for (j = 0; j < mem_list[child].size / 4; j++) + *(a++) = *(b++); + + // both *ad's change + // the float is now where the free was and the + // free goes up by the size of the float + // (which has come down) + + mem_list[child].ad = mem_list[cur_block].ad; + mem_list[cur_block].ad += mem_list[child].size; + + // the status of the mem_list blocks must + // remain the same, so... + + // our child gets this when we become its + // child and it our parent + original_parent = mem_list[cur_block].parent; + + // the free's child becomes its parent + mem_list[cur_block].parent = child; + + // the new child inherits its previous childs + // child + mem_list[cur_block].child = mem_list[child].child; + + // save this - see next line + end_child = mem_list[child].child; + + // the floats parent becomes its child + mem_list[child].child = cur_block; + mem_list[child].parent = original_parent; + + // if the child had a child + if (end_child != -1) { + // then its parent is now the new child + mem_list[end_child].parent = cur_block; + } + + // if the base block was the true base parent + if (original_parent == -1) { + // then the child that has moved down + // becomes the base block as it sits + // at the lowest possible memory + // location + base_mem_block = child; + } else { + // otherwise the parent of the current + // free block - that is now the child + // - gets a new child, that child + // being previously the child of the + // child of the original parent + mem_list[original_parent].child = child; + } + } else { // if (mem_list[mem_list[cur_block].child].state == MEM_lock) + // the child of current is locked - move to it + // move to next one along - either locked or + // END + cur_block=mem_list[cur_block].child; } - else //if (mem_list[mem_list[cur_block].child].state==MEM_lock) //the child of current is locked - move to it - cur_block=mem_list[cur_block].child; //move to next one along - either locked or END - + } else { + // move to next one along, the current must be + // floating, locked, or a NULL slot + cur_block = mem_list[cur_block].child; } - else - { - cur_block=mem_list[cur_block].child; //move to next one along, the current must be floating, locked, or a NULL slot - } - - } - while(cur_block!=-1); //while the block we've just done is not the final block + } while (cur_block != -1); // while the block we've just done is not the final block - return(-1); //no luck, couldn't find a big enough block + return -1; //no luck, couldn't find a big enough block } -//------------------------------------------------------------------------------------ -void Mem_debug(void) //Tony11Apr96 -{ -//gets called with Talloc, Mem_free, Mem_lock & Mem_float if MEMDEBUG has been #defined -//otherwise can be called at any time anywhere else - - int j; - char inf[][20]= - { - {"MEM_null"}, - {"MEM_free"}, - {"MEM_locked"}, - {"MEM_float"} + +void Mem_debug(void) { //Tony11Apr96 + // gets called with Talloc, Mem_free, Mem_lock & Mem_float if + // MEMDEBUG has been #defined otherwise can be called at any time + // anywhere else + + int j; + char inf[][20] = { + { "MEM_null" }, + { "MEM_free" }, + { "MEM_locked" }, + { "MEM_float" } }; Zdebug("\nbase %d total %d", base_mem_block, total_blocks); - -//first in mem list order - for (j=0;j<MAX_mem_blocks;j++) - { - if (mem_list[j].state==MEM_null) + // first in mem list order + for (j = 0; j < MAX_mem_blocks; j++) { + if (mem_list[j].state == MEM_null) Zdebug("%d- NULL", j); else - Zdebug("%d- state %s, ad %d, size %d, p %d, c %d, id %d", j, - inf[mem_list[j].state], - mem_list[j].ad, mem_list[j].size, mem_list[j].parent, mem_list[j].child, mem_list[j].uid); + Zdebug("%d- state %s, ad %d, size %d, p %d, c %d, id %d", + j, inf[mem_list[j].state], mem_list[j].ad, + mem_list[j].size, mem_list[j].parent, + mem_list[j].child, mem_list[j].uid); } - -//now in child/parent order - j=base_mem_block; - do - { + // now in child/parent order + j = base_mem_block; + do { Zdebug(" %d- state %s, ad %d, size %d, p %d, c %d", j, - inf[mem_list[j].state], - mem_list[j].ad, mem_list[j].size, mem_list[j].parent, mem_list[j].child, mem_list[j].uid); + inf[mem_list[j].state], mem_list[j].ad, + mem_list[j].size, mem_list[j].parent, + mem_list[j].child, mem_list[j].uid); - j=mem_list[j].child; - } - while (j!=-1); + j = mem_list[j].child; + } while (j != -1); } -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -mem *Twalloc(uint32 size, uint32 type, uint32 unique_id) //tony12Feb97 -{ -//the high level Talloc -//can ask the resman to remove old resources to make space - will either do it or halt the system - - mem *membloc; - int j; - uint32 free=0; - - while( VirtualDefrag(size) ) - { - if (!res_man.Help_the_aged_out()) //trash the oldest closed resource - { + +mem *Twalloc(uint32 size, uint32 type, uint32 unique_id) { // tony12Feb97 + // the high level Talloc + // can ask the resman to remove old resources to make space - will + // either do it or halt the system + + mem *membloc; + int j; + uint32 free = 0; + + while (VirtualDefrag(size)) { + // trash the oldest closed resource + if (!res_man.Help_the_aged_out()) { Zdebug("Twalloc ran out of memory! %d %d %d\n", size, type, unique_id); ExitWithReport("Twalloc ran out of memory!"); } @@ -430,59 +506,57 @@ mem *Twalloc(uint32 size, uint32 type, uint32 unique_id) //tony12Feb97 membloc = Talloc(size, type, unique_id); - if (membloc == 0) - { + if (membloc == 0) { Zdebug("Talloc failed to get memory VirtualDefrag said was there"); ExitWithReport("Talloc failed to get memory VirtualDefrag said was there"); } - j=base_mem_block; - do - { + j = base_mem_block; + do { - if (mem_list[j].state==MEM_free) - free+=mem_list[j].size; + if (mem_list[j].state == MEM_free) + free += mem_list[j].size; - j=mem_list[j].child; - } - while (j!=-1); + j = mem_list[j].child; + } while (j != -1); - return(membloc); //return the pointer to the memory + // return the pointer to the memory + return membloc; } +// Maximum allowed wasted memory. +#define MAX_WASTAGE 51200 -#define MAX_WASTAGE 51200 // Maximum allowed wasted memory. - -int32 VirtualDefrag( uint32 size ) // Chris - 07 April '97 -{ - // - // Virutually defrags memory... +int32 VirtualDefrag(uint32 size) { // Chris - 07 April '97 + // Virutually defrags memory... // - // Used to determine if there is potentially are large enough free block available is the - // real defragger was allowed to run. + // Used to determine if there is potentially are large enough free + // block available is the real defragger was allowed to run. // - // The idea being that Twalloc will call this and help_the_aged_out until we indicate that - // it is possible to obtain a large enough free block. This way the defragger need only - // run once to yield the required block size. + // The idea being that Twalloc will call this and help_the_aged_out + // until we indicate that it is possible to obtain a large enough + // free block. This way the defragger need only run once to yield the + // required block size. // - // The reason for its current slowness is that the defragger is potentially called several - // times, each time shifting upto 20Megs around, to obtain the required free block. - // - int32 cur_block; - uint32 currentBubbleSize = 0; + // The reason for its current slowness is that the defragger is + // potentially called several times, each time shifting upto 20Megs + // around, to obtain the required free block. + + int32 cur_block; + uint32 currentBubbleSize = 0; - cur_block=base_mem_block; + cur_block = base_mem_block; suggestedStart = base_mem_block; - do - { - if (mem_list[cur_block].state == MEM_free) - { - // Add a little intelligence. At the start the oldest resources are at the bottom of the - // tube. However there will be some air at the top. Thus bubbles will be - // created at the bottom and float to the top. If we ignore the top gap - // then a large enough bubble will form lower down the tube. Thus less memory - // will need to be shifted. + do { + if (mem_list[cur_block].state == MEM_free) { + // Add a little intelligence. At the start the oldest + // resources are at the bottom of the tube. However + // there will be some air at the top. Thus bubbles + // will be created at the bottom and float to the + // top. If we ignore the top gap then a large enough + // bubble will form lower down the tube. Thus less + // memory will need to be shifted. if (mem_list[cur_block].child != -1) currentBubbleSize += mem_list[cur_block].size; @@ -491,22 +565,15 @@ int32 VirtualDefrag( uint32 size ) // Chris - 07 April '97 if (currentBubbleSize >= size) return 0; - } - else if (mem_list[cur_block].state == MEM_locked) - { + } else if (mem_list[cur_block].state == MEM_locked) { currentBubbleSize = 0; - suggestedStart = mem_list[cur_block].child; // Any free block of the correct size will be above this locked block. + // Any free block of the correct size will be above + // this locked block. + suggestedStart = mem_list[cur_block].child; } cur_block = mem_list[cur_block].child; - } - while(cur_block != -1); + } while (cur_block != -1); - return(1); + return 1; } - -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ diff --git a/sword2/memory.h b/sword2/memory.h index 0ea75a47dc..0effce6043 100644 --- a/sword2/memory.h +++ b/sword2/memory.h @@ -21,62 +21,58 @@ #define MEMORY_H #include "common/scummsys.h" -//#include "src\driver96.h" - -typedef struct -{ - uint32 state; - uint32 age; // *not used* - uint32 size; - int32 parent; //who is before us - int32 child; //who is after us - uint32 uid; //id of a position in the resList or some other unique id - for the visual display only - uint8 *ad; +typedef struct { + uint32 state; + uint32 age; // *not used* + uint32 size; + int32 parent; // who is before us + int32 child; // who is after us + // id of a position in the resList or some other unique id - for the + // visual display only + uint32 uid; + uint8 *ad; } mem; - -#define MEM_null 0 //null -#define MEM_free 1 -#define MEM_locked 2 -#define MEM_float 3 +#define MEM_null 0 // null +#define MEM_free 1 +#define MEM_locked 2 +#define MEM_float 3 //--------------------------------------- // MEMORY BLOCKS -#define MAX_mem_blocks 999 +#define MAX_mem_blocks 999 // maintain at a good 50% higher than the // highest recorded value from the on-screen info //--------------------------------------- -#define UID_memman 0xffffffff -#define UID_NULL 0xfffffffe //FREE -#define UID_font 0xfffffffd -#define UID_temp 0xfffffffc +#define UID_memman 0xffffffff +#define UID_NULL 0xfffffffe // FREE +#define UID_font 0xfffffffd +#define UID_temp 0xfffffffc #define UID_decompression_buffer 0xfffffffb -#define UID_shrink_buffer 0xfffffffa -#define UID_con_sprite 0xfffffff9 -#define UID_text_sprite 0xfffffff8 -#define UID_walk_anim 0xfffffff7 -#define UID_savegame_buffer 0xfffffff6 +#define UID_shrink_buffer 0xfffffffa +#define UID_con_sprite 0xfffffff9 +#define UID_text_sprite 0xfffffff8 +#define UID_walk_anim 0xfffffff7 +#define UID_savegame_buffer 0xfffffff6 #define UID_restoregame_buffer 0xfffffff5 -void Init_memory_manager(void); -void Close_memory_manager(void); //Tony2Oct96 -//mem *Talloc(uint32 size, uint32 type, uint32 unique_id); //low level -mem *Twalloc(uint32 size, uint32 type, uint32 unique_id); //high level -void Free_mem(mem *block); -void Float_mem(mem *block); -void Lock_mem(mem *block); -void Mem_debug(void); -void Visual_mem_display(void); -int32 Defrag_mem(uint32 req_size); //Tony10Apr96 - +void Init_memory_manager(void); +void Close_memory_manager(void); // Tony2Oct96 +mem *Twalloc(uint32 size, uint32 type, uint32 unique_id); // high level +void Free_mem(mem *block); +void Float_mem(mem *block); +void Lock_mem(mem *block); +void Mem_debug(void); +void Visual_mem_display(void); +int32 Defrag_mem(uint32 req_size); // Tony10Apr96 -extern uint32 total_blocks; -extern uint32 base_mem_block; -extern mem mem_list[MAX_mem_blocks]; -extern uint32 total_free_memory; +extern uint32 total_blocks; +extern uint32 base_mem_block; +extern mem mem_list[MAX_mem_blocks]; +extern uint32 total_free_memory; #endif diff --git a/sword2/sync.cpp b/sword2/sync.cpp index 6611a80c3a..7d3c6adfd9 100644 --- a/sword2/sync.cpp +++ b/sword2/sync.cpp @@ -17,165 +17,111 @@ * $Header$ */ -//------------------------------------------------------------------------------------ -#include <stdio.h> - #include "stdafx.h" -//#include "src\driver96.h" -#include "console.h" #include "debug.h" #include "defs.h" #include "interpreter.h" -#include "memory.h" -#include "object.h" #include "sync.h" -//------------------------------------------------------------------------------------ -typedef struct -{ - uint32 id; - uint32 sync; -} _sync_unit; //haaaaaaaa - -#define MAX_syncs 10 //there wont be many will there. probably 2 at most i reckon - - -_sync_unit sync_list[MAX_syncs]; - -//------------------------------------------------------------------------------------ -void Init_sync_system(void) //Tony27Nov96 -{ -//set list to 0's - - uint32 j; +typedef struct { + uint32 id; + uint32 sync; +} _sync_unit; // haaaaaaaa +// there wont be many will there. probably 2 at most i reckon +#define MAX_syncs 10 - for (j=0;j<MAX_syncs;j++) - sync_list[j].id=0; - +_sync_unit sync_list[MAX_syncs]; +void Init_sync_system(void) { // Tony27Nov96 + // set list to 0's + for (int j = 0; j < MAX_syncs; j++) + sync_list[j].id = 0; } -//------------------------------------------------------------------------------------ -int32 FN_send_sync(int32 *params) //Tony27Nov96 -{ -//param 0 sync's recipient -//param 1 sync value - - - uint32 current_sync=0; +int32 FN_send_sync(int32 *params) { // Tony27Nov96 + //param 0 sync's recipient + //param 1 sync value - if (sync_list[current_sync].id) - { - do - current_sync++; - while(sync_list[current_sync].id); //zip along until we find a free slot - + for (int i = 0; i < MAX_syncs; i++) { + if (sync_list[i].id == 0) { + // Zdebug(" %d sending sync %d to %d", ID, params[1], params[0]); + sync_list[i].id = params[0]; + sync_list[i].sync = params[1]; + return IR_CONT; + } } -// Zdebug(" %d sending sync %d to %d", ID, params[1], params[0]); - + // The original code didn't even check for this condition, so maybe + // it should be a fatal error? - sync_list[current_sync].id=params[0]; - sync_list[current_sync].sync=params[1]; - - - - return(IR_CONT); + warning("No free sync slot"); + return IR_CONT; } -//------------------------------------------------------------------------------------ -void Clear_syncs(uint32 id) //Tony27Nov96 -{ -//clear any syncs registered for this id -//call this just after the id has been processed - uint32 j; +void Clear_syncs(uint32 id) { // Tony27Nov96 + // clear any syncs registered for this id + // call this just after the id has been processed + // there could in theory be more than one sync waiting for us so + // clear the lot - -//there could in theory be more than one sync waiting for us so clear the lot - - for (j=0;j<MAX_syncs;j++) - if (sync_list[j].id==id) - { //Zdebug("removing sync %d for %d", j, id); - sync_list[j].id=0; + for (int i = 0; i < MAX_syncs; i++) { + if (sync_list[i].id == id) { + // Zdebug("removing sync %d for %d", i, id); + sync_list[i].id = 0; } - - + } } -//------------------------------------------------------------------------------------ -uint32 Get_sync(void) //Tony27Nov96 -{ - // check for a sync waiting for this character - // - called from system code eg. from inside FN_anim(), to see if animation to be quit - - uint32 j; - - - for (j=0;j<MAX_syncs;j++) - if (sync_list[j].id == ID) - return(1); //means sync found Tony12July97 - -// return(sync_list[j].sync); //return sync value waiting - +uint32 Get_sync(void) { // Tony27Nov96 + // check for a sync waiting for this character + // - called from system code eg. from inside FN_anim(), to see if + // animation to be quit - return(0); //no sync found + for (int i = 0; i < MAX_syncs; i++) { + if (sync_list[i].id == ID) { + // means sync found Tony12July97 + return 1; + } + } + // no sync found + return 0; } -//------------------------------------------------------------------------------------ -int32 FN_get_sync(int32 *params) //Tony27Nov96 -{ -// check for a sync waiting for this character -// - called from script -//params none - - - uint32 j; - - - for (j=0;j<MAX_syncs;j++) - if (sync_list[j].id == ID) { - RESULT=sync_list[j].sync; - return(IR_CONT); //return sync value waiting +int32 FN_get_sync(int32 *params) { // Tony27Nov96 + // check for a sync waiting for this character + // - called from script + // params none + + for (int i = 0; i < MAX_syncs; i++) { + if (sync_list[i].id == ID) { + // return sync value waiting + RESULT = sync_list[i].sync; + return IR_CONT; } + } - RESULT=0; - - // if (params); what is the point of this... khalek - - return(IR_CONT); //no sync found - + // no sync found + RESULT = 0; + return IR_CONT; } -//------------------------------------------------------------------------------------ -int32 FN_wait_sync(int32 *params) //Tony27Nov96 -{ -//keep calling until a sync recieved - -//params none - - uint32 j; +int32 FN_wait_sync(int32 *params) { // Tony27Nov96 + // keep calling until a sync recieved + // params none - j=ID; + // Zdebug("%d waits", ID); - -// Zdebug("%d waits", ID); - - - for (j=0;j<MAX_syncs;j++) { - if (sync_list[j].id == ID) { - RESULT=sync_list[j].sync; + for (int i = 0; i < MAX_syncs; i++) { + if (sync_list[i].id == ID) { + // return sync value waiting //Zdebug(" go"); - return(IR_CONT); //return sync value waiting + RESULT = sync_list[i].sync; + return IR_CONT; } } - // if (params); // what is the point of this... - khalek - - return(IR_REPEAT); //back again next cycle - + // back again next cycle + return IR_REPEAT; } -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ - diff --git a/sword2/sync.h b/sword2/sync.h index 4db00c4731..13f1b7d45a 100644 --- a/sword2/sync.h +++ b/sword2/sync.h @@ -20,13 +20,10 @@ #ifndef _SYNC #define _SYNC -//#include "src\driver96.h" #include "object.h" - -void Init_sync_system(void); //Tony27Nov96 -void Clear_syncs(uint32 id); //Tony27Nov96 -uint32 Get_sync(void); //Tony27Nov96 - +void Init_sync_system(void); // Tony27Nov96 +void Clear_syncs(uint32 id); // Tony27Nov96 +uint32 Get_sync(void); // Tony27Nov96 #endif |