Skip to content

Commit 5be5036

Browse files
committed
CTL: Add a CTL sources to the UMF
This commit introduces sources and compilation of the CTL. Signed-off-by: Krzysztof Filipek <[email protected]>
1 parent a08a121 commit 5be5036

File tree

7 files changed

+1687
-1
lines changed

7 files changed

+1687
-1
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ add_subdirectory(coarse)
2626

2727
set(UMF_LIBS $<BUILD_INTERFACE:umf_utils> $<BUILD_INTERFACE:coarse>)
2828

29+
set(CTL_SOURCES
30+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/alloc.c
31+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/ctl.c)
32+
2933
if(LINUX)
30-
set(BA_SOURCES ${BA_SOURCES}
34+
set(BA_SOURCES ${BA_SOURCES} ${CTL_SOURCES}
3135
${CMAKE_CURRENT_SOURCE_DIR}/base_alloc/base_alloc_linux.c)
3236
elseif(WINDOWS)
3337
set(BA_SOURCES ${BA_SOURCES}

src/ctl/alloc.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright 2019-2024, Intel Corporation */
3+
4+
#include <errno.h>
5+
6+
#include "alloc.h"
7+
#include <string.h>
8+
9+
// #include "fault_injection.h"
10+
11+
Malloc_func fn_malloc = malloc;
12+
Realloc_func fn_realloc = realloc;
13+
14+
#if FAULT_INJECTION
15+
#include "log_internal.h"
16+
17+
static __thread int malloc_num;
18+
static __thread int fail_malloc_num;
19+
static __thread const char *fail_malloc_from;
20+
21+
void *
22+
_flt_Malloc(size_t size, const char *func)
23+
{
24+
if (fail_malloc_from && strcmp(func, fail_malloc_from) == 0) {
25+
if (++malloc_num == fail_malloc_num) {
26+
errno = ENOMEM;
27+
return NULL;
28+
}
29+
}
30+
return fn_malloc(size);
31+
}
32+
33+
static __thread int realloc_num;
34+
static __thread int fail_realloc_num;
35+
static __thread const char *fail_realloc_from;
36+
37+
void *
38+
_flt_Realloc(void *ptr, size_t size, const char *func)
39+
{
40+
if (fail_realloc_from && strcmp(func, fail_realloc_from) == 0) {
41+
if (++realloc_num == fail_realloc_num) {
42+
errno = ENOMEM;
43+
return NULL;
44+
}
45+
}
46+
return fn_realloc(ptr, size);
47+
}
48+
49+
void
50+
core_inject_fault_at(enum pmem_allocation_type type, int nth, const char *at)
51+
{
52+
switch (type) {
53+
case PMEM_MALLOC:
54+
malloc_num = 0;
55+
fail_malloc_num = nth;
56+
fail_malloc_from = at;
57+
break;
58+
case PMEM_REALLOC:
59+
realloc_num = 0;
60+
fail_realloc_num = nth;
61+
fail_realloc_from = at;
62+
break;
63+
default:
64+
CORE_LOG_FATAL("unknown allocation type");
65+
}
66+
}
67+
68+
int
69+
core_fault_injection_enabled(void)
70+
{
71+
return 1;
72+
}
73+
#else
74+
void *_Malloc(size_t size) {
75+
return fn_malloc(size);
76+
}
77+
78+
void *_Realloc(void *ptr, size_t size) {
79+
return fn_realloc(ptr, size);
80+
}
81+
#endif
82+
83+
void set_func_malloc(void *(*malloc_func)(size_t size)) {
84+
fn_malloc = (malloc_func == NULL) ? malloc : malloc_func;
85+
}
86+
87+
void set_func_realloc(void *(*realloc_func)(void *ptr, size_t size)) {
88+
fn_realloc = (realloc_func == NULL) ? realloc : realloc_func;
89+
}
90+
91+
/*
92+
* our versions of malloc & friends start off pointing to the libc versions
93+
*/
94+
Free_func Free = free;
95+
Strdup_func Strdup = strdup;
96+
97+
/*
98+
* Zalloc -- allocate zeroed memory
99+
*/
100+
void *
101+
Zalloc(size_t sz)
102+
{
103+
void *ret = Malloc(sz);
104+
if (!ret)
105+
return NULL;
106+
return memset(ret, 0, sz);
107+
}
108+
109+
/*
110+
* util_set_alloc_funcs -- allow one to override malloc, etc.
111+
*/
112+
void
113+
util_set_alloc_funcs(void *(*malloc_func)(size_t size),
114+
void (*free_func)(void *ptr),
115+
void *(*realloc_func)(void *ptr, size_t size),
116+
char *(*strdup_func)(const char *s))
117+
{
118+
set_func_malloc(malloc_func);
119+
Free = (free_func == NULL) ? free : free_func;
120+
set_func_realloc(realloc_func);
121+
Strdup = (strdup_func == NULL) ? strdup : strdup_func;
122+
}

src/ctl/alloc.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
/* Copyright 2019-2020, Intel Corporation */
3+
4+
#ifndef COMMON_ALLOC_H
5+
#define COMMON_ALLOC_H
6+
7+
#include <stdlib.h>
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
typedef void *(*Malloc_func)(size_t size);
14+
typedef void *(*Realloc_func)(void *ptr, size_t size);
15+
16+
extern Malloc_func fn_malloc;
17+
extern Realloc_func fn_realloc;
18+
19+
#if FAULT_INJECTION
20+
void *_flt_Malloc(size_t, const char *);
21+
void *_flt_Realloc(void *, size_t, const char *);
22+
23+
#define Malloc(size) _flt_Malloc(size, __func__)
24+
#define Realloc(ptr, size) _flt_Realloc(ptr, size, __func__)
25+
#else
26+
void *_Malloc(size_t);
27+
void *_Realloc(void *, size_t);
28+
29+
#define Malloc(size) _Malloc(size)
30+
#define Realloc(ptr, size) _Realloc(ptr, size)
31+
#endif
32+
33+
void set_func_malloc(void *(*malloc_func)(size_t size));
34+
void set_func_realloc(void *(*realloc_func)(void *ptr, size_t size));
35+
36+
/*
37+
* overridable names for malloc & friends used by this library
38+
*/
39+
typedef void (*Free_func)(void *ptr);
40+
typedef char *(*Strdup_func)(const char *s);
41+
42+
extern Free_func Free;
43+
extern Strdup_func Strdup;
44+
extern void *Zalloc(size_t sz);
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif
49+
#endif

0 commit comments

Comments
 (0)