45 #include "top_exception.h"    46 #include "bj_stream.h"    51 template <
bool> 
struct ILLEGAL_USE_OF_OBJECT;
    52 template <> 
struct ILLEGAL_USE_OF_OBJECT<true>{};
    53 #define OBJECT_COPY_ERROR ILLEGAL_USE_OF_OBJECT<false>()    58 #if defined(FULL_DEBUG) && defined(DBG_GLB_MEM_USE) && defined(DBG_GLB_MEM_WITH_PT_DIR)    59 #define DBG_USING_PT_DIR    62 #ifdef DBG_USING_PT_DIR    63 #define MEM_PT_DIR(prm) prm    65 #define MEM_PT_DIR(prm)     68 #ifdef DBG_USING_PT_DIR    69 extern  bool    dbg_keeping_ptdir;
    71 void    dbg_add_to_ptdir(
void* pt_val);
    72 void    dbg_del_from_ptdir(
void* pt_val);
    73 void    dbg_print_ptdir();
    80 call_assert(
bool vv_ck, 
const char* file, 
int line, 
    81             const char* ck_str, 
const char* msg = NULL_PT);
    85 void* bj_memcpy(
void *dest, 
const void *src, 
size_t n){
    86     return memcpy(dest, src, n);
    90 void* bj_memset(
void *s, 
int c, 
size_t n){
    91     return memset(s, c, n);
    94 #define glb_assert(vv) call_assert(vv, as_pt_char(__FILE__), __LINE__, as_pt_char(#vv))    96 #define glb_assert_2(vv, ostmsg) \    97     call_assert(vv, as_pt_char(__FILE__), __LINE__, as_pt_char(#vv), (ostmsg))   101 #define DBG_COND_COMM(cond, comm)   \   104             bj_ostream& os = bj_dbg; \   113 #define DBG_CK(prm)     DBG(glb_assert(prm))   115 #define DBG_CK_2(prm, comms1) \   116     DBG_COND_COMM((! (prm)), \   126 #define DBG_THROW(prm)      DBG(prm)   128 #define DBG_THROW_CK(prm)   DBG_CK(prm)   136 #if defined(FULL_DEBUG) && defined(DBG_GLB_MEM_USE)   137 #define MEM_CTRL(prm) prm   139 #define MEM_CTRL(prm)    143 #define MEM_SRTY(prm) prm   145 #define MEM_SRTY(prm) ;   156 #define DBG_TPL_ALLOC()    175 typedef long            error_code_t;
   176 typedef unsigned long       mem_size;
   177 typedef char            t_1byte;
   178 typedef unsigned int        t_4byte;
   179 typedef t_4byte         t_dword;
   181 #define MAX_UTYPE(type)     ((type)(-1))   183 #define MAX_MEM_SZ      MAX_UTYPE(mem_size)   189     mex_memout_in_mem_alloc_1,
   190     mex_memout_in_mem_alloc_2,
   191     mex_memout_in_mem_sec_re_alloc_1,
   192     mex_memout_in_mem_re_alloc_1,
   193     mex_memout_in_mem_re_alloc_2
   197 class mem_exception : 
public top_exception {
   199     mem_exception(
long the_id = 0) : top_exception(the_id)
   206 #define MEM_CK(prm) MEM_CTRL(if(glb_pt_mem_stat != NULL){DBG_CK(prm);})   208 #if defined(FULL_DEBUG) && defined(DBG_GLB_MEM_USE)   212 extern glb_mem_data* glb_pt_mem_stat;
   213 extern glb_mem_data MEM_STATS;
   218     mem_size        num_bytes_in_use;
   219     mem_size        num_bytes_available;
   222         num_bytes_in_use = 0;
   223         num_bytes_available = 0;
   233     if(glb_pt_mem_stat != NULL){
   236     glb_pt_mem_stat = &MEM_STATS;
   237     MEM_CK(glb_pt_mem_stat->num_bytes_in_use == 0);
   243     if(glb_pt_mem_stat == NULL){
   246     MEM_CK(glb_pt_mem_stat->num_bytes_in_use == 0);
   247     glb_pt_mem_stat = NULL_PT;
   252 mem_get_num_by_in_use(){
   253     if(glb_pt_mem_stat == NULL_PT){
   256     return glb_pt_mem_stat->num_bytes_in_use;
   261 mem_inc_num_by_in_use(mem_size val){
   262     if(glb_pt_mem_stat == NULL_PT){
   265     glb_pt_mem_stat->num_bytes_in_use += val;
   270 mem_dec_num_by_in_use(mem_size val){
   271     if(glb_pt_mem_stat == NULL_PT){
   274     glb_pt_mem_stat->num_bytes_in_use -= val;
   279 mem_get_num_by_available(){
   280     if(glb_pt_mem_stat == NULL_PT){
   283     return glb_pt_mem_stat->num_bytes_available;
   288 mem_set_num_by_available(mem_size val){
   289     if(glb_pt_mem_stat == NULL_PT){
   292     glb_pt_mem_stat->num_bytes_available = val;
   300 template<
class obj_t> 
static inline obj_t* 
   301 tpl_malloc(
size_t the_size = 1){
   303     mem_size mem_sz = the_size * 
sizeof(obj_t);
   305         MEM_CK((MAX_MEM_SZ - mem_sz) > mem_get_num_by_in_use());
   306         mem_inc_num_by_in_use(mem_sz);
   308         if( (mem_get_num_by_available() > 0) && 
   309             (mem_get_num_by_in_use() > mem_get_num_by_available()) )
   311             throw mem_exception(mex_memout_in_mem_alloc_1);
   315     obj_t*   tmp = (obj_t*)malloc(mem_sz);
   316     if((tmp == NULL_PT) && (the_size != 0)){
   317         throw mem_exception(mex_memout_in_mem_alloc_2);
   319     MEM_PT_DIR(dbg_add_to_ptdir(tmp));
   323 template<
class obj_t> 
static inline obj_t* 
   324 tpl_secure_realloc(obj_t* ptr, 
size_t old_size, 
size_t the_size){
   326     MEM_CK(the_size > old_size);
   328     mem_size mem_sz = the_size * 
sizeof(obj_t);
   329     obj_t*   tmp = (obj_t*)malloc(mem_sz);
   330     if((tmp == NULL_PT) && (the_size != 0)){
   331         throw mem_exception(mex_memout_in_mem_sec_re_alloc_1);
   333     MEM_PT_DIR(dbg_add_to_ptdir(tmp));
   336         mem_size old_mem_sz = old_size * 
sizeof(obj_t);
   337         bj_memcpy(tmp, ptr, old_mem_sz);
   338         bj_memset(ptr, 0, old_mem_sz);
   340         MEM_PT_DIR(dbg_del_from_ptdir(ptr));
   345 template<
class obj_t> 
static inline obj_t* 
   346 tpl_realloc(obj_t* ptr, 
size_t old_size, 
size_t the_size){
   348     mem_size mem_sz = the_size * 
sizeof(obj_t);
   350         mem_size old_mem_sz = old_size * 
sizeof(obj_t);
   351         MEM_CK(mem_get_num_by_in_use() >= old_mem_sz);
   352         mem_dec_num_by_in_use(old_mem_sz);
   353         MEM_CK((MAX_MEM_SZ - mem_sz) > mem_get_num_by_in_use());
   354         mem_inc_num_by_in_use(mem_sz);
   356         if( (mem_get_num_by_available() > 0) && 
   357             (mem_get_num_by_in_use() > mem_get_num_by_available()) )
   359             throw mem_exception(mex_memout_in_mem_re_alloc_1);
   362     MEM_PT_DIR(dbg_del_from_ptdir(ptr));
   363     obj_t*   tmp = (obj_t*)realloc((
void*)ptr, mem_sz);
   364     if((tmp == NULL_PT) && (the_size != 0)){
   365         throw mem_exception(mex_memout_in_mem_re_alloc_2);
   367     MEM_PT_DIR(dbg_add_to_ptdir(tmp));
   371 template<
class obj_t> 
static inline void    372 tpl_free(obj_t*& ptr, 
size_t the_size = 1){
   376             mem_size s_old_mem_sz = the_size * 
sizeof(obj_t);
   379         MEM_PT_DIR(dbg_del_from_ptdir(ptr));
   383         mem_size old_mem_sz = the_size * 
sizeof(obj_t);
   384         MEM_CK(mem_get_num_by_in_use() >= old_mem_sz);
   385         mem_dec_num_by_in_use(old_mem_sz);