Author: mturk
Date: Wed Sep  6 05:30:48 2006
New Revision: 440699

URL: http://svn.apache.org/viewvc?view=rev&rev=440699
Log:
Throw OutOfMemoryError instead doing core dump
for the objects that failed to allocate.

Modified:
    tomcat/connectors/trunk/jni/native/include/tcn.h
    tomcat/connectors/trunk/jni/native/src/error.c
    tomcat/connectors/trunk/jni/native/src/network.c
    tomcat/connectors/trunk/jni/native/src/poll.c

Modified: tomcat/connectors/trunk/jni/native/include/tcn.h
URL: 
http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/include/tcn.h?view=diff&rev=440699&r1=440698&r2=440699
==============================================================================
--- tomcat/connectors/trunk/jni/native/include/tcn.h (original)
+++ tomcat/connectors/trunk/jni/native/include/tcn.h Wed Sep  6 05:30:48 2006
@@ -154,6 +154,7 @@
 /* Private helper functions */
 void            tcn_Throw(JNIEnv *, const char *, ...);
 void            tcn_ThrowException(JNIEnv *, const char *);
+void            tcn_ThrowMemoryException(JNIEnv *, const char *, int, const 
char *);
 void            tcn_ThrowAPRException(JNIEnv *, apr_status_t);
 jstring         tcn_new_string(JNIEnv *, const char *);
 jstring         tcn_new_stringn(JNIEnv *, const char *, size_t);
@@ -189,6 +190,13 @@
         if (c##V)                \
             free(c##V);          \
     TCN_END_MACRO
+
+#define TCN_CHECK_ALLOCATED(x)                              \
+        if (x == NULL) {                                    \
+            tcn_ThrowMemoryException(e, __FILE__, __LINE__, \
+            "APR memory allocation failed");                \
+            goto cleanup;                                   \
+        } else (void)(0)
 
 #define TCN_THROW_IF_ERR(x, r)                  \
     TCN_BEGIN_MACRO                             \

Modified: tomcat/connectors/trunk/jni/native/src/error.c
URL: 
http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/error.c?view=diff&rev=440699&r1=440698&r2=440699
==============================================================================
--- tomcat/connectors/trunk/jni/native/src/error.c (original)
+++ tomcat/connectors/trunk/jni/native/src/error.c Wed Sep  6 05:30:48 2006
@@ -51,6 +51,34 @@
 
 }
 
+void tcn_ThrowMemoryException(JNIEnv *env, const char *file, int line, const 
char *msg)
+{
+    jclass javaExceptionClass;
+    javaExceptionClass = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
+    if (javaExceptionClass == NULL) {
+        fprintf(stderr, "Cannot find java/lang/OutOfMemoryError\n");
+        return;
+    }
+
+    if (file) {
+        char fmt[TCN_BUFFER_SZ];
+        char *f = (char *)(file + strlen(file) - 1);
+        while (f != file && '\\' != *f && '/' != *f) {
+            f--;
+        }
+        if (f != file) {
+            f++;
+        }
+        sprintf(fmt, "%s for [%s::%04d]", msg, line, f);
+        (*env)->ThrowNew(env, javaExceptionClass, &fmt[0]);
+    }
+    else
+        (*env)->ThrowNew(env, javaExceptionClass, msg);
+    (*env)->DeleteLocalRef(env, javaExceptionClass);
+
+}
+
+
 void tcn_Throw(JNIEnv *env, const char *fmt, ...)
 {
     char msg[TCN_BUFFER_SZ] = {'\0'};

Modified: tomcat/connectors/trunk/jni/native/src/network.c
URL: 
http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/network.c?view=diff&rev=440699&r1=440698&r2=440699
==============================================================================
--- tomcat/connectors/trunk/jni/native/src/network.c (original)
+++ tomcat/connectors/trunk/jni/native/src/network.c Wed Sep  6 05:30:48 2006
@@ -188,6 +188,7 @@
     sp_created++;
 #endif
     a = (tcn_socket_t *)apr_pcalloc(p, sizeof(tcn_socket_t));
+    TCN_CHECK_ALLOCATED(a);
     a->sock = s;
     a->pool = p;
     if (family >= 0)
@@ -325,6 +326,7 @@
         apr_atomic_inc32(&sp_accepted);
 #endif
         a = (tcn_socket_t *)apr_pcalloc(p, sizeof(tcn_socket_t));
+        TCN_CHECK_ALLOCATED(a);
         a->sock   = n;
         a->pool   = p;
         a->net    = &apr_socket_layer;
@@ -362,6 +364,7 @@
         apr_atomic_inc32(&sp_accepted);
 #endif
         a = (tcn_socket_t *)apr_pcalloc(p, sizeof(tcn_socket_t));
+        TCN_CHECK_ALLOCATED(a);
         a->sock   = n;
         a->pool   = p;
         a->net    = &apr_socket_layer;

Modified: tomcat/connectors/trunk/jni/native/src/poll.c
URL: 
http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/poll.c?view=diff&rev=440699&r1=440698&r2=440699
==============================================================================
--- tomcat/connectors/trunk/jni/native/src/poll.c (original)
+++ tomcat/connectors/trunk/jni/native/src/poll.c Wed Sep  6 05:30:48 2006
@@ -121,10 +121,14 @@
                          (apr_uint32_t)size, p, f), pollset);
     }
     tps = apr_pcalloc(p, sizeof(tcn_pollset_t));
+    TCN_CHECK_ALLOCATED(tps);
     tps->pollset = pollset;
     tps->set        = apr_palloc(p, size * sizeof(jlong) * 2);
+    TCN_CHECK_ALLOCATED(tps->set);
     tps->socket_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
+    TCN_CHECK_ALLOCATED(tps->socket_set);
     tps->socket_ttl = apr_palloc(p, size * sizeof(apr_interval_time_t));
+    TCN_CHECK_ALLOCATED(tps->socket_set);
     tps->nelts  = 0;
     tps->nalloc = size;
     tps->pool   = p;



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to