Advertisement
historic_bruno

Untitled

Sep 29th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.88 KB | None | 0 0
  1. Index: source/lib/allocators/arena.h
  2. ===================================================================
  3. --- source/lib/allocators/arena.h   (revision 13914)
  4. +++ source/lib/allocators/arena.h   (working copy)
  5. @@ -1,4 +1,4 @@
  6. -/* Copyright (c) 2010 Wildfire Games
  7. +/* Copyright (c) 2013 Wildfire Games
  8.   *
  9.   * Permission is hereby granted, free of charge, to any person obtaining
  10.   * a copy of this software and associated documentation files (the
  11. @@ -83,6 +83,88 @@
  12.  
  13.  LIB_API void TestArena();
  14.  
  15. +
  16. +/**
  17. + * allocator design parameters:
  18. + * - grow dynamically with a fixed chunkSize
  19. + * - for frequent allocations of size << chunkSize
  20. + * - no reallocations, pointers remain valid
  21. + **/
  22. +class DynamicArena
  23. +{
  24. +   struct ArenaChunk
  25. +   {
  26. +       bool Available(size_t size)
  27. +       {
  28. +           return size <= (capacity - end);
  29. +       }
  30. +
  31. +       uintptr_t Allocate(size_t size)
  32. +       {
  33. +           if (!Available(size))
  34. +               return 0;
  35. +           uintptr_t ptr = storage + end;
  36. +           end += size;
  37. +           return ptr;
  38. +       }
  39. +
  40. +       uintptr_t storage;
  41. +       size_t end;
  42. +       size_t capacity;
  43. +       ArenaChunk* next;
  44. +   };
  45. +
  46. +   NONCOPYABLE(DynamicArena);
  47. +public:
  48. +   DynamicArena(size_t chunkSize) : chunkSize(chunkSize), head(NULL)
  49. +   {
  50. +   }
  51. +
  52. +   ~DynamicArena()
  53. +   {
  54. +       ArenaChunk* chunk = head;
  55. +       while (chunk != NULL)
  56. +       {
  57. +           ArenaChunk* next = chunk->next;
  58. +           free(chunk);
  59. +           chunk = next;
  60. +       }
  61. +   }
  62. +
  63. +   void* allocate(size_t size)
  64. +   {
  65. +       if (size > chunkSize)
  66. +       {
  67. +           debug_warn(L"DynamicArena cannot allocate more than chunk size");
  68. +           throw std::bad_alloc();
  69. +       }
  70. +       else if (!head || !head->Available(size))
  71. +       {
  72. +           // For efficiency, do a single allocation with the ArenaChunk and its storage
  73. +           ArenaChunk* oldHead = head;
  74. +           head = (ArenaChunk*)malloc(sizeof(ArenaChunk) + chunkSize);
  75. +           ENSURE(head);
  76. +           head->storage = sizeof(ArenaChunk) + uintptr_t(head);
  77. +           head->end = 0;
  78. +           head->capacity = chunkSize;
  79. +           head->next = oldHead;
  80. +       }
  81. +
  82. +       return (void*)head->Allocate(size);
  83. +   }
  84. +
  85. +   void deallocate(void* UNUSED(p), size_t UNUSED(size))
  86. +   {
  87. +       // ignored
  88. +   }
  89. +
  90. +private:
  91. +
  92. +   const size_t chunkSize;
  93. +   ArenaChunk *head;
  94. +};
  95. +
  96. +
  97.  }  // namespace Allocators
  98.  
  99.  #endif // #ifndef INCLUDED_ALLOCATORS_ARENA
  100. Index: source/simulation2/serialization/BinarySerializer.cpp
  101. ===================================================================
  102. --- source/simulation2/serialization/BinarySerializer.cpp   (revision 13914)
  103. +++ source/simulation2/serialization/BinarySerializer.cpp   (working copy)
  104. @@ -57,7 +57,7 @@
  105.  
  106.  CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(ScriptInterface& scriptInterface, ISerializer& serializer) :
  107.     m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_Rooter(m_ScriptInterface),
  108. -   m_ScriptBackrefsArena(16*MiB), m_ScriptBackrefs(backrefs_t::key_compare(), ScriptBackrefsAlloc(m_ScriptBackrefsArena)), m_ScriptBackrefsNext(1)
  109. +   m_ScriptBackrefsArena(1 * MiB), m_ScriptBackrefs(backrefs_t::key_compare(), ScriptBackrefsAlloc(m_ScriptBackrefsArena)), m_ScriptBackrefsNext(1)
  110.  {
  111.  }
  112.  
  113. Index: source/simulation2/serialization/BinarySerializer.h
  114. ===================================================================
  115. --- source/simulation2/serialization/BinarySerializer.h (revision 13914)
  116. +++ source/simulation2/serialization/BinarySerializer.h (working copy)
  117. @@ -65,10 +65,10 @@
  118.     ISerializer& m_Serializer;
  119.  
  120.     // Pooling helps since we do a lot of short-lived allocations
  121. -   typedef ProxyAllocator<std::pair<JSObject* const, u32>, Allocators::Arena<> > ScriptBackrefsAlloc;
  122. +   typedef ProxyAllocator<std::pair<JSObject* const, u32>, Allocators::DynamicArena> ScriptBackrefsAlloc;
  123.     typedef std::map<JSObject*, u32, std::less<JSObject*>, ScriptBackrefsAlloc> backrefs_t;
  124.  
  125. -   Allocators::Arena<> m_ScriptBackrefsArena;
  126. +   Allocators::DynamicArena m_ScriptBackrefsArena;
  127.     backrefs_t m_ScriptBackrefs;
  128.     u32 m_ScriptBackrefsNext;
  129.     u32 GetScriptBackrefTag(JSObject* obj);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement