ecere/com/instance; containers: MemTracking code
[sdk] / ecere / src / com / instance.ec
index c486597..24e718e 100644 (file)
@@ -1,5 +1,7 @@
 namespace com;
 
+// #define DISABLE_MEMMGR
+
 import "BinaryTree"
 import "OldList"
 import "String"
@@ -29,6 +31,11 @@ import "Mutex"
 #define REDZONE 0
 #endif
 
+
+#ifdef _DEBUG
+// #define MEMTRACKING
+#endif
+
 #ifdef MEMINFO
 import "Thread"
 static define MAX_MEMORY_LOC = 40;
@@ -49,6 +56,28 @@ static uint memoryErrorsCount = 0;
 default:
 #define property _property
 
+#if defined(DISABLE_MEMMGR)
+
+#ifndef ECERE_BOOTSTRAP
+# include <malloc.h>
+#endif
+
+#if defined(__WIN32__)
+#ifdef ECERE_BOOTSTRAP
+uintsize _msize(void * p);
+#endif
+
+#  define msize _msize
+#else
+#ifdef ECERE_BOOTSTRAP
+uintsize malloc_usable_size(void * p);
+#endif
+
+#  define msize malloc_usable_size
+#endif
+
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 
@@ -82,6 +111,9 @@ default:
 extern int __ecereVMethodID_class_OnGetDataFromString;
 
 // IMPLEMENTATION FOR THESE IN _instance.c:
+#if defined(__MINGW32__) && !defined(_W64) && __GNUC__ < 4
+dllexport int isblank(int c);
+#endif
 bool Instance_LocateModule(const char * name, const char * fileName);
 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, const char *** argvPtr);
 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload);
@@ -671,7 +703,7 @@ bool allocateInternal;
 #endif
 
 static uint TOTAL_MEM = 0;
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static uint OUTSIDE_MEM = 0;
 #endif
 
@@ -684,6 +716,9 @@ private class MemBlock : struct
    MemBlock prev, next;
    MemPart part;
    uint size;
+#if defined(_DEBUG) && !defined(MEMINFO) && defined(MEMTRACKING)
+   Class _class;
+#endif
 };
 
 private class MemPart : struct
