1 /* 2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 */ 8 #ifndef CLIENT_MEMORY_ALLOCATOR_H 9 #define CLIENT_MEMORY_ALLOCATOR_H 10 11 12 #include "MultiLocker.h" 13 14 #include <util/DoublyLinkedList.h> 15 16 17 class ServerApp; 18 struct chunk; 19 struct block; 20 21 struct chunk : DoublyLinkedListLinkImpl<struct chunk> { 22 area_id area; 23 uint8* base; 24 size_t size; 25 }; 26 27 struct block : DoublyLinkedListLinkImpl<struct block> { 28 struct chunk* chunk; 29 uint8* base; 30 size_t size; 31 }; 32 33 typedef DoublyLinkedList<block> block_list; 34 typedef DoublyLinkedList<chunk> chunk_list; 35 36 37 class ClientMemoryAllocator { 38 public: 39 ClientMemoryAllocator(ServerApp* application); 40 ~ClientMemoryAllocator(); 41 42 void* Allocate(size_t size, block** _address, 43 bool& newArea); 44 void Free(block* cookie); 45 46 void Dump(); 47 48 private: 49 struct block* _AllocateChunk(size_t size, bool& newArea); 50 51 private: 52 ServerApp* fApplication; 53 chunk_list fChunks; 54 block_list fFreeBlocks; 55 }; 56 57 58 class AreaMemory { 59 public: 60 virtual ~AreaMemory() {} 61 62 virtual area_id Area() = 0; 63 virtual uint8* Address() = 0; 64 virtual uint32 AreaOffset() = 0; 65 }; 66 67 68 class ClientMemory : public AreaMemory { 69 public: 70 ClientMemory(); 71 72 virtual ~ClientMemory(); 73 74 void* Allocate(ClientMemoryAllocator* allocator, 75 size_t size, bool& newArea); 76 77 virtual area_id Area(); 78 virtual uint8* Address(); 79 virtual uint32 AreaOffset(); 80 81 private: 82 ClientMemoryAllocator* fAllocator; 83 block* fBlock; 84 }; 85 86 87 /*! Just clones an existing area. */ 88 class ClonedAreaMemory : public AreaMemory{ 89 public: 90 ClonedAreaMemory(); 91 virtual ~ClonedAreaMemory(); 92 93 void* Clone(area_id area, uint32 offset); 94 95 virtual area_id Area(); 96 virtual uint8* Address(); 97 virtual uint32 AreaOffset(); 98 99 private: 100 area_id fClonedArea; 101 uint32 fOffset; 102 uint8* fBase; 103 }; 104 105 106 #endif /* CLIENT_MEMORY_ALLOCATOR_H */ 107