CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
malloc_arena.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_MALLOC_ARENA_H_
6 #define CVMFS_MALLOC_ARENA_H_
7 
8 #include <inttypes.h>
9 #include <stdint.h>
10 
11 #include <cassert>
12 #include <new>
13 
14 // TODO(jblomer): the arena size could be a template parameter. In order to
15 // reduce code duplication, all functions not requiring the arena size could be
16 // moved to a base class.
17 
66 class MallocArena {
67  public:
72  static const int kMinBlockSize = 24;
73 
77  static inline MallocArena *GetMallocArena(void *ptr, unsigned arena_size) {
78  void *arena = reinterpret_cast<void *>(
79  uintptr_t(ptr) & ~(uintptr_t(arena_size) - uintptr_t(1)));
80  return *reinterpret_cast<MallocArena **>(arena);
81  }
82 
83  explicit MallocArena(unsigned arena_size);
84  static MallocArena *CreateInitialized(unsigned arena_size,
85  unsigned char pattern);
86  ~MallocArena();
87 
88  void *Malloc(const uint32_t size);
89  void Free(void *ptr);
90  inline bool Contains(void *ptr) const {
91  return GetMallocArena(ptr, arena_size_) == this;
92  }
93  uint32_t GetSize(void *ptr) const;
94  bool IsEmpty() const { return no_reserved_ == 0; }
95 
96  private:
97  static const char kTagAvail = 0;
98  static const char kTagReserved = 1;
99 
104  struct AvailBlockCtl {
105  AvailBlockCtl *GetNextPtr(char *base) {
106  return reinterpret_cast<AvailBlockCtl *>(base + link_next);
107  }
108  AvailBlockCtl *GetPrevPtr(char *base) {
109  return reinterpret_cast<AvailBlockCtl *>(base + link_prev);
110  }
111  int32_t ConvertToLink(char *base) {
112  return reinterpret_cast<char *>(this) - base;
113  }
114  void ShrinkTo(int32_t smaller_size) {
115  size = smaller_size;
116  new (AvailBlockTag::GetTagLocation(this)) AvailBlockTag(smaller_size);
117  }
118  int32_t size; // always positive
119  int32_t link_next; // offset in the arena; saves 4 bytes on 64bit archs
120  int32_t link_prev; // offset in the arena; saves 4 bytes on 64bit archs
121  };
122 
126  struct AvailBlockTag {
127  explicit AvailBlockTag(int32_t s) : size(s), tag(kTagAvail) { }
128  static void *GetTagLocation(AvailBlockCtl *block) {
129  return
130  reinterpret_cast<char *>(block) + block->size - sizeof(AvailBlockTag);
131  }
132  int32_t size;
133  char padding[3];
134  char tag;
135  };
136 
142  public:
143  explicit ReservedBlockCtl(int32_t s) : size_(-s) {
144  char *base = reinterpret_cast<char *>(this);
145  *(base + s - 1) = kTagReserved;
146  }
147  int32_t size() const { assert(size_ <= 0); return -size_; }
148 
149  private:
150  int32_t size_; // always negative
151  };
152 
153  void UnlinkAvailBlock(AvailBlockCtl *block);
154  void EnqueueAvailBlock(AvailBlockCtl *block);
155  AvailBlockCtl *FindAvailBlock(const int32_t block_size);
156  void *ReserveBlock(AvailBlockCtl *block, int32_t block_size);
157 
165  char *arena_;
166 
174  uint32_t no_reserved_;
175  unsigned arena_size_;
176 }; // class MallocArena
177 
178 #endif // CVMFS_MALLOC_ARENA_H_
MallocArena(unsigned arena_size)
AvailBlockCtl * FindAvailBlock(const int32_t block_size)
Definition: malloc_arena.cc:24
static MallocArena * GetMallocArena(void *ptr, unsigned arena_size)
Definition: malloc_arena.h:77
void ShrinkTo(int32_t smaller_size)
Definition: malloc_arena.h:114
AvailBlockCtl * rover_
The free block where the next search starts.
Definition: malloc_arena.h:173
static void * GetTagLocation(AvailBlockCtl *block)
Definition: malloc_arena.h:128
assert((mem||(size==0))&&"Out Of Memory")
void UnlinkAvailBlock(AvailBlockCtl *block)
int32_t ConvertToLink(char *base)
Definition: malloc_arena.h:111
static const int kMinBlockSize
Definition: malloc_arena.h:72
void * ReserveBlock(AvailBlockCtl *block, int32_t block_size)
AvailBlockCtl * GetNextPtr(char *base)
Definition: malloc_arena.h:105
void EnqueueAvailBlock(AvailBlockCtl *block)
static const char kTagReserved
Definition: malloc_arena.h:98
static MallocArena * CreateInitialized(unsigned arena_size, unsigned char pattern)
AvailBlockCtl * head_avail_
Definition: malloc_arena.h:172
static const char kTagAvail
Definition: malloc_arena.h:97
unsigned arena_size_
Definition: malloc_arena.h:175
AvailBlockCtl * GetPrevPtr(char *base)
Definition: malloc_arena.h:108
void Free(void *ptr)
Definition: malloc_arena.cc:55
bool IsEmpty() const
Definition: malloc_arena.h:94
bool Contains(void *ptr) const
Definition: malloc_arena.h:90
void * Malloc(const uint32_t size)
char * arena_
The memory block.
Definition: malloc_arena.h:165
uint32_t GetSize(void *ptr) const
static void size_t size
Definition: smalloc.h:47
uint32_t no_reserved_
Definition: malloc_arena.h:174