Skip to content

Commit c00f97f

Browse files
samuelkgutierrezrhc54
authored andcommitted
TMA: Integrate more custom memory allocator infrastructure.
This commit further integrates TMA infrastructure into the base object system. Some key memory allocations there were outside a provided TMA's purview; now they are captured. Signed-off-by: Samuel K. Gutierrez <samuel@lanl.gov>
1 parent 1286709 commit c00f97f

File tree

3 files changed

+59
-50
lines changed

3 files changed

+59
-50
lines changed

src/class/pmix_object.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Copyright (c) 2016 Research Organization for Information Science
1414
* and Technology (RIST). All rights reserved.
1515
* Copyright (c) 2021 Nanook Consulting. All rights reserved.
16+
* Copyright (c) 2022 Triad National Security, LLC. All rights reserved.
1617
* $COPYRIGHT$
1718
*
1819
* Additional copyrights may follow
@@ -65,13 +66,13 @@ static const int increment = 10;
6566
/*
6667
* Local functions
6768
*/
68-
static void save_class(pmix_class_t *cls);
69-
static void expand_array(void);
69+
static void save_class(pmix_class_t *cls, pmix_tma_t *tma);
70+
static void expand_array(pmix_tma_t *tma);
7071

7172
/*
7273
* Lazy initialization of class descriptor.
7374
*/
74-
void pmix_class_initialize(pmix_class_t *cls)
75+
void pmix_class_initialize(pmix_class_t *cls, pmix_tma_t *tma)
7576
{
7677
pmix_class_t *c;
7778
pmix_construct_t *cls_construct_array;
@@ -122,7 +123,7 @@ void pmix_class_initialize(pmix_class_t *cls)
122123
* plus for each a NULL-sentinel
123124
*/
124125

125-
cls->cls_construct_array = (void (**)(pmix_object_t *)) malloc(
126+
cls->cls_construct_array = (void (**)(pmix_object_t *)) pmix_tma_malloc(tma,
126127
(cls_construct_array_count + cls_destruct_array_count + 2) * sizeof(pmix_construct_t));
127128
if (NULL == cls->cls_construct_array) {
128129
perror("Out of memory");
@@ -153,7 +154,7 @@ void pmix_class_initialize(pmix_class_t *cls)
153154
*cls_destruct_array = NULL; /* end marker for the destructors */
154155

155156
cls->cls_initialized = pmix_class_init_epoch;
156-
save_class(cls);
157+
save_class(cls, tma);
157158

158159
/* All done */
159160

@@ -188,25 +189,25 @@ int pmix_class_finalize(void)
188189
return 0;
189190
}
190191

191-
static void save_class(pmix_class_t *cls)
192+
static void save_class(pmix_class_t *cls, pmix_tma_t *tma)
192193
{
193194
if (num_classes >= max_classes) {
194-
expand_array();
195+
expand_array(tma);
195196
}
196197

197198
classes[num_classes] = cls->cls_construct_array;
198199
++num_classes;
199200
}
200201

201-
static void expand_array(void)
202+
static void expand_array(pmix_tma_t *tma)
202203
{
203204
int i;
204205

205206
max_classes += increment;
206207
if (NULL == classes) {
207-
classes = (void **) calloc(max_classes, sizeof(void *));
208+
classes = (void **) pmix_tma_calloc(tma, max_classes, sizeof(void *));
208209
} else {
209-
classes = (void **) realloc(classes, sizeof(void *) * max_classes);
210+
classes = (void **) pmix_tma_realloc(tma, classes, sizeof(void *) * max_classes);
210211
}
211212
if (NULL == classes) {
212213
perror("class malloc failed");

src/class/pmix_object.h

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,10 @@ typedef struct pmix_tma {
231231
* Like memmove(), it returns a pointer to the content's destination.
232232
*/
233233
void *(*tma_memmove)(struct pmix_tma *tma, const void *src, size_t n);
234+
/** Pointer to the TMA's free() function. */
235+
void (*tma_free)(struct pmix_tma *, void *);
234236
/** Pointer inside the memory allocation arena. */
235237
void *arena;
236-
/**
237-
* When true free() and realloc() cannot be used,
238-
* and memory allocation functions cannot fail.
239-
*/
240-
bool dontfree;
241238
} pmix_tma_t;
242239

243240
static inline void *pmix_tma_malloc(pmix_tma_t *tma, size_t size)
@@ -249,6 +246,15 @@ static inline void *pmix_tma_malloc(pmix_tma_t *tma, size_t size)
249246
}
250247
}
251248

249+
static inline void *pmix_tma_calloc(pmix_tma_t *tma, size_t nmemb, size_t size)
250+
{
251+
if (NULL != tma) {
252+
return tma->tma_calloc(tma, nmemb, size);
253+
} else {
254+
return calloc(nmemb, size);
255+
}
256+
}
257+
252258
static inline void *pmix_tma_realloc(pmix_tma_t *tma, void *ptr, size_t size)
253259
{
254260
if (NULL != tma) {
@@ -267,6 +273,15 @@ static inline char *pmix_tma_strdup(pmix_tma_t *tma, const char *src)
267273
}
268274
}
269275

276+
static inline void pmix_tma_free(pmix_tma_t *tma, void *ptr)
277+
{
278+
if (NULL != tma) {
279+
tma->tma_free(tma, ptr);
280+
} else {
281+
free(ptr);
282+
}
283+
}
284+
270285
/**
271286
* Class descriptor.
272287
*
@@ -306,8 +321,8 @@ PMIX_EXPORT extern int pmix_class_init_epoch;
306321
.obj_tma.tma_realloc = NULL, \
307322
.obj_tma.tma_strdup = NULL, \
308323
.obj_tma.tma_memmove = NULL, \
324+
.obj_tma.tma_free = NULL, \
309325
.obj_tma.arena = NULL, \
310-
.obj_tma.dontfree = false, \
311326
.cls_init_file_name = __FILE__, \
312327
.cls_init_lineno = __LINE__ \
313328
}
@@ -322,8 +337,8 @@ PMIX_EXPORT extern int pmix_class_init_epoch;
322337
.obj_tma.tma_realloc = NULL, \
323338
.obj_tma.tma_strdup = NULL, \
324339
.obj_tma.tma_memmove = NULL, \
325-
.obj_tma.arena = NULL, \
326-
.obj_tma.dontfree = false \
340+
.obj_tma.tma_free = NULL, \
341+
.obj_tma.arena = NULL \
327342
}
328343
#endif
329344

@@ -412,12 +427,12 @@ static inline pmix_object_t *pmix_obj_new_debug_tma(pmix_class_t *type, pmix_tma
412427
((type *)pmix_obj_new_debug_tma(PMIX_CLASS(type), NULL, __FILE__, __LINE__))
413428

414429
#define PMIX_NEW_TMA(type, tma) \
415-
((type *)pmix_obj_new_debug_tma(PMIX_CLASS(type), tma, __FILE__, __LINE__))
430+
((type *)pmix_obj_new_debug_tma(PMIX_CLASS(type), (tma), __FILE__, __LINE__))
416431

417432
#else
418433
#define PMIX_NEW_NO_TMA(type) ((type *)pmix_obj_new_tma(PMIX_CLASS(type), NULL))
419434

420-
#define PMIX_NEW_TMA(type, tma) ((type *)pmix_obj_new_tma(PMIX_CLASS(type), tma))
435+
#define PMIX_NEW_TMA(type, tma) ((type *)pmix_obj_new_tma(PMIX_CLASS(type), (tma)))
421436

422437
#endif /* PMIX_ENABLE_DEBUG */
423438

@@ -486,7 +501,10 @@ static inline pmix_object_t *pmix_obj_new_debug_tma(pmix_class_t *type, pmix_tma
486501
PMIX_SET_MAGIC_ID((object), 0); \
487502
pmix_obj_run_destructors(_obj); \
488503
PMIX_REMEMBER_FILE_AND_LINENO(object, __FILE__, __LINE__); \
489-
if (!(_obj->obj_tma.dontfree)) { \
504+
if (NULL != _obj->obj_tma.tma_free) { \
505+
pmix_tma_free(&_obj->obj_tma, object); \
506+
} \
507+
else { \
490508
free(object); \
491509
} \
492510
object = NULL; \
@@ -498,7 +516,10 @@ static inline pmix_object_t *pmix_obj_new_debug_tma(pmix_class_t *type, pmix_tma
498516
pmix_object_t *_obj = (pmix_object_t *) object; \
499517
if (0 == pmix_obj_update(_obj, -1)) { \
500518
pmix_obj_run_destructors(_obj); \
501-
if (!(_obj->obj_tma.dontfree)) { \
519+
if (NULL != _obj->obj_tma.tma_free) { \
520+
pmix_tma_free(&_obj->obj_tma, object); \
521+
} \
522+
else { \
502523
free(object); \
503524
} \
504525
object = NULL; \
@@ -512,32 +533,26 @@ static inline pmix_object_t *pmix_obj_new_debug_tma(pmix_class_t *type, pmix_tma
512533
* @param object Pointer to the object
513534
* @param type The object type
514535
*/
515-
static inline void pmix_obj_construct_tma(pmix_object_t *obj, pmix_tma_t *t)
536+
static inline void pmix_obj_construct_tma(pmix_object_t *obj, pmix_tma_t *tma)
516537
{
517-
if (NULL == t) {
538+
if (NULL == tma) {
518539
obj->obj_tma.tma_malloc = NULL;
519540
obj->obj_tma.tma_calloc = NULL;
520541
obj->obj_tma.tma_realloc = NULL;
521542
obj->obj_tma.tma_strdup = NULL;
522543
obj->obj_tma.tma_memmove = NULL;
544+
obj->obj_tma.tma_free = NULL;
523545
obj->obj_tma.arena = NULL;
524-
obj->obj_tma.dontfree = false;
525546
} else {
526-
obj->obj_tma.tma_malloc = t->tma_malloc;
527-
obj->obj_tma.tma_calloc = t->tma_calloc;
528-
obj->obj_tma.tma_realloc = t->tma_realloc;
529-
obj->obj_tma.tma_strdup = t->tma_strdup;
530-
obj->obj_tma.tma_memmove = t->tma_memmove;
531-
obj->obj_tma.arena = t->arena;
532-
obj->obj_tma.dontfree = t->dontfree;
547+
obj->obj_tma = *tma;
533548
}
534549
}
535550

536551
#define PMIX_CONSTRUCT_INTERNAL_TMA(object, type, t) \
537552
do { \
538553
PMIX_SET_MAGIC_ID((object), PMIX_OBJ_MAGIC_ID); \
539554
if (pmix_class_init_epoch != (type)->cls_initialized) { \
540-
pmix_class_initialize((type)); \
555+
pmix_class_initialize((type), (t)); \
541556
} \
542557
((pmix_object_t *) (object))->obj_class = (type); \
543558
((pmix_object_t *) (object))->obj_reference_count = 1; \
@@ -547,9 +562,9 @@ static inline void pmix_obj_construct_tma(pmix_object_t *obj, pmix_tma_t *t)
547562
} while (0)
548563

549564

550-
#define PMIX_CONSTRUCT_TMA(object, type, t) \
551-
do { \
552-
PMIX_CONSTRUCT_INTERNAL_TMA((object), PMIX_CLASS(type), t); \
565+
#define PMIX_CONSTRUCT_TMA(object, type, t) \
566+
do { \
567+
PMIX_CONSTRUCT_INTERNAL_TMA((object), PMIX_CLASS(type), (t)); \
553568
} while (0)
554569

555570

@@ -594,7 +609,7 @@ PMIX_CLASS_DECLARATION(pmix_object_t);
594609
*
595610
* @param class Pointer to class descriptor
596611
*/
597-
PMIX_EXPORT void pmix_class_initialize(pmix_class_t *);
612+
PMIX_EXPORT void pmix_class_initialize(pmix_class_t *cls, pmix_tma_t *tma);
598613

599614
/**
600615
* Shut down the class system and release all memory
@@ -668,13 +683,10 @@ static inline pmix_object_t *pmix_obj_new_tma(pmix_class_t *cls, pmix_tma_t *tma
668683
pmix_object_t *object;
669684
assert(cls->cls_sizeof >= sizeof(pmix_object_t));
670685

671-
if (NULL == tma) {
672-
object = (pmix_object_t *) malloc(cls->cls_sizeof);
673-
} else {
674-
object = (pmix_object_t *) pmix_tma_malloc(tma, cls->cls_sizeof);
675-
}
686+
object = (pmix_object_t *) pmix_tma_malloc(tma, cls->cls_sizeof);
687+
676688
if (pmix_class_init_epoch != cls->cls_initialized) {
677-
pmix_class_initialize(cls);
689+
pmix_class_initialize(cls, tma);
678690
}
679691
if (NULL != object) {
680692
#if PMIX_ENABLE_DEBUG
@@ -704,8 +716,8 @@ static inline pmix_object_t *pmix_obj_new_tma(pmix_class_t *cls, pmix_tma_t *tma
704716
object->obj_tma.tma_calloc = NULL;
705717
object->obj_tma.tma_realloc = NULL;
706718
object->obj_tma.tma_strdup = NULL;
719+
object->obj_tma.tma_free = NULL;
707720
object->obj_tma.arena = NULL;
708-
object->obj_tma.dontfree = false;
709721
} else {
710722
object->obj_tma = *tma;
711723
}
@@ -714,11 +726,6 @@ static inline pmix_object_t *pmix_obj_new_tma(pmix_class_t *cls, pmix_tma_t *tma
714726
return object;
715727
}
716728

717-
static inline pmix_object_t *pmix_obj_new(pmix_class_t *cls)
718-
{
719-
return pmix_obj_new_tma(cls, NULL);
720-
}
721-
722729
/**
723730
* Atomically update the object's reference count by some increment.
724731
*

src/mca/gds/shmem/gds_shmem.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,9 @@ tma_construct(
750750
tma->tma_realloc = NULL;
751751
tma->tma_strdup = tma_strdup;
752752
tma->tma_memmove = tma_memmove;
753+
// TODO(skg) Add free()
754+
tma->tma_free = NULL;
753755
tma->arena = NULL;
754-
tma->dontfree = 1;
755756
}
756757

757758
static void

0 commit comments

Comments
 (0)