From: Howard Chu Date: Sat, 12 Apr 2003 05:12:40 +0000 (+0000) Subject: Added sl_context() to return the context associated with an alloc'd ptr. X-Git-Tag: OPENLDAP_REL_ENG_2_2_0ALPHA~383 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=be28bf247e67c727dc58b38692f157a90fb75844;p=openldap Added sl_context() to return the context associated with an alloc'd ptr. Improved ch_malloc/sl_malloc compatibility. --- diff --git a/servers/slapd/ch_malloc.c b/servers/slapd/ch_malloc.c index 8084b7e2d0..92d6a7da26 100644 --- a/servers/slapd/ch_malloc.c +++ b/servers/slapd/ch_malloc.c @@ -53,7 +53,7 @@ ch_realloc( ber_len_t size ) { - void *new; + void *new, *ctx; if ( block == NULL ) { return( ch_malloc( size ) ); @@ -63,6 +63,11 @@ ch_realloc( ch_free( block ); } + ctx = sl_context( block ); + if ( ctx ) { + return sl_realloc( block, size, ctx ); + } + if ( (new = (void *) ber_memrealloc_x( block, size, NULL )) == NULL ) { #ifdef NEW_LOGGING LDAP_LOG( OPERATION, ERR, @@ -126,6 +131,13 @@ ch_strdup( void ch_free( void *ptr ) { - ber_memfree_x( ptr, NULL ); + void *ctx; + + ctx = sl_context( ptr ); + if (ctx) { + sl_free( ptr, ctx ); + } else { + ber_memfree_x( ptr, NULL ); + } } diff --git a/servers/slapd/filter.c b/servers/slapd/filter.c index 19acd9e38d..a2cbc28b2e 100644 --- a/servers/slapd/filter.c +++ b/servers/slapd/filter.c @@ -602,8 +602,8 @@ filter_free( Filter *f ) { Operation op; - op.o_tmpmemctx = NULL; - op.o_tmpmfuncs = &ch_mfuncs; + op.o_tmpmemctx = sl_context( f ); + op.o_tmpmfuncs = &sl_mfuncs; filter_free_x( &op, f ); } diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index 0ea64da225..ce50890671 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -951,6 +951,7 @@ LDAP_SLAPD_F (void) sl_free LDAP_P(( void *, void *ctx )); LDAP_SLAPD_F (void *) sl_mem_create LDAP_P(( ber_len_t size, void *ctx )); LDAP_SLAPD_F (void *) sl_mark LDAP_P(( void *ctx )); LDAP_SLAPD_F (void) sl_release LDAP_P(( void *, void *ctx )); +LDAP_SLAPD_F (void *) sl_context LDAP_P(( void *ptr )); /* * starttls.c diff --git a/servers/slapd/search.c b/servers/slapd/search.c index 8aca9c3723..f94198b2fd 100644 --- a/servers/slapd/search.c +++ b/servers/slapd/search.c @@ -45,7 +45,6 @@ do_search( int manageDSAit; #ifdef LDAP_SLAPI char **attrs = NULL; - Operation ch_op; #endif #ifdef NEW_LOGGING @@ -136,20 +135,7 @@ do_search( #endif /* filter - returns a "normalized" version */ -#ifdef LDAP_SLAPI - /* - * SLAPI computed search rewriter plugins can reset the search - * filter, and because they have no way to specify the use of - * the thread-local malloc replacement, we must always use - * ch_malloc() to allocate the filter when SLAPI is enabled. - */ - ch_op = *op; /* struct copy */ - ch_op.o_tmpmemctx = NULL; - ch_op.o_tmpmfuncs = &ch_mfuncs; - rs->sr_err = get_filter( &ch_op, op->o_ber, &op->ors_filter, &rs->sr_text ); -#else rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text ); -#endif /* LDAP_SLAPI */ if( rs->sr_err != LDAP_SUCCESS ) { if( rs->sr_err == SLAPD_DISCONNECT ) { rs->sr_err = LDAP_PROTOCOL_ERROR; @@ -410,11 +396,7 @@ return_results:; if( op->o_req_ndn.bv_val != NULL) sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx ); if( op->ors_filterstr.bv_val != NULL) op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx ); -#ifdef LDAP_SLAPI - if( op->ors_filter != NULL) filter_free( op->ors_filter ); -#else if( op->ors_filter != NULL) filter_free_x( op, op->ors_filter ); -#endif if( op->ors_attrs != NULL ) op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx ); #ifdef LDAP_SLAPI if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx ); diff --git a/servers/slapd/sl_malloc.c b/servers/slapd/sl_malloc.c index 8e037662d7..070cb8d43f 100644 --- a/servers/slapd/sl_malloc.c +++ b/servers/slapd/sl_malloc.c @@ -26,8 +26,8 @@ sl_mem_destroy( { struct slab_heap *sh = data; - ch_free(sh->h_base); - ch_free(sh); + ber_memfree_x(sh->h_base, NULL); + ber_memfree_x(sh, NULL); } BER_MEMALLOC_FN sl_malloc; @@ -130,18 +130,42 @@ sl_realloc( void *ptr, ber_len_t size, void *ctx ) /* Not our memory? */ if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) { - return ch_realloc( ptr, size ); + /* duplicate of ch_realloc behavior, oh well */ + new = ber_memrealloc_x( ptr, size, NULL ); + if (new ) { + return new; + } +#ifdef NEW_LOGGING + LDAP_LOG( OPERATION, ERR, + "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 ); +#else + Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n", + (long) size, 0, 0 ); +#endif + assert( 0 ); + exit( EXIT_FAILURE ); } - if ( size == 0 ) return NULL; + if ( size == 0 ) { + sl_free( ptr, ctx ); + return NULL; + } /* round up to doubleword boundary */ size += pad + sizeof( ber_len_t ); size &= ~pad; - /* Never shrink blocks, always alloc if growth needed */ + /* Never shrink blocks */ if (size <= p[-1]) { new = p; + + /* If reallocing the last block, we can grow it */ + } else if ( (char *)ptr + p[-1] == sh->h_last ) { + new = p; + sh->h_last += size - p[-1]; + p[-1] = size; + + /* Nowhere to grow, need to alloc and copy */ } else { new = sl_malloc( size, ctx ); AC_MEMCPY( new, ptr, p[-1] ); @@ -153,9 +177,13 @@ void sl_free( void *ptr, void *ctx ) { struct slab_heap *sh = ctx; + ber_len_t *p = (ber_len_t *)ptr; if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) { - ch_free( ptr ); + ber_memfree_x( ptr, NULL ); + } else if ( (char *)ptr + p[-1] == sh->h_last ) { + p--; + sh->h_last = p; } } @@ -179,3 +207,19 @@ sl_mark( void *ctx ) return ret; } + +void * +sl_context( void *ptr ) +{ + struct slab_heap *sh = NULL; + void *ctx; + + ctx = ldap_pvt_thread_pool_context(); + + ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL ); + + if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) { + return sh; + } + return NULL; +}