1 /* 2 * Copyright 2006, 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 status_t InitCheck(); 43 44 void *Allocate(size_t size, void** _address, bool& newArea); 45 void Free(void* cookie); 46 47 area_id Area(void* cookie); 48 uint32 AreaOffset(void* cookie); 49 50 bool Lock(); 51 void Unlock(); 52 53 private: 54 struct block *_AllocateChunk(size_t size, bool& newArea); 55 56 ServerApp* fApplication; 57 MultiLocker fLock; 58 chunk_list fChunks; 59 block_list fFreeBlocks; 60 }; 61 62 #endif /* CLIENT_MEMORY_ALLOCATOR_H */ 63