mspan_allocate_from_superblock(char *base, mspan *superblock)
{
char *spanbase;
+ char *result;
uint16 object_size;
uint16 total;
if (superblock->nused >= total)
return NULL;
- /* Try to reuse a previously-freed object. */
+ /* Do the allocation. */
spanbase = base + superblock->first_page * MSPAN_PAGE_SIZE;
if (superblock->firstfree != MSPAN_FIRSTFREE_NONE)
{
- uint16 *firstfree;
-
- firstfree =
- (uint16 *) (spanbase + (superblock->firstfree * object_size));
- superblock->firstfree = *firstfree;
- return (char *) firstfree;
+ /* There's a freed object available for reuse. Allocate it. */
+ result = spanbase + (superblock->firstfree * object_size);
+ superblock->firstfree = ((uint16 *) result)[0];
+ }
+ else
+ {
+ /* Carve out an object from not-previously-used part of span. */
+ result = spanbase + (superblock->ninitialized * object_size);
+ ++superblock->ninitialized;
}
- /*
- * Carve out space from the uninitialized portion of the span. This
- * should always work, since we already verified that nused < total.
- */
-
- /*
- * XXX. Implementation needed.
- */
- return NULL;
+ /* Update counter and return result. */
+ ++superblock->nused;
+ Assert(superblock->nused <= superblock->ninitialized);
+ Assert(result < spanbase + object_size * total);
+ return result;
}
/*