summaryrefslogtreecommitdiff
path: root/isl_blk.c
diff options
context:
space:
mode:
authorSven Verdoolaege <skimo@kotnet.org>2008-08-13 15:01:57 +0200
committerSven Verdoolaege <skimo@kotnet.org>2008-08-25 10:15:07 +0200
commitc4084e48d715859a1ce39847ffd4412535d8a263 (patch)
tree9125adc09f429be369282fd93426b773a918bc1c /isl_blk.c
parentc46fd758a6b4982a79e707981b7d9e5fa8fe1f50 (diff)
downloadisl-c4084e48d715859a1ce39847ffd4412535d8a263.tar.gz
isl-c4084e48d715859a1ce39847ffd4412535d8a263.tar.bz2
isl-c4084e48d715859a1ce39847ffd4412535d8a263.zip
keep cache of blocks of isl_ints
Diffstat (limited to 'isl_blk.c')
-rw-r--r--isl_blk.c87
1 files changed, 69 insertions, 18 deletions
diff --git a/isl_blk.c b/isl_blk.c
index 0cb833cb..246b40d4 100644
--- a/isl_blk.c
+++ b/isl_blk.c
@@ -1,49 +1,80 @@
#include "isl_blk.h"
+#include "isl_ctx.h"
struct isl_blk isl_blk_empty()
{
struct isl_blk block;
+ block.size = 0;
+ block.data = NULL;
+ return block;
+}
+
+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;
}
+int isl_blk_is_error(struct isl_blk block)
+{
+ return block.size == -1 && block.data == NULL;
+}
+
struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
{
+ int i;
struct isl_blk 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]);
- }
+ if (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;
+ }
+ block = ctx->cache[best];
+ if (--ctx->n_cached != best)
+ ctx->cache[best] = ctx->cache[ctx->n_cached];
+ } else
+ block = isl_blk_empty();
- return block;
+ return isl_blk_extend(ctx, block, n);
}
struct isl_blk 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;
@@ -51,3 +82,23 @@ void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
isl_int_clear(block.data[i]);
free(block.data);
}
+
+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;
+}