Arena.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 1998-2000 Netscape Communications Corporation.
  3. * Copyright (C) 2003-6 Apple Computer
  4. * Copyright (C) Research In Motion Limited 2010. All rights reserved.
  5. *
  6. * Other contributors:
  7. * Nick Blievers <nickb@adacel.com.au>
  8. * Jeff Hostetler <jeff@nerdone.com>
  9. * Tom Rini <trini@kernel.crashing.org>
  10. * Raffaele Sena <raff@netwinder.org>
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. * Alternatively, the contents of this file may be used under the terms
  27. * of either the Mozilla Public License Version 1.1, found at
  28. * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
  29. * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
  30. * (the "GPL"), in which case the provisions of the MPL or the GPL are
  31. * applicable instead of those above. If you wish to allow use of your
  32. * version of this file only under the terms of one of those two
  33. * licenses (the MPL or the GPL) and not to allow others to use your
  34. * version of this file under the LGPL, indicate your decision by
  35. * deletingthe provisions above and replace them with the notice and
  36. * other provisions required by the MPL or the GPL, as the case may be.
  37. * If you do not delete the provisions above, a recipient may use your
  38. * version of this file under any of the LGPL, the MPL or the GPL.
  39. */
  40. #ifndef Arena_h
  41. #define Arena_h
  42. // FIXME: We'd always like to use AllocAlignmentInteger for Arena alignment
  43. // but there is concern over the memory growth this may cause.
  44. #ifdef WTF_USE_ARENA_ALLOC_ALIGNMENT_INTEGER
  45. #define ARENA_ALIGN_MASK (sizeof(WTF::AllocAlignmentInteger) - 1)
  46. #else
  47. #define ARENA_ALIGN_MASK 3
  48. #endif
  49. namespace WebCore {
  50. typedef uintptr_t uword;
  51. struct Arena {
  52. Arena* next; // next arena
  53. uword base; // aligned base address
  54. uword limit; // end of arena (1+last byte)
  55. uword avail; // points to next available byte in arena
  56. };
  57. struct ArenaPool {
  58. Arena first; // first arena in pool list.
  59. Arena* current; // current arena.
  60. unsigned int arenasize;
  61. uword mask; // Mask (power-of-2 - 1)
  62. };
  63. void InitArenaPool(ArenaPool*, const char* name, unsigned int size, unsigned int align);
  64. void FinishArenaPool(ArenaPool*);
  65. void* ArenaAllocate(ArenaPool*, unsigned int numBytes, unsigned int& bytesAllocated);
  66. #define ARENA_ALIGN(n) (((uword)(n) + ARENA_ALIGN_MASK) & ~ARENA_ALIGN_MASK)
  67. #define INIT_ARENA_POOL(pool, name, size) InitArenaPool(pool, name, size, ARENA_ALIGN_MASK + 1)
  68. #define ARENA_ALLOCATE(p, pool, nb, bytesAllocated) \
  69. Arena* _a = (pool)->current; \
  70. unsigned int _nb = ARENA_ALIGN(nb); \
  71. uword _p = _a->avail; \
  72. uword _q = _p + _nb; \
  73. if (_q > _a->limit) \
  74. _p = (uword)ArenaAllocate(pool, _nb, *bytesAllocated); \
  75. else \
  76. _a->avail = _q; \
  77. p = (void*)_p;
  78. }
  79. #endif