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
13 struct isl_blk
isl_blk_empty()
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()
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
)
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
)
50 } else if (ctx
->cache
[i
].size
>= n
&&
51 ctx
->cache
[i
].size
< ctx
->cache
[best
].size
)
54 block
= ctx
->cache
[best
];
55 if (--ctx
->n_cached
!= best
)
56 ctx
->cache
[best
] = ctx
->cache
[ctx
->n_cached
];
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
,
69 if (block
.size
>= new_n
)
73 block
.data
= isl_realloc_array(ctx
, block
.data
, isl_int
, new_n
);
76 return isl_blk_error();
79 for (i
= block
.size
; i
< new_n
; ++i
)
80 isl_int_init(block
.data
[i
]);
86 static void isl_blk_free_force(struct isl_ctx
*ctx
, struct isl_blk block
)
90 for (i
= 0; i
< block
.size
; ++i
)
91 isl_int_clear(block
.data
[i
]);
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
))
100 if (ctx
->n_cached
< ISL_BLK_CACHE_SIZE
)
101 ctx
->cache
[ctx
->n_cached
++] = block
;
103 isl_blk_free_force(ctx
, block
);
106 void isl_blk_clear_cache(struct isl_ctx
*ctx
)
110 for (i
= 0; i
< ctx
->n_cached
; ++i
)
111 isl_blk_free_force(ctx
, ctx
->cache
[i
]);