ALL.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : ALL.H
  21. //Description : General-purpose header file
  22. #ifndef __ALL_H
  23. #define __ALL_H
  24. //--------- Include other headers -------//
  25. #ifndef __WINDOWS_
  26. #include <windows.h>
  27. #endif
  28. #ifndef __OMISC_H
  29. #include <OMISC.h>
  30. #endif
  31. #ifndef __OSTR_H
  32. #include <OSTR.h>
  33. #endif
  34. #ifndef __OFILE_H
  35. #include <OFILE.h>
  36. #endif
  37. #ifndef __GAMEDEF_H
  38. #include <GAMEDEF.h>
  39. #endif
  40. //-------- Define macro functions -------//
  41. #define max(a,b) (((a) > (b)) ? (a) : (b))
  42. #define min(a,b) (((a) < (b)) ? (a) : (b))
  43. //---------- Debug functions -------//
  44. #ifdef DEBUG
  45. void __cdecl debug_msg( char* fmt, ... );
  46. #endif
  47. //---------- define class Mem ----------//
  48. struct MemInfo;
  49. class Mem
  50. {
  51. public :
  52. MemInfo* info_array;
  53. short ptr_num;
  54. short ptr_used;
  55. public :
  56. Mem();
  57. ~Mem();
  58. char* add(unsigned,char*,int);
  59. char* add_clear(unsigned,char*,int);
  60. char* resize(void*,unsigned,char*,int);
  61. char* resize_keep_data(void *orgPtr, unsigned orgSize, unsigned newSize, char* fileName, int fileLine);
  62. void del(void*,char*,int);
  63. int get_mem_size(void *memPtr);
  64. };
  65. char* mem_resize_keep_data(void*,unsigned,unsigned); // called when DEBUG mode is off
  66. extern Mem mem;
  67. //------- Define Class Error ------------//
  68. typedef void (*ExtraHandler)();
  69. class Error
  70. {
  71. private:
  72. ExtraHandler extra_handler; // extra error handler
  73. public:
  74. Error();
  75. void internal(char*,char*,int);
  76. void mem();
  77. void msg(char*,...);
  78. void run(char*,...);
  79. void set_extra_handler(ExtraHandler extraHandler) { extra_handler = extraHandler; }
  80. };
  81. extern Error err;
  82. //-------- error handling functions ----------//
  83. #ifdef DEBUG
  84. #define err_when(cond) if(cond) err.internal(NULL,__FILE__, __LINE__)
  85. #define err_here() err.internal(NULL,__FILE__, __LINE__)
  86. #define err_if(cond) if(cond)
  87. #define err_else else
  88. #define err_now(msg) err.run(msg) // internal error
  89. // always use err_if(cond) together with err_now(), so when debug is turn off, these two statement will turn off
  90. #else
  91. #define err_when(cond)
  92. #define err_here()
  93. #define err_if(cond)
  94. #define err_else
  95. #define err_now(msg)
  96. #endif
  97. //------ memory allocation functions --------//
  98. #ifndef NO_MEM_CLASS
  99. #define mem_add(memSize) mem.add(memSize, __FILE__, __LINE__)
  100. #define mem_add_clear(memSize) mem.add_clear(memSize, __FILE__, __LINE__)
  101. #define mem_resize(orgPtr,newSize) mem.resize(orgPtr, newSize, __FILE__, __LINE__)
  102. #define mem_del(memPtr) mem.del(memPtr, __FILE__, __LINE__)
  103. #define mem_resize_keep_data(orgPtr, orgSize, newSize) \
  104. mem.resize_keep_data(orgPtr, orgSize, newSize, __FILE__, __LINE__)
  105. #else
  106. #define mem_add(memSize) ((char*)malloc(memSize))
  107. #define mem_add_clear(memSize) ((char*)calloc(1,memSize))
  108. #define mem_resize(orgPtr, newSize) ((char*)realloc(orgPtr,newSize))
  109. #define mem_del(memPtr) free(memPtr)
  110. // mem_resize_keep_data() will be called directly when not in DEBUG mode
  111. #endif
  112. //--------------------------------------------//
  113. #endif