1 /* 2 * Copyright 2007, Hugo Santos. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Hugo Santos, hugosantos@gmail.com 7 */ 8 #ifndef SLAB_PRIVATE_H 9 #define SLAB_PRIVATE_H 10 11 12 #include <stddef.h> 13 14 #include <slab/Slab.h> 15 16 17 //#define TRACE_SLAB 18 #ifdef TRACE_SLAB 19 #define TRACE_CACHE(cache, format, args...) \ 20 dprintf("Cache[%p, %s] " format "\n", cache, cache->name , ##args) 21 #else 22 #define TRACE_CACHE(cache, format, bananas...) do { } while (0) 23 #endif 24 25 26 #define COMPONENT_PARANOIA_LEVEL OBJECT_CACHE_PARANOIA 27 #include <debug_paranoia.h> 28 29 30 31 static const size_t kMinObjectAlignment = 8; 32 33 34 struct ObjectCache; 35 struct object_depot; 36 37 38 void request_memory_manager_maintenance(); 39 40 void* block_alloc(size_t size, size_t alignment, uint32 flags); 41 void* block_alloc_early(size_t size); 42 void block_free(void* block, uint32 flags); 43 void block_allocator_init_boot(); 44 void block_allocator_init_rest(); 45 46 void dump_object_depot(object_depot* depot); 47 int dump_object_depot(int argCount, char** args); 48 int dump_depot_magazine(int argCount, char** args); 49 50 51 template<typename Type> 52 static inline Type* 53 _pop(Type*& head) 54 { 55 Type* oldHead = head; 56 head = head->next; 57 return oldHead; 58 } 59 60 61 template<typename Type> 62 static inline void 63 _push(Type*& head, Type* object) 64 { 65 object->next = head; 66 head = object; 67 } 68 69 70 static inline void* 71 slab_internal_alloc(size_t size, uint32 flags) 72 { 73 if (flags & CACHE_DURING_BOOT) 74 return block_alloc_early(size); 75 76 return block_alloc(size, 0, flags); 77 } 78 79 80 static inline void 81 slab_internal_free(void* buffer, uint32 flags) 82 { 83 block_free(buffer, flags); 84 } 85 86 87 #if PARANOID_KERNEL_MALLOC || PARANOID_KERNEL_FREE 88 static inline void* 89 fill_block(void* buffer, size_t size, uint32 pattern) 90 { 91 if (buffer == NULL) 92 return NULL; 93 94 size &= ~(sizeof(pattern) - 1); 95 for (size_t i = 0; i < size / sizeof(pattern); i++) 96 ((uint32*)buffer)[i] = pattern; 97 98 return buffer; 99 } 100 #endif 101 102 103 static inline void* 104 fill_allocated_block(void* buffer, size_t size) 105 { 106 #if PARANOID_KERNEL_MALLOC 107 return fill_block(buffer, size, 0xcccccccc); 108 #else 109 return buffer; 110 #endif 111 } 112 113 114 static inline void* 115 fill_freed_block(void* buffer, size_t size) 116 { 117 #if PARANOID_KERNEL_FREE 118 return fill_block(buffer, size, 0xdeadbeef); 119 #else 120 return buffer; 121 #endif 122 } 123 124 #endif // SLAB_PRIVATE_H 125