Exemple #1
0
char *
slapi_ch_strdup ( const char* s1 )
{
    char* newmem;
    unsigned long	lsize;
	
	/* strdup pukes on NULL strings...bail out now */
	if(NULL == s1)
		return NULL;

	lsize = strlen(s1) + sizeof(unsigned long) + 1;
	newmem = slapi_ch_malloc( lsize );
	sprintf(newmem, "%s", s1);	

	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_strdup);
    PR_INCREMENT_COUNTER(slapi_ch_counter_created);
    PR_INCREMENT_COUNTER(slapi_ch_counter_exist);

    return newmem;
}
Exemple #2
0
char *
slapi_ch_realloc( char *block, unsigned long size )
{
	char	*newmem;
    unsigned long lsize;
	unsigned long origsize;
	char *realblock;
	char *realnewmem;

	if ( block == NULL ) {
		return( slapi_ch_malloc( size ) );
	}

	if (size <= 0) {
		log_negative_alloc_msg( "realloc", "bytes", size );
		return block;
	}

	lsize = size + sizeof(unsigned long);
	if (lsize <= 1024) {
		newmem = slapi_ch_realloc_core( block, lsize );
	} else if (lsize <= 67108864) {
		/* return 2KB ~ 64MB memory to memory pool */
		unsigned long roundup = 1;
		int n = 0;
		while (1) {
			roundup <<= 1;
			n++;
			if (roundup >= lsize) {
				break;
			}
		}
		PR_ASSERT(n >= 11 && n <= 26);
		newmem = (char *)mempool_get(n-11);	/* 11: 2^11 = 2K */
		if (NULL == newmem) {
			newmem = slapi_ch_realloc_core( block, roundup );
		} else {
			realblock = block - sizeof(unsigned long);
			origsize = *(unsigned long *)realblock - sizeof(unsigned long);;
			memcpy(newmem, block, origsize);
			slapi_ch_free_string(&block);
		}
	} else {
		realblock = block - sizeof(unsigned long);
		origsize = *(unsigned long *)realblock - sizeof(unsigned long);;
		newmem = slapi_ch_mmap( size );
		memcpy(newmem, block, origsize);
		realnewmem = newmem - sizeof(unsigned long);
		*(unsigned long *)realnewmem = lsize;
		slapi_ch_free_string(&block);
	}
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_realloc);

	return( newmem );
}
Exemple #3
0
char*
slapi_ch_strdup ( const char* s1)
{
    char* newmem;
	
	/* strdup pukes on NULL strings...bail out now */
	if(NULL == s1)
		return NULL;
	newmem = strdup (s1);
    if (newmem == NULL) {
		int	oserr = errno;
        oom_occurred();

		slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
		    "strdup of %lu characters failed; OS error %d (%s)%s\n",
			(unsigned long)strlen(s1), oserr, slapd_system_strerror( oserr ),
			oom_advice );
		exit (1);
    }
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_strdup);
    PR_INCREMENT_COUNTER(slapi_ch_counter_created);
    PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
#if defined(_WIN32) && defined(DEBUG)
	if(recording)
	{
		add_memory_record(newmem,strlen(s1)+1);
	}
