This source file includes following definitions.
- mca_memheap_ptmalloc_module_init
- mca_memheap_ptmalloc_alloc
- mca_memheap_ptmalloc_align
- mca_memheap_ptmalloc_realloc
- mca_memheap_ptmalloc_free
- mca_memheap_ptmalloc_finalize
- mca_memheap_ptmalloc_getpagesize
- mca_memheap_ptmalloc_sbrk
1
2
3
4
5
6
7
8
9
10
11
12 #include "oshmem_config.h"
13 #include "oshmem/proc/proc.h"
14 #include "oshmem/mca/spml/spml.h"
15 #include "oshmem/mca/memheap/memheap.h"
16 #include "oshmem/mca/memheap/ptmalloc/memheap_ptmalloc.h"
17 #include "oshmem/mca/memheap/ptmalloc/memheap_ptmalloc_component.h"
18 #include "oshmem/mca/memheap/base/base.h"
19 #include "opal/class/opal_hash_table.h"
20 #include "opal/class/opal_object.h"
21
22 mca_memheap_ptmalloc_module_t memheap_ptmalloc = {
23 {
24 &mca_memheap_ptmalloc_component,
25 mca_memheap_ptmalloc_finalize,
26 mca_memheap_ptmalloc_alloc,
27 mca_memheap_ptmalloc_align,
28 mca_memheap_ptmalloc_realloc,
29 mca_memheap_ptmalloc_free,
30
31 mca_memheap_ptmalloc_alloc,
32 mca_memheap_ptmalloc_free,
33
34 mca_memheap_base_get_mkey,
35 mca_memheap_base_is_symmetric_addr,
36 mca_memheap_modex_recv_all,
37
38 0
39 },
40 100
41 };
42
43
44
45
46
47 int mca_memheap_ptmalloc_module_init(memheap_context_t *context)
48 {
49 if (!context || !context->user_size || !context->private_size) {
50 return OSHMEM_ERR_BAD_PARAM;
51 }
52
53
54 OBJ_CONSTRUCT(&memheap_ptmalloc.lock, opal_mutex_t);
55 memheap_ptmalloc.base = context->user_base_addr;
56 memheap_ptmalloc.cur_size = 0;
57 memheap_ptmalloc.max_size = context->user_size + context->private_size;
58 memheap_ptmalloc.max_alloc_size = context->user_size;
59
60 MEMHEAP_VERBOSE(1,
61 "symmetric heap memory (user+private): %llu bytes",
62 (unsigned long long)(context->user_size + context->private_size));
63
64
65
66 return OSHMEM_SUCCESS;
67
68 }
69
70
71
72
73
74 int mca_memheap_ptmalloc_alloc(size_t size, void** p_buff)
75 {
76 if (size > memheap_ptmalloc.max_alloc_size) {
77 *p_buff = 0;
78 return OSHMEM_ERR_OUT_OF_RESOURCE;
79 }
80
81 OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
82 *p_buff = dlmalloc(size);
83 OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
84
85 if (NULL == *p_buff)
86 return OSHMEM_ERROR;
87
88 MCA_SPML_CALL(memuse_hook(*p_buff, size));
89 return OSHMEM_SUCCESS;
90 }
91
92 int mca_memheap_ptmalloc_align(size_t align, size_t size, void **p_buff)
93 {
94 if (size > memheap_ptmalloc.max_alloc_size) {
95 *p_buff = 0;
96 return OSHMEM_ERR_OUT_OF_RESOURCE;
97 }
98
99 if (align == 0) {
100 *p_buff = 0;
101 return OSHMEM_ERROR;
102 }
103
104
105 if (align & (align - 1)) {
106 *p_buff = 0;
107 return OSHMEM_ERROR;
108 }
109
110 OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
111 *p_buff = dlmemalign(align, size);
112 OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
113
114 if (NULL == *p_buff)
115 return OSHMEM_ERROR;
116
117 MCA_SPML_CALL(memuse_hook(*p_buff, size));
118 return OSHMEM_SUCCESS;
119 }
120
121 int mca_memheap_ptmalloc_realloc(size_t new_size,
122 void *p_buff,
123 void **p_new_buff)
124 {
125 if (new_size > memheap_ptmalloc.max_alloc_size) {
126 *p_new_buff = 0;
127 return OSHMEM_ERR_OUT_OF_RESOURCE;
128 }
129
130 OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
131 *p_new_buff = dlrealloc(p_buff, new_size);
132 OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
133
134 if (!*p_new_buff)
135 return OSHMEM_ERR_OUT_OF_RESOURCE;
136
137 MCA_SPML_CALL(memuse_hook(*p_new_buff, new_size));
138 return OSHMEM_SUCCESS;
139 }
140
141
142
143
144
145 int mca_memheap_ptmalloc_free(void* ptr)
146 {
147 OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
148 dlfree(ptr);
149 OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
150 return OSHMEM_SUCCESS;
151 }
152
153 int mca_memheap_ptmalloc_finalize()
154 {
155 MEMHEAP_VERBOSE(5, "deregistering symmetric heap");
156 return OSHMEM_SUCCESS;
157 }
158
159 int mca_memheap_ptmalloc_getpagesize(void)
160 {
161 return 2 * 1024 * 1024;
162 }
163
164
165 #define PTMALLOC_MAX_SIZE_T (~(size_t)0)
166 #define PTMALLOC_MFAIL ((void*)(PTMALLOC_MAX_SIZE_T))
167 void *mca_memheap_ptmalloc_sbrk(size_t size)
168 {
169 char *ret;
170
171 if (memheap_ptmalloc.cur_size + size > memheap_ptmalloc.max_size) {
172 return PTMALLOC_MFAIL ;
173 }
174
175 ret = (char *) memheap_ptmalloc.base + memheap_ptmalloc.cur_size;
176 memheap_ptmalloc.cur_size += size;
177
178 return ret;
179 }
180