let dynamic allocation of table work

This commit is contained in:
Ben Gras 2006-06-27 14:15:47 +00:00
parent 607fb6bf7f
commit 59bedc2074

View File

@ -120,38 +120,23 @@ cpf_new_grantslot(void)
* necessary. If no free slot is found and the grow failed, * necessary. If no free slot is found and the grow failed,
* return -1. Otherwise, return grant slot number. * return -1. Otherwise, return grant slot number.
*/ */
static cp_grant_id_t i = 0;
cp_grant_id_t g; cp_grant_id_t g;
int n = 0;
/* Any slots at all? */
if(ngrants < 1) {
errno = ENOSPC;
return -1;
}
/* Find free slot. */ /* Find free slot. */
for(g = i % ngrants; for(g = 0; g < ngrants && (grants[g].cp_flags & CPF_USED); g++)
n < ngrants && (grants[g].cp_flags & CPF_USED); n++) ;
g = (g+1) % ngrants;
/* Where to start searching next time. */
i = g+1;
assert(g <= ngrants); assert(g <= ngrants);
assert(n <= ngrants);
/* No free slot found? */ /* No free slot found? */
if(n == ngrants) { if(g == ngrants) {
cpf_grow(); cpf_grow();
assert(n <= ngrants); /* ngrants can't shrink. */ assert(g <= ngrants); /* ngrants can't shrink. */
if(n == ngrants) { if(g == ngrants) {
/* ngrants hasn't increased. */ /* ngrants hasn't increased. */
errno = ENOSPC; errno = ENOSPC;
return -1; return -1;
} }
/* The new grant is the first available new grant slot. */
g = n;
} }
/* Basic sanity checks - if we get this far, g must be a valid, /* Basic sanity checks - if we get this far, g must be a valid,
@ -287,7 +272,7 @@ int n;
for(i = 0; i < n; i++) { for(i = 0; i < n; i++) {
if((grant_ids[i] = cpf_new_grantslot()) < 0) if((grant_ids[i] = cpf_new_grantslot()) < 0)
break; break;
cpf_revoke(grant_ids[i]); grants[grant_ids[i]].cp_flags = CPF_USED;
} }
/* return however many grants were assigned. */ /* return however many grants were assigned. */