Exemple #1
0
/** @see Allocator->malloc() */
static void* allocatorMalloc(size_t size, const struct Allocator* allocator)
{
    struct CanaryAllocator_pvt* ctx = Identity_cast((struct CanaryAllocator_pvt*) allocator);
    // get it on an even number of ints.
    uint32_t* out = ctx->alloc->malloc(SIZE_BYTES(size), ctx->alloc);
    return newAllocation(ctx, out, SIZE_INTS(size));
}
Exemple #2
0
struct Allocator* Allocator__child(struct Allocator* allocator, const char* file, int line)
{
    struct Allocator_pvt* parent = Identity_check((struct Allocator_pvt*) allocator);
    check(parent);

    struct Allocator_pvt stackChild = {
        .pub = {
            .fileName = file,
            .lineNum = line,
        },
        .rootAlloc = parent->rootAlloc
    };
    Identity_set(&stackChild);
    #ifdef Allocator_USE_CANARIES
        stackChild.nextCanary = stackChild.canary = parent->nextCanary;
    #endif

    struct Allocator_pvt* child =
        newAllocation(&stackChild, sizeof(struct Allocator_pvt), file, line);
    Bits_memcpy(child, &stackChild, sizeof(struct Allocator_pvt));

    // Link the child into the parent's allocator list
    connect(parent, child, file, line);

    check(parent);
    return &child->pub;
}
Exemple #3
0
void* Allocator__malloc(struct Allocator* allocator,
                        unsigned long length,
                        const char* fileName,
                        int lineNum)
{
    struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*) allocator);
    return newAllocation(ctx, length, fileName, lineNum);
}
Exemple #4
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));
}