X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_blk.c;h=29844d5c6d39ffcdc3c9e24e314aec9c8d158a8b;hb=de51a9bc4da5dd3f1f9f57c2362da6f9752c44e0;hp=a7a4496d0bbd62330d626e9349ec1a2734851ed0;hpb=cc726006058136865f8c2f496d3df57b9f937ea5;p=platform%2Fupstream%2Fisl.git diff --git a/isl_blk.c b/isl_blk.c index a7a4496..29844d5 100644 --- a/isl_blk.c +++ b/isl_blk.c @@ -1,41 +1,68 @@ -#include "isl_blk.h" +/* + * Copyright 2008-2009 Katholieke Universiteit Leuven + * + * Use of this software is governed by the MIT license + * + * Written by Sven Verdoolaege, K.U.Leuven, Departement + * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium + */ -struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n) +#include +#include + +/* The maximal number of cache misses before first element is evicted */ +#define ISL_BLK_MAX_MISS 100 + +struct isl_blk isl_blk_empty() { struct isl_blk block; + block.size = 0; + block.data = NULL; + return block; +} - block.data = isl_alloc_array(ctx, isl_int, n); - if (!block.data) - block.size = -1; - else { - int i; - block.size = n; - for (i = 0; i < n; ++i) - isl_int_init(block.data[i]); - } +static int isl_blk_is_empty(struct isl_blk block) +{ + return block.size == 0 && block.data == NULL; +} +static struct isl_blk isl_blk_error() +{ + struct isl_blk block; + block.size = -1; + block.data = NULL; return block; } -struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block, +int isl_blk_is_error(struct isl_blk block) +{ + return block.size == -1 && block.data == NULL; +} + +static struct isl_blk extend(struct isl_ctx *ctx, struct isl_blk block, size_t new_n) { + int i; + isl_int *p; + if (block.size >= new_n) return block; + + p = block.data; block.data = isl_realloc_array(ctx, block.data, isl_int, new_n); - if (!block.data) - block.size = -1; - else { - int i; - for (i = block.size; i < new_n; ++i) - isl_int_init(block.data[i]); - block.size = new_n; + if (!block.data) { + free(p); + return isl_blk_error(); } + for (i = block.size; i < new_n; ++i) + isl_int_init(block.data[i]); + block.size = new_n; + return block; } -void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block) +static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block) { int i; @@ -43,3 +70,65 @@ void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block) isl_int_clear(block.data[i]); free(block.data); } + +struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n) +{ + int i; + struct isl_blk block; + + block = isl_blk_empty(); + if (n && ctx->n_cached) { + int best = 0; + for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached; ++i) { + if (ctx->cache[best].size < n) { + if (ctx->cache[i].size > ctx->cache[best].size) + best = i; + } else if (ctx->cache[i].size >= n && + ctx->cache[i].size < ctx->cache[best].size) + best = i; + } + if (ctx->cache[best].size < 2 * n + 100) { + block = ctx->cache[best]; + if (--ctx->n_cached != best) + ctx->cache[best] = ctx->cache[ctx->n_cached]; + if (best == 0) + ctx->n_miss = 0; + } else if (ctx->n_miss++ >= ISL_BLK_MAX_MISS) { + isl_blk_free_force(ctx, ctx->cache[0]); + if (--ctx->n_cached != 0) + ctx->cache[0] = ctx->cache[ctx->n_cached]; + ctx->n_miss = 0; + } + } + + return extend(ctx, block, n); +} + +struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block, + size_t new_n) +{ + if (isl_blk_is_empty(block)) + return isl_blk_alloc(ctx, new_n); + + return extend(ctx, block, new_n); +} + +void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block) +{ + if (isl_blk_is_empty(block) || isl_blk_is_error(block)) + return; + + if (ctx->n_cached < ISL_BLK_CACHE_SIZE) + ctx->cache[ctx->n_cached++] = block; + else + isl_blk_free_force(ctx, block); +} + +void isl_blk_clear_cache(struct isl_ctx *ctx) +{ + int i; + + for (i = 0; i < ctx->n_cached; ++i) + isl_blk_free_force(ctx, ctx->cache[i]); + ctx->n_cached = 0; +}