CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
malloc_heap.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_MALLOC_HEAP_H_
6 #define CVMFS_MALLOC_HEAP_H_
7 
8 #include <inttypes.h>
9 #include <stdint.h>
10 
11 #include <cstdlib>
12 
13 #include "util/async.h"
14 
36 class MallocHeap {
37  public:
38  struct BlockPtr {
39  BlockPtr() : pointer(NULL) { }
40  explicit BlockPtr(void *p) : pointer(p) { }
41  void *pointer;
42  };
43 
44  // Pointer to the callback method invoked for each memory block that gets
45  // compacted.
47 
48  MallocHeap(uint64_t capacity, CallbackPtr callback_ptr);
49  ~MallocHeap();
50 
51  void *Allocate(uint64_t size, void *header, unsigned header_size);
52  void *Expand(void *block, uint64_t new_size);
53  void MarkFree(void *block);
54  uint64_t GetSize(void *block);
55  void Compact();
56 
57  inline uint64_t num_blocks() { return num_blocks_; }
58  inline uint64_t used_bytes() { return gauge_; }
59  inline uint64_t stored_bytes() { return stored_; }
60  inline uint64_t compacted_bytes() {
61  return stored_ + num_blocks_ * sizeof(Tag);
62  }
63  inline uint64_t capacity() { return capacity_; }
64  inline double utilization() {
65  return static_cast<double>(stored_) / static_cast<double>(gauge_);
66  }
67  bool HasSpaceFor(uint64_t nbytes);
68 
69  private:
73  static const unsigned kMinCapacity = 1024;
74 
79  struct Tag {
80  Tag() : size(0) { }
81  explicit Tag(int64_t s) : size(s) { }
82  inline uint64_t GetSize() {
83  if (size < 0) return -size;
84  return size;
85  }
86  inline bool IsFree() { return size < 0; }
87  inline Tag *JumpToNext() {
88  return reinterpret_cast<Tag *>(
89  reinterpret_cast<unsigned char *>(this) + sizeof(Tag) + GetSize());
90  }
91  inline unsigned char *GetBlock() {
92  return reinterpret_cast<unsigned char *>(this) + sizeof(Tag);
93  }
97  int64_t size;
98  };
99 
107  uint64_t capacity_;
111  uint64_t gauge_;
115  uint64_t stored_;
119  uint64_t num_blocks_;
123  unsigned char *heap_;
124 }; // class MallocHeap
125 
126 #endif // CVMFS_MALLOC_HEAP_H_
CallbackPtr callback_ptr_
Definition: malloc_heap.h:103
static const unsigned kMinCapacity
Definition: malloc_heap.h:73
uint64_t num_blocks()
Definition: malloc_heap.h:57
void * Allocate(uint64_t size, void *header, unsigned header_size)
Definition: malloc_heap.cc:16
unsigned char * heap_
Definition: malloc_heap.h:123
MallocHeap(uint64_t capacity, CallbackPtr callback_ptr)
Definition: malloc_heap.cc:113
uint64_t capacity_
Definition: malloc_heap.h:107
uint64_t GetSize()
Definition: malloc_heap.h:82
uint64_t gauge_
Definition: malloc_heap.h:111
Tag(int64_t s)
Definition: malloc_heap.h:81
void * Expand(void *block, uint64_t new_size)
Definition: malloc_heap.cc:79
uint64_t GetSize(void *block)
Definition: malloc_heap.cc:106
Callbackable< BlockPtr >::CallbackTN * CallbackPtr
Definition: malloc_heap.h:46
uint64_t stored_
Definition: malloc_heap.h:115
void MarkFree(void *block)
Definition: malloc_heap.cc:94
uint64_t compacted_bytes()
Definition: malloc_heap.h:60
uint64_t stored_bytes()
Definition: malloc_heap.h:59
Tag * JumpToNext()
Definition: malloc_heap.h:87
double utilization()
Definition: malloc_heap.h:64
uint64_t used_bytes()
Definition: malloc_heap.h:58
uint64_t capacity()
Definition: malloc_heap.h:63
bool HasSpaceFor(uint64_t nbytes)
Definition: malloc_heap.cc:89
unsigned char * GetBlock()
Definition: malloc_heap.h:91
static void size_t size
Definition: smalloc.h:47
uint64_t num_blocks_
Definition: malloc_heap.h:119
void Compact()
Definition: malloc_heap.cc:39