Improved ch_malloc/sl_malloc compatibility.
ber_len_t size
)
{
- void *new;
+ void *new, *ctx;
if ( block == NULL ) {
return( ch_malloc( size ) );
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,
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 );
+ }
}
{
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 );
}
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
int manageDSAit;
#ifdef LDAP_SLAPI
char **attrs = NULL;
- Operation ch_op;
#endif
#ifdef NEW_LOGGING
#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;
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 );
{
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;
/* 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] );
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;
}
}
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;
+}