/js/src/nanojit/Allocator.h

http://github.com/zpao/v8monkey · C Header · 143 lines · 64 code · 19 blank · 60 comment · 4 complexity · 404a43062200e1dec86cb0cf5271e8f1 MD5 · raw file

  1. /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
  2. /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is [Open Source Virtual Machine].
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Adobe System Incorporated.
  20. * Portions created by the Initial Developer are Copyright (C) 2009
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Adobe AS3 Team
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #ifndef __nanojit_Allocator__
  40. #define __nanojit_Allocator__
  41. namespace nanojit
  42. {
  43. /**
  44. * Allocator is a bump-pointer allocator with an SPI for getting more
  45. * memory from embedder-implemented allocator, such as malloc()/free().
  46. *
  47. * alloc() never returns NULL. The implementation of allocChunk()
  48. * is expected to perform a longjmp or exception when an allocation can't
  49. * proceed. fallibleAlloc() (and fallibleAllocChunk()) may return NULL.
  50. * They should be used for large allocations whose failure can be handled
  51. * without too much difficulty.
  52. */
  53. class Allocator {
  54. public:
  55. Allocator();
  56. ~Allocator();
  57. // Usable space in the minimum chunk size; there are also a few bytes
  58. // used for administration.
  59. static const size_t MIN_CHUNK_SZB = 2000;
  60. void reset();
  61. /** alloc memory, never return null. */
  62. void* alloc(size_t nbytes) {
  63. void* p;
  64. nbytes = (nbytes + 7) & ~7; // round up
  65. if (current_top + nbytes <= current_limit) {
  66. p = current_top;
  67. current_top += nbytes;
  68. } else {
  69. p = allocSlow(nbytes, /* fallible = */false);
  70. NanoAssert(p);
  71. }
  72. return p;
  73. }
  74. /** alloc memory, maybe return null. */
  75. void* fallibleAlloc(size_t nbytes) {
  76. void* p;
  77. nbytes = (nbytes + 7) & ~7; // round up
  78. if (current_top + nbytes <= current_limit) {
  79. p = current_top;
  80. current_top += nbytes;
  81. } else {
  82. p = allocSlow(nbytes, /* fallible = */true);
  83. }
  84. return p;
  85. }
  86. size_t getBytesAllocated(size_t(*my_malloc_usable_size)(void *));
  87. protected:
  88. void* allocSlow(size_t nbytes, bool fallible = false);
  89. bool fill(size_t minbytes, bool fallible);
  90. class Chunk {
  91. public:
  92. Chunk* prev;
  93. size_t size;
  94. int64_t data[1]; // int64_t forces 8-byte alignment.
  95. };
  96. Chunk* current_chunk;
  97. char* current_top;
  98. char* current_limit;
  99. // allocator SPI
  100. /** allocate another block from a host provided allocator */
  101. void* allocChunk(size_t nbytes, bool fallible);
  102. /** free back to the same allocator */
  103. void freeChunk(void*);
  104. /** hook for post-reset action. */
  105. void postReset();
  106. };
  107. }
  108. /** global new overload enabling this pattern: new (allocator) T(...) */
  109. inline void* operator new(size_t size, nanojit::Allocator &a) {
  110. return a.alloc(size);
  111. }
  112. /** global new overload enabling this pattern: new (allocator) T(...) */
  113. inline void* operator new(size_t size, nanojit::Allocator *a) {
  114. return a->alloc(size);
  115. }
  116. /** global new[] overload enabling this pattern: new (allocator) T[] */
  117. inline void* operator new[](size_t size, nanojit::Allocator& a) {
  118. return a.alloc(size);
  119. }
  120. /** global new[] overload enabling this pattern: new (allocator) T[] */
  121. inline void* operator new[](size_t size, nanojit::Allocator* a) {
  122. return a->alloc(size);
  123. }
  124. #endif // __nanojit_Allocator__