Hermes  0.9.5-beta
Hierarchical Distributed I/O Buffering System
memory_management.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Distributed under BSD 3-Clause license. *
3  * Copyright by The HDF Group. *
4  * Copyright by the Illinois Institute of Technology. *
5  * All rights reserved. *
6  * *
7  * This file is part of Hermes. The full Hermes copyright notice, including *
8  * terms governing use, modification, and redistribution, is contained in *
9  * the COPYING file, which can be found at the top directory. If you do not *
10  * have access to the file, you may request a copy from help@hdfgroup.org. *
11  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #ifndef HERMES_MEMORY_MANAGEMENT_H_
14 #define HERMES_MEMORY_MANAGEMENT_H_
15 
16 #include <assert.h>
17 
18 #include <atomic>
19 
20 #include "hermes_types.h"
21 
28 namespace hermes {
29 
30 typedef void(ArenaErrorFunc)();
36 struct TicketMutex {
37  std::atomic<u32> ticket;
38  std::atomic<u32> serving;
39 };
40 
44 struct Ticket {
46  bool acquired;
47  Ticket() : ticket(-1), acquired(false) {}
48 };
49 
53 struct RwLock {
55  std::atomic<u32> readers;
56  std::atomic<bool> writer_waiting;
57 };
58 
62 struct ArenaInfo {
64  size_t total;
65 };
66 
84 struct Arena {
86  u8 *base;
88  size_t used;
90  size_t capacity;
95 };
96 
100 struct Heap {
118  bool grows_up;
119 };
120 
125  size_t size;
126 };
127 
131 struct FreeBlock {
136 };
137 
143  size_t used;
144 };
145 
172  size_t used;
173 
174  ScopedTemporaryMemory() = delete;
176  ScopedTemporaryMemory &operator=(const ScopedTemporaryMemory &) = delete;
177 
183  explicit ScopedTemporaryMemory(Arena *backing_arena)
184  : arena(backing_arena), used(backing_arena->used) {
185  // TODO(chogan): Currently not threadsafe unless each thread has a different
186  // `backing_arena`
187  if (++backing_arena->temp_count > 1) {
189  }
190  }
191 
197  assert(arena->used >= used);
198  arena->used = used;
199  assert(arena->temp_count > 0);
200  arena->temp_count--;
201  }
202 
207  operator Arena *() { return arena; }
208 };
209 
213 TemporaryMemory BeginTemporaryMemory(Arena *arena);
214 
218 void EndTemporaryMemory(TemporaryMemory *temp_memory);
219 
229 void InitArena(Arena *arena, size_t bytes, u8 *base);
230 
239 Arena InitArenaAndAllocate(size_t bytes);
240 
251 void DestroyArena(Arena *arena);
252 
260 size_t GetRemainingCapacity(Arena *arena);
261 
272 void GrowArena(Arena *arena, size_t new_size);
273 
286 u8 *PushSize(Arena *arena, size_t size, size_t alignment = 8);
287 
299 u8 *PushSizeAndClear(Arena *arena, size_t size, size_t alignment = 8);
300 
312 template <typename T>
313 inline T *PushStruct(Arena *arena, size_t alignment = 8) {
314  T *result = reinterpret_cast<T *>(PushSize(arena, sizeof(T), alignment));
315 
316  return result;
317 }
318 
330 template <typename T>
331 inline T *PushClearedStruct(Arena *arena, size_t alignment = 8) {
332  T *result =
333  reinterpret_cast<T *>(PushSizeAndClear(arena, sizeof(T), alignment));
334 
335  return result;
336 }
337 
350 template <typename T>
351 inline T *PushArray(Arena *arena, int count, size_t alignment = 8) {
352  T *result =
353  reinterpret_cast<T *>(PushSize(arena, sizeof(T) * count, alignment));
354 
355  return result;
356 }
357 
368 template <typename T>
369 inline T *PushClearedArray(Arena *arena, int count, size_t alignment = 8) {
370  T *result = reinterpret_cast<T *>(
371  PushSizeAndClear(arena, sizeof(T) * count, alignment));
372 
373  return result;
374 }
375 u8 *HeapPushSize(Heap *heap, u32 size);
380 template <typename T>
381 inline T *HeapPushStruct(Heap *heap) {
382  T *result = reinterpret_cast<T *>(HeapPushSize(heap, sizeof(T)));
383 
384  return result;
385 }
386 
390 template <typename T>
391 inline T *HeapPushArray(Heap *heap, u32 count) {
392  T *result = reinterpret_cast<T *>(HeapPushSize(heap, count * sizeof(T)));
393 
394  return result;
395 }
396 
397 Heap *InitHeapInArena(Arena *arena, bool grows_up = true, u16 alignment = 8);
398 void HeapFree(Heap *heap, void *ptr);
399 void *HeapRealloc(Heap *heap, void *ptr, size_t size);
400 u32 GetHeapOffset(Heap *heap, u8 *ptr);
401 FreeBlock *NextFreeBlock(Heap *heap, FreeBlock *block);
402 FreeBlock *GetHeapFreeList(Heap *heap);
403 u8 *HeapOffsetToPtr(Heap *heap, u32 offset);
404 u8 *HeapExtentToPtr(Heap *heap);
405 
406 void BeginTicketMutex(TicketMutex *mutex);
407 void EndTicketMutex(TicketMutex *mutex);
408 
409 bool BeginReaderLock(RwLock *lock);
410 void EndReaderLock(RwLock *lock);
411 void BeginWriterLock(RwLock *lock);
412 void EndWriterLock(RwLock *lock);
413 
414 } // namespace hermes
415 
416 #endif // HERMES_MEMORY_MANAGEMENT_H_
#define HERMES_NOT_IMPLEMENTED_YET
Definition: hermes_types.h:191
Definition: adapter_utils.cc:35
T * PushClearedStruct(Arena *arena, size_t alignment=8)
Definition: memory_management.h:331
void EndWriterLock(RwLock *lock)
Definition: memory_management.cc:618
size_t GetRemainingCapacity(Arena *arena)
Definition: memory_management.cc:103
bool BeginReaderLock(RwLock *lock)
Definition: memory_management.cc:567
FreeBlock * NextFreeBlock(Heap *heap, FreeBlock *block)
Definition: memory_management.cc:211
u8 * HeapOffsetToPtr(Heap *heap, u32 offset)
Definition: memory_management.cc:228
Heap * InitHeapInArena(Arena *arena, bool grows_up, u16 alignment)
Definition: memory_management.cc:290
void BeginWriterLock(RwLock *lock)
Definition: memory_management.cc:601
uint16_t u16
Definition: hermes_types.h:41
u8 * PushSizeAndClear(Arena *arena, size_t size, size_t alignment)
Definition: memory_management.cc:170
void HeapFree(Heap *heap, void *ptr)
Definition: memory_management.cc:460
T * PushArray(Arena *arena, int count, size_t alignment=8)
Definition: memory_management.h:351
void EndReaderLock(RwLock *lock)
Definition: memory_management.cc:580
TemporaryMemory BeginTemporaryMemory(Arena *arena)
Definition: memory_management.cc:128
void EndTicketMutex(TicketMutex *mutex)
Definition: memory_management.cc:557
T * HeapPushStruct(Heap *heap)
Definition: memory_management.h:381
@ kArenaType_Count
Definition: hermes_types.h:244
uint8_t u8
Definition: hermes_types.h:40
u32 GetHeapOffset(Heap *heap, u8 *ptr)
Definition: memory_management.cc:242
void() ArenaErrorFunc()
Definition: memory_management.h:30
void BeginTicketMutex(TicketMutex *mutex)
Definition: memory_management.cc:539
void EndTemporaryMemory(TemporaryMemory *temp_memory)
Definition: memory_management.cc:139
u8 * HeapExtentToPtr(Heap *heap)
Definition: memory_management.cc:274
void GrowArena(Arena *arena, size_t new_size)
The maximum capacity of arena becomes new_size.
Definition: memory_management.cc:112
u8 * PushSize(Arena *arena, size_t size, size_t alignment)
Definition: memory_management.cc:145
uint32_t u32
Definition: hermes_types.h:42
T * PushStruct(Arena *arena, size_t alignment=8)
Definition: memory_management.h:313
void DestroyArena(Arena *arena)
Definition: memory_management.cc:92
int32_t i32
Definition: hermes_types.h:46
FreeBlock * GetHeapFreeList(Heap *heap)
Definition: memory_management.cc:195
Arena InitArenaAndAllocate(size_t bytes)
Definition: memory_management.cc:81
void * HeapRealloc(Heap *heap, void *ptr, size_t size)
Definition: memory_management.cc:493
T * HeapPushArray(Heap *heap, u32 count)
Definition: memory_management.h:391
void InitArena(Arena *arena, size_t bytes, u8 *base)
Definition: memory_management.cc:71
T * PushClearedArray(Arena *arena, int count, size_t alignment=8)
Definition: memory_management.h:369
u8 * HeapPushSize(Heap *heap, u32 size)
Definition: memory_management.cc:414
Definition: memory_management.h:84
i32 temp_count
Definition: memory_management.h:94
ArenaErrorFunc * error_handler
Definition: memory_management.h:92
size_t capacity
Definition: memory_management.h:90
size_t used
Definition: memory_management.h:88
u8 * base
Definition: memory_management.h:86
Definition: memory_management.h:62
size_t sizes[kArenaType_Count]
Definition: memory_management.h:63
size_t total
Definition: memory_management.h:64
Definition: memory_management.h:124
size_t size
Definition: memory_management.h:125
Definition: memory_management.h:131
u32 size
Definition: memory_management.h:135
u32 next_offset
Definition: memory_management.h:133
Definition: memory_management.h:100
bool grows_up
Definition: memory_management.h:118
ArenaErrorFunc * error_handler
Definition: memory_management.h:102
u32 base_offset
Definition: memory_management.h:106
TicketMutex mutex
Definition: memory_management.h:104
u32 free_list_offset
Definition: memory_management.h:108
u32 extent
Definition: memory_management.h:113
u16 alignment
Definition: memory_management.h:116
Definition: memory_management.h:53
std::atomic< bool > writer_waiting
Definition: memory_management.h:56
TicketMutex mutex
Definition: memory_management.h:54
std::atomic< u32 > readers
Definition: memory_management.h:55
Definition: memory_management.h:168
Arena * arena
Definition: memory_management.h:170
ScopedTemporaryMemory(Arena *backing_arena)
Definition: memory_management.h:183
size_t used
Definition: memory_management.h:172
~ScopedTemporaryMemory()
Definition: memory_management.h:196
Definition: memory_management.h:141
size_t used
Definition: memory_management.h:143
Arena * arena
Definition: memory_management.h:142
Definition: memory_management.h:44
Ticket()
Definition: memory_management.h:47
bool acquired
Definition: memory_management.h:46
u32 ticket
Definition: memory_management.h:45
Definition: memory_management.h:36
std::atomic< u32 > serving
Definition: memory_management.h:38
std::atomic< u32 > ticket
Definition: memory_management.h:37