Beispiel #1
0
void*
MemPool_grab(MemoryPool *self, size_t amount) {
    INCREASE_TO_WORD_MULTIPLE(amount);
    self->last_buf = self->buf;

    // Verify that we have enough stocked up, otherwise get more.
    self->buf += amount;
    if (self->buf >= self->limit) {
        // Get enough mem from system or die trying.
        S_init_arena(self, amount);
        self->last_buf = self->buf;
        self->buf += amount;
    }

    // Track bytes we've allocated from this pool.
    self->consumed += amount;

    return self->last_buf;
}
Beispiel #2
0
void*
MemPool_Grab_IMP(MemoryPool *self, size_t amount) {
    MemoryPoolIVARS *const ivars = MemPool_IVARS(self);
    INCREASE_TO_WORD_MULTIPLE(amount);
    ivars->last_buf = ivars->buf;

    // Verify that we have enough stocked up, otherwise get more.
    ivars->buf += amount;
    if (ivars->buf >= ivars->limit) {
        // Get enough mem from system or die trying.
        S_init_arena(self, ivars, amount);
        ivars->last_buf = ivars->buf;
        ivars->buf += amount;
    }

    // Track bytes we've allocated from this pool.
    ivars->consumed += amount;

    return ivars->last_buf;
}
Beispiel #3
0
void
MemPool_resize(MemoryPool *self, void *ptr, size_t new_amount) {
    const size_t last_amount = self->buf - self->last_buf;
    INCREASE_TO_WORD_MULTIPLE(new_amount);

    if (ptr != self->last_buf) {
        THROW(ERR, "Not the last pointer allocated.");
    }
    else {
        if (new_amount <= last_amount) {
            const size_t difference = last_amount - new_amount;
            self->buf      -= difference;
            self->consumed -= difference;
        }
        else {
            THROW(ERR, "Can't resize to greater amount: %u64 > %u64",
                  (uint64_t)new_amount, (uint64_t)last_amount);
        }
    }
}
Beispiel #4
0
void
MemPool_Resize_IMP(MemoryPool *self, void *ptr, size_t new_amount) {
    MemoryPoolIVARS *const ivars = MemPool_IVARS(self);
    const size_t last_amount = (size_t)(ivars->buf - ivars->last_buf);
    INCREASE_TO_WORD_MULTIPLE(new_amount);

    if (ptr != ivars->last_buf) {
        THROW(ERR, "Not the last pointer allocated.");
    }
    else {
        if (new_amount <= last_amount) {
            const size_t difference = last_amount - new_amount;
            ivars->buf      -= difference;
            ivars->consumed -= difference;
        }
        else {
            THROW(ERR, "Can't resize to greater amount: %u64 > %u64",
                  (uint64_t)new_amount, (uint64_t)last_amount);
        }
    }
}