cinvoke-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cinvoke-svn] r70 - trunk/cinvoke/bindings/java/org/cinvoke


From: will
Subject: [cinvoke-svn] r70 - trunk/cinvoke/bindings/java/org/cinvoke
Date: 4 Jul 2006 01:28:22 -0400

Author: will
Date: 2006-07-04 01:28:22 -0400 (Tue, 04 Jul 2006)
New Revision: 70

Modified:
   trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java
Log:
implemented some array marshaling


Modified: trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java      2006-07-04 
04:25:11 UTC (rev 69)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java      2006-07-04 
05:28:22 UTC (rev 70)
@@ -128,7 +128,7 @@
                        else
                                return null;
                } finally {
-                       freeParams(method, params);
+                       freeParams(method, params, args);
                }
        }
 
@@ -240,48 +240,126 @@
                Class[] pclasses = method.getParameterTypes();
 
                for (int i = 0; i < ret.length; i++) {
-                       if (pclasses[i].isArray()) {
-                               // XXX
-                       } else if (pclasses[i].isInterface()) {
+                       if (pclasses[i].isArray())
+                               ret[i] = 
marshalArray(pclasses[i].getComponentType(),
+                                               rawparams[i]);
+                       else if (pclasses[i].isInterface()) {
                                // XXX create callback
-                       } else if (pclasses[i].equals(String.class)) {
-                               long l;
-                               if (_encoding == CInvoke.ENC.UNICODE)
-                                       l = 
Natives.stringToPtrUnicode((String)rawparams[i]);
-                               else 
-                                       l = 
Natives.stringToPtrUTF8((String)rawparams[i]);
-                               if (l == 0)
-                                       throw new OutOfMemoryError();
-                               ret[i] = new Ptr(l);
-                       } else
-                               ret[i] = rawparams[i];
+                       } else if (pclasses[i].equals(String.class))
+                               ret[i] = marshalString((String)rawparams[i]);
+                       else
+                               ret[i] = marshalBasic(rawparams[i], 
pclasses[i]);
                }
 
                return ret;
        }
 
-       private void freeParams(Method method, Object[] params) {
+       private void freeParams(Method method, Object[] params,
+               Object[] rawparams) {
                Class[] pclasses = method.getParameterTypes();
                for (int i = 0; i < params.length; i++) {
-                       if (pclasses[i].isArray() || 
pclasses[i].equals(String.class)) {
-                               Natives.free(((Ptr)params[i]).longValue());
+                       if (pclasses[i].isArray()) {
+                               long l = ((Ptr)params[i]).longValue();
+                               if (l != 0) {
+                                       Class comp = 
pclasses[i].getComponentType();
+                                       if (comp.equals(String.class)) {
+                                               long incr = sizeof(comp);
+                                               long tmp = l;
+                                               int len = 
Array.getLength(rawparams[i]);
+                                               for (int j = 0; j < len; j++) {
+                                                       Natives.free(tmp);
+                                                       tmp += incr;
+                                               }
+                                       }
+                                       Natives.free(l);
+                               }
                        } else if (pclasses[i].isInterface()) {
                                // XXX delete callback
+                       } else if (pclasses[i].equals(String.class)) {
+                               Natives.free(((Ptr)params[i]).longValue());
                        }
                }
        }
 
        private Object unmarshalReturnValue(Object ret, Class type) {
                if (type.equals(String.class)) {
+                       Ptr p = (Ptr)ret;
+                       if (p.longValue() == 0) return null;
                        if (_encoding == CInvoke.ENC.UNICODE)
-                               return 
Natives.ptrToStringUnicode(((Ptr)ret).longValue(), -1);
+                               return 
Natives.ptrToStringUnicode(p.longValue(), -1);
                        else
-                               return 
Natives.ptrToStringUTF8(((Ptr)ret).longValue());
+                               return Natives.ptrToStringUTF8(p.longValue());
                } else
                        return ret;
        }
 
+       private void marshalStruct(long outp, Object s, Class type) {
+               if (s == null)
+                       throw new CInvokeError("Invalid null value");
+               // XXX
+               // thought: have writeValue return int == size written
+       }
+
+       private Object marshalBasic(Object o, Class cls) {
+               if (o == null) {
+                       if (cls.equals(Ptr.class))
+                               return new Ptr(0);
+                       else
+                               throw new CInvokeError("Invalid null value");
+               } else
+                       return o;
+       }
+
+       private Ptr marshalString(String str) {
+               if (str == null) return new Ptr(0);
+
+               long l;
+               if (_encoding == CInvoke.ENC.UNICODE)
+                       l = Natives.stringToPtrUnicode(str);
+               else 
+                       l = Natives.stringToPtrUTF8(str);
+               if (l == 0)
+                       throw new OutOfMemoryError();
+               return new Ptr(l);
+       }
+
+       private Ptr marshalArray(Class eltype, Object val) {
+               if (val == null) return new Ptr(0);
+
+               int numels = Array.getLength(val);
+               int elsize = sizeof(eltype);
+               int len = numels * elsize;
+               
+               long ret = Natives.alloc(len);
+               if (ret == 0)
+                       throw new OutOfMemoryError();
+               
+               boolean string = false;
+               boolean strct = false;
+               int itype = gettypeint(eltype, false);
+               if (itype == -999)
+                       strct = true;
+               if (eltype.equals(String.class))
+                       string = true;
+
+               long r = ret;
+               for (int i = 0; i < numels; i++) {
+                       Object o = Array.get(val, i);
+                       if (strct)
+                               marshalStruct(r, o, eltype);
+                       else if (string)
+                               Natives.writeValue(r, marshalString((String)o), 
itype);
+                       else
+                               Natives.writeValue(r, marshalBasic(o, eltype), 
itype);
+                       r += elsize;
+               }
+               
+               return new Ptr(ret);
+       }
+
        private void unmarshalArray(Ptr ptr, Object[] arr, Class type) {
+               if (ptr.longValue() == 0)
+                       throw new CInvokeError("Reading array from null 
pointer");
                // XXX
        }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]