FreeBSD/contrib/bsnmp/snmpd/main.c
/*
 * Request ID handling.
 *
 * Allocate a new range of request ids. Use a first fit algorithm.
 */
Request ID handling.
Allocate a new range of request ids. Use a first fit algorithm.
Request ID handling . Allocate a new range of request ids . Use a first fit algorithm .
u_int
reqid_allocate(int size, struct lmodule *mod)
{
	u_int type;
	struct idrange *r, *r1;
	if (size <= 0 || size > INT32_MAX) {
		syslog(LOG_CRIT, "%s: size out of range: %d", __func__, size);
		return (0);
	}
	do {
		if ((type = next_idrange++) == UINT_MAX)
			next_idrange = 1;
		TAILQ_FOREACH(r, &idrange_list, link)
			if (r->type == type)
				break;
	} while(r != NULL);
	if (TAILQ_EMPTY(&idrange_list))
		r = NULL;
	else {
		r = TAILQ_FIRST(&idrange_list);
		if (r->base < size) {
			while((r1 = TAILQ_NEXT(r, link)) != NULL) {
				if (r1->base - (r->base + r->size) >= size)
					break;
				r = r1;
			}
			r = r1;
		}
		if (r == NULL) {
			r1 = TAILQ_LAST(&idrange_list, idrange_list);
			if (INT32_MAX - size + 1 < r1->base + r1->size) {
				syslog(LOG_ERR, "out of id ranges (%u)", size);
				return (0);
			}
		}
	}
	if ((r1 = malloc(sizeof(struct idrange))) == NULL) {
		syslog(LOG_ERR, "%s: %m", __FUNCTION__);
		return (0);
	}
	r1->type = type;
	r1->size = size;
	r1->owner = mod;
	if (TAILQ_EMPTY(&idrange_list) || r == TAILQ_FIRST(&idrange_list)) {
		r1->base = 0;
		TAILQ_INSERT_HEAD(&idrange_list, r1, link);
	} else if (r == NULL) {
		r = TAILQ_LAST(&idrange_list, idrange_list);
		r1->base = r->base + r->size;
		TAILQ_INSERT_TAIL(&idrange_list, r1, link);
	} else {
		r = TAILQ_PREV(r, idrange_list, link);
		r1->base = r->base + r->size;
		TAILQ_INSERT_AFTER(&idrange_list, r, r1, link);
	}
	r1->next = r1->base;
	return (type);
}
u_int reqid_allocate ( int size , struct lmodule * mod ) { u_int type ; struct idrange * r , * r1 ; if ( size <= 0 || size > INT32_MAX ) { syslog ( LOG_CRIT , " " , __func__ , size ) ; return ( 0 ) ; } do { if ( ( type = next_idrange ++ ) == UINT_MAX ) next_idrange = 1 ; TAILQ_FOREACH ( r , & idrange_list , link ) if ( r -> type == type ) break ; } while ( r != NULL ) ; if ( TAILQ_EMPTY ( & idrange_list ) ) r = NULL ; else { r = TAILQ_FIRST ( & idrange_list ) ; if ( r -> base < size ) { while ( ( r1 = TAILQ_NEXT ( r , link ) ) != NULL ) { if ( r1 -> base - ( r -> base + r -> size ) >= size ) break ; r = r1 ; } r = r1 ; } if ( r == NULL ) { r1 = TAILQ_LAST ( & idrange_list , idrange_list ) ; if ( INT32_MAX - size + 1 < r1 -> base + r1 -> size ) { syslog ( LOG_ERR , " " , size ) ; return ( 0 ) ; } } } if ( ( r1 = malloc ( sizeof ( struct idrange ) ) ) == NULL ) { syslog ( LOG_ERR , " " , __FUNCTION__ ) ; return ( 0 ) ; } r1 -> type = type ; r1 -> size = size ; r1 -> owner = mod ; if ( TAILQ_EMPTY ( & idrange_list ) || r == TAILQ_FIRST ( & idrange_list ) ) { r1 -> base = 0 ; TAILQ_INSERT_HEAD ( & idrange_list , r1 , link ) ; } else if ( r == NULL ) { r = TAILQ_LAST ( & idrange_list , idrange_list ) ; r1 -> base = r -> base + r -> size ; TAILQ_INSERT_TAIL ( & idrange_list , r1 , link ) ; } else { r = TAILQ_PREV ( r , idrange_list , link ) ; r1 -> base = r -> base + r -> size ; TAILQ_INSERT_AFTER ( & idrange_list , r , r1 , link ) ; } r1 -> next = r1 -> base ; return ( type ) ; }
Request ID handling.
Request ID handling .
/* allocate a type id */ /* find a range */ /* allocate structure */