ecere/com/instance: Commented out code testing Template fixes
[sdk] / ecere / src / com / instance.ec
index e1ec582..67aa56e 100644 (file)
@@ -1,5 +1,7 @@
 namespace com;
 
+// #define DISABLE_MEMMGR
+
 import "BinaryTree"
 import "OldList"
 import "String"
@@ -49,6 +51,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 +106,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 +698,7 @@ bool allocateInternal;
 #endif
 
 static uint TOTAL_MEM = 0;
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static uint OUTSIDE_MEM = 0;
 #endif
 
@@ -1050,7 +1077,7 @@ private struct BlockPool
    }
 };
 
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static BlockPool * pools; //[NUM_POOLS];
 
 /*static uint PosFibonacci(uint number)
@@ -1161,7 +1188,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 +1214,7 @@ static void InitMemory()
 }
 #endif
 
-#ifndef MEMINFO
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
 static void * _mymalloc(unsigned int size)
 {
    MemBlock block = null;
@@ -1281,6 +1308,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 +1385,7 @@ static void * _mycrealloc(void * pointer, unsigned int size)
    }
    return newPointer;
 }
-#endif
 
-#ifndef MEMINFO
 #undef realloc
 #undef crealloc
 #undef malloc
@@ -1375,8 +1401,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 +1462,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 +1526,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 +1625,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 +1729,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 +1812,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 +1846,8 @@ static void _free(void * pointer)
 #if !defined(ECERE_BOOTSTRAP)
       if(memMutex != pointer) memMutex.Release();
 #endif
+
+#endif
    }
 }
 
@@ -1806,6 +1866,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 +1964,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 +2050,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
 
@@ -2642,7 +2711,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 +2943,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);
 
@@ -2922,6 +2994,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);
@@ -5101,6 +5189,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 +5449,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 +5646,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);
@@ -6286,9 +6402,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);
@@ -6339,12 +6452,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);
 
 }