]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/cache.c
s/<stdlib.h>/<ac/stdlib.h>/
[openldap] / libraries / libldap / cache.c
index 2e47d68694ebd0f19e9d8b00410e76aba48b02ae..4a0984b18fcce6a792c17463eebbe55a87201ebd 100644 (file)
@@ -1,67 +1,44 @@
 /*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1993 The Regents of the University of Michigan.
  *  All rights reserved.
  *
  *  cache.c - local caching support for LDAP
  */
 
-#ifndef NO_CACHE
-
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1993 The Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
+#include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#ifdef MACOS
-#include <time.h>
-#include "macos.h"
-#else /* MACOS */
-#if defined( DOS ) || defined( _WIN32 )
-#include <malloc.h>
-#include "msdos.h"
-#ifdef NCSA
-#include "externs.h"
-#endif /* NCSA */
-#ifdef WINSOCK
-#include <time.h>
-#endif /* WINSOCK */
-#else /* DOS */
-#include <sys/types.h>
-#include <sys/socket.h>
-#endif /* DOS */
-#endif /* MACOS */
-
-#include "lber.h"
-#include "ldap.h"
+
+#include <ac/stdlib.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "ldap-int.h"
 
-#ifdef NEEDPROTOS
-static int             cache_hash( BerElement *ber );
-static LDAPMessage     *msg_dup( LDAPMessage *msg );
-static int             request_cmp( BerElement *req1, BerElement *req2 );
-static int             chain_contains_dn( LDAPMessage *msg, char *dn );
-static long            msg_size( LDAPMessage *msg );
-static void            check_cache_memused( LDAPCache *lc );
-static void            uncache_entry_or_req( LDAP *ld, char *dn, int msgid );
-#else /* NEEDPROTOS */
-static int             cache_hash();
-static LDAPMessage     *msg_dup();
-static int             request_cmp();
-static int             chain_contains_dn();
-static long            msg_size();
-static void            check_cache_memused();
-static void            uncache_entry_or_req();
-#endif /* NEEDPROTOS */
+#ifndef LDAP_NOCACHE
+
+static int             cache_hash LDAP_P(( BerElement *ber ));
+static LDAPMessage     *msg_dup LDAP_P(( LDAPMessage *msg ));
+static int             request_cmp LDAP_P(( BerElement *req1, BerElement *req2 ));
+static int             chain_contains_dn LDAP_P(( LDAPMessage *msg, LDAP_CONST char *dn ));
+static long            msg_size LDAP_P(( LDAPMessage *msg ));
+static void            check_cache_memused LDAP_P(( LDAPCache *lc ));
+static void            uncache_entry_or_req LDAP_P(( LDAP *ld, LDAP_CONST char *dn, int msgid ));
 
