isl_blk.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #include <isl_blk.h>
  10. #include <isl_ctx_private.h>
  11. /* The maximal number of cache misses before first element is evicted */
  12. #define ISL_BLK_MAX_MISS 100
  13. struct isl_blk isl_blk_empty()
  14. {
  15. struct isl_blk block;
  16. block.size = 0;
  17. block.data = NULL;
  18. return block;
  19. }
  20. static int isl_blk_is_empty(struct isl_blk block)
  21. {
  22. return block.size == 0 && block.data == NULL;
  23. }
  24. static struct isl_blk isl_blk_error()
  25. {
  26. struct isl_blk block;
  27. block.size = -1;
  28. block.data = NULL;
  29. return block;
  30. }
  31. int isl_blk_is_error(struct isl_blk block)
  32. {
  33. return block.size == -1 && block.data == NULL;
  34. }
  35. static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block)
  36. {
  37. int i;
  38. for (i = 0; i < block.size; ++i)
  39. isl_int_clear(block.data[i]);
  40. free(block.data);
  41. }
  42. static struct isl_blk extend(struct isl_ctx *ctx, struct isl_blk block,
  43. size_t new_n)
  44. {
  45. int i;
  46. isl_int *p;
  47. if (block.size >= new_n)
  48. return block;
  49. p = isl_realloc_array(ctx, block.data, isl_int, new_n);
  50. if (!p) {
  51. isl_blk_free_force(ctx, block);
  52. return isl_blk_error();
  53. }
  54. block.data = p;
  55. for (i = block.size; i < new_n; ++i)
  56. isl_int_init(block.data[i]);
  57. block.size = new_n;
  58. return block;
  59. }
  60. struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
  61. {
  62. int i;
  63. struct isl_blk block;
  64. block = isl_blk_empty();
  65. if (n && ctx->n_cached) {
  66. int best = 0;
  67. for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached; ++i) {
  68. if (ctx->cache[best].size < n) {
  69. if (ctx->cache[i].size > ctx->cache[best].size)
  70. best = i;
  71. } else if (ctx->cache[i].size >= n &&
  72. ctx->cache[i].size < ctx->cache[best].size)
  73. best = i;
  74. }
  75. if (ctx->cache[best].size < 2 * n + 100) {
  76. block = ctx->cache[best];
  77. if (--ctx->n_cached != best)
  78. ctx->cache[best] = ctx->cache[ctx->n_cached];
  79. if (best == 0)
  80. ctx->n_miss = 0;
  81. } else if (ctx->n_miss++ >= ISL_BLK_MAX_MISS) {
  82. isl_blk_free_force(ctx, ctx->cache[0]);
  83. if (--ctx->n_cached != 0)
  84. ctx->cache[0] = ctx->cache[ctx->n_cached];
  85. ctx->n_miss = 0;
  86. }
  87. }
  88. return extend(ctx, block, n);
  89. }
  90. struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block,
  91. size_t new_n)
  92. {
  93. if (isl_blk_is_empty(block))
  94. return isl_blk_alloc(ctx, new_n);
  95. return extend(ctx, block, new_n);
  96. }
  97. void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
  98. {
  99. if (isl_blk_is_empty(block) || isl_blk_is_error(block))
  100. return;
  101. if (ctx->n_cached < ISL_BLK_CACHE_SIZE)
  102. ctx->cache[ctx->n_cached++] = block;
  103. else
  104. isl_blk_free_force(ctx, block);
  105. }
  106. void isl_blk_clear_cache(struct isl_ctx *ctx)
  107. {
  108. int i;
  109. for (i = 0; i < ctx->n_cached; ++i)
  110. isl_blk_free_force(ctx, ctx->cache[i]);
  111. ctx->n_cached = 0;
  112. }