Changeset 8

Show
Ignore:
Timestamp:
04/16/07 11:48:20 (2 years ago)
Author:
bwilliams
Message:

Update from ross, fixing null pointer problem and update to the java wrappers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/binary-mq/libsipc/bindings/java/com/tresys/sipc/Sipc.java

    r4 r8  
    2525 * @author David Windsor <dwindsor@tresys.com> 
    2626 * @author Norman Patrick <npatrick@tresys.com> 
     27 * @author L. Ross Raszewski <lraszewski@tresys.com> 
    2728 */ 
    2829public abstract class Sipc { 
     
    4142        protected long handle; /* internal handle */ 
    4243 
     44        protected long size_handle; /* internal message size handle */ 
    4345        private java.nio.ByteBuffer dataPtr = null; /* internal pointer to data */ 
    4446 
     
    7779        public void Init(boolean write) { 
    7880                handle = libsipc.sipc_init(Key, GetIPCType(), DATA_LEN, write ? 1 : 0); 
     81                size_handle=libsipc.sipc_create_length(); 
    7982        } 
    8083 
     
    8689                if(handle != 0) { 
    8790                        libsipc.sipc_destroy_handle(handle); 
     91                        libsipc.sipc_destroy_length(size_handle); 
     92                        size_handle=0; 
    8893                        handle = 0; 
    8994                        dataPtr = null; 
     
    146151         */ 
    147152        public java.nio.ByteBuffer ReadData() { 
    148                 return libsipc.sipc_recv_data(handle, GetIPCType()); 
    149         } 
    150  
     153                return libsipc.sipc_recv_data(handle, GetIPCType(), size_handle); 
     154        } 
     155 
     156   /** 
     157        * Returns the size of the last successful ReadData() operation. 
     158        * This should be identical to the capacity of the buffer returned by 
     159        * ReadData().  Return value is undefined if ReadData has not been called. 
     160        *  
     161        * @return The size of the buffer returned by the most recent call to ReadData() 
     162        */ 
     163        public int GetReadSize() { 
     164                return libsipc.sipc_get_length(size_handle); 
     165        } 
    151166        /** 
    152167         * Get a reference to the IPC handle's internal data buffer. 
     
    159174        public java.nio.ByteBuffer GetDataPtr() { 
    160175                if(isConnected && dataPtr == null) { 
    161                         dataPtr = libsipc.sipc_get_data_ptr(handle, DATA_LEN); 
    162                 } 
     176                        dataPtr = libsipc.sipc_get_data_ptr(handle); 
     177                } 
     178                 
    163179 
    164180                return dataPtr; 
     
    169185         * connected state for this to succeed. 
    170186         * 
     187         * @param len Length of the message to be sent 
    171188         * @return  0 on success, <0 on error. 
    172189         * @see     #Connect() Connect 
    173190         */ 
     191        public int SendData(int len) { 
     192                return libsipc.sipc_send_data(handle, len); 
     193        } 
     194         
     195        /** 
     196         * Send data to the communications channel. The amount of data to  
     197         * send is determined by the current limit of the data buffer. 
     198         * 
     199         * @return 0 on success, <0 on error 
     200         * @see #SendData(int) SendData(int) 
     201         */ 
    174202        public int SendData() { 
    175                 return libsipc.sipc_send_data(handle); 
     203                return SendData(dataPtr.limit()); 
    176204        } 
    177205} 
  • branches/binary-mq/libsipc/bindings/java/com/tresys/sipc/libsipc.java

    r4 r8  
     1/* This file has been hand-modified -- do not regenerate */ 
     2 
    13/* ---------------------------------------------------------------------------- 
    24 * This file was automatically generated by SWIG (http://www.swig.org). 
     
    911package com.tresys.sipc; 
    1012 
    11 final class libsipc { 
     13class libsipc { 
     14 
    1215        static { 
    1316                System.loadLibrary("sipcwrapper"); 
    1417        } 
    15  
    16         public final static int SIPC_SYSV_SHM = 0; 
    17  
    18         public final static int SIPC_SYSV_MQUEUES = 1; 
    19  
    20         public final static int SIPC_NUM_TYPES = 2; 
    21  
    22         public final static native long sipc_init(String key, int ipc_type, 
    23                                                                                                 long size, int sender); 
    24  
    25         public final static native void sipc_destroy_handle(long handle); 
    26  
    27         public final static native void sipc_destroy_resource(String key, 
    28                                                                                                                         int ipc_type); 
    29  
    30         public final static native int sipc_create(String key, int ipc_type); 
    31  
    32         public final static native int sipc_connect(long handle); 
    33  
    34         public final static native void sipc_disconnect(long handle); 
    35  
    36         public final static native int sipc_send_data(long handle); 
    37  
    38         public final static native java.nio.ByteBuffer sipc_recv_data(long handle, 
    39                                                                                                                                         int ipc_type); 
    40  
    41         public final static native int sipc_shm_recv_done(long handle); 
    42  
    43         public final static native java.nio.ByteBuffer sipc_get_data_ptr( 
    44                                                                                                                                                 long jarg1, 
    45                                                                                                                                                 long size); 
     18  public final static int SIPC_SYSV_SHM=0; 
     19  public final static int SIPC_SYSV_MQUEUES=1; 
     20  public final static int SIPC_NUM_TYPES=2; 
     21  public final static native long sipc_init(String jarg1, int jarg2, long jarg3, int jarg4); 
     22  public final static native void sipc_destroy_handle(long jarg1); 
     23  public final static native void sipc_destroy_resource(String jarg1, int jarg2); 
     24  public final static native int sipc_create(String jarg1, int jarg2); 
     25  public final static native int sipc_connect(long jarg1); 
     26  public final static native void sipc_disconnect(long jarg1); 
     27  public final static native int sipc_send_data(long jarg1, int jarg2); 
     28    /* This method is for internal use by sipc_recv_data */ 
     29  private final static native byte[] sipc_recv_datad(long handle,  
     30                                                     int ipc_type,  
     31                                                     long size_handle); 
     32    /* Wrap the result of the native call in a ByteBuffer */   
     33  public final static java.nio.ByteBuffer sipc_recv_data(long handle,  
     34                                                           int ipc_type,  
     35                                                           long size_handle) 
     36    { 
     37        byte [] buf=sipc_recv_datad(handle,ipc_type,size_handle); 
     38        return (buf==null? null: 
     39                java.nio.ByteBuffer.wrap(buf,0,sipc_get_length(size_handle))); 
     40    } 
     41  public final static native java.nio.ByteBuffer sipc_get_data_ptr(long jarg1); 
     42  public final static native void sipc_error(long jarg1, String jarg2); 
     43  public final static native int sipc_shm_recv_done(long jarg1); 
     44  public final static native long sipc_create_length(); 
     45  public final static native int sipc_get_length(long jarg1); 
     46  public final static native void sipc_destroy_length(long jarg1); 
    4647} 
  • branches/binary-mq/libsipc/bindings/java/examples/MQ_Reader.java

    r4 r8  
    6161                ByteBuffer bbuf = queue.ReadData(); 
    6262                String data = ByteBufferToString(bbuf); 
    63                 while(data.length() > 0) { 
    64                         /* Check for an end of transmission marker */ 
    65                         if(data.equals(data_end)) 
    66                                 break; 
    67  
    68                         System.out.println(data); 
    69                         bbuf.clear(); 
    70                         bbuf = queue.ReadData(); 
    71                         data = ByteBufferToString(bbuf); 
     63                System.out.println("Recieved " + queue.GetReadSize()); 
     64                while(bbuf!=null && data.length() > 0) {  
     65                    /* Read off the End-Of-Transmission Marker */ 
     66                    queue.ReadData(); 
     67                     
     68                    /* Check for an end of transmission marker */ 
     69                    if(data.startsWith(data_end)) 
     70                        break; 
     71                     
     72                    System.out.println(data); 
     73                    bbuf.clear(); 
     74                    bbuf = queue.ReadData(); 
     75                    data = ByteBufferToString(bbuf); 
    7276                } 
    73  
    7477                queue.Disconnect(); 
    7578        } 
  • branches/binary-mq/libsipc/bindings/java/examples/MQ_Sender.java

    r4 r8  
    2323import java.io.*; 
    2424import com.tresys.sipc.*; 
     25 
     26import java.nio.*; 
     27import java.nio.charset.*; 
    2528 
    2629/** 
     
    7376                        java.nio.channels.FileChannel fc = stream.getChannel(); 
    7477                        int read = fc.read(buffer); 
    75                         buffer.flip(); 
    76  
    77                         if(read < queue.DATA_LEN) 
    78                                 buffer.put((byte) 0); 
    79  
    8078                        while(read >= 0) { 
    81                                 if(queue.SendData() < 0) 
     79                            /*Explicit-length version of  
     80                              SendData: Send the number of bytes we read */ 
     81                                if(queue.SendData(read) < 0) 
    8282                                        throw new IOException("Cannot send data."); 
    8383 
    8484                                buffer.clear(); 
    85                                 read = fc.read(buffer); 
    86                                 if(read < queue.DATA_LEN) 
    87                                         buffer.put((byte) 0); 
     85                                read = fc.read(buffer); 
    8886                        } 
    8987 
     
    9290                        buffer.put(MQ_Sender.data_end.getBytes()); 
    9391                        buffer.put((byte) 0); 
     92                        /* Inferred-length version of SendData:  
     93                           flip the buffer to set its limit, and  
     94                           Sipc will use that as the data length 
     95                        */ 
     96                        buffer.flip(); 
    9497                        if(queue.SendData() < 0) 
    9598                                throw new IOException("Cannot send end of transmission marker"); 
  • branches/binary-mq/libsipc/bindings/java/sipc_java_wrapper/sipc_wrap.c

    r4 r8  
     1/* This file has been modified by hand. Do not regenerate it. */ 
     2 
     3 
    14/* ---------------------------------------------------------------------------- 
    25 * This file was automatically generated by SWIG (http://www.swig.org). 
     
    1417 * ----------------------------------------------------------------------------- */ 
    1518 
    16 /* template workaround for compilers that cannot correctly implement the C++ standard */ 
     19/* template workaround for compilers that cannot correctly implement the  
     20   C++ standard */ 
    1721#ifndef SWIGTEMPLATEDISAMBIGUATOR 
    1822# if defined(__SUNPRO_CC) 
     
    7276#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 
    7377#  ifndef GCC_HASCLASSVISIBILITY 
    74 #    define GCC_HASCLASSVISIBILITY 
     78#    define GCC_HASCLASSVIS\IBILITY 
    7579#  endif 
    7680#endif 
     
    173177 
    174178 
    175  /* Includes the header in the wrapper code */ 
    176  #include <sipc/sipc.h> 
    177   
     179/* Includes the header in the wrapper code */ 
     180#include <sipc/sipc.h> 
     181 
     182/* Need the internal definition of the sipc structure to generate the output 
     183   bytebuffer */ 
     184struct sipc { 
     185        key_t key; 
     186        int ipc_type; 
     187        int sender; 
     188        union { 
     189                int fd; /* file desciptor for socket connections */ 
     190                struct shm { /* shm data */ 
     191                        int shmid; /* ID of segment */ 
     192                        int msqid; /* notify channel */ 
     193                        /* sipc_t *mqueue; handle to notify channel */ 
     194                } s; 
     195                int msqid; /* message queue */ 
     196        }; 
     197 
     198        char *data; 
     199        size_t len; 
     200        size_t msg_len; /* Length in bytes of the next message */ 
     201        size_t copied;  /* Number of bytes xmitted so far */ 
     202        struct sipc_func_table *funcs; 
     203}; 
    178204 
    179205#ifdef __cplusplus 
     
    181207#endif 
    182208 
    183 // sipc_init 
    184 JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1init(JNIEnv *jenv, jclass jcls, jstring key, jint ipc_type, jlong size, jint sender) { 
     209JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1init(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2, jlong jarg3, jint jarg4) { 
    185210  jlong jresult = 0 ; 
    186211  char *arg1 = (char *) 0 ; 
     
    193218  (void)jcls; 
    194219  arg1 = 0; 
    195   if (key) { 
    196     arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0); 
     220  if (jarg1) { 
     221    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 
    197222    if (!arg1) return 0; 
    198223  } 
    199   arg2 = (int)ipc_type;  
    200   arg3 = (size_t)size;  
    201   arg4 = (int)sender;  
     224  arg2 = (int)jarg2;  
     225  arg3 = (size_t)jarg3;  
     226  arg4 = (int)jarg4;  
    202227  result = (sipc_t *)sipc_init(arg1,arg2,arg3,arg4); 
    203228  *(sipc_t **)&jresult = result;  
    204   if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, key, arg1); 
    205   return jresult; 
    206 } 
    207  
    208 // sipc_destroy_handle 
    209 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1handle(JNIEnv *jenv, jclass jcls, jlong handle) { 
    210   sipc_t *arg1 = (sipc_t *) 0 ; 
    211    
    212   (void)jenv; 
    213   (void)jcls; 
    214   arg1 = *(sipc_t **)&handle;  
     229  if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, arg1); 
     230  return jresult; 
     231} 
     232 
     233 
     234JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1handle(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
     235  sipc_t *arg1 = (sipc_t *) 0 ; 
     236   
     237  (void)jenv; 
     238  (void)jcls; 
     239  arg1 = *(sipc_t **)&jarg1;  
    215240  sipc_destroy_handle(arg1); 
    216241} 
    217242 
    218 // sipc_destroy_resource 
    219 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1resource(JNIEnv *jenv, jclass jcls, jstring key, jint ipc_type) { 
     243 
     244JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1resource(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) { 
    220245  char *arg1 = (char *) 0 ; 
    221246  int arg2 ; 
     
    224249  (void)jcls; 
    225250  arg1 = 0; 
    226   if (key) { 
    227     arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0); 
     251  if (jarg1) { 
     252    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 
    228253    if (!arg1) return ; 
    229254  } 
    230   arg2 = (int)ipc_type;  
     255  arg2 = (int)jarg2;  
    231256  sipc_destroy_resource(arg1,arg2); 
    232   if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, key, arg1); 
    233 } 
    234  
    235 // sipc_create 
    236 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1create(JNIEnv *jenv, jclass jcls, jstring key, jint ipc_type) { 
     257  if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, arg1); 
     258} 
     259 
     260 
     261JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1create(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) { 
    237262  jint jresult = 0 ; 
    238263  char *arg1 = (char *) 0 ; 
     
    243268  (void)jcls; 
    244269  arg1 = 0; 
    245   if (key) { 
    246     arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0); 
     270  if (jarg1) { 
     271    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 
    247272    if (!arg1) return 0; 
    248273  } 
    249   arg2 = (int)ipc_type;  
     274  arg2 = (int)jarg2;  
    250275  result = (int)sipc_create(arg1,arg2); 
    251276  jresult = (jint)result;  
    252   if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, key, arg1); 
    253   return jresult; 
    254 } 
    255  
    256 // sipc_connect 
    257 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1connect(JNIEnv *jenv, jclass jcls, jlong handle) { 
     277  if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, arg1); 
     278  return jresult; 
     279} 
     280 
     281 
     282JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1connect(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
    258283  jint jresult = 0 ; 
    259284  sipc_t *arg1 = (sipc_t *) 0 ; 
     
    262287  (void)jenv; 
    263288  (void)jcls; 
    264   arg1 = *(sipc_t **)&handle;  
     289  arg1 = *(sipc_t **)&jarg1;  
    265290  result = (int)sipc_connect(arg1); 
    266291  jresult = (jint)result;  
     
    268293} 
    269294 
    270 // sipc_disconnect 
    271 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1disconnect(JNIEnv *jenv, jclass jcls, jlong handle) { 
    272   sipc_t *arg1 = (sipc_t *) 0 ; 
    273    
    274   (void)jenv; 
    275   (void)jcls; 
    276   arg1 = *(sipc_t **)&handle;  
     295 
     296JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1disconnect(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
     297  sipc_t *arg1 = (sipc_t *) 0 ; 
     298   
     299  (void)jenv; 
     300  (void)jcls; 
     301  arg1 = *(sipc_t **)&jarg1;  
    277302  sipc_disconnect(arg1); 
    278303} 
    279304 
    280 // sipc_send_data 
    281 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1send_1data(JNIEnv *jenv, jclass jcls, jlong handle) { 
     305 
     306JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1send_1data(JNIEnv *jenv, jclass jcls, jlong jarg1, jint jarg2) { 
    282307  jint jresult = 0 ; 
    283308  sipc_t *arg1 = (sipc_t *) 0 ; 
    284   int result; 
    285    
    286   (void)jenv; 
    287   (void)jcls; 
    288   arg1 = *(sipc_t **)&handle;  
    289   result = (int)sipc_send_data(arg1); 
     309  int arg2 ; 
     310  int result; 
     311   
     312  (void)jenv; 
     313  (void)jcls; 
     314  arg1 = *(sipc_t **)&jarg1;  
     315  arg2 = (int)jarg2;  
     316  result = (int)sipc_send_data(arg1,arg2); 
    290317  jresult = (jint)result;  
    291318  return jresult; 
     
    293320 
    294321 
    295 // sipc_recv_data 
    296 JNIEXPORT jobject JNICALL Java_com_tresys_sipc_libsipc_sipc_1recv_1data(JNIEnv *jenv, jclass jcls, jlong handle, jint ipc_type) { 
    297   jobject jresult = 0
     322  JNIEXPORT jbyteArray JNICALL Java_com_tresys_sipc_libsipc_sipc_1recv_1datad(JNIEnv *jenv, jclass jcls, jlong handle, jint ipc_type, jlong size_handle) { 
     323 
     324  jbyteArray jresult = (jbyteArray) 0
    298325  sipc_t *arg1 = (sipc_t *) 0 ; 
    299326  char *arg2 = NULL; 
    300327  int arg3 = 0; 
    301328  int result; 
    302    
    303   (void)jenv; 
    304   (void)jcls; 
    305   arg1 = *(sipc_t **)&handle;  
     329 
     330  (void)jenv; 
     331  (void)jcls; 
     332  arg1 = *(sipc_t **)&handle; 
    306333  result = (int)sipc_recv_data(arg1, &arg2, &arg3); 
    307    
     334 
     335  *(*(int **)&size_handle)=arg3; 
     336 
    308337  if (result == 0) 
    309338  { 
    310         jresult = (*jenv)->NewDirectByteBuffer(jenv, arg2, arg3); 
     339    /*   
     340        jresult = (*jenv)->NewDirectByteBuffer(jenv, arg2, arg3); 
     341          LRR: The original form above 
     342          leaks memory like a sieve.  Instead, we ask java for a new  
     343          array and copy the output there.  This incurs a performance  
     344          penalty, but it's better than leaking memory. 
     345    */ 
     346    jbyteArray jba=(*jenv)->NewByteArray(jenv,arg3); 
     347    (*jenv)->SetByteArrayRegion(jenv,jba,0,arg3,arg2); 
     348    jresult=jba; 
     349  
     350 
    311351  } 
    312    
    313   if (ipc_type == (jint)SIPC_SYSV_MQUEUES) 
    314     free(arg2); 
    315          
    316   return jresult; 
    317 
    318  
    319 //sipc_get_data_ptr 
    320 JNIEXPORT jobject JNICALL Java_com_tresys_sipc_libsipc_sipc_1get_1data_1ptr(JNIEnv *jenv, jclass jcls, jlong handle, jlong size) { 
    321   jobject jresult = 0 ; 
    322   sipc_t *arg1 = (sipc_t *) 0 ; 
    323   char *result = NULL; 
    324    
    325   (void)jenv; 
    326   (void)jcls; 
    327   arg1 = *(sipc_t **)&handle;  
    328   result = sipc_get_data_ptr(arg1); 
    329   if(result) 
    330   { 
    331         jresult = (*jenv)->NewDirectByteBuffer(jenv, result, size); 
     352    if (ipc_type==(jint) SIPC_SYSV_MQUEUES) 
     353      { 
     354        free(arg2); 
     355      } 
     356   
     357  return jresult; 
     358 
     359
     360 
     361 
     362 
     363JNIEXPORT jobject JNICALL Java_com_tresys_sipc_libsipc_sipc_1get_1data_1ptr(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
     364  jstring jresult = 0 ; 
     365  sipc_t *arg1 = (sipc_t *) 0 ; 
     366  char *result = 0 ; 
     367   
     368  (void)jenv; 
     369  (void)jcls; 
     370  arg1 = *(sipc_t **)&jarg1;  
     371  result = (char *)sipc_get_data_ptr(arg1); 
     372  if (result) jresult = (*jenv)->NewDirectByteBuffer(jenv, result, arg1->len); 
     373  return jresult; 
     374
     375 
     376JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1error(JNIEnv *jenv, jclass jcls, jlong jarg1, jstring jarg2) { 
     377  sipc_t *arg1 = (sipc_t *) 0 ; 
     378  char *arg2 = (char *) 0 ; 
     379  void *arg3 = 0 ; 
     380   
     381  (void)jenv; 
     382  (void)jcls; 
     383  arg1 = *(sipc_t **)&jarg1;  
     384  arg2 = 0; 
     385  if (jarg2) { 
     386    arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); 
     387    if (!arg2) return ; 
    332388  } 
    333   return jresult; 
    334 
    335  
    336 // sipc_shm_recv_done 
    337 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1shm_1recv_1done(JNIEnv *jenv, jclass jcls, jlong handle) { 
     389  sipc_error(arg1,(char const *)arg2,arg3); 
     390  if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, arg2); 
     391
     392 
     393 
     394JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1shm_1recv_1done(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
    338395  jint jresult = 0 ; 
    339396  sipc_t *arg1 = (sipc_t *) 0 ; 
     
    342399  (void)jenv; 
    343400  (void)jcls; 
    344   arg1 = *(sipc_t **)&handle;  
     401  arg1 = *(sipc_t **)&jarg1;  
    345402  result = (int)sipc_shm_recv_done(arg1); 
    346403  jresult = (jint)result;  
     
    349406 
    350407 
     408  /* Methods to allocate and unbox the int pointer used for returning  
     409     message size */ 
     410static int *sipc_create_length() 
     411{ 
     412  return (int *)malloc(sizeof(int)); 
     413} 
     414 
     415static int sipc_get_length(int *arg) 
     416{ 
     417  if (arg) return *arg; else return -1; 
     418} 
     419 
     420static void sipc_destroy_length(int *arg) 
     421{  
     422  if (arg) free(arg); 
     423} 
     424 
     425 
     426JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1create_1length(JNIEnv *jenv, jclass jcls) { 
     427  jlong jresult = 0 ; 
     428  int *result = 0 ; 
     429   
     430  (void)jenv; 
     431  (void)jcls; 
     432  result = (int *)sipc_create_length(); 
     433  *(int **)&jresult = result;  
     434  return jresult; 
     435} 
     436 
     437JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1get_1length(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
     438  jint jresult = 0 ; 
     439  int *arg1 = (int *) 0 ; 
     440  int result; 
     441   
     442  (void)jenv; 
     443  (void)jcls; 
     444  arg1 = *(int **)&jarg1;  
     445  result = (int)sipc_get_length(arg1); 
     446  jresult = (jint)result;  
     447  return jresult; 
     448} 
     449 
     450JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1length(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
     451  int *arg1 = (int *) 0 ; 
     452   
     453  (void)jenv; 
     454  (void)jcls; 
     455  arg1 = *(int **)&jarg1;  
     456  sipc_destroy_length(arg1); 
     457} 
     458 
     459 
    351460#ifdef __cplusplus 
    352461} 
  • branches/binary-mq/libsipc/src/sipc_mqueue.c

    r4 r8  
    302302} 
    303303 
     304/* Finds the length in digits of a number (approximates log10+1 for ints) */ 
     305static int qlog(int x) 
     306{ 
     307  int i=0; 
     308  while(x) 
     309    { 
     310      i++; 
     311      x/=10; 
     312    } 
     313  return i; 
     314} 
     315 
    304316static int mqueue_send_msg_len(sipc_t *sipc, int len) 
    305317{ 
    306         struct msgbuf mbuf; 
    307         mbuf.mtype = SIPC_MSG_LEN; 
    308  
     318        struct msgbuf *mbuf; 
     319   
    309320        if (len < 0)  
    310321                return -1; 
    311         sprintf(mbuf.mtext, "%d", len); 
    312  
    313         if (msgsnd(sipc->msqid, (struct msgbuf *)&mbuf, sizeof(mbuf) + strlen(mbuf.mtext) + 1, 0) < 0) { 
    314                 sipc_error(sipc, "msgsnd: %s\n", strerror(errno)); 
    315                 return -1; 
    316         } 
    317  
     322        mbuf=calloc(1,sizeof(struct msgbuf)+qlog(len)+1); 
     323        if (!mbuf) { sipc_error(sipc,"Out of memory!\n"); return -1; } 
     324 
     325        mbuf->mtype = SIPC_MSG_LEN; 
     326 
     327        sprintf(mbuf->mtext, "%d", len); 
     328 
     329        if (msgsnd(sipc->msqid, mbuf, sizeof(struct msgbuf) +  
     330                   strlen(mbuf->mtext) + 1, 0) < 0) { 
     331          free(mbuf); 
     332          sipc_error(sipc, "msgsnd: %s\n", strerror(errno)); 
     333          return -1; 
     334        } 
     335        free(mbuf); 
    318336        return 0; 
    319337}