Esempio n. 1
0
/** @see Allocator->realloc() */
static void* allocatorRealloc(const void* original, size_t length, const struct Allocator* allocator)
{
    if (original == NULL) {
        return allocatorMalloc(length, allocator);
    }

    // Need to pointer to make sure we dont copy too much.
    struct BufferAllocator_context* context =
        (struct BufferAllocator_context*) allocator->context;
    char* pointer = context->pointer;
    size_t amountToClone =
        length < (size_t)(pointer - (char*)original) ? length : (size_t)(pointer - (char*)original);

    // The likelyhood of nothing having been allocated since is almost 0 so we will always create a new
    // allocation and copy into it.
    void* newAlloc = allocator->malloc(length, allocator);
    Bits_memcpy(newAlloc, original, amountToClone);
    return newAlloc;
}
Esempio n. 2
0
/** @see Allocator->realloc() */
static void* allocatorRealloc(const void* original,
                              size_t size,
                              const struct Allocator* allocator)
{
    struct CanaryAllocator_pvt* ctx = Identity_cast((struct CanaryAllocator_pvt*) allocator);
    if (((uint8_t*) original) == NULL) {
        return allocatorMalloc(size, allocator);
    }
    uint32_t* beginCanary = ((uint32_t*) original) - 1;
    Assert_always(*beginCanary == ctx->canaryValue);
    for (int i = 0; i < ctx->canaryCount; i++) {
        if (ctx->canaries[i] == beginCanary) {
            Assert_always(ctx->canaryCount > i + 1 && *ctx->canaries[i + 1] == ctx->canaryValue);
            for (int j = i + 2; j < ctx->canaryCount; j++) {
                ctx->canaries[j - 2] = ctx->canaries[j];
            }
            ctx->canaryCount -= 2;
            break;
        }
    }
    uint32_t* out = ctx->alloc->realloc(((uint32_t*)original) - 1, SIZE_BYTES(size), ctx->alloc);
    return newAllocation(ctx, out, SIZE_INTS(size));
}
Esempio n. 3
0
/** @see Allocator->clone() */
static void* allocatorClone(size_t length, const struct Allocator* allocator, const void* toClone)
{
    void* pointer = allocatorMalloc(length, allocator);
    Bits_memcpy(pointer, toClone, length);
    return pointer;
}
Esempio n. 4
0
/** @see Allocator->calloc() */
static void* allocatorCalloc(size_t length, size_t count, const struct Allocator* allocator)
{
    void* pointer = allocatorMalloc(length * count, allocator);
    Bits_memset(pointer, 0, length * count);
    return pointer;
}