sync.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2009 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * sync.c the Netwide Disassembler synchronisation processing module
  35. */
  36. #include "compiler.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <limits.h>
  40. #include "nasmlib.h"
  41. #include "sync.h"
  42. #define SYNC_MAX_SHIFT 31
  43. #define SYNC_MAX_SIZE (1U << SYNC_MAX_SHIFT)
  44. /* initial # of sync points (*must* be power of two)*/
  45. #define SYNC_INITIAL_CHUNK (1U << 12)
  46. /*
  47. * This lot manages the current set of sync points by means of a
  48. * heap (priority queue) structure.
  49. */
  50. static struct Sync {
  51. uint64_t pos;
  52. uint32_t length;
  53. } *synx;
  54. static uint32_t max_synx, nsynx;
  55. static inline void swap_sync(uint32_t dst, uint32_t src)
  56. {
  57. struct Sync t = synx[dst];
  58. synx[dst] = synx[src];
  59. synx[src] = t;
  60. }
  61. void init_sync(void)
  62. {
  63. max_synx = SYNC_INITIAL_CHUNK;
  64. synx = nasm_malloc((max_synx + 1) * sizeof(*synx));
  65. nsynx = 0;
  66. }
  67. void add_sync(uint64_t pos, uint32_t length)
  68. {
  69. uint32_t i;
  70. if (nsynx >= max_synx) {
  71. if (max_synx >= SYNC_MAX_SIZE) /* too many sync points! */
  72. return;
  73. max_synx = (max_synx << 1);
  74. synx = nasm_realloc(synx, (max_synx + 1) * sizeof(*synx));
  75. }
  76. nsynx++;
  77. synx[nsynx].pos = pos;
  78. synx[nsynx].length = length;
  79. for (i = nsynx; i > 1; i /= 2) {
  80. if (synx[i / 2].pos > synx[i].pos)
  81. swap_sync(i / 2, i);
  82. }
  83. }
  84. uint64_t next_sync(uint64_t position, uint32_t *length)
  85. {
  86. while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
  87. uint32_t i, j;
  88. swap_sync(nsynx, 1);
  89. nsynx--;
  90. i = 1;
  91. while (i * 2 <= nsynx) {
  92. j = i * 2;
  93. if (synx[j].pos < synx[i].pos &&
  94. (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
  95. swap_sync(j, i);
  96. i = j;
  97. } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
  98. swap_sync(j + 1, i);
  99. i = j + 1;
  100. } else
  101. break;
  102. }
  103. }
  104. if (nsynx > 0) {
  105. if (length)
  106. *length = synx[1].length;
  107. return synx[1].pos;
  108. } else {
  109. if (length)
  110. *length = 0L;
  111. return 0;
  112. }
  113. }