NVMLib  very early alpha
A library to optimally use a Hybrid RAM setup.
malloc.h
Go to the documentation of this file.
1 #ifndef __NVM_MALLOC__
2 #define __NVM_MALLOC__
3 
4 /*
5 * This file contains the definitions of all the functions related to
6 * `Heap allocation` of variables.
7 *
8 * Idea :
9 * (if to be sent to NVRAM)
10 * ........................ libpmem.h
11 * /
12 * memalloc() ------
13 * \........................ malloc()
14 * (if to be sent to DRAM)
15 *
16 */
17 
18 #include "globals.h"
19 #include "pool.h"
20 #include <stdint.h>
21 #include <libiberty/splay-tree.h>
22 #include <stdarg.h>
23 
24 
25 splay_tree addr2MemOID_read;
26 splay_tree addr2MemOID_write;
27 
29 typedef struct MEMoid_st {
32  size_t size;
33 
34  // All of these are in object_maintainance.h
35  // uint32_t num_reads;
36  // uint32_t num_writes;
37  // uint64_t *access_bitmap;
39 
40 // Allocates the requested space in NVRAM and returns the offset of
41 // the pointer to the allocated space.
42 // This is called internally by `get_current_free_offset()`
43 uint64_t allot_first_free_offset_pool(uint64_t pool_id, size_t size);
44 
45 MEMoid allot_first_free_offset(size_t size);
46 
50 
52 static const MEMoid MEMOID_NULL = { 0, 0, 0 };
53 
54 // The user facing fnction to allocate memory.
55 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
56 #define NUMARGS(...) (sizeof((int[]){0, ##__VA_ARGS__})/sizeof(int)-1)
57 
58 // User doesn't worry about where the object is placed
59 //
60 // Note this supports both with and wothout `which_ram`
61 MEMoidKey _memalloc(size_t size, const char *file, const char *func, const int line, int num_args, ...);
62 
78 #define memalloc(size, ...) _memalloc(size, __FILENAME__, __func__, __LINE__, NUMARGS(__VA_ARGS__), ##__VA_ARGS__)
79 // User specifies where to put the object
80 // #define memmalloc(size, which_ram) _memalloc(size, which_ram, __FILENAME__, __func__, __LINE__)
81 
82 // Returns the direct pointer to the mem-object
83 void* get_memobj_direct(MEMoid obj);
84 
85 void* _key_get_last(MEMoidKey key);
86 void* _key_get_first(MEMoidKey key);
87 
88 #define KEY_FIRST(key) (_key_get_first(key))
89 #define KEY_LAST(key) (_key_get_last(key))
90 
91 // #define memfree(o) _memfree((o).oidkey, sizeof(__typeof__(*(o)._type)))
92 // #define memfree(o) _memfree((o).oidkey, get_MEMoid(o).size)
93 void _memfree(MEMoidKey oidkey);
94 
108 #define memfree(o) _memfree((o).oidkey)
109 
112  cmp_addr
113 };
114 
116 typedef struct addr2memoid_key {
117  enum splay_comp comp;
118  union {
119  void* addr;
121  };
122 
124 
125 void init_splay();
126 #endif // !__NVM_MALLOC__
MEMoid_st::offset
uint64_t offset
Definition: malloc.h:31
cmp_addr
@ cmp_addr
Definition: malloc.h:112
addr2MemOID_write
splay_tree addr2MemOID_write
Definition: malloc.h:26
allot_first_free_offset_pool
uint64_t allot_first_free_offset_pool(uint64_t pool_id, size_t size)
Allocates first free memory chunk of the given size in the given pool specified by pool_id.
Definition: malloc.c:48
get_memobj_direct
void * get_memobj_direct(MEMoid obj)
The function to return the actual memptr for a given MEMoid object.
Definition: malloc.c:316
addr2memoid_key
struct addr2memoid_key addr2memoid_key
The structure used in spaly_tree which used for getting MEMoid from the memory address.
_key_get_last
void * _key_get_last(MEMoidKey key)
Definition: malloc.c:392
MEMoid
struct MEMoid_st MEMoid
The struct that stores the memptr for the object.
splay_comp
splay_comp
Definition: malloc.h:110
MEMoid_st
The struct that stores the memptr for the object.
Definition: malloc.h:29
MEMoidKey
uint64_t MEMoidKey
The key of the HashTable that contains <MEMoidKey, MEMoid>.
Definition: malloc.h:49
MEMoid_st::size
size_t size
Definition: malloc.h:32
globals.h
_memfree
void _memfree(MEMoidKey oidkey)
The fucntion to free the allocated memory location.
Definition: malloc.c:358
addr2memoid_key::key
MEMoidKey key
Definition: malloc.h:120
pool.h
uint64_t
__uint64_t uint64_t
Definition: globals.h:51
addr2memoid_key::comp
enum splay_comp comp
Definition: malloc.h:117
cmp_node
@ cmp_node
Definition: malloc.h:111
_memalloc
MEMoidKey _memalloc(size_t size, const char *file, const char *func, const int line, int num_args,...)
Allocates the memory requested and returns a MEMoidKey.
Definition: malloc.c:202
addr2memoid_key::addr
void * addr
Definition: malloc.h:119
MEMOID_NULL
static const MEMoid MEMOID_NULL
Just a dummy obj.
Definition: malloc.h:52
addr2MemOID_read
splay_tree addr2MemOID_read
Definition: malloc.h:25
MEMoid_st::pool_id
uint64_t pool_id
Definition: malloc.h:30
init_splay
void init_splay()
Definition: malloc.c:442
_key_get_first
void * _key_get_first(MEMoidKey key)
Definition: malloc.c:384
addr2memoid_key
The structure used in spaly_tree which used for getting MEMoid from the memory address.
Definition: malloc.h:116
allot_first_free_offset
MEMoid allot_first_free_offset(size_t size)
Allocates the first free memory chunk of the given size.
Definition: malloc.c:104