@@ -1050,7 +1085,7 @@ private struct BlockPool
    }
 };
 
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static BlockPool * pools; //[NUM_POOLS];
 
 /*static uint PosFibonacci(uint number)
@@ -1161,7 +1196,7 @@ public uint pow2i(uint number)
    return 1<<log2i(number);
 }
 
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static bool memoryInitialized = false;
 static void InitMemory()
 {
@@ -1187,7 +1222,7 @@ static void InitMemory()
 }
 #endif
 
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static void * _mymalloc(unsigned int size)
 {
    MemBlock block = null;
@@ -1200,6 +1235,9 @@ static void * _mymalloc(unsigned int size)
          block = pools[p].Add();
          if(block)
          {
+#if defined(_DEBUG) && defined(MEMTRACKING)
+            block._class = null;
+#endif
             block.size = size;
             pools[p].usedSpace += size;
          }
@@ -1212,6 +1250,9 @@ static void * _mymalloc(unsigned int size)
             TOTAL_MEM += sizeof(class MemBlock) + size;
             OUTSIDE_MEM += sizeof(class MemBlock) + size;
             block.part = null;
+#if defined(_DEBUG) && defined(MEMTRACKING)
+            block._class = null;
+#endif
             block.size = size;
          }
       }
@@ -1281,6 +1322,7 @@ static void * _myrealloc(void * pointer, unsigned int size)
             TOTAL_MEM += size - newBlock.size;
             OUTSIDE_MEM += size - newBlock.size;
             newPointer = ((struct MemBlock *)newBlock + 1);
+            newBlock.size = size;
          }
       }
    }
@@ -1357,9 +1399,7 @@ static void * _mycrealloc(void * pointer, unsigned int size)
    }
    return newPointer;
 }
-#endif
 
-#ifndef MEMINFO
 #undef realloc
 #undef crealloc
 #undef malloc
@@ -1375,8 +1415,12 @@ static void * _mycrealloc(void * pointer, unsigned int size)
 
 static void * _malloc(unsigned int size)
 {
+#if defined(DISABLE_MEMMGR) && !defined(MEMINFO)
+   return size ? malloc(size) : null;
+#else
    void * pointer;
 
+
 #if !defined(ECERE_BOOTSTRAP)
    memMutex.Wait();
 #endif
@@ -1432,11 +1476,16 @@ static void * _malloc(unsigned int size)
    }
 #endif
    return pointer ? ((byte*)pointer + REDZONE) : null;
+#endif
 }
 
 static void * _calloc(int n, unsigned int size)
 {
+#if defined(DISABLE_MEMMGR) && !defined(MEMINFO)
+   return size ? calloc(n, size) : null;
+#else
    void * pointer;
+
 #if !defined(ECERE_BOOTSTRAP)
    memMutex.Wait();
 #endif
@@ -1491,11 +1540,18 @@ static void * _calloc(int n, unsigned int size)
    }
 #endif
    return pointer ? ((byte*)pointer + REDZONE) : null;
+#endif
 }
 
 static void * _realloc(void * pointer, unsigned int size)
 {
+#if defined(DISABLE_MEMMGR) && !defined(MEMINFO)
+   if(!size) { free(pointer); return null; }
+   return realloc(pointer, size);
+
+#else
    if(!size) { _free(pointer); return null; }
+
 #if !defined(ECERE_BOOTSTRAP)
    memMutex.Wait();
 #endif
@@ -1583,11 +1639,23 @@ static void * _realloc(void * pointer, unsigned int size)
    memMutex.Release();
 #endif
    return pointer ? ((byte *)pointer + REDZONE) : null;
+#endif
 }
 
 static void * _crealloc(void * pointer, unsigned int size)
 {
-   if(!size) return null;
+#if defined(DISABLE_MEMMGR) && !defined(MEMINFO)
+   uintsize s = pointer ? msize(pointer) : 0;
+   void * p;
+   if(!size) { free(pointer); return null; }
+
+   p = realloc(pointer, size);
+   if(size > s)
+      memset((byte *)p + s, 0, size - s);
+   return p;
+#else
+   if(!size) { _free(pointer); return null; }
+
 #if !defined(ECERE_BOOTSTRAP)
    memMutex.Wait();
 #endif
@@ -1675,12 +1743,16 @@ static void * _crealloc(void * pointer, unsigned int size)
    memMutex.Release();
 #endif
    return pointer ? ((byte *)pointer + REDZONE) : null;
+#endif
 }
 
 static void _free(void * pointer)
 {
    if(pointer)
    {
+#if defined(DISABLE_MEMMGR) && !defined(MEMINFO)
+      free(pointer);
+#else
 #if !defined(ECERE_BOOTSTRAP)
       if(memMutex != pointer) memMutex.Wait();
 #endif
@@ -1754,13 +1826,13 @@ static void _free(void * pointer)
                {
                   if(address[-c-1] != 0xAB)
                   {
-                     printf("Buffer Underrun\n");
+                     printf("Buffer Underrun (%d bytes before)\n", c+1);
                      memoryErrorsCount++;
                      block.OutputStacks(block.freed);
                   }
                   if(address[c + size] != 0xAB)
                   {
-                     printf("Buffer Overrun\n");
+                     printf("Buffer Overrun (%d bytes past block)\n", c);
                      memoryErrorsCount++;
                      block.OutputStacks(block.freed);
                   }
@@ -1788,6 +1860,8 @@ static void _free(void * pointer)
 #if !defined(ECERE_BOOTSTRAP)
       if(memMutex != pointer) memMutex.Release();
 #endif
+
+#endif
    }
 }
 
@@ -1806,6 +1880,14 @@ public void memswap(byte * a, byte * b, uint size)
    }
 }
 
+public void CheckConsistency()
+{
+#ifdef MEMINFO
+   if(!memBlocks.Check())
+      printf("Memory Blocks Tree Integrity Failed\n");
+#endif
+}
+
 public void CheckMemory()
 {
 #ifdef MEMINFO
@@ -1896,13 +1978,13 @@ public void CheckMemory()
       {
          if(address[-c-1] != 0xAB)
          {
-            printf("Buffer Underrun\n");
+            printf("Buffer Underrun (%d bytes before)\n", c + 1);
             memoryErrorsCount++;
             block.OutputStacks(block.freed);
          }
          if(address[c + size] != 0xAB)
          {
-            printf("Buffer Overrun\n");
+            printf("Buffer Overrun (%d bytes past)\n", c);
             memoryErrorsCount++;
             block.OutputStacks(block.freed);
          }
@@ -1982,7 +2064,8 @@ static void FixDerivativesBase(Class base, Class mod)
       // _class.memberID = _class.startMemberID = (base && (type == normalClass || type == noHeadClass || type == structClass)) ? base.memberID : 0;
 
       if(type == normalClass || type == noHeadClass)
-         _class.offset = (base && (base.templateClass ? base.templateClass.structSize : base.structSize) && base.type != systemClass) ? (base.templateClass ? base.templateClass.structSize : base.structSize) : ((type == noHeadClass) ? 0 : sizeof(class Instance));
+         // Use 'memberOffset' for nohead class as the members get added without padding
+         _class.offset = (base && (base.templateClass ? (type == normalClass ? base.templateClass.structSize : base.templateClass.memberOffset) : (type == normalClass ? base.structSize : base.memberOffset)) && base.type != systemClass) ? (base.templateClass ? base.templateClass.structSize : base.structSize) : ((type == noHeadClass) ? 0 : sizeof(class Instance));
       else
          _class.offset = 0; // Force set to 0
 
@@ -2044,6 +2127,7 @@ static void FixDerivativesBase(Class base, Class mod)
             _class._vTbl = renew _class._vTbl void *[_class.vTblSize];
             // memmove(_class._vTbl + mod.base.vTblSize, _class._vTbl + baseClass.vTblSize, (mod.base.vTblSize - baseClass.vTblSize) * sizeof(void *));
             memmove(_class._vTbl + mod.base.vTblSize, _class._vTbl + baseClass.vTblSize, (_class.vTblSize - mod.vTblSize) * sizeof(void *));
+            memcpy(_class._vTbl + baseClass.vTblSize, mod._vTbl + baseClass.vTblSize, (mod.base.vTblSize - baseClass.vTblSize) * sizeof(void *));
 
             updateStart = baseClass.vTblSize;
             updateEnd = updateStart + mod.base.vTblSize - baseClass.vTblSize;
@@ -2642,7 +2726,9 @@ public dllexport Class eSystem_RegisterClass(ClassType type, const char * name,
          }
          _class.memberID = _class.startMemberID = (base && (type == normalClass || type == noHeadClass || type == structClass)) ? base.memberID : 0;
          if(type == normalClass || type == noHeadClass)
-            _class.offset = (base && base.structSize && base.type != systemClass) ? base.structSize : ((type == noHeadClass) ? 0 : ((force64Bits && inCompiler && fixed) ? 24 : (force32Bits && inCompiler && fixed) ? 12 : sizeof(class Instance)));
+            _class.offset = (base && base.structSize && base.type != systemClass) ?
+               // Use 'memberOffset' for nohead class as the members get added without padding
+               (base.type == normalClass ? base.structSize : base.memberOffset) : ((type == noHeadClass) ? 0 : ((force64Bits && inCompiler && fixed) ? 24 : (force32Bits && inCompiler && fixed) ? 12 : sizeof(class Instance)));
          else
             _class.offset = 0;   // Force set to 0 for redefinitions
 
@@ -2872,7 +2958,8 @@ static void FreeTemplate(Class template)
    if(template.nameSpace)
    {
       BTNamedLink link = (BTNamedLink)template.nameSpace->classes.FindString(template.name);
-      template.nameSpace->classes.Delete((BTNode)link);
+      if(link)
+         template.nameSpace->classes.Delete((BTNode)link);
    }
    FreeTemplateArgs(template);
 
@@ -2887,7 +2974,10 @@ static void FreeTemplate(Class template)
       template.derivatives.Delete(deriv);
    }
 
-   _free(template);
+   if(template.module)
+      template.module.classes.Delete(template);
+   else
+      _free(template);
 }
 
 static void FreeTemplates(Class _class)
@@ -2922,6 +3012,22 @@ public dllexport void eClass_Unregister(Class _class)
    ClassProperty classProp;
    ClassTemplateParameter param;
 
+   if(_class.templateClass)
+   {
+      // Unregistering templates... Used in IDE to address crash on Ecere classes templatized with imported modules
+      OldLink templateLink;
+      for(templateLink = _class.templateClass.templatized.first; templateLink; templateLink = templateLink.next)
+      {
+         if(templateLink.data == _class)
+         {
+            _class.templateClass.templatized.Delete(templateLink);
+            break;
+         }
+      }
+      FreeTemplate(_class);
+      return;
+   }
+
    delete _class._vTbl;
 
    FreeTemplates(_class);
@@ -3286,6 +3392,12 @@ public dllexport Class eSystem_FindClass(Module module, const char * name)
                templatedClass.numParams = 0;
                templatedClass.derivatives = { };
                templatedClass.templatized = { };
+               templatedClass.module = module;
+               templatedClass.count = 0; // TOCHECK: Keeping track of individual templatized classes?
+               templatedClass.prev = null;
+               templatedClass.next = null;
+
+               module.classes.Add(templatedClass);
 
                ComputeClassParameters(templatedClass, templateParams, module);
 
@@ -3520,9 +3632,11 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
             {
                case type:
                   argument.dataTypeString = CopyString(value);
-                  argument.dataTypeClass = eSystem_FindClass(_class.module, value);
-                  if(!argument.dataTypeClass) argument.dataTypeClass = eSystem_FindClass(_class.module.application, value);
-                  if(!argument.dataTypeClass) argument.dataTypeClass = eSystem_FindClass(findModule, value);
+                  argument.dataTypeClass = eSystem_FindClass(findModule, value);
+                  if(!argument.dataTypeClass)
+                     argument.dataTypeClass = eSystem_FindClass(_class.module, value);
+                  if(!argument.dataTypeClass)
+                     argument.dataTypeClass = eSystem_FindClass(_class.module.application, value);
                   break;
                case expression:
                {
@@ -3620,11 +3734,11 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
             CopyTemplateArg(param, templatedClass.templateArgs[curParamID]);
             if(param.type == type && param.defaultArg.dataTypeString)
             {
-               templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module, param.defaultArg.dataTypeString);
+               templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(findModule, param.defaultArg.dataTypeString);
                if(!templatedClass.templateArgs[curParamID].dataTypeClass)
-                  templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module.application, param.defaultArg.dataTypeString);
+                  templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module, param.defaultArg.dataTypeString);
                if(!templatedClass.templateArgs[curParamID].dataTypeClass)
-                  templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(findModule, param.defaultArg.dataTypeString);
+                  templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module.application, param.defaultArg.dataTypeString);
             }
          }
          curParamID++;
@@ -3748,7 +3862,9 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
                      FreeTemplateArg(templatedClass, param, c);
 
                      arg->dataTypeString = CopyString(templateString);
-                     arg->dataTypeClass = eSystem_FindClass(templatedClass.module, templateString);
+                     arg->dataTypeClass = eSystem_FindClass(findModule, templateString);
+                     if(!arg->dataTypeClass)
+                        arg->dataTypeClass = eSystem_FindClass(templatedClass.module, templateString);
                      if(!arg->dataTypeClass)
                         arg->dataTypeClass = eSystem_FindClass(templatedClass.module.application, templateString);
                   }
@@ -3800,11 +3916,11 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
                CopyTemplateArg(param, templatedClass.templateArgs[curParamID]);
                if(param.type == type && param.defaultArg.dataTypeString)
                {
-                  templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module, param.defaultArg.dataTypeString);
+                  templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(findModule, param.defaultArg.dataTypeString);
                   if(!templatedClass.templateArgs[curParamID].dataTypeClass)
-                     templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module.application, param.defaultArg.dataTypeString);
+                     templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module, param.defaultArg.dataTypeString);
                   if(!templatedClass.templateArgs[curParamID].dataTypeClass)
-                     templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(findModule, param.defaultArg.dataTypeString);
+                     templatedClass.templateArgs[curParamID].dataTypeClass = eSystem_FindClass(templatedClass.module.application, param.defaultArg.dataTypeString);
                }
             }
             curParamID++;
@@ -3902,10 +4018,10 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
                         id++;
                      }
                   }
-                  memberClass = eSystem_FindClass(templatedClass.module, className);
                   // TESTING: Added this here...
+                  memberClass = eSystem_FindClass(findModule, className);
                   if(!memberClass)
-                     memberClass = eSystem_FindClass(findModule, className);
+                     memberClass = eSystem_FindClass(templatedClass.module, className);
                   if(!memberClass)
                      memberClass = eSystem_FindClass(templatedClass.module.application, className);
                }
@@ -4083,7 +4199,7 @@ public dllexport Method eClass_AddMethod(Class _class, const char * name, const
                if(method.vid >= _class.vTblSize)
                   printf("error: virtual methods overriding failure\n");
                else
-                  _class._vTbl[method.vid] = function ? function : DefaultFunction;
+                  _class._vTbl[method.vid] = function ? function : (void *)DefaultFunction;
                for(deriv = _class.derivatives.first; deriv; deriv = deriv.next)
                {
                   Class derivClass = deriv.data;
@@ -4152,7 +4268,7 @@ public dllexport Method eClass_AddVirtualMethod(Class _class, const char * name,
                if(method.vid >= _class.vTblSize)
                   printf("error: virtual methods overriding failure\n");
                else
-                  _class._vTbl[method.vid] = function ? function : DefaultFunction;
+                  _class._vTbl[method.vid] = function ? function : (void *)DefaultFunction;
             }
             else
                base = null;
@@ -4174,7 +4290,7 @@ public dllexport Method eClass_AddVirtualMethod(Class _class, const char * name,
          };
          _class.methods.Add((BTNode)method);
          _class._vTbl = renew _class._vTbl void *[_class.vTblSize];
-         _class._vTbl[method.vid] = function ? function : DefaultFunction;
+         _class._vTbl[method.vid] = function ? function : (void *)DefaultFunction;
 
          // TODO: Fix derived classes
          if(_class.derivatives.first || _class.templatized.first)
@@ -4481,6 +4597,14 @@ public dllexport void * eInstance_New(Class _class)
       allocateClass = null;
    memMutex.Release();
 #endif
+
+#if defined(_DEBUG) && !defined(MEMINFO) && defined(MEMTRACKING)
+      {
+         MemBlock block = (MemBlock)((byte *)instance - sizeof(class MemBlock));
+         block._class = _class;
+      }
+#endif
+
       if(_class.type == normalClass)
       {
          instance._class = _class;
@@ -4503,6 +4627,7 @@ public dllexport void eInstance_Evolve(Instance * instancePtr, Class _class)
    if(_class && instancePtr && *instancePtr)
    {
       bool wasApp = false, wasGuiApp = false;
+      Instance oldInstance = *instancePtr;
       Instance instance = (Instance)renew *instancePtr byte[_class.structSize];
       Class fromClass = instance._class;
       *instancePtr = instance;
@@ -4571,7 +4696,8 @@ public dllexport void eInstance_Evolve(Instance * instancePtr, Class _class)
             for(templateLink = _class.templatized.first; templateLink; templateLink = templateLink.next)
             {
                Class template = templateLink.data;
-               template.module = _class.module;
+               if(template.module == oldInstance)
+                  template.module = _class.module;
             }
          }
 
@@ -4580,11 +4706,13 @@ public dllexport void eInstance_Evolve(Instance * instancePtr, Class _class)
             for(_class = module.classes.first; _class; _class = _class.next)
             {
                OldLink templateLink;
+               Module oldModule = _class.module;
                _class.module = module;
                for(templateLink = _class.templatized.first; templateLink; templateLink = templateLink.next)
                {
                   Class template = templateLink.data;
-                  template.module = _class.module;
+                  if(template.module == oldModule)
+                     template.module = _class.module;
                }
             }
          }
@@ -5032,7 +5160,7 @@ public dllexport void eInstance_SetMethod(Instance instance, const char * name,
                memcpy(instance._vTbl, instance._class._vTbl,
                   sizeof(int(*)()) * instance._class.vTblSize);
             }
-            instance._vTbl[method.vid] = function ? function : DefaultFunction;
+            instance._vTbl[method.vid] = function ? function : (void *)DefaultFunction;
          }
       }
    }
@@ -5101,6 +5229,21 @@ public dllexport DataMember eClass_AddDataMember(Class _class, const char * name
          if(alignment)
          {
             bool pointerAlignment = alignment == 0xF000F000;
+            bool force64Bits = (_class.module.application.isGUIApp & 2) ? true : false;
+            bool force32Bits = (_class.module.application.isGUIApp & 4) ? true : false;
+            if((force32Bits || force64Bits) && !strcmp(_class.name, "AVLNode") && !strcmp(name, "__ecerePrivateData0"))
+            {
+               if(force64Bits)
+               {
+                  type = "byte[32]";
+                  size = 32;
+               }
+               if(force32Bits)
+               {
+                  type = "byte[16]";
+                  size = 16;
+               }
+            }
 
             if(pointerAlignment) alignment = sizeof(void *);
 
@@ -5346,7 +5489,7 @@ static Module Module_Load(Module fromModule, const char * name, AccessMode impor
       }
       else
       {
-         char * libLocation = null;
+         const char * libLocation = null;
 #if defined(__ANDROID__)
          libLocation = AndroidInterface_GetLibLocation();
 #endif
@@ -5543,6 +5686,19 @@ static void NameSpace_Free(NameSpace parentNameSpace)
    NameSpace * nameSpace;
    delete (void *)parentNameSpace.name;
 
+         /*   {
+      BTNamedLink n, next;
+      for(n = (BTNamedLink)parentNameSpace.classes.first; n; n = next)
+      {
+         Class c = n.data;
+
+         next = (BTNamedLink)((BTNode)n).next;
+
+         if(c.templateClass)
+            eClass_Unregister(c);
+      }
+   }         */
+
    while((nameSpace = (NameSpace *)parentNameSpace.nameSpaces.first))
    {
       NameSpace_Free(nameSpace);
@@ -5640,17 +5796,26 @@ static void Module_Destructor(Module module)
       if(_class.nameSpace)
       {
          BTNamedLink classLink = (BTNamedLink)_class.nameSpace->classes.FindString(_class.name);
-         OldLink t;
-         for(t = _class.templatized.first; t; t = t.next)
+         if(classLink)
          {
-            Class template = t.data;
-            BTNamedLink link;
-            link = (BTNamedLink)template.nameSpace->classes.FindString(template.name);
+            OldLink t;
+            for(t = _class.templatized.first; t; t = t.next)
+            {
+               Class template = t.data;
+               BTNamedLink link;
+               link = (BTNamedLink)template.nameSpace->classes.FindString(template.name);
 
-            template.nameSpace->classes.Delete((BTNode)link);
-            template.nameSpace = null;
+               template.nameSpace->classes.Delete((BTNode)link);
+               template.nameSpace = null;
+            }
+            _class.nameSpace->classes.Delete((BTNode)classLink);
          }
-         _class.nameSpace->classes.Delete((BTNode)classLink);
+#ifdef _DEBUG
+         else
+         {
+            printf("Warning: Could not find %s in namespace classes while destructing module %s\n", _class.name, module.name);
+         }
+#endif
          _class.nameSpace = null;
       }
       _class.module = null;
@@ -6277,9 +6442,6 @@ static void LoadCOM(Module module)
    eSystem_RegisterFunction("system", "int system(const char*)", system, module, baseSystemAccess);
    eSystem_RegisterFunction("atoi", "int atoi(const char*)", atoi, module, baseSystemAccess);
    eSystem_RegisterFunction("atof", "double atof(const char*)", atof, module, baseSystemAccess);
-   eSystem_RegisterFunction("tolower", "int tolower(int)", tolower, module, baseSystemAccess);
-   eSystem_RegisterFunction("toupper", "int toupper(int)", toupper, module, baseSystemAccess);
-   eSystem_RegisterFunction("isdigit", "bool isdigit(int)", isdigit, module, baseSystemAccess);
    eSystem_RegisterFunction("memset", "void * memset(void * area, int value, uintsize count)", memset, module, baseSystemAccess);
    eSystem_RegisterFunction("getenv", "char * getenv(const char * name)", getenv, module, baseSystemAccess);
    eSystem_RegisterFunction("rename", "int rename(const char *oldpath, const char *newpath)", rename, module, baseSystemAccess);
@@ -6330,12 +6492,17 @@ static void LoadCOM(Module module)
    eSystem_RegisterFunction("fputs", "int fputs(const char *, void * stream)", fputs, module, baseSystemAccess);
 
    // --- Ctype ---
+   eSystem_RegisterFunction("tolower", "int tolower(int)", tolower, module, baseSystemAccess);
+   eSystem_RegisterFunction("toupper", "int toupper(int)", toupper, module, baseSystemAccess);
+   eSystem_RegisterFunction("isdigit", "bool isdigit(int)", isdigit, module, baseSystemAccess);
+   eSystem_RegisterFunction("isxdigit","bool isxdigit(int)", isxdigit, module, baseSystemAccess);
    eSystem_RegisterFunction("isalnum", "int isalnum(int c)", isalnum, module, baseSystemAccess);
    eSystem_RegisterFunction("isspace", "int isspace(int c)", isspace, module, baseSystemAccess);
    eSystem_RegisterFunction("isalpha", "int isalpha(int c)", isalpha, module, baseSystemAccess);
    eSystem_RegisterFunction("islower", "int islower(int c)", islower, module, baseSystemAccess);
    eSystem_RegisterFunction("isupper", "int isupper(int c)", isupper, module, baseSystemAccess);
    eSystem_RegisterFunction("isprint", "int isprint(int c)", isprint, module, baseSystemAccess);
+   eSystem_RegisterFunction("isblank", "int isblank(int c)", isblank, module, baseSystemAccess);
 
 }
 
