Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: source/lib/allocators/arena.h
- ===================================================================
- --- source/lib/allocators/arena.h (revision 13914)
- +++ source/lib/allocators/arena.h (working copy)
- @@ -1,4 +1,4 @@
- -/* Copyright (c) 2010 Wildfire Games
- +/* Copyright (c) 2013 Wildfire Games
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- @@ -83,6 +83,88 @@
- LIB_API void TestArena();
- +
- +/**
- + * allocator design parameters:
- + * - grow dynamically with a fixed chunkSize
- + * - for frequent allocations of size << chunkSize
- + * - no reallocations, pointers remain valid
- + **/
- +class DynamicArena
- +{
- + struct ArenaChunk
- + {
- + bool Available(size_t size)
- + {
- + return size <= (capacity - end);
- + }
- +
- + uintptr_t Allocate(size_t size)
- + {
- + if (!Available(size))
- + return 0;
- + uintptr_t ptr = storage + end;
- + end += size;
- + return ptr;
- + }
- +
- + uintptr_t storage;
- + size_t end;
- + size_t capacity;
- + ArenaChunk* next;
- + };
- +
- + NONCOPYABLE(DynamicArena);
- +public:
- + DynamicArena(size_t chunkSize) : chunkSize(chunkSize), head(NULL)
- + {
- + }
- +
- + ~DynamicArena()
- + {
- + ArenaChunk* chunk = head;
- + while (chunk != NULL)
- + {
- + ArenaChunk* next = chunk->next;
- + free(chunk);
- + chunk = next;
- + }
- + }
- +
- + void* allocate(size_t size)
- + {
- + if (size > chunkSize)
- + {
- + debug_warn(L"DynamicArena cannot allocate more than chunk size");
- + throw std::bad_alloc();
- + }
- + else if (!head || !head->Available(size))
- + {
- + // For efficiency, do a single allocation with the ArenaChunk and its storage
- + ArenaChunk* oldHead = head;
- + head = (ArenaChunk*)malloc(sizeof(ArenaChunk) + chunkSize);
- + ENSURE(head);
- + head->storage = sizeof(ArenaChunk) + uintptr_t(head);
- + head->end = 0;
- + head->capacity = chunkSize;
- + head->next = oldHead;
- + }
- +
- + return (void*)head->Allocate(size);
- + }
- +
- + void deallocate(void* UNUSED(p), size_t UNUSED(size))
- + {
- + // ignored
- + }
- +
- +private:
- +
- + const size_t chunkSize;
- + ArenaChunk *head;
- +};
- +
- +
- } // namespace Allocators
- #endif // #ifndef INCLUDED_ALLOCATORS_ARENA
- Index: source/simulation2/serialization/BinarySerializer.cpp
- ===================================================================
- --- source/simulation2/serialization/BinarySerializer.cpp (revision 13914)
- +++ source/simulation2/serialization/BinarySerializer.cpp (working copy)
- @@ -57,7 +57,7 @@
- CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(ScriptInterface& scriptInterface, ISerializer& serializer) :
- m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_Rooter(m_ScriptInterface),
- - m_ScriptBackrefsArena(16*MiB), m_ScriptBackrefs(backrefs_t::key_compare(), ScriptBackrefsAlloc(m_ScriptBackrefsArena)), m_ScriptBackrefsNext(1)
- + m_ScriptBackrefsArena(1 * MiB), m_ScriptBackrefs(backrefs_t::key_compare(), ScriptBackrefsAlloc(m_ScriptBackrefsArena)), m_ScriptBackrefsNext(1)
- {
- }
- Index: source/simulation2/serialization/BinarySerializer.h
- ===================================================================
- --- source/simulation2/serialization/BinarySerializer.h (revision 13914)
- +++ source/simulation2/serialization/BinarySerializer.h (working copy)
- @@ -65,10 +65,10 @@
- ISerializer& m_Serializer;
- // Pooling helps since we do a lot of short-lived allocations
- - typedef ProxyAllocator<std::pair<JSObject* const, u32>, Allocators::Arena<> > ScriptBackrefsAlloc;
- + typedef ProxyAllocator<std::pair<JSObject* const, u32>, Allocators::DynamicArena> ScriptBackrefsAlloc;
- typedef std::map<JSObject*, u32, std::less<JSObject*>, ScriptBackrefsAlloc> backrefs_t;
- - Allocators::Arena<> m_ScriptBackrefsArena;
- + Allocators::DynamicArena m_ScriptBackrefsArena;
- backrefs_t m_ScriptBackrefs;
- u32 m_ScriptBackrefsNext;
- u32 GetScriptBackrefTag(JSObject* obj);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement