add isl_printer
[isl.git] / isl_blk.c
blob53465abb51650e4da1166b558f48af4f77bd130a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include "isl_blk.h"
11 #include "isl_ctx.h"
13 struct isl_blk isl_blk_empty()
15 struct isl_blk block;
16 block.size = 0;
17 block.data = NULL;
18 return block;
21 static int isl_blk_is_empty(struct isl_blk block)
23 return block.size == 0 && block.data == NULL;
26 static struct isl_blk isl_blk_error()
28 struct isl_blk block;
29 block.size = -1;
30 block.data = NULL;
31 return block;
34 int isl_blk_is_error(struct isl_blk block)
36 return block.size == -1 && block.data == NULL;
39 struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
41 int i;
42 struct isl_blk block;
44 if (ctx->n_cached) {
45 int best = 0;
46 for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached; ++i) {
47 if (ctx->cache[best].size < n) {
48 if (ctx->cache[i].size > ctx->cache[best].size)
49 best = i;
50 } else if (ctx->cache[i].size >= n &&
51 ctx->cache[i].size < ctx->cache[best].size)
52 best = i;
54 block = ctx->cache[best];
55 if (--ctx->n_cached != best)
56 ctx->cache[best] = ctx->cache[ctx->n_cached];
57 } else
58 block = isl_blk_empty();
60 return isl_blk_extend(ctx, block, n);
63 struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block,
64 size_t new_n)
66 int i;
67 isl_int *p;
69 if (block.size >= new_n)
70 return block;
72 p = block.data;
73 block.data = isl_realloc_array(ctx, block.data, isl_int, new_n);
74 if (!block.data) {
75 free(p);
76 return isl_blk_error();
79 for (i = block.size; i < new_n; ++i)
80 isl_int_init(block.data[i]);
81 block.size = new_n;
83 return block;
86 static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block)
88 int i;
90 for (i = 0; i < block.size; ++i)
91 isl_int_clear(block.data[i]);
92 free(block.data);
95 void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
97 if (isl_blk_is_empty(block) || isl_blk_is_error(block))
98 return;
100 if (ctx->n_cached < ISL_BLK_CACHE_SIZE)
101 ctx->cache[ctx->n_cached++] = block;
102 else
103 isl_blk_free_force(ctx, block);
106 void isl_blk_clear_cache(struct isl_ctx *ctx)
108 int i;
110 for (i = 0; i < ctx->n_cached; ++i)
111 isl_blk_free_force(ctx, ctx->cache[i]);
112 ctx->n_cached = 0;