@@ -6738,6 +6905,7 @@ public int ISO8859_1toUTF8(const char * source, char * dest, int max)
 {
    int c;
    int d = 0;
+   byte * byteDest = (byte *)dest;
    for(c = 0; source[c]; c++)
    {
       unichar ch = ((byte *)source)[c];
@@ -6748,28 +6916,28 @@ public int ISO8859_1toUTF8(const char * source, char * dest, int max)
       if(ch < 0x80)
       {
          if(d + 1 >= max) break;
-         dest[d++] = (char)ch;
+         byteDest[d++] = (char)ch;
       }
       else if(ch < 0x800)
       {
          if(d + 2 >= max) break;
-         dest[d++] = 0xC0 | (byte)((ch & 0x7C0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xC0 | (byte)((ch & 0x7C0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
       else if(ch < 0x10000)
       {
          if(d + 3 >= max) break;
-         dest[d++] = 0xE0 | (byte)((ch & 0xF000) >> 12);
-         dest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xE0 | (byte)((ch & 0xF000) >> 12);
+         byteDest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
       else
       {
          if(d + 4 >= max) break;
-         dest[d++] = 0xF0 | (byte)((ch & 0x1C0000) >> 18);
-         dest[d++] = 0x80 | (byte)((ch & 0x3F000) >> 12);
-         dest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xF0 | (byte)((ch & 0x1C0000) >> 18);
+         byteDest[d++] = 0x80 | (byte)((ch & 0x3F000) >> 12);
+         byteDest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
    }
    dest[d] = 0;
@@ -6781,12 +6949,12 @@ public char * UTF16toUTF8(const uint16 * source)
    int c;
    int d = 0;
    int len;
-   char * dest;
+   byte * dest;
    uint16 u16;
    bool invert = false;
 
    for(len = 0; source[len]; len++);
-   dest = new char[len * 3 + 1];
+   dest = new byte[len * 3 + 1];
    for(c = 0; (u16 = source[c]); c++)
    {
       unichar ch;
@@ -6826,8 +6994,8 @@ public char * UTF16toUTF8(const uint16 * source)
       }
    }
    dest[d] = 0;
-   dest = renew dest char[d+1];
-   return dest;
+   dest = renew dest byte[d+1];
+   return (char *)dest;
 }
 
 public int UTF16toUTF8Buffer(const uint16 * source, char * dest, int max)
@@ -6835,6 +7003,7 @@ public int UTF16toUTF8Buffer(const uint16 * source, char * dest, int max)
    int c;
    int d = 0;
    uint16 u16;
+   byte * byteDest = (byte *)dest;
    for(c = 0; (u16 = source[c]); c++)
    {
       unichar ch;
@@ -6846,31 +7015,31 @@ public int UTF16toUTF8Buffer(const uint16 * source, char * dest, int max)
       if(ch < 0x80)
       {
          if(d + 1 >= max) break;
-         dest[d++] = (char)ch;
+         byteDest[d++] = (char)ch;
       }
       else if(ch < 0x800)
       {
          if(d + 2 >= max) break;
-         dest[d++] = 0xC0 | (byte)((ch & 0x7C0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xC0 | (byte)((ch & 0x7C0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
       else if(ch < 0x10000)
       {
          if(d + 3 >= max) break;
-         dest[d++] = 0xE0 | (byte)((ch & 0xF000) >> 12);
-         dest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xE0 | (byte)((ch & 0xF000) >> 12);
+         byteDest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
       else
       {
          if(d + 4 >= max) break;
-         dest[d++] = 0xF0 | (byte)((ch & 0x1C0000) >> 18);
-         dest[d++] = 0x80 | (byte)((ch & 0x3F000) >> 12);
-         dest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xF0 | (byte)((ch & 0x1C0000) >> 18);
+         byteDest[d++] = 0x80 | (byte)((ch & 0x3F000) >> 12);
+         byteDest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
    }
-   dest[d] = 0;
+   byteDest[d] = 0;
    return d;
 }
 
@@ -6967,7 +7136,7 @@ public int UTF8toUTF16Buffer(const char * source, uint16 * dest, int max)
          if(codePoint > 0xFFFF)
          {
             uint16 lead = (uint16)(LEAD_OFFSET + (codePoint >> 10));
-            uint16 trail = 0xDC00 + (uint16)(codePoint & 0x3FF);
+            uint16 trail = (uint16)(0xDC00 | (codePoint & 0x3FF));
             if(d >= max - 1) break;
             dest[d++] = lead;
             dest[d++] = trail;
@@ -6989,36 +7158,37 @@ public int UTF32toUTF8Len(const unichar * source, int count, char * dest, int ma
    int c;
    int d = 0;
    uint32 ch;
+   byte * byteDest = (byte *)dest;
    for(c = 0; c<count && (ch = source[c]); c++)
    {
       if(ch < 0x80)
       {
          if(d + 1 >= max) break;
-         dest[d++] = (char)ch;
+         byteDest[d++] = (char)ch;
       }
       else if(ch < 0x800)
       {
          if(d + 2 >= max) break;
-         dest[d++] = 0xC0 | (byte)((ch & 0x7C0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xC0 | (byte)((ch & 0x7C0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
       else if(ch < 0x10000)
       {
          if(d + 3 >= max) break;
-         dest[d++] = 0xE0 | (byte)((ch & 0xF000) >> 12);
-         dest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xE0 | (byte)((ch & 0xF000) >> 12);
+         byteDest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
       else
       {
          if(d + 4 >= max) break;
-         dest[d++] = 0xF0 | (byte)((ch & 0x1C0000) >> 18);
-         dest[d++] = 0x80 | (byte)((ch & 0x3F000) >> 12);
-         dest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
-         dest[d++] = 0x80 | (byte)(ch & 0x03F);
+         byteDest[d++] = 0xF0 | (byte)((ch & 0x1C0000) >> 18);
+         byteDest[d++] = 0x80 | (byte)((ch & 0x3F000) >> 12);
+         byteDest[d++] = 0x80 | (byte)((ch & 0xFC0) >> 6);
+         byteDest[d++] = 0x80 | (byte)(ch & 0x03F);
       }
    }
-   dest[d] = 0;
+   byteDest[d] = 0;
    return d;
 }
 
@@ -7062,7 +7232,7 @@ public uint16 * UTF8toUTF16(const char * source, int * wordCount)
          if(codePoint > 0xFFFF)
          {
             uint16 lead = (uint16)(LEAD_OFFSET + (codePoint >> 10));
-            uint16 trail = 0xDC00 + (uint16)(codePoint & 0x3FF);
+            uint16 trail = (uint16)(0xDC00 | (codePoint & 0x3FF));
             dest[d++] = lead;
             dest[d++] = trail;
          }
@@ -7079,3 +7249,77 @@ public uint16 * UTF8toUTF16(const char * source, int * wordCount)
 }
 
 namespace com;
+
+#if defined(_DEBUG) && !defined(MEMINFO) && defined(MEMTRACKING)
+import "Map"
+
+Map<Class, int> blocksByClass { };
+#endif
+
+public void queryMemInfo(char * string)
+{
+#if defined(_DEBUG) && !defined(MEMINFO) && defined(MEMTRACKING)
+   char s[1024];
+   int p;
+   uint numBlocks = 0;
+   //uintsize nonClassBytes = 0;
+   sprintf(s, "Total System Memory Usage: %.02f\n", TOTAL_MEM / 1048576.0f);
+   strcat(string, s);
+
+   for(p = 0; pools && p < NUM_POOLS; p++)
+   {
+      BlockPool * pool = &pools[p];
+      if(pool->totalSize)
+      {
+         numBlocks += pool->totalSize;
+         sprintf(s, "%8d bytes: %d blocks in %d parts (%.02f mb used; taking up %.02f mb space)\n",
+            pool->blockSize, pool->numBlocks, pool->numParts, pool->usedSpace / 1048576.0f, pool->totalSize * pool->blockSpace / 1048576.0f);
+         strcat(string, s);
+      }
+   }
+/*
+
+   blocksByClass.Free();
+   memMutex.Wait();
+   for(p = 0; pools && p < NUM_POOLS; p++)
+   {
+      BlockPool * pool = &pools[p];
+      MemBlock block;
+      for(block = pool->first; block; block = block.next)
+      {
+         Class c = block._class;
+         blocksByClass[c]++;
+         if(!c)
+            nonClassBytes += block.size;
+      }
+   }
+   memMutex.Release();
+
+   //for(c : blocksByClass)
+   {
+      MapIterator<Class, int> it { map = blocksByClass };
+      while(it.Next())
+      {
+         int c = it.data;
+         Class _class = it.key; //&c;
+         uintsize size = _class ? _class.structSize : nonClassBytes;
+         float totalSize = (float)size * (_class ? c : 1) / 1048576.0f;
+         if(totalSize > 1)
+         {
+            sprintf(s, "%s (%d bytes): %d instances (%.02f mb used)\n", _class ? _class.name : "(none)", (int)size, c, totalSize);
+            strcat(string, s);
+         }
+      }
+   }
+*/
+   sprintf(s, "Non-pooled memory: %.02f\n", OUTSIDE_MEM / 1048576.0f);
+   strcat(string, s);
+   sprintf(s, "Total Blocks Count: %d (%.02f mb overhead)\n", numBlocks, (float)sizeof(struct MemBlock) * numBlocks / 1048576.0f);
+   strcat(string, s);
+#ifdef MEMORYGUARD
+   sprintf(s, "MemoryGuard: %d blocks (%.02f mb RedZone, %.02f mb MemInfo)\n", memBlocks.count,
+      numBlocks * 2 * REDZONE / 1048576.0f, sizeof(struct MemInfo) * memBlocks.count / 1048576.0f);
+   strcat(string, s);
+#endif
+#endif
+}