zend_static_allocator.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@zend.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "zend_static_allocator.h"
  20. /* Not checking emalloc() and erealloc() return values as they are supposed to bailout */
  21. inline static void block_init(Block *block, zend_uint block_size)
  22. {
  23. block->pos = block->bp = (char *) emalloc(block_size);
  24. block->end = block->bp + block_size;
  25. }
  26. inline static char *block_allocate(Block *block, zend_uint size)
  27. {
  28. char *retval = block->pos;
  29. if ((block->pos += size) >= block->end) {
  30. return (char *)NULL;
  31. }
  32. return retval;
  33. }
  34. inline static void block_destroy(Block *block)
  35. {
  36. efree(block->bp);
  37. }
  38. void static_allocator_init(StaticAllocator *sa)
  39. {
  40. sa->Blocks = (Block *) emalloc(sizeof(Block));
  41. block_init(sa->Blocks, ALLOCATOR_BLOCK_SIZE);
  42. sa->num_blocks = 1;
  43. sa->current_block = 0;
  44. }
  45. char *static_allocator_allocate(StaticAllocator *sa, zend_uint size)
  46. {
  47. char *retval;
  48. retval = block_allocate(&sa->Blocks[sa->current_block], size);
  49. if (retval) {
  50. return retval;
  51. }
  52. sa->Blocks = (Block *) erealloc(sa->Blocks, ++sa->num_blocks);
  53. sa->current_block++;
  54. block_init(&sa->Blocks[sa->current_block], (size > ALLOCATOR_BLOCK_SIZE) ? size : ALLOCATOR_BLOCK_SIZE);
  55. retval = block_allocate(&sa->Blocks[sa->current_block], size);
  56. return retval;
  57. }
  58. void static_allocator_destroy(StaticAllocator *sa)
  59. {
  60. zend_uint i;
  61. for (i=0; i<sa->num_blocks; i++) {
  62. block_free(&sa->Blocks[i]);
  63. }
  64. efree(sa->Blocks);
  65. }
  66. /*
  67. * Local variables:
  68. * tab-width: 4
  69. * c-basic-offset: 4
  70. * indent-tabs-mode: t
  71. * End:
  72. */