4 struct isl_blk isl_blk_empty()
12 static int isl_blk_is_empty(struct isl_blk block)
14 return block.size == 0 && block.data == NULL;
17 static struct isl_blk isl_blk_error()
25 int isl_blk_is_error(struct isl_blk block)
27 return block.size == -1 && block.data == NULL;
30 struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
37 for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached; ++i) {
38 if (ctx->cache[best].size < n) {
39 if (ctx->cache[i].size > ctx->cache[best].size)
41 } else if (ctx->cache[i].size >= n &&
42 ctx->cache[i].size < ctx->cache[best].size)
45 block = ctx->cache[best];
46 if (--ctx->n_cached != best)
47 ctx->cache[best] = ctx->cache[ctx->n_cached];
49 block = isl_blk_empty();
51 return isl_blk_extend(ctx, block, n);
54 struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block,
60 if (block.size >= new_n)
64 block.data = isl_realloc_array(ctx, block.data, isl_int, new_n);
67 return isl_blk_error();
70 for (i = block.size; i < new_n; ++i)
71 isl_int_init(block.data[i]);
77 static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block)
81 for (i = 0; i < block.size; ++i)
82 isl_int_clear(block.data[i]);
86 void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
88 if (isl_blk_is_empty(block) || isl_blk_is_error(block))
91 if (ctx->n_cached < ISL_BLK_CACHE_SIZE)
92 ctx->cache[ctx->n_cached++] = block;
94 isl_blk_free_force(ctx, block);
97 void isl_blk_clear_cache(struct isl_ctx *ctx)
101 for (i = 0; i < ctx->n_cached; ++i)
102 isl_blk_free_force(ctx, ctx->cache[i]);