tre-mem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. tre-mem.c - TRE memory allocator
  3. Copyright (c) 2001-2009 Ville Laurikari <[email protected]>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. This memory allocator is for allocating small memory blocks efficiently
  27. in terms of memory overhead and execution speed. The allocated blocks
  28. cannot be freed individually, only all at once. There can be multiple
  29. allocators, though.
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "tre.h"
  34. /*
  35. This memory allocator is for allocating small memory blocks efficiently
  36. in terms of memory overhead and execution speed. The allocated blocks
  37. cannot be freed individually, only all at once. There can be multiple
  38. allocators, though.
  39. */
  40. /* Returns a new memory allocator or NULL if out of memory. */
  41. tre_mem_t
  42. tre_mem_new_impl(int provided, void *provided_block)
  43. {
  44. tre_mem_t mem;
  45. if (provided)
  46. {
  47. mem = provided_block;
  48. memset(mem, 0, sizeof(*mem));
  49. }
  50. else
  51. mem = xcalloc(1, sizeof(*mem));
  52. if (mem == NULL)
  53. return NULL;
  54. return mem;
  55. }
  56. /* Frees the memory allocator and all memory allocated with it. */
  57. void
  58. tre_mem_destroy(tre_mem_t mem)
  59. {
  60. tre_list_t *tmp, *l = mem->blocks;
  61. while (l != NULL)
  62. {
  63. xfree(l->data);
  64. tmp = l->next;
  65. xfree(l);
  66. l = tmp;
  67. }
  68. xfree(mem);
  69. }
  70. /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
  71. allocated block or NULL if an underlying malloc() failed. */
  72. void *
  73. tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
  74. int zero, size_t size)
  75. {
  76. void *ptr;
  77. if (mem->failed)
  78. {
  79. return NULL;
  80. }
  81. if (mem->n < size)
  82. {
  83. /* We need more memory than is available in the current block.
  84. Allocate a new block. */
  85. tre_list_t *l;
  86. if (provided)
  87. {
  88. if (provided_block == NULL)
  89. {
  90. mem->failed = 1;
  91. return NULL;
  92. }
  93. mem->ptr = provided_block;
  94. mem->n = TRE_MEM_BLOCK_SIZE;
  95. }
  96. else
  97. {
  98. int block_size;
  99. if (size * 8 > TRE_MEM_BLOCK_SIZE)
  100. block_size = size * 8;
  101. else
  102. block_size = TRE_MEM_BLOCK_SIZE;
  103. l = xmalloc(sizeof(*l));
  104. if (l == NULL)
  105. {
  106. mem->failed = 1;
  107. return NULL;
  108. }
  109. l->data = xmalloc(block_size);
  110. if (l->data == NULL)
  111. {
  112. xfree(l);
  113. mem->failed = 1;
  114. return NULL;
  115. }
  116. l->next = NULL;
  117. if (mem->current != NULL)
  118. mem->current->next = l;
  119. if (mem->blocks == NULL)
  120. mem->blocks = l;
  121. mem->current = l;
  122. mem->ptr = l->data;
  123. mem->n = block_size;
  124. }
  125. }
  126. /* Make sure the next pointer will be aligned. */
  127. size += ALIGN(mem->ptr + size, long);
  128. /* Allocate from current block. */
  129. ptr = mem->ptr;
  130. mem->ptr += size;
  131. mem->n -= size;
  132. /* Set to zero if needed. */
  133. if (zero)
  134. memset(ptr, 0, size);
  135. return ptr;
  136. }