assocdat.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * YASM associated data storage (libyasm internal use)
  3. *
  4. * Copyright (C) 2003-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "util.h"
  28. #include "coretype.h"
  29. #include "assocdat.h"
  30. typedef struct assoc_data_item {
  31. const yasm_assoc_data_callback *callback;
  32. void *data;
  33. } assoc_data_item;
  34. struct yasm__assoc_data {
  35. assoc_data_item *vector;
  36. size_t size;
  37. size_t alloc;
  38. };
  39. yasm__assoc_data *
  40. yasm__assoc_data_create(void)
  41. {
  42. yasm__assoc_data *assoc_data = yasm_xmalloc(sizeof(yasm__assoc_data));
  43. assoc_data->size = 0;
  44. assoc_data->alloc = 2;
  45. assoc_data->vector = yasm_xmalloc(assoc_data->alloc *
  46. sizeof(assoc_data_item));
  47. return assoc_data;
  48. }
  49. void *
  50. yasm__assoc_data_get(yasm__assoc_data *assoc_data,
  51. const yasm_assoc_data_callback *callback)
  52. {
  53. size_t i;
  54. if (!assoc_data)
  55. return NULL;
  56. for (i=0; i<assoc_data->size; i++) {
  57. if (assoc_data->vector[i].callback == callback)
  58. return assoc_data->vector[i].data;
  59. }
  60. return NULL;
  61. }
  62. yasm__assoc_data *
  63. yasm__assoc_data_add(yasm__assoc_data *assoc_data_arg,
  64. const yasm_assoc_data_callback *callback, void *data)
  65. {
  66. yasm__assoc_data *assoc_data;
  67. assoc_data_item *item = NULL;
  68. size_t i;
  69. /* Create a new assoc_data if necessary */
  70. if (assoc_data_arg)
  71. assoc_data = assoc_data_arg;
  72. else
  73. assoc_data = yasm__assoc_data_create();
  74. /* See if there's already assocated data for this callback */
  75. for (i=0; i<assoc_data->size; i++) {
  76. if (assoc_data->vector[i].callback == callback) {
  77. item = &assoc_data->vector[i];
  78. break;
  79. }
  80. }
  81. /* No? Then append a new one */
  82. if (!item) {
  83. assoc_data->size++;
  84. if (assoc_data->size > assoc_data->alloc) {
  85. assoc_data->alloc *= 2;
  86. assoc_data->vector =
  87. yasm_xrealloc(assoc_data->vector,
  88. assoc_data->alloc * sizeof(assoc_data_item));
  89. }
  90. item = &assoc_data->vector[assoc_data->size-1];
  91. item->callback = callback;
  92. item->data = NULL;
  93. }
  94. /* Delete existing data (if any) */
  95. if (item->data && item->data != data)
  96. item->callback->destroy(item->data);
  97. item->data = data;
  98. return assoc_data;
  99. }
  100. void
  101. yasm__assoc_data_destroy(yasm__assoc_data *assoc_data)
  102. {
  103. size_t i;
  104. if (!assoc_data)
  105. return;
  106. for (i=0; i<assoc_data->size; i++)
  107. assoc_data->vector[i].callback->destroy(assoc_data->vector[i].data);
  108. yasm_xfree(assoc_data->vector);
  109. yasm_xfree(assoc_data);
  110. }
  111. void
  112. yasm__assoc_data_print(const yasm__assoc_data *assoc_data, FILE *f,
  113. int indent_level)
  114. {
  115. /*TODO*/
  116. }