The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
mm.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include <stdlib.h>
33 #include <sys/mman.h>
34 #include <pthread.h>
35 #include <core/core.h>
36 #include <arch/atomic.h>
37 #include <mm/dymelor.h>
38 
39 struct segment {
40  unsigned char *base;
41  unsigned char *brk;
42 };
43 
44 struct buddy {
45  spinlock_t lock;
46  size_t size;
47  size_t longest[] __attribute__((aligned(sizeof(size_t))));
48 };
49 
50 extern size_t __page_size;
51 #define PAGE_SIZE ({ \
52  if(unlikely(__page_size == 0))\
53  __page_size = getpagesize();\
54  __page_size;\
55  })
56 
57 struct slab_header {
58 #ifndef NDEBUG
59  atomic_t presence;
60 #endif
61  struct slab_header *prev, *next;
62  uint64_t slots;
63  uintptr_t refcount;
64  struct slab_header *page;
65  uint8_t data[] __attribute__((aligned(sizeof(void *))));
66 };
67 
68 struct slab_chain {
69  spinlock_t lock;
70  size_t itemsize, itemcount;
71  size_t slabsize, pages_per_alloc;
72  uint64_t initial_slotmask, empty_slotmask;
73  uintptr_t alignment_mask;
74  struct slab_header *partial, *empty, *full;
75 };
76 
77 struct memory_map {
78  malloc_state *m_state;
79  struct buddy *buddy;
80  struct slab_chain *slab;
81  struct segment *segment;
82 };
83 
84 #define PER_LP_PREALLOCATED_MEMORY (262144L * PAGE_SIZE) // This should be power of 2 multiplied by a page size. This is 1GB per LP.
85 #define BUDDY_GRANULARITY PAGE_SIZE // This is the smallest chunk released by the buddy in bytes. PER_LP_PREALLOCATED_MEMORY/BUDDY_GRANULARITY must be integer and a power of 2
86 
87 extern bool allocator_init(void);
88 extern void allocator_fini(void);
89 extern void segment_init(void);
90 extern struct segment *get_segment(GID_t i);
91 extern void *get_base_pointer(GID_t gid);
92 
93 extern void initialize_memory_map(struct lp_struct *lp);
94 extern void finalize_memory_map(struct lp_struct *lp);
95 
96 extern struct buddy *buddy_new(struct lp_struct *,
97  unsigned long num_of_fragments);
98 void buddy_destroy(struct buddy *);
99 
100 extern struct slab_chain *slab_init(const size_t itemsize);
101 extern void *slab_alloc(struct slab_chain *const sch);
102 extern void slab_free(struct slab_chain *const sch, const void *const addr);
Definition: mm.h:57
Core ROOT-Sim functionalities.
Definition: mm.h:77
Dynamic Memory Logger and Restorer (DyMeLoR)
Definition: mm.h:39
Definition: mm.h:44
struct buddy * buddy_new(struct lp_struct *lp, size_t num_of_fragments)
Definition: buddy.c:78
Atomic operations.
Definition: mm.h:68
Definition of a GID.
Definition: core.h:132
Definition of the memory map.
Definition: dymelor.h:119