+#endif
 
 int
 ldap_enable_cache( LDAP *ld, long timeout, long maxmem )
 {
+#ifndef LDAP_NOCACHE
        if ( ld->ld_cache == NULLLDCACHE ) {
-               if (( ld->ld_cache = (LDAPCache *)malloc( sizeof( LDAPCache )))
+               if (( ld->ld_cache = (LDAPCache *)LDAP_MALLOC( sizeof( LDAPCache )))
                    == NULLLDCACHE ) {
                        ld->ld_errno = LDAP_NO_MEMORY;
                        return( -1 );
@@ -75,15 +52,20 @@ ldap_enable_cache( LDAP *ld, long timeout, long maxmem )
        check_cache_memused( ld->ld_cache );
        ld->ld_cache->lc_enabled = 1;
        return( 0 );
+#else 
+       return( -1 );
+#endif
 }
 
 
 void
 ldap_disable_cache( LDAP *ld )
 {
+#ifndef LDAP_NOCACHE
        if ( ld->ld_cache != NULLLDCACHE ) {
                ld->ld_cache->lc_enabled = 0;
        }
+#endif
 }
 
 
@@ -91,26 +73,31 @@ ldap_disable_cache( LDAP *ld )
 void
 ldap_set_cache_options( LDAP *ld, unsigned long opts )
 {
+#ifndef LDAP_NOCACHE
        if ( ld->ld_cache != NULLLDCACHE ) {
                ld->ld_cache->lc_options = opts;
        }
+#endif
 }
        
 
 void
 ldap_destroy_cache( LDAP *ld )
 {
+#ifndef LDAP_NOCACHE
        if ( ld->ld_cache != NULLLDCACHE ) {
                ldap_flush_cache( ld );
-               free( (char *)ld->ld_cache );
+               LDAP_FREE( (char *)ld->ld_cache );
                ld->ld_cache = NULLLDCACHE;
        }
+#endif
 }
 
 
 void
 ldap_flush_cache( LDAP *ld )
 {
+#ifndef LDAP_NOCACHE
        int             i;
        LDAPMessage     *m, *next;
 
@@ -135,32 +122,39 @@ ldap_flush_cache( LDAP *ld )
                }
                ld->ld_cache->lc_memused = sizeof( LDAPCache );
        }
+#endif
 }
 
 
 void
 ldap_uncache_request( LDAP *ld, int msgid )
 {
+#ifndef LDAP_NOCACHE
        Debug( LDAP_DEBUG_TRACE, "ldap_uncache_request %d ld_cache %lx\n",
            msgid, (long) ld->ld_cache, 0 );
 
        uncache_entry_or_req( ld, NULL, msgid );
+#endif
 }
 
 
 void
-ldap_uncache_entry( LDAP *ld, char *dn )
+ldap_uncache_entry( LDAP *ld, LDAP_CONST char *dn )
 {
+#ifndef LDAP_NOCACHE
        Debug( LDAP_DEBUG_TRACE, "ldap_uncache_entry %s ld_cache %lx\n",
            dn, (long) ld->ld_cache, 0 );
 
        uncache_entry_or_req( ld, dn, 0 );
+#endif
 }
 
 
+#ifndef LDAP_NOCACHE
+
 static void
 uncache_entry_or_req( LDAP *ld,
-       char *dn,               /* if non-NULL, uncache entry */
+       const char *dn,         /* if non-NULL, uncache entry */
        int msgid )             /* request to uncache (if dn == NULL) */
 {
        int             i;
@@ -214,10 +208,12 @@ uncache_entry_or_req( LDAP *ld,
        }
 }
 
+#endif
 
 void
 ldap_add_request_to_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 {
+#ifndef LDAP_NOCACHE
        LDAPMessage     *new;
        long            len;
 
@@ -229,17 +225,17 @@ ldap_add_request_to_cache( LDAP *ld, unsigned long msgtype, BerElement *request
                return;
        }
 
-       if (( new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
+       if (( new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
            != NULL ) {
                if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
-                       free( (char *)new );
+                       LDAP_FREE( (char *)new );
                        return;
                }
                len = request->ber_ptr - request->ber_buf;
-               if (( new->lm_ber->ber_buf = (char *) malloc( (size_t)len ))
+               if (( new->lm_ber->ber_buf = (char *) ber_memalloc( (size_t)len ))
                    == NULL ) {
                        ber_free( new->lm_ber, 0 );
-                       free( (char *)new );
+                       LDAP_FREE( (char *)new );
                        ld->ld_errno = LDAP_NO_MEMORY;
                        return;
                }
@@ -254,12 +250,14 @@ ldap_add_request_to_cache( LDAP *ld, unsigned long msgtype, BerElement *request
        } else {
                ld->ld_errno = LDAP_NO_MEMORY;
        }
+#endif
 }
 
 
 void
 ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
 {
+#ifndef LDAP_NOCACHE
        LDAPMessage     *m, **mp, *req, *new, *prev;
        int             err, keep;
 
@@ -273,6 +271,7 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
        }
 
        if ( result->lm_msgtype != LDAP_RES_SEARCH_ENTRY &&
+           result->lm_msgtype != LDAP_RES_SEARCH_REFERENCE &&
            result->lm_msgtype != LDAP_RES_SEARCH_RESULT &&
            result->lm_msgtype != LDAP_RES_COMPARE ) {
                /*
@@ -360,6 +359,7 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
                Debug( LDAP_DEBUG_TRACE, "artc: msgid not in request list\n",
                    0, 0, 0 );
        }
+#endif
 }
 
 
@@ -373,10 +373,11 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
 int
 ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 {
+#ifndef LDAP_NOCACHE
        LDAPMessage     *m, *new, *prev, *next;
        BerElement      reqber;
        int             first, hash;
-       unsigned long   validtime;
+       time_t  c_time;
 
        Debug( LDAP_DEBUG_TRACE, "ldap_check_cache\n", 0, 0, 0 );
 
@@ -388,14 +389,14 @@ ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
        reqber.ber_buf = reqber.ber_ptr = request->ber_buf;
        reqber.ber_end = request->ber_ptr;
 
-       validtime = (long)time( NULL ) - ld->ld_cache->lc_timeout;
+       c_time = time( NULL );
 
        prev = NULLMSG;
        hash = cache_hash( &reqber );
        for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULLMSG; m = next ) {
                Debug( LDAP_DEBUG_TRACE,"cc: examining id %d,type %d\n",
                    m->lm_msgid, m->lm_msgtype, 0 );
-               if ( m->lm_time < validtime ) {
+               if ( difftime(c_time, m->lm_time) > ld->ld_cache->lc_timeout ) {
                        /* delete expired message */
                        next = m->lm_next;
                        if ( prev == NULL ) {
@@ -446,8 +447,12 @@ ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 
        Debug( LDAP_DEBUG_TRACE, "cc: result returned from cache\n", 0, 0, 0 );
        return( 0 );
+#else
+       return( -1 );
+#endif
 }
 
+#ifndef LDAP_NOCACHE
 
 static int
 cache_hash( BerElement *ber )
@@ -478,17 +483,17 @@ msg_dup( LDAPMessage *msg )
        LDAPMessage     *new;
        long            len;
 
-       if (( new = (LDAPMessage *)malloc( sizeof(LDAPMessage))) != NULL ) {
+       if (( new = (LDAPMessage *)LDAP_MALLOC( sizeof(LDAPMessage))) != NULL ) {
                *new = *msg;    /* struct copy */
                if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULLBER ) {
-                       free( (char *)new );
+                       LDAP_FREE( (char *)new );
                        return( NULLMSG );
                }
                len = msg->lm_ber->ber_end - msg->lm_ber->ber_buf;
-               if (( new->lm_ber->ber_buf = (char *) malloc(
+               if (( new->lm_ber->ber_buf = (char *) ber_memalloc(
                    (size_t)len )) == NULL ) {
                        ber_free( new->lm_ber, 0 );
-                       free( (char *)new );
+                       LDAP_FREE( (char *)new );
                        return( NULLMSG );
                }
                SAFEMEMCPY( new->lm_ber->ber_buf, msg->lm_ber->ber_buf,
@@ -526,7 +531,9 @@ request_cmp( BerElement *req1, BerElement *req2 )
        /*
         * check remaining length and bytes if necessary
         */
-       if (( len = r1.ber_end - r1.ber_ptr ) != r2.ber_end - r2.ber_ptr ) {
+       if (( len = r1.ber_end - r1.ber_ptr ) !=
+               (unsigned long) (r2.ber_end - r2.ber_ptr) )
+       {
                return( -1 );   /* different lengths */
        }
        return( memcmp( r1.ber_ptr, r2.ber_ptr, (size_t)len ));
@@ -534,7 +541,7 @@ request_cmp( BerElement *req1, BerElement *req2 )
 
 
 static int
-chain_contains_dn( LDAPMessage *msg, char *dn )
+chain_contains_dn( LDAPMessage *msg, const char *dn )
 {
        LDAPMessage     *m;
        BerElement      ber;
@@ -549,7 +556,7 @@ chain_contains_dn( LDAPMessage *msg, char *dn )
        ber = *msg->lm_ber;     /* struct copy */
        if ( ber_scanf( &ber, "{i{a", &msgid, &s ) != LBER_ERROR ) {
            rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
-           free( s );
+           LDAP_FREE( s );
            if ( rc != 0 ) {
                return( rc );
            }
@@ -570,7 +577,7 @@ chain_contains_dn( LDAPMessage *msg, char *dn )
                ber = *m->lm_ber;       /* struct copy */
                if ( ber_scanf( &ber, "{a", &s ) != LBER_ERROR ) {
                        rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
-                       free( s );
+                       LDAP_FREE( s );
                }
        }
 
@@ -611,26 +618,27 @@ check_cache_memused( LDAPCache *lc )
  *    } while ( cache size is > SIZE_FACTOR * lc_maxmem )
  */
        int             i;
-       unsigned long   remove_threshold, validtime;
+       unsigned long   remove_threshold;
+       time_t c_time;
        LDAPMessage     *m, *prev, *next;
 
        Debug( LDAP_DEBUG_TRACE, "check_cache_memused: %ld bytes in use (%ld max)\n",
            lc->lc_memused, lc->lc_maxmem, 0 );
 
-       if ( lc->lc_maxmem <= sizeof( LDAPCache )
+       if ( (unsigned) lc->lc_maxmem <= sizeof( LDAPCache )
            || lc->lc_memused <= lc->lc_maxmem * SIZE_FACTOR ) {
                return;
        }
 
        remove_threshold = lc->lc_timeout;
        while ( lc->lc_memused > lc->lc_maxmem * SIZE_FACTOR ) {
-               validtime = (long)time( NULL ) - remove_threshold;
+               c_time = time( NULL );
                for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
                        prev = NULLMSG;
                        for ( m = lc->lc_buckets[ i ]; m != NULLMSG;
                            m = next ) {
                                next = m->lm_next;
-                               if ( m->lm_time < validtime ) {
+                               if ( difftime(c_time, m->lm_time) > remove_threshold) {
                                        if ( prev == NULLMSG ) {
                                                lc->lc_buckets[ i ] = next;
                                        } else {