namespace com;
+// #define DISABLE_MEMMGR
+
import "BinaryTree"
import "OldList"
import "String"
import "dataTypes"
+//#define JUST_CHECK_LEAKS
+//#define JUST_CHECK_BOUNDARIES
+
+
#if defined(ECERE_BOOTSTRAP) || defined(ECERE_STATIC)
#define dllexport
#if !defined(ECERE_BOOTSTRAP)
#define REDZONE 256
#endif
*/
-#ifndef REDZONE
+
+#if defined(JUST_CHECK_LEAKS) || !defined(REDZONE)
+#undef REDZONE
#define REDZONE 0
#endif
+
+#ifdef _DEBUG
+// #define MEMTRACKING
+#endif
+
#ifdef MEMINFO
import "Thread"
static define MAX_MEMORY_LOC = 40;
+static define MAX_STACK_FRAMES = 1000;
static class MemStack : BTNode
{
- const char * frames[1000];
+ const char * frames[MAX_STACK_FRAMES];
int pos;
bool recurse;
};
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>
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);
public class Angle : double;
-public class unichar : uint32
+public class ::unichar : uint32
{
const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
};
+// Forward declarations to hook on to libec:
+class ::Type;
+class ::Instantiation;
+class ::ClassDefinition;
+
public class Property : struct
{
public:
stack.key = GetCurrentThreadID();
memStacks.Add(stack);
}
- if(stack.pos < 1000)
+ if(stack.pos < MAX_STACK_FRAMES)
stack.frames[stack.pos++] = loc;
- else
- printf("");
memMutex.Release();
#endif
}
{
stack.pos--;
}
- else
- printf("");
memMutex.Release();
#endif
}
{
class_fixed
SelfWatcher prev, next;
- void (*callback)(Instance);
+ void (*callback)(void *);
Property _property;
};
Class prev, next;
const char * name;
int offset, structSize;
- int (** _vTbl)();
+ void ** _vTbl;
int vTblSize;
- int (*Constructor)(Instance);
- void (*Destructor)(Instance);
+ bool (*Constructor)(void *);
+ void (*Destructor)(void *);
int offsetClass, sizeClass;
Class base;
bool internalDecl;
void * data;
bool computeSize;
- int structAlignment;
+ short structAlignment; short pointerAlignment;
int destructionWatchOffset;
bool fixed;
OldList delayedCPValues;
ClassTemplateArgument * templateArgs;
Class templateClass;
OldList templatized;
- int numParams;
+ int numParams; // TOTAL number of params including all base classes; use templateParams.count for this level
bool isInstanceClass;
bool byValueSystemClass;
OldList members;
BinaryTree membersAlpha;
int memberOffset;
- int structAlignment;
+ short structAlignment; short pointerAlignment;
};
public class BitMember : struct
public:
class_fixed
OldList values;
- int largest;
+ int64 largest;
};
class Watcher : struct
{
class_fixed
Watcher prev, next;
- void (*callback)(Instance, Instance);
+ void (*callback)(void *, void *);
Instance object;
};
bool freed;
const char * _class;
uint id;
- const char * allocLoc[MAX_MEMORY_LOC];
- const char * freeLoc[MAX_MEMORY_LOC];
+ char * allocLoc[MAX_MEMORY_LOC];
+ char * freeLoc[MAX_MEMORY_LOC];
bool internal;
void OutputStacks(bool showFree)
bool recurse = false;
static int blockID;
//Class allocateClass;
-const char * allocateClass;
+char * allocateClass;
bool allocateInternal;
#endif
static uint TOTAL_MEM = 0;
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
static uint OUTSIDE_MEM = 0;
+#endif
#if !defined(ECERE_BOOTSTRAP)
static Mutex memMutex { };
MemBlock prev, next;
MemPart part;
uint size;
+#if !defined(MEMINFO) && defined(MEMTRACKING)
+ Class _class;
+#endif
};
private class MemPart : struct
void Remove(MemBlock block)
{
+ MemPart part = block.part;
/*if(blockSize == 28)
printf("BlockPool::Remove (%d)\n", blockSize);*/
if(block.prev)
printf("Setting new free block: part = %x\n", block.part);
}*/
- block.part.blocksUsed--;
+ part.blocksUsed--;
numBlocks--;
- block.part.pool->usedSpace -= block.size;
+ part.pool->usedSpace -= block.size;
- if(!block.part.blocksUsed && numBlocks && totalSize > numBlocks + numBlocks / 2)
+ if(!part.blocksUsed && numBlocks && totalSize > numBlocks + numBlocks / 2)
{
MemBlock next = free, prev = null;
- MemPart part = block.part;
free = null;
totalSize -= part.size;
/*if(blockSize == 28)
}
};
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
static BlockPool * pools; //[NUM_POOLS];
/*static uint PosFibonacci(uint number)
}
}
*/
+
static uint log1_5i(uint number)
{
uint pos;
break;
current = current * 3 / 2;
if(current == 1) current = 2;
+ if(current & 7) current += 8 - (current & 7);
}
return pos;
}
{
current = current * 3 / 2;
if(current == 1) current = 2;
+ if(current & 7) current += 8 - (current & 7);
}
return (uint)current;
}
return (uint)current;
current = current * 3 / 2;
if(current == 1) current = 2;
+ if(current & 7) current += 8 - (current & 7);
}
return (uint)current;
}
+#endif
// -- Math Helpers ---
public uint log2i(uint number)
return 1<<log2i(number);
}
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
static bool memoryInitialized = false;
static void InitMemory()
{
pools[c].Expand(Max(1, expansion));
}
}
+#endif
+#if !defined(MEMINFO) && !defined(DISABLE_MEMMGR)
static void * _mymalloc(unsigned int size)
{
MemBlock block = null;
block = pools[p].Add();
if(block)
{
+#if defined(MEMTRACKING)
+ block._class = null;
+#endif
block.size = size;
pools[p].usedSpace += size;
}
TOTAL_MEM += sizeof(class MemBlock) + size;
OUTSIDE_MEM += sizeof(class MemBlock) + size;
block.part = null;
+#if defined(MEMTRACKING)
+ block._class = null;
+#endif
block.size = size;
}
}
return pointer;
}
+
static void _myfree(void * pointer)
{
if(pointer)
TOTAL_MEM += size - newBlock.size;
OUTSIDE_MEM += size - newBlock.size;
newPointer = ((struct MemBlock *)newBlock + 1);
+ newBlock.size = size;
}
}
}
return newPointer;
}
-#ifndef MEMINFO
#undef realloc
#undef crealloc
#undef malloc
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
}
#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
}
#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
if(block)
{
+#if defined(JUST_CHECK_LEAKS) || defined(JUST_CHECK_BOUNDARIES)
+ memcpy((byte *)pointer + REDZONE, (byte *)block.key, Min(block.size, size));
+ free((byte *)block.key - REDZONE);
+ memBlocks.Remove(block);
+ free(block);
+#else
if(block.freed)
{
memcpy((byte *)pointer + REDZONE, block.oldmem, Min(block.size, size));
memset((byte *)block.key - REDZONE, 0xEC, block.size + REDZONE * 2);
block.freed = true;
}
+#endif
}
if(!recurse && !stack.recurse)
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
if(block)
{
+#if defined(JUST_CHECK_LEAKS) || defined(JUST_CHECK_BOUNDARIES)
+ memcpy((byte *)pointer + REDZONE, (byte *)block.key, Min(block.size, size));
+ free((byte *)block.key - REDZONE);
+ memBlocks.Remove(block);
+ free(block);
+#else
if(block.freed)
{
memcpy((byte *)pointer + REDZONE, block.oldmem, Min(block.size, size));
memset((byte *)block.key - REDZONE, 0xEC, block.size + REDZONE * 2);
block.freed = true;
}
+#endif
}
if(!recurse && !stack.recurse)
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
{
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);
}
}
block.freed = true;
+#if defined(JUST_CHECK_LEAKS) || defined(JUST_CHECK_BOUNDARIES)
+ free((byte *)block.key - REDZONE);
+ memBlocks.Remove(block);
+ free(block);
+#else
block.oldmem = (byte *)malloc(block.size + REDZONE * 2);
if(block.oldmem)
{
memset((byte *)block.key - REDZONE, 0xEC, block.size + REDZONE * 2);
memcpy(block.freeLoc, stack.frames + stack.pos - Min(stack.pos, MAX_MEMORY_LOC), Min(stack.pos, MAX_MEMORY_LOC) * sizeof(char *));
+#endif
}
stack.recurse = false;
}
#if !defined(ECERE_BOOTSTRAP)
if(memMutex != pointer) memMutex.Release();
#endif
+
+#endif
}
}
}
}
+public void CheckConsistency()
+{
+#ifdef MEMINFO
+ if(!memBlocks.Check())
+ printf("Memory Blocks Tree Integrity Failed\n");
+#endif
+}
+
public void CheckMemory()
{
#ifdef MEMINFO
}
}
- while(block = (MemInfo)memBlocks.root)
+ while((block = (MemInfo)memBlocks.root))
{
byte * address;
int c;
{
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);
}
printf("Memory Check Completed.\n");
#if defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
if(memoryErrorsCount)
- getch();
+ system("pause");
#endif
#endif
}
// _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));
+ if(_class.structAlignment && (_class.offset % _class.structAlignment))
+ _class.offset += _class.structAlignment - _class.offset % _class.structAlignment;
+ }
+ else
+ _class.offset = 0; // Force set to 0
if(type == structClass)
{
_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;
}
public dllexport Class eSystem_RegisterClass(ClassType type, const char * name, const char * baseName, int size, int sizeClass,
- bool (* Constructor)(void *),void (* Destructor)(void *),
+ bool (* Constructor)(void *), void (* Destructor)(void *),
Module module, AccessMode declMode, AccessMode inheritanceAccess)
{
int start = 0, c;
}
{
NameSpace * ns = _class.nameSpace;
- while(ns->parent &&
+ while(ns != nameSpace &&
+ ns->parent &&
!ns->classes.first &&
!ns->functions.first &&
!ns->defines.first &&
}
_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
// For cross-bitness-compiling
if(crossBits)
{
size = 3*(force32Bits ? 4 : 8);
_class.structAlignment = force32Bits ? 4 : 8; // FileListing is problematic because it is a struct with private data that the user allocates
+ _class.pointerAlignment = 1;
}
// These we want to recompute inside the IDE to help the debugger
else if(!strcmp(name, "ecere::com::Class")) size = 0; // 616
else if(!strcmp(name, "ecere::sys::OldList")) size = 0; // 32
else if(!strcmp(name, "ecere::sys::Item")) size = 0;
else if(!strcmp(name, "ecere::sys::NamedLink")) size = 0;
+ else if(!strcmp(name, "ecere::sys::NamedLink64")) size = 0;
else if(!strcmp(name, "ecere::sys::OldLink")) size = 0;
else if(!strcmp(name, "ecere::sys::NamedItem")) size = 0;
else if(!strcmp(name, "ecere::sys::NamedItem64")) size = 0;
}
_class.offsetClass = offsetClass;
_class.sizeClass = totalSizeClass;
- _class.Constructor = (void *)Constructor;
+ _class.Constructor = Constructor;
_class.Destructor = Destructor;
if(_class.type != systemClass)
_class.type = type;
}
}
-static void FreeEnumValue(NamedLink value)
+static void FreeEnumValue(NamedLink64 value)
{
delete value.name;
}
{
case type:
delete (void *)template.templateArgs[id].dataTypeString;
+ template.templateArgs[id].dataTypeClass = null;
break;
case identifier:
delete (void *)template.templateArgs[id].memberString;
{
case type:
delete (void *)template.templateArgs[id].dataTypeString;
+ template.templateArgs[id].dataTypeClass = null;
break;
case identifier:
delete (void *)template.templateArgs[id].memberString;
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);
- delete (void *)template.fullName;
- delete (void *)template.name;
- delete (void *)template.templateArgs;
- delete (void *)template.dataTypeString;
+ FreeTemplatesDerivatives(template);
+ FreeTemplateArgs(template);
while((deriv = template.derivatives.first))
{
template.derivatives.Delete(deriv);
}
- _free(template);
+ delete (void *)template.fullName;
+ delete (void *)template.name;
+ delete template.templateArgs;
+ delete (void *)template.dataTypeString;
+
+ if(template.module)
+ template.module.classes.Delete(template);
+ else
+ _free(template);
}
static void FreeTemplates(Class _class)
}
FreeTemplateArgs(_class);
- //if(_class.templateArgs)
- //printf("Deleting Template args for %s\n", _class.name);
delete _class.templateArgs;
delete (void *)_class.dataTypeString;
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);
- FreeTemplateArgs(_class);
- delete _class.templateArgs;
-
while((template = _class.templatized.first))
{
FreeTemplate(template.data);
else if(ch >= 'A' && ch <= 'Z')
ch -= ('A'- 10);
else
- {
- if(endString)
- *endString = string + c;
// Invalid character
break;
- }
if(ch < base)
{
value *= base;
value += ch;
}
else
- {
- if(endString)
- *endString = string + c;
// Invalid character
break;
- }
}
+ if(endString)
+ *endString = string + c;
+
return sign*value;
}
else if(ch >= 'A' && ch <= 'Z')
ch -= ('A' - 10);
else
- {
- if(endString)
- *endString = string + c;
// Invalid character
break;
- }
if(ch < base)
{
value *= base;
value += ch;
}
else
- {
- if(endString)
- *endString = string + c;
// Invalid character
break;
- }
}
+ if(endString)
+ *endString = string + c;
return sign*value;
}
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);
{
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:
{
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++;
{
int id = p;
Class sClass;
+ // NOTE: This struct 'arg' here is only to build up templateString
ClassTemplateArgument arg;
for(sClass = expClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
arg = expClass.templateArgs[id];
{
if(cParam.type == type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
{
- arg.dataTypeString = templatedClass.templateArgs[p].dataTypeString;
- arg.dataTypeClass = templatedClass.templateArgs[p].dataTypeClass;
+ arg = templatedClass.templateArgs[p];
break;
}
}
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);
}
{
FreeTemplateArg(templatedClass, param, c);
+ // TRICKY: This copies from equivalent parameters
arg->dataTypeString = templatedClass.templateArgs[p].dataTypeString;
arg->dataTypeClass = templatedClass.templateArgs[p].dataTypeClass;
CopyTemplateArg(cParam, arg);
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++;
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);
}
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 : null; //(void *)DefaultFunction;
for(deriv = _class.derivatives.first; deriv; deriv = deriv.next)
{
Class derivClass = deriv.data;
Method method
{
name = CopyString(name),
- function = function ? function : DefaultFunction;
+ function = function ? function : null; //DefaultFunction;
_class = _class;
dataTypeString = CopyString(type);
memberAccess = declMode;
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 : null; //(void *)DefaultFunction;
}
else
base = null;
Method method
{
name = CopyString(name);
- function = function ? function : DefaultFunction;
+ function = function ? function : null; //DefaultFunction;
type = virtualMethod;
_class = _class;
vid = _class.vTblSize++;
};
_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 : null; //(void *)DefaultFunction;
// TODO: Fix derived classes
if(_class.derivatives.first || _class.templatized.first)
- FixDerivativeVirtualMethod(_class, name, method.vid, function ? function : (void *)DefaultFunction, type);
+ FixDerivativeVirtualMethod(_class, name, method.vid, function ? function : null /*(void *)DefaultFunction*/, type);
return method;
}
}
size *= 3;
}
instance = _calloc(1, size);
+ if(!instance && size)
+ printf("Failed to allocate memory instantiating %s object!\n", _class.name);
+ else if(!size)
+ printf("Warning: 0 size instantiating %s object!\n", _class.name);
}
#ifdef MEMINFO
allocateClass = null;
memMutex.Release();
#endif
- if(_class.type == normalClass)
+
+#if !defined(MEMINFO) && defined(MEMTRACKING)
+ {
+ MemBlock block = (MemBlock)((byte *)instance - sizeof(class MemBlock));
+ block._class = _class;
+ }
+#endif
+
+ if(instance && _class.type == normalClass)
{
instance._class = _class;
// Copy the virtual table initially
instance._vTbl = _class._vTbl;
}
- if(!ConstructInstance(instance, _class, null))
+ if(instance && !ConstructInstance(instance, _class, null))
{
_free(instance);
instance = null;
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;
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;
}
}
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;
}
}
}
memcpy(instance._vTbl, instance._class._vTbl,
sizeof(int(*)()) * instance._class.vTblSize);
}
- instance._vTbl[method.vid] = function ? function : DefaultFunction;
+ instance._vTbl[method.vid] = function ? function : null; //(void *)DefaultFunction;
}
}
}
for(deriv = _class.derivatives.first; deriv; deriv = deriv.next)
FixOffsets(deriv.data);
}
-
+ // F000F000 will mean a pointer size alignment
public dllexport DataMember eClass_AddDataMember(Class _class, const char * name, const char * type, unsigned int size, unsigned int alignment, AccessMode declMode)
{
if(_class && 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 = force64Bits ? 8 : force32Bits ? 4 : sizeof(void *);
+
+ if(pointerAlignment && _class.structAlignment <= 4)
+ _class.pointerAlignment = 1;
+ else if(!pointerAlignment && alignment >= 8)
+ _class.pointerAlignment = 0;
+
_class.structAlignment = Max(_class.structAlignment, alignment);
+ if(_class.offset % alignment)
+ {
+ _class.structSize += alignment - (_class.offset % alignment);
+ _class.offset += alignment - (_class.offset % alignment);
+ }
if(_class.memberOffset % alignment)
_class.memberOffset += alignment - (_class.memberOffset % alignment);
}
}
return null;
}
-
+ // F000F000 will mean a pointer size alignment
public dllexport DataMember eMember_AddDataMember(DataMember member, const char * name, const char * type, unsigned int size, unsigned int alignment, AccessMode declMode)
{
if(name && !member.membersAlpha.FindString(name))
if(alignment)
{
+ bool pointerAlignment = alignment == 0xF000F000;
+ bool force64Bits = false; //(member._class.module.application.isGUIApp & 2) ? true : false;
+ bool force32Bits = false; //(member._class.module.application.isGUIApp & 4) ? true : false;
+ if(pointerAlignment) alignment = force64Bits ? 8 : force32Bits ? 4 : sizeof(void *);
+
+ if(pointerAlignment && member.structAlignment <= 4)
+ member.pointerAlignment = 1;
+ else if(!pointerAlignment && alignment >= 8)
+ member.pointerAlignment = 0;
+
member.structAlignment = Max(member.structAlignment, alignment);
if(member.memberOffset % alignment)
else
addTo.memberID += dataMember.memberID;
+ if(dataMember.pointerAlignment && dataMember.structAlignment <= 4)
+ addTo.pointerAlignment = 1;
+ else if(!dataMember.pointerAlignment && dataMember.structAlignment >= 8)
+ addTo.pointerAlignment = 0;
+
addTo.structAlignment = Max(addTo.structAlignment, dataMember.structAlignment);
+
dataMember.offset = (addTo.type == unionMember) ? 0 : addTo.memberOffset;
if(dataMember.structAlignment)
{
- addTo.structAlignment = Max(addTo.structAlignment, dataMember.structAlignment);
-
if(addTo.memberOffset % dataMember.structAlignment)
addTo.memberOffset += dataMember.structAlignment - (addTo.memberOffset % dataMember.structAlignment);
}
//dataMember.id = _class.memberID++;
dataMember.id = _class.memberID;
+ if(dataMember.pointerAlignment && dataMember.structAlignment <= 4)
+ _class.pointerAlignment = 1;
+ else if(!dataMember.pointerAlignment && dataMember.structAlignment >= 8)
+ _class.pointerAlignment = 0;
+
_class.structAlignment = Max(_class.structAlignment, dataMember.structAlignment);
if(dataMember.type == unionMember)
_class.memberID += 1;
if(dataMember.structAlignment)
{
- _class.structAlignment = Max(_class.structAlignment, dataMember.structAlignment);
-
if(_class.memberOffset % dataMember.structAlignment)
_class.memberOffset += dataMember.structAlignment - (_class.memberOffset % dataMember.structAlignment);
}
}
else
{
- char * libLocation = null;
+ const char * libLocation = null;
#if defined(__ANDROID__)
libLocation = AndroidInterface_GetLibLocation();
#endif
{
name = !strcmp(module.name, "ecereCOM") ? "ecere" : "ecereCOM";
if((!Load && !strcmp(module.name, "ecereCOM")) ||
- (Load && (!__thisModule || !__thisModule.name || !strcmp(__thisModule.name, "ecereCOM")) && Load != COM_LOAD_FUNCTION))
+ (Load && (!__thisModule || !__thisModule.name || !strcmp(__thisModule.name, "ecereCOM")) && Load != (void *)COM_LOAD_FUNCTION))
{
Module module;
for(module = fromModule.application.allModules.first; module; module = module.next)
}
}
-public dllexport void eEnum_AddFixedValue(Class _class, const char * string, int value)
+public dllexport void eEnum_AddFixedValue(Class _class, const char * string, int64 value)
{
if(_class && _class.type == enumClass)
{
EnumClassData data = (EnumClassData)_class.data;
- NamedLink item;
+ NamedLink64 item;
for(item = data.values.first; item; item = item.next)
if(!strcmp(item.name, string))
break;
if(!item)
{
- data.values.Add(NamedLink { data = (void *)value, name = CopyString(string) });
+ data.values.Add(NamedLink64 { data = value, name = CopyString(string) });
if(value > data.largest)
data.largest = value;
}
}
}
-public dllexport int eEnum_AddValue(Class _class, const char * string)
+public dllexport int64 eEnum_AddValue(Class _class, const char * string)
{
if(_class && _class.type == enumClass)
{
EnumClassData data = (EnumClassData)_class.data;
- int value = ((int) data.largest) + 1;
- NamedLink item;
+ int64 value = data.largest + 1;
+ NamedLink64 item;
for(item = data.values.first; item; item = item.next)
if(!strcmp(item.name, string))
break;
if(!item)
{
- data.values.Add(NamedLink { data = (void *)value, name = CopyString(string) });
+ data.values.Add(NamedLink64 { data = value, name = CopyString(string) });
if(value > data.largest)
data.largest = value;
return value;
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);
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;
}
}
-public dllexport void eInstance_WatchDestruction(Instance instance, Instance object, void (*callback)(Instance, Instance))
+public dllexport void eInstance_WatchDestruction(Instance instance, Instance object, void (*callback)(void *, void *))
{
OldList * watchers = (OldList *)((byte *)instance + instance._class.destructionWatchOffset);
watchers->Add(Watcher { callback = callback, object = object });
return Instance_LocateModule(name, fileName);
}
+/*
+#if (defined(__WORDSIZE) && __WORDSIZE == 8) || defined(__x86_64__)
+#define _64BIT 1
+#else
+#define _64BIT 0
+#endif
+
+#define arch_PointerSize sizeof(void *)
+#define structSize_Instance (_64BIT ? 24 : 12)
+#define structSize_Module (_64BIT ? 560 : 300)
+#define structSize_BinaryTree (_64BIT ? 32 : 16)
+#define structSize_OldList (_64BIT ? 32 : 20)
+#define structSize_NamedLink64 (_64BIT ? 32 : 24)
+#define structSize_ClassTemplateArgument (_64BIT ? 16 : 8)
+#define structSize_ClassTemplateParameter (_64BIT ? 64 : 40)
+#define structSize_OldLink (_64BIT ? 24 : 12)
+#define structSize_BTNamedLink (_64BIT ? 48 : 24)
+#define structSize_Application (_64BIT ? 800 : 428)
+#define structSize_Watcher (_64BIT ? 32 : 16)
+#define structSize_SelfWatcher (_64BIT ? 32 : 16)
+#define structSize_GlobalFunction (_64BIT ? 72 : 36)
+#define structSize_DefinedExpression (_64BIT ? 40 : 20)
+#define structSize_BitMember (_64BIT ? 96 : 64)
+#define structSize_DataMember (_64BIT ? 160 : 96)
+#define structSize_ClassProperty (_64BIT ? 80 : 40)
+#define structSize_Method (_64BIT ? 96 : 52)
+#define structSize_Property (_64BIT ? 152 : 88)
+#define structSize_Class (_64BIT ? 624 : 376)
+*/
+
static void LoadCOM(Module module)
{
bool force64Bits = (module.application.isGUIApp & 2) ? true : false;
instanceClass.memberID = -3;
instanceClass.startMemberID = -3;
- eClass_AddDataMember(instanceClass, "_vTbl", "int (**)()", pointerSize, pointerSize, publicAccess);
+ eClass_AddDataMember(instanceClass, "_vTbl", "void **", pointerSize, pointerSize, publicAccess);
eClass_AddDataMember(instanceClass, "_class", "ecere::com::Class", pointerSize, pointerSize, publicAccess);
eClass_AddDataMember(instanceClass, "_refCount", "int", sizeof(int), sizeof(int), publicAccess);
}
eSystem_RegisterFunction("qsort", "void qsort(void *, uintsize, uintsize, int (*)(void *, void *))", qsort, module, baseSystemAccess);
eSystem_RegisterFunction("strtod", "double strtod(const char*, char**)", strtod, module, baseSystemAccess);
eSystem_RegisterFunction("strtol", "int strtol(const char*, char**, int base)", strtol, module, baseSystemAccess);
+ eSystem_RegisterFunction("strtoul", "unsigned long strtoul(const char * nptr, char ** endptr, int base)", strtoul, module, baseSystemAccess);
+ eSystem_RegisterFunction("strtoll", "int64 strtoll(const char * nptr, char ** endptr, int base)", strtoll, module, baseSystemAccess);
+ eSystem_RegisterFunction("strtoull", "uint64 strtoull(const char * nptr, char ** endptr, int base)", strtoull, module, baseSystemAccess);
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);
eSystem_RegisterFunction("strcspn", "uintsize strcspn(const char *, const char *)", strcspn, module, baseSystemAccess);
eSystem_RegisterFunction("strpbrk", "char * strpbrk(const char *, const char *)", strpbrk, module, baseSystemAccess);
- eSystem_RegisterDefine("fstrcmp", "(GetRuntimePlatform() == win32) ? strcmpi : strcmp", module, baseSystemAccess);
+ eSystem_RegisterDefine("fstrcmp", "(__runtimePlatform == win32) ? strcmpi : strcmp", module, baseSystemAccess);
//#if defined(__GNUC__)
eSystem_RegisterDefine("strcmpi", "strcasecmp", module, baseSystemAccess);
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("strtoul", "unsigned long strtoul(const char * nptr, char ** endptr, int base)", strtoul, module, baseSystemAccess);
- eSystem_RegisterFunction("strtoll", "int64 strtoll(const char * nptr, char ** endptr, int base)", strtoul, module, baseSystemAccess);
- eSystem_RegisterFunction("strtoull", "uint64 strtoull(const char * nptr, char ** endptr, int base)", strtoul, module, baseSystemAccess);
+ eSystem_RegisterFunction("isblank", "int isblank(int c)", isblank, module, baseSystemAccess);
}
{
int c;
int d = 0;
+ byte * byteDest = (byte *)dest;
for(c = 0; source[c]; c++)
{
unichar ch = ((byte *)source)[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;
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;
}
}
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)
int c;
int d = 0;
uint16 u16;
+ byte * byteDest = (byte *)dest;
for(c = 0; (u16 = source[c]); c++)
{
unichar ch;
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;
}
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;
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;
}
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;
}
}
namespace com;
+
+#if !defined(MEMINFO) && defined(MEMTRACKING)
+import "Map"
+
+Map<Class, int> blocksByClass { };
+Map<Class, uintsize> sizeByClass { };
+#endif
+
+public void queryMemInfo(char * string)
+{
+#if !defined(MEMINFO) && defined(MEMTRACKING) && !defined(DISABLE_MEMMGR)
+ char s[1024];
+ int p;
+ uint numBlocks = 0;
+ uintsize totalMemUsed = 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);
+ totalMemUsed += pool->usedSpace;
+ strcat(string, s);
+ }
+ }
+
+
+ blocksByClass.Free();
+ sizeByClass.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]++;
+ sizeByClass[c] += 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 = sizeByClass[_class];
+ float totalSize = (float) size / 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 Memory in use: %.02f\n", (float)(totalMemUsed + 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
+}