Jump to content

  • Log In with Google      Sign In   
  • Create Account


FDsagizi

Member Since 03 Apr 2009
Offline Last Active Apr 27 2014 09:09 PM

Topics I've Started

change memory allocator for CScriptArray addon

27 January 2014 - 12:11 AM

Hi smile.png , in our project - game. We are use only script addon, and we need use Our memory allocator 

and

I create possible to change memory allocator for addon CScriptArray

 

scriptarray.patch

 

 
Index: scriptarray.cpp
===================================================================
--- scriptarray.cpp (revision 1823)
+++ scriptarray.cpp (working copy)
@@ -13,6 +13,14 @@
 static void RegisterScriptArray_Native(asIScriptEngine *engine);
 static void RegisterScriptArray_Generic(asIScriptEngine *engine);
 
+asALLOCFUNC_t CScriptArray::userAlloc = ::malloc;
+asFREEFUNC_t CScriptArray::userFree = ::free;
+
+void CScriptArray::asSetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc){
+ userAlloc = allocFunc;
+ userFree = freeFunc;
+}
+
 struct SArrayBuffer
 {
  asDWORD maxElements;
@@ -36,13 +44,16 @@
 static void CleanupObjectTypeArrayCache(asIObjectType *type)
 {
  SArrayCache *cache = reinterpret_cast<SArrayCache*>(type->GetUserData(ARRAY_CACHE));
- if( cache )
- delete cache;
+ if( cache ){
+    CScriptArray::userFree( cache );
+    cache = nullptr;
+ }
 }
 
 static CScriptArray* ScriptArrayFactory2(asIObjectType *ot, asUINT length)
 {
- CScriptArray *a = new CScriptArray(length, ot);
+ void *data = CScriptArray::userAlloc( sizeof( CScriptArray ) );
+ CScriptArray *a = new( data ) CScriptArray(length, ot);
 
  // It's possible the constructor raised a script exception, in which case we
  // need to free the memory and return null instead, else we get a memory leak.
@@ -58,8 +69,8 @@
 
 static CScriptArray* ScriptArrayListFactory(asIObjectType *ot, void *initList)
 {
- CScriptArray *a = new CScriptArray(ot, initList);
-
+ void *data = CScriptArray::userAlloc( sizeof( CScriptArray ) );
+ CScriptArray *a = new( data ) CScriptArray(ot, initList);
  // It's possible the constructor raised a script exception, in which case we
  // need to free the memory and return null instead, else we get a memory leak.
  asIScriptContext *ctx = asGetActiveContext();
@@ -74,8 +85,8 @@
 
 static CScriptArray* ScriptArrayFactoryDefVal(asIObjectType *ot, asUINT length, void *defVal)
 {
- CScriptArray *a = new CScriptArray(length, defVal, ot);
-
+ void *data = CScriptArray::userAlloc( sizeof( CScriptArray ) );
+ CScriptArray *a = new( data ) CScriptArray(length, defVal, ot);
  // It's possible the constructor raised a script exception, in which case we
  // need to free the memory and return null instead, else we get a memory leak.
  asIScriptContext *ctx = asGetActiveContext();
@@ -268,6 +279,7 @@
  // Same as removeAt
  r = engine->RegisterObjectMethod("array<T>", "void erase(uint)", asMETHOD(CScriptArray, RemoveAt), asCALL_THISCALL); assert( r >= 0 );
 #endif
+
 }
 
 CScriptArray &CScriptArray::operator=(const CScriptArray &other)
@@ -523,11 +535,8 @@
 
  // Allocate memory for the buffer
  SArrayBuffer *newBuffer;
- #if defined(__S3E__) // Marmalade doesn't understand (nothrow)
- newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*maxElements];
- #else
- newBuffer = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1 + elementSize*maxElements];
- #endif
+ newBuffer =  (SArrayBuffer*)userAlloc( sizeof(SArrayBuffer)-1 + elementSize*maxElements );
+
  if( newBuffer )
  {
  newBuffer->numElements = buffer->numElements;
@@ -545,8 +554,7 @@
  memcpy(newBuffer->data, buffer->data, buffer->numElements*elementSize);
 
  // Release the old buffer
- delete[] (asBYTE*)buffer;
-
+ userFree( buffer );
  buffer = newBuffer;
 }
 
@@ -584,11 +592,8 @@
  {
  // Allocate memory for the buffer
  SArrayBuffer *newBuffer;
- #if defined(__S3E__) // Marmalade doesn't understand (nothrow)
- newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
- #else
- newBuffer = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
- #endif
+ newBuffer = (SArrayBuffer*)userAlloc( sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta) );
+
  if( newBuffer )
  {
  newBuffer->numElements = buffer->numElements + delta;
@@ -614,7 +619,7 @@
  Construct(newBuffer, at, at+delta);
 
  // Release the old buffer
- delete[] (asBYTE*)buffer;
+ userFree( buffer );
 
  buffer = newBuffer;
  }
@@ -752,19 +757,11 @@
 {
  if( subTypeId & asTYPEID_MASK_OBJECT )
  {
- #if defined(__S3E__) // Marmalade doesn't understand (nothrow)
- *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
- #else
- *buf = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
- #endif
+ *buf = (SArrayBuffer*)userAlloc( sizeof(SArrayBuffer)-1+sizeof(void*)*numElements );
  }
  else
  {
- #if defined(__S3E__)
- *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
- #else
- *buf = (SArrayBuffer*)new (nothrow) asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
- #endif
+ *buf = (SArrayBuffer*)userAlloc( sizeof(SArrayBuffer)-1+elementSize*numElements ); 
  }
 
  if( *buf )
@@ -786,9 +783,8 @@
 void CScriptArray::DeleteBuffer(SArrayBuffer *buf)
 {
  Destruct(buf, 0, buf->numElements);
-
  // Free the buffer
- delete[] (asBYTE*)buf;
+ userFree( buf );
 }
 
 // internal
@@ -1446,7 +1442,9 @@
  }
 
  // Create the cache
- cache = new SArrayCache();
+ void *data = CScriptArray::userAlloc( sizeof( SArrayCache ) );
+ cache = new( data ) SArrayCache();
+
  memset(cache, 0, sizeof(SArrayCache));
 
  // If the sub type is a handle to const, then the methods must be const too
@@ -1569,7 +1567,8 @@
  {
  // When reaching 0 no more references to this instance
  // exists and the object should be destroyed
- delete this;
+ this->~CScriptArray();
+ userFree( (void*)this );
  }
 }
 
