Beispiel #1
0
void fm_free(struct fm_block* qm, void* p)
#endif
{
	struct fm_frag* f,*n;

	#ifdef DBG_F_MALLOC
	LM_DBG("params(%p, %p), called from %s: %s(%d)\n", qm, p, file, func, line);
	if (p>(void*)qm->last_frag || p<(void*)qm->first_frag){
		LM_CRIT("bad pointer %p (out of memory block!) - aborting\n", p);
		abort();
	}
	#endif
	if (p==0) {
		LM_DBG("free(0) called\n");
		return;
	}
	f=(struct fm_frag*) ((char*)p-sizeof(struct fm_frag));

	#ifdef DBG_F_MALLOC
	LM_DBG("freeing block alloc'ed from %s: %s(%ld)\n", f->file, f->func,
			f->line);
	f->file=file;
	f->func=func;
	f->line=line;
	#endif

join:

	if( qm->large_limit < qm->large_space )
		goto no_join;

	n = FRAG_NEXT(f);

	if (((char*)n < (char*)qm->last_frag) &&  n->prev )
	{

		fm_remove_free(qm, n);
		/* join */
		f->size += n->size + FRAG_OVERHEAD;

		#if defined(DBG_F_MALLOC) || defined(STATISTICS)
		//qm->real_used -= FRAG_OVERHEAD;
		qm->used += FRAG_OVERHEAD;
		#endif

		goto join;
	}

no_join:

	fm_insert_free(qm, f);
#if defined(DBG_F_MALLOC) || defined(STATISTICS)
	qm->fragments -= 1;
#endif
	pkg_threshold_check();
}
Beispiel #2
0
void* fm_realloc(struct fm_block* qm, void* p, unsigned long size)
#endif
{
	struct fm_frag *f;
	unsigned long diff;
	unsigned long orig_size;
	struct fm_frag *n;
	void *ptr;
	
	
	#ifdef DBG_F_MALLOC
	LM_DBG("params(%p, %p, %lu), called from %s: %s(%d)\n", qm, p, size,
			file, func, line);
	if ((p)&&(p>(void*)qm->last_frag || p<(void*)qm->first_frag)){
		LM_CRIT("bad pointer %p (out of memory block!) - aborting\n", p);
		abort();
	}
	#endif
	if (size==0) {
		if (p)
	#ifdef DBG_F_MALLOC
			fm_free(qm, p, file, func, line);
	#else
			fm_free(qm, p);
	#endif
		pkg_threshold_check();
		return 0;
	}
	if (p==0)
	#ifdef DBG_F_MALLOC
		return fm_malloc(qm, size, file, func, line);
	#else
		return fm_malloc(qm, size);
	#endif
	f=(struct fm_frag*) ((char*)p-sizeof(struct fm_frag));
	#ifdef DBG_F_MALLOC
	LM_DBG("realloc'ing frag %p alloc'ed from %s: %s(%ld)\n",
			f, f->file, f->func, f->line);
	#endif
	size=ROUNDUP(size);
	orig_size=f->size;
	if (f->size > size){
		/* shrink */
		#ifdef DBG_F_MALLOC
		LM_DBG("shrinking from %lu to %lu\n", f->size, size);
		fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line);
		#else
		fm_split_frag(qm, f, size);
		#endif

	}else if (f->size<size){
		/* grow */
		
		#ifdef DBG_F_MALLOC
		LM_DBG("growing from %lu to %lu\n", f->size, size);
		#endif
		
		diff=size-f->size;
		n=FRAG_NEXT(f);
		
		if (((char*)n < (char*)qm->last_frag) &&  n->prev &&
		 ((n->size+FRAG_OVERHEAD)>=diff)){

			fm_remove_free(qm,n);
			/* join */
			f->size += n->size + FRAG_OVERHEAD;

			#if defined(DBG_F_MALLOC) || defined(STATISTICS)
			//qm->real_used -= FRAG_OVERHEAD;
			qm->used += FRAG_OVERHEAD;
			#endif

			/* split it if necessary */
			if (f->size > size){
				#ifdef DBG_F_MALLOC
				fm_split_frag(qm, f, size, file, "fragm. from fm_realloc",
						line);
				#else
				fm_split_frag(qm, f, size);
				#endif
			}
		}else{
			/* could not join => realloc */
			#ifdef DBG_F_MALLOC
			ptr=fm_malloc(qm, size, file, func, line);
			#else
			ptr = fm_malloc(qm, size);
			#endif
			if (ptr) {
				/* copy, need by libssl */
				memcpy(ptr, p, orig_size);
				#ifdef DBG_F_MALLOC
				fm_free(qm, p, file, func, line);
				#else
				fm_free(qm, p);
				#endif
			}
			p = ptr;
		}
	}else{
		/* do nothing */
	#ifdef DBG_F_MALLOC
		LM_DBG("doing nothing, same size: %lu - %lu\n", f->size, size);
	#endif
	}
	#ifdef DBG_F_MALLOC
	LM_DBG("returning %p\n", p);
	#endif

	#if defined(DBG_F_MALLOC) || defined(STATISTICS)
	if (qm->max_real_used<qm->real_used)
		qm->max_real_used=qm->real_used;
	#endif

	pkg_threshold_check();
	return p;
}
Beispiel #3
0
void* fm_malloc(struct fm_block* qm, unsigned long size)
#endif
{
	struct fm_frag* frag,*n;
	unsigned int hash;
		
	#ifdef DBG_F_MALLOC
	LM_DBG("params (%p, %lu), called from %s: %s(%d)\n", qm, size, file, func,
			line);
	#endif
	
	/*size must be a multiple of 8*/
	size=ROUNDUP(size);
	
	/*search for a suitable free frag*/

	for(hash=GET_HASH(size);hash<F_HASH_SIZE;hash++){
		frag=qm->free_hash[hash].first;
		for( ; frag; frag = frag->u.nxt_free )
			if ( frag->size >= size ) goto found;
		/* try in a bigger bucket */
	}
	/* not found, bad! */

	LM_WARN("Not enough free memory, will atempt defragmenation\n");

	for( frag = qm->first_frag; (char*)frag < (char*)qm->last_frag;  )
	{
		n = FRAG_NEXT(frag);

		if ( ((char*)n < (char*)qm->last_frag) &&  n->prev && frag->prev )
		{
			/* detach frag*/
			fm_remove_free(qm, frag);
			
			do
			{
				fm_remove_free(qm, n);
				frag->size += n->size + FRAG_OVERHEAD;

				#if defined(DBG_F_MALLOC) || defined(STATISTICS)
				//qm->real_used -= FRAG_OVERHEAD;
				qm->used += FRAG_OVERHEAD;
				#endif

				if( frag->size >size )
					goto solved;
				
				n = FRAG_NEXT(frag);
			}
			while
			( ((char*)n < (char*)qm->last_frag) &&  n->prev);

			fm_insert_free(qm,frag);
			
		}

		frag = n;
	}

	pkg_threshold_check();
	return 0;


		

found:
	/* we found it!*/
	
	fm_remove_free(qm,frag);
	
	/*see if we'll use full frag, or we'll split it in 2*/
	
	#ifdef DBG_F_MALLOC
	fm_split_frag(qm, frag, size, file, func, line);

	frag->file=file;
	frag->func=func;
	frag->line=line;
	frag->check=ST_CHECK_PATTERN;
	LM_DBG("params(%p, %lu), returns address %p \n", qm, size,
		(char*)frag+sizeof(struct fm_frag));
	#else
	fm_split_frag(qm, frag, size);
	#endif

solved:

	#if defined(DBG_F_MALLOC) || defined(STATISTICS)
	if (qm->max_real_used<qm->real_used)
		qm->max_real_used=qm->real_used;
	#endif

	pkg_threshold_check();
	return (char*)frag+sizeof(struct fm_frag);
}
Beispiel #4
0
void* fm_malloc(struct fm_block* qm, unsigned long size)
#endif
{
    struct fm_frag* frag,*n;
    unsigned int hash;

#ifdef DBG_MALLOC
    LM_GEN1(memlog, "%s_malloc(%lu), called from %s: %s(%d)\n", qm->name, size, file, func,
            line);
#endif

    /*size must be a multiple of 8*/
    size=ROUNDUP(size);

    /*search for a suitable free frag*/

    for(hash=GET_HASH(size); hash<F_HASH_SIZE; hash++) {
        frag=qm->free_hash[hash].first;
        for( ; frag; frag = frag->u.nxt_free )
            if ( frag->size >= size ) goto found;
        /* try in a bigger bucket */
    }
    /* not found, bad! */

#if defined(DBG_MALLOC) || defined(STATISTICS)
    LM_ERR(oom_errorf, qm->name, qm->size - qm->real_used,
           qm->name[0] == 'p' ? "M" : "m");
    LM_INFO("attempting defragmentation... (need %lu bytes)\n", size);
#else
    LM_ERR(oom_nostats_errorf, qm->name, qm->name[0] == 'p' ? "M" : "m");
    LM_INFO("attempting defragmentation... (need %lu bytes)\n", size);
#endif

    for( frag = qm->first_frag; (char*)frag < (char*)qm->last_frag;  )
    {
        n = FRAG_NEXT(frag);

        if ( ((char*)n < (char*)qm->last_frag) &&  n->prev && frag->prev )
        {
            /* detach frag*/
            fm_remove_free(qm, frag);

            do
            {
                fm_remove_free(qm, n);
                frag->size += n->size + FRAG_OVERHEAD;

#if defined(DBG_MALLOC) || defined(STATISTICS)
                //qm->real_used -= FRAG_OVERHEAD;
                qm->used += FRAG_OVERHEAD;
#endif

                if( frag->size >size ) {
#ifdef DBG_MALLOC
                    /* mark it as "busy" */
                    frag->is_free = 0;
#endif

                    goto solved;
                }

                n = FRAG_NEXT(frag);
            }
            while
            ( ((char*)n < (char*)qm->last_frag) &&  n->prev);

            fm_insert_free(qm,frag);

        }

        frag = n;
    }

    LM_INFO("unable to alloc a big enough fragment!\n");
    pkg_threshold_check();
    return 0;


found:
    /* we found it!*/

    fm_remove_free(qm,frag);

#ifdef DBG_MALLOC
    /* mark it as "busy" */
    frag->is_free = 0;
#endif

    /*see if we'll use full frag, or we'll split it in 2*/

#ifdef DBG_MALLOC
    fm_split_frag(qm, frag, size, file, func, line);

    frag->file=file;
    frag->func=func;
    frag->line=line;
    frag->check=ST_CHECK_PATTERN;
    LM_GEN1(memlog, "%s_malloc(%lu), returns address %p\n", qm->name, size,
            (char*)frag+sizeof(struct fm_frag));
#else
    fm_split_frag(qm, frag, size);
#endif

solved:

#if defined(DBG_MALLOC) || defined(STATISTICS)
    if (qm->max_real_used<qm->real_used)
        qm->max_real_used=qm->real_used;
    qm->fragments += 1;
#endif

    pkg_threshold_check();
    return (char*)frag+sizeof(struct fm_frag);
}