bin/varnishd/storage/storage_file.c
static struct smf *
alloc_smf(struct smf_sc *sc, off_t bytes)
{
	struct smf *sp, *sp2;
	off_t b;

	AZ(bytes % sc->pagesize);
	b = bytes / sc->pagesize;
	if (b >= NBUCKET)
		b = NBUCKET - 1;
	sp = NULL;
	for (; b < NBUCKET - 1; b++) {
		sp = VTAILQ_FIRST(&sc->free[b]);
		if (sp != NULL)
			break;
	}
	if (sp == NULL) {
		VTAILQ_FOREACH(sp, &sc->free[NBUCKET -1], status)
			if (sp->size >= bytes)
				break;
	}
	if (sp == NULL)
		return (sp);

	assert(sp->size >= bytes);
	remfree(sc, sp);

	if (sp->size == bytes) {
		sp->alloc = 1;
		VTAILQ_INSERT_TAIL(&sc->used, sp, status);
		return (sp);
	}

	/* Split from front */
	sp2 = malloc(sizeof *sp2);
	XXXAN(sp2);
	sc->stats->g_smf++;
	*sp2 = *sp;

	sp->offset += bytes;
	sp->ptr += bytes;
	sp->size -= bytes;

	sp2->size = bytes;
	sp2->alloc = 1;
	VTAILQ_INSERT_BEFORE(sp, sp2, order);
	VTAILQ_INSERT_TAIL(&sc->used, sp2, status);
	insfree(sc, sp);
	return (sp2);
}
/*--------------------------------------------------------------------
 * Allocate a range from the first free range that is large enough.
 */
Allocate a range from the first free range that is large enough.
Allocate a range from the first free range that is large enough .
static struct smf *
alloc_smf(struct smf_sc *sc, off_t bytes)
{
	struct smf *sp, *sp2;
	off_t b;
	AZ(bytes % sc->pagesize);
	b = bytes / sc->pagesize;
	if (b >= NBUCKET)
		b = NBUCKET - 1;
	sp = NULL;
	for (; b < NBUCKET - 1; b++) {
		sp = VTAILQ_FIRST(&sc->free[b]);
		if (sp != NULL)
			break;
	}
	if (sp == NULL) {
		VTAILQ_FOREACH(sp, &sc->free[NBUCKET -1], status)
			if (sp->size >= bytes)
				break;
	}
	if (sp == NULL)
		return (sp);
	assert(sp->size >= bytes);
	remfree(sc, sp);
	if (sp->size == bytes) {
		sp->alloc = 1;
		VTAILQ_INSERT_TAIL(&sc->used, sp, status);
		return (sp);
	}
	sp2 = malloc(sizeof *sp2);
	XXXAN(sp2);
	sc->stats->g_smf++;
	*sp2 = *sp;
	sp->offset += bytes;
	sp->ptr += bytes;
	sp->size -= bytes;
	sp2->size = bytes;
	sp2->alloc = 1;
	VTAILQ_INSERT_BEFORE(sp, sp2, order);
	VTAILQ_INSERT_TAIL(&sc->used, sp2, status);
	insfree(sc, sp);
	return (sp2);
}
static struct smf * alloc_smf ( struct smf_sc * sc , off_t bytes ) { struct smf * sp , * sp2 ; off_t b ; AZ ( bytes % sc -> pagesize ) ; b = bytes / sc -> pagesize ; if ( b >= NBUCKET ) b = NBUCKET - 1 ; sp = NULL ; for ( ; b < NBUCKET - 1 ; b ++ ) { sp = VTAILQ_FIRST ( & sc -> free [ b ] ) ; if ( sp != NULL ) break ; } if ( sp == NULL ) { VTAILQ_FOREACH ( sp , & sc -> free [ NBUCKET - 1 ] , status ) if ( sp -> size >= bytes ) break ; } if ( sp == NULL ) return ( sp ) ; assert ( sp -> size >= bytes ) ; remfree ( sc , sp ) ; if ( sp -> size == bytes ) { sp -> alloc = 1 ; VTAILQ_INSERT_TAIL ( & sc -> used , sp , status ) ; return ( sp ) ; } sp2 = malloc ( sizeof * sp2 ) ; XXXAN ( sp2 ) ; sc -> stats -> g_smf ++ ; * sp2 = * sp ; sp -> offset += bytes ; sp -> ptr += bytes ; sp -> size -= bytes ; sp2 -> size = bytes ; sp2 -> alloc = 1 ; VTAILQ_INSERT_BEFORE ( sp , sp2 , order ) ; VTAILQ_INSERT_TAIL ( & sc -> used , sp2 , status ) ; insfree ( sc , sp ) ; return ( sp2 ) ; }
Allocate a range from the first free range that is large enough.
Allocate a range from the first free range that is large enough .
/* Split from front */