Index: scriptarray.h
===================================================================
--- scriptarray.h (revision 1823)
+++ scriptarray.h (working copy)
@@ -25,6 +25,9 @@
 class CScriptArray
 {
 public:
+ static asALLOCFUNC_t userAlloc;
+ static asFREEFUNC_t  userFree;
+ static void asSetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
  CScriptArray(asIObjectType *ot, void *initBuf); // Called from script when initialized with list
  CScriptArray(asUINT length, asIObjectType *ot);
  CScriptArray(asUINT length, void *defVal, asIObjectType *ot);
 

 

 

 

 

 

 


asOBJ_VALUE | asOBJ_APP_CLASS_CD do not call Destructor on ARMv7

24 January 2014 - 10:47 AM

Hi smile.png

On Win32, Linux, MaxOs work fine, bug on - ARM( iPad ) 

 

after call C++ function, and pass in here asOBJ_VALUE type( my string's ), he create a copy, but not call destructor

 

simple code

void startGame(){
   MyStr m1;
   TestMyStrLeak( m1 );
}

C++

int con_count = 0;
int des_count = 0;

class MyStr{
public:
    MyStr(){
        std::cout << "CONSTR: " << ++con_count << "\n";
    }
    MyStr( const MyStr &other ){
        std::cout << "CONSTR Ohter: " << ++con_count << "\n";
    }
    ~MyStr(){
        std::cout << "DESTR: " << ++des_count << "\n";
    }
    static void Construct(MyStr *thisPointer){
        new(thisPointer) MyStr();
    }
    static void CopyConstruct(const MyStr &other, MyStr *thisPointer){
        new(thisPointer) MyStr(other);
    }
    static void Destruct(MyStr *thisPointer){
        thisPointer->~MyStr();
    }
};

void MemLeakMyStr( MyStr my_str ){
    std::cout << "L\n";
}

void RegMyStr( asIScriptEngine *en ){
    int r;
    r=en->RegisterObjectType("MyStr", sizeof(MyStr), asOBJ_VALUE | asOBJ_APP_CLASS_CD ); nu_assert( r >= 0 );
    r=en->RegisterObjectBehaviour("MyStr", asBEHAVE_CONSTRUCT,  "void f()",             asFUNCTION(MyStr::Construct), asCALL_CDECL_OBJLAST); nu_assert( r >= 0 );
    r=en->RegisterObjectBehaviour("MyStr", asBEHAVE_CONSTRUCT,  "void f(const MyStr &)",asFUNCTION(MyStr::CopyConstruct), asCALL_CDECL_OBJLAST); nu_assert( r >= 0 );
    r=en->RegisterObjectBehaviour("MyStr", asBEHAVE_DESTRUCT,   "void f()",              asFUNCTION(MyStr::Destruct),  asCALL_CDECL_OBJLAST); nu_assert( r >= 0 );
    r=en->RegisterGlobalFunction( "void TestMyStrLeak(MyStr name )",asFUNCTION(MemLeakMyStr),asCALL_CDECL);nu_assert(r>=0);
}

output:

 

CONSTR: 1

CONSTR Ohter: 2

L

DESTR: 1

 

Engine flags:

 

engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, false);     

engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);     

engine->SetEngineProperty( asEP_ALLOW_IMPLICIT_HANDLE_TYPES, true );     

engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, true);     

engine->SetEngineProperty( asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, true );     

engine->SetEngineProperty(asEP_COPY_SCRIPT_SECTIONS, true);

 

 

With ref type ( MyStr & name ) or generic type, work fine

Use last svn version


Can't implicitly convert from 'const TestClass@' to 'TestClass@&'.

22 January 2014 - 02:08 AM

Hi smile.png  Im here againe) And soon show our game)

 

Today update for new AS from svn. and found bug:

 

Message : Can't implicitly convert from 'const TestClass@' to 'TestClass@&'.

Sample code:

class TestClass{}

void Test(){
   array<TestClass@> test;
   TestClass @s = test.Size() > 0 ? test[i] : null;
}

@a = @b = c;

17 October 2013 - 10:34 PM

Hello smile.png

 

This code crashed.

class TestClass{}
TestClass @t1;
TestClass @t2;
void startGame( string &param ){
    TestClass @t = TestClass();
    @t1 = @t2 = t;
}

Implicit conversion of value is not exact

30 September 2013 - 03:10 AM

Hi. some times i have problem with float

AS work strange with this.

 

example, that code not work because he not understand '0', he think is a "int".

float ret = true ? 0 : 0.1; 

and ok, that code make warning! 

 

next code, work wrong

float f = 0.1;
float ret = 0.2;
ret += false ? 0 : f * 15 ; // ( 15 * 0.1 == 1.5 ) then 1.5 + 0.2 == 1.7 ok ?
output( "ret: " + ret ); // BUT result == 1.2

PARTNERS