#endif
    return newmem;
}
Exemple #4
0
char *
slapi_ch_calloc( unsigned long nelem, unsigned long size )
{
	char	*newmem;
    unsigned long	lsize;

	if (size <= 0) {
		log_negative_alloc_msg( "calloc", "bytes", size );
		return 0;
	}

	if (nelem <= 0) {
		log_negative_alloc_msg( "calloc", "elements", nelem );
		return 0;
	}

	lsize = nelem * size + sizeof(unsigned long);
	if (lsize <= 1024) {
		newmem = slapi_ch_calloc_core( lsize );
	} else if (lsize <= 67108864) {
		/* return 2KB ~ 64MB memory to memory pool */
		unsigned long roundup = 1;
		int n = 0;
		while (1) {
			roundup <<= 1;
			n++;
			if (roundup >= lsize) {
				break;
			}
		}
		PR_ASSERT(n >= 11 && n <= 26);
		newmem = (char *)mempool_get(n-11);	/* 11: 2^11 = 2K */
		if (NULL == newmem) {
			newmem = slapi_ch_calloc_core( roundup );
		} else {
			memset (newmem, 0, size * nelem);
		}
	} else {
		unsigned long mysize = size * nelem;
		newmem = slapi_ch_mmap( mysize );
		memset(newmem, 0, mysize);
	}
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_calloc);
    PR_INCREMENT_COUNTER(slapi_ch_counter_created);
    PR_INCREMENT_COUNTER(slapi_ch_counter_exist);

	return( newmem );
}
Exemple #5
0
/*
 * Function: slapi_ch_free 
 *
 * Returns: nothing 
 *
 * Description: frees the pointer, and then sets it to NULL to 
 *              prevent free-memory writes. 
 *              Note: pass in the address of the pointer you want to free.
 *              Note: you can pass in null pointers, it's cool.
 *
 * Implementation: get the size from the size space, and determine the behavior
 *                 based upon the size:
 *      1B ~ 1KB: call system free
 *      1KB + 1B ~ 64MB: return memory to mempool
 *      64MB + 1B ~ : call munmap
 */
void 
slapi_ch_free(void **ptr)
{
	void *realptr;
	unsigned long size;

	if (ptr==NULL || *ptr == NULL){
		return;
	}

	realptr = (void *)((char *)*ptr - sizeof(unsigned long));
	size = *(unsigned long *)realptr;
	if (size <= 1024) {
		free (realptr);
	} else if (size <= 67108864) {
		/* return 2KB ~ 64MB memory to memory pool */
		unsigned long roundup = 1;
		int n = 0;
		int rc = LDAP_SUCCESS;
		while (1) {
			roundup <<= 1;
			n++;
			if (roundup >= size) {
				break;
			}
		}
        PR_ASSERT(n >= 11 && n <= 26);
        rc = mempool_return(n-11, *ptr, (mempool_cleanup_callback)NULL);
        if (LDAP_SUCCESS != rc) {
			free (realptr);
        }
	} else {
		slapi_ch_munmap_no_roundup( ptr, size );
	}
	*ptr = NULL;

	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_free);
    PR_DECREMENT_COUNTER(slapi_ch_counter_exist);
	return;
}
Exemple #6
0
char *
slapi_ch_calloc(
    unsigned long	nelem,
    unsigned long	size
)
{
	char	*newmem;

	if (size <= 0) {
		log_negative_alloc_msg( "calloc", "bytes", size );
		return 0;
	}

	if (nelem <= 0) {
		log_negative_alloc_msg( "calloc", "elements", nelem );
		return 0;
	}

	if ( (newmem = (char *) calloc( nelem, size )) == NULL ) {
		int	oserr = errno;

	  	oom_occurred();
		slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
		    "calloc of %lu elems of %lu bytes failed; OS error %d (%s)%s\n",
			nelem, size, oserr, slapd_system_strerror( oserr ), oom_advice );
		exit( 1 );
	}
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_calloc);
    PR_INCREMENT_COUNTER(slapi_ch_counter_created);
    PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
#if defined(_WIN32) && defined(DEBUG)
	if(recording)
	{
		add_memory_record(newmem,size);
	}
#endif
	return( newmem );
}
Exemple #7
0
/*
 *  Function: slapi_ch_free 
 *
 *  Returns: nothing 
 *
 *  Description: frees the pointer, and then sets it to NULL to 
 *               prevent free-memory writes. 
 *               Note: pass in the address of the pointer you want to free.
 *               Note: you can pass in null pointers, it's cool.
 */
void 
slapi_ch_free(void **ptr)
{
	if (ptr==NULL || *ptr == NULL){
	return;
	}

#if defined(_WIN32) && defined(DEBUG)
	if(recording)
	{
		remove_memory_record(*ptr);
	}
#endif
	free (*ptr);
	*ptr = NULL;
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_free);
    PR_DECREMENT_COUNTER(slapi_ch_counter_exist);
	return;
}