/opengles/src/codegen/heap.c

http://ftk.googlecode.com/ · C · 133 lines · 64 code · 32 blank · 37 comment · 5 complexity · 7eceb1a3d14f45ed05e20e440f75b32b MD5 · raw file

  1. /****************************************************************************/
  2. /* */
  3. /* Copyright (c) 2004, Hans-Martin Will. All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or without */
  6. /* modification, are permitted provided that the following conditions are */
  7. /* met: */
  8. /* */
  9. /* * Redistributions of source code must retain the above copyright */
  10. /* notice, this list of conditions and the following disclaimer. */
  11. /* */
  12. /* * Redistributions in binary form must reproduce the above copyright */
  13. /* notice, this list of conditions and the following disclaimer in the */
  14. /* documentation and/or other materials provided with the distribution. */
  15. /* */
  16. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
  17. /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
  18. /* LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
  19. /* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER */
  20. /* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  21. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, */
  22. /* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
  23. /* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  24. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
  25. /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
  26. /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  27. /* */
  28. /****************************************************************************/
  29. #include "heap.h"
  30. #define HEAP_ALIGNMENT 8
  31. typedef struct block_t
  32. {
  33. struct block_t * next;
  34. char * base;
  35. size_t total;
  36. size_t current;
  37. } block_t;
  38. struct cg_heap_t
  39. {
  40. block_t * blocks;
  41. size_t default_block_size;
  42. };
  43. static block_t * create_block(size_t block_size)
  44. {
  45. block_t * result = (block_t *) malloc(sizeof(block_t));
  46. result->base = (char *) malloc(block_size);
  47. result->total = block_size;
  48. result->current = 0;
  49. result->next = (block_t *) 0;
  50. return result;
  51. }
  52. cg_heap_t * cg_heap_create(size_t default_block_size)
  53. /************************************************************************/
  54. /* Create a new heap object. */
  55. /************************************************************************/
  56. {
  57. cg_heap_t * heap = (cg_heap_t *) malloc(sizeof(cg_heap_t));
  58. heap->blocks = create_block(default_block_size);
  59. heap->default_block_size = default_block_size;
  60. return heap;
  61. }
  62. void cg_heap_destroy(cg_heap_t * heap)
  63. /************************************************************************/
  64. /* Destroy a heap and deallocate any memory belonging to it */
  65. /************************************************************************/
  66. {
  67. block_t * current, *next;
  68. for (current = heap->blocks; current != (block_t *) 0; current = next)
  69. {
  70. next = current->next;
  71. free(current->base);
  72. free(current);
  73. }
  74. free(heap);
  75. }
  76. void * cg_heap_allocate(cg_heap_t * heap, size_t amount)
  77. /************************************************************************/
  78. /* Allocate a block of memory of the given size on the heap. */
  79. /************************************************************************/
  80. {
  81. void * result;
  82. amount = (amount + HEAP_ALIGNMENT - 1) & ~(HEAP_ALIGNMENT - 1);
  83. if (heap->blocks->total - heap->blocks->current < amount)
  84. {
  85. block_t * new_block;
  86. if (amount > heap->default_block_size)
  87. {
  88. new_block = create_block(amount);
  89. }
  90. else
  91. {
  92. new_block = create_block(heap->default_block_size);
  93. }
  94. new_block->next = heap->blocks;
  95. heap->blocks = new_block;
  96. }
  97. result = heap->blocks->base + heap->blocks->current;
  98. heap->blocks->current += amount;
  99. memset(result, 0, amount);
  100. return result;
  101. }