Provide framework for auto initialise/deinitialise of the library
[openssl.git] / crypto / mem_dbg.c
index 8580447c0a16be9c09fae2915122a1f0feed14b5..05590442d14f4d4643d5c27c18557e438acab8d2 100644 (file)
@@ -1,4 +1,3 @@
-/* crypto/mem_dbg.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -331,29 +330,31 @@ static unsigned long app_info_hash(const APP_INFO *a)
     return (ret);
 }
 
-static APP_INFO *pop_info(void)
+/* returns 1 if there was an info to pop, 0 if the stack was empty. */
+static int pop_info(void)
 {
     APP_INFO tmp;
-    APP_INFO *ret = NULL;
+    APP_INFO *current = NULL;
 
     if (amih != NULL) {
         CRYPTO_THREADID_current(&tmp.threadid);
-        if ((ret = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
-            APP_INFO *next = ret->next;
+        if ((current = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
+            APP_INFO *next = current->next;
 
             if (next != NULL) {
                 next->references++;
                 (void)lh_APP_INFO_insert(amih, next);
             }
-            if (--(ret->references) <= 0) {
-                ret->next = NULL;
+            if (--(current->references) <= 0) {
+                current->next = NULL;
                 if (next != NULL)
                     next->references--;
-                OPENSSL_free(ret);
+                OPENSSL_free(current);
             }
+            return 1;
         }
     }
-    return (ret);
+    return 0;
 }
 
 int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
@@ -396,7 +397,7 @@ int CRYPTO_mem_debug_pop(void)
 
     if (mem_check_on()) {
         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
-        ret = (pop_info() != NULL);
+        ret = pop_info();
         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
     }
     return (ret);
@@ -693,8 +694,8 @@ int CRYPTO_mem_leaks_fp(FILE *fp)
     BIO *b;
     int ret;
 
-    if (mh == NULL)
-        return 0;
+    if (mh == NULL && amih == NULL)
+        return 1;
     /*
      * Need to turn off memory checking when allocated BIOs ... especially as
      * we're creating them at a time when we're trying to check we've not