Skip to content

Commit d53938f

Browse files
authored
Merge pull request #253 from ldorau/Update_pool_size_after_freeing_most_of_the_first_pool
Fix computing ptr and update pool_size
2 parents 5b9672a + 70a3fb6 commit d53938f

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/base_alloc/base_alloc_linear.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
193193
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
194194
(pool->metadata.pool_size > page_size)) {
195195
// we can free the first (main) pool except of the first page containing the metadata
196-
void *ptr = pool + page_size;
196+
void *ptr = (char *)pool + page_size;
197197
size_t size = pool->metadata.pool_size - page_size;
198198
ba_os_free(ptr, size);
199+
// update pool_size
200+
pool->metadata.pool_size = page_size;
199201
}
200202
_DEBUG_EXECUTE(ba_debug_checks(pool));
201203
util_mutex_unlock(&pool->metadata.lock);

src/utils/utils_common.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
#include <stdlib.h>
1616
#include <string.h>
1717

18+
#ifndef _WIN32
19+
#include <sys/syscall.h>
20+
#include <unistd.h>
21+
#endif
22+
1823
#ifdef __cplusplus
1924
extern "C" {
2025
#endif
@@ -31,7 +36,7 @@ extern "C" {
3136

3237
#define __TLS __declspec(thread)
3338

34-
static inline char *os_getenv(const char *name) {
39+
static inline char *util_getenv(const char *name) {
3540
char *buffer;
3641
size_t numberOfElements;
3742
errno_t err = _dupenv_s(&buffer, &numberOfElements, name);
@@ -42,28 +47,33 @@ static inline char *os_getenv(const char *name) {
4247
return buffer;
4348
}
4449

45-
static inline void os_free_getenv(char *val) { free(val); }
50+
static inline void util_free_getenv(char *val) { free(val); }
51+
52+
// TODO: implement util_get_page_size() for Windows
53+
static inline size_t util_get_page_size(void) { return 4096; }
4654

4755
#else /* Linux */
4856

4957
#define __TLS __thread
5058

51-
static inline char *os_getenv(const char *name) { return getenv(name); }
52-
static inline void os_free_getenv(const char *val) {
59+
static inline char *util_getenv(const char *name) { return getenv(name); }
60+
static inline void util_free_getenv(const char *val) {
5361
(void)val; // unused
5462
}
5563

64+
static inline size_t util_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }
65+
5666
#endif /* _WIN32 */
5767

5868
// check if we are running in the proxy library
5969
static inline int is_running_in_proxy_lib(void) {
6070
int is_in_proxy_lib_val = 0;
61-
char *ld_preload = os_getenv("LD_PRELOAD");
71+
char *ld_preload = util_getenv("LD_PRELOAD");
6272
if (ld_preload && strstr(ld_preload, "libumf_proxy.so")) {
6373
is_in_proxy_lib_val = 1;
6474
}
6575

66-
os_free_getenv(ld_preload);
76+
util_free_getenv(ld_preload);
6777
return is_in_proxy_lib_val;
6878
}
6979

test/common/test_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ static inline void UT_OUT(const char *format, ...) {
7070
(unsigned long long)(rhs)), \
7171
0)))
7272

73+
#ifndef ALIGN_UP
7374
#define ALIGN_UP(size, align) (((size) + (align)-1) & ~((align)-1))
75+
#endif
7476

7577
int bufferIsFilledWithChar(void *ptr, size_t size, char c);
7678

test/test_base_alloc_linear.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <thread>
1111

1212
#include "base_alloc_linear.h"
13+
#include "utils_common.h"
1314

1415
#include "base.hpp"
1516
#include "test_helpers.h"
@@ -56,9 +57,14 @@ TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
5657

5758
srand(0);
5859

60+
// The first pool should be bigger than one page,
61+
// but not big enough to hold all allocations,
62+
// so that there were more pools allocated.
63+
// This is needed to test freeing the first pool.
64+
size_t pool_size = 2 * util_get_page_size();
65+
5966
auto pool = std::shared_ptr<umf_ba_linear_pool_t>(
60-
umf_ba_linear_create(NTHREADS * ITERATIONS * MAX_ALLOCATION_SIZE),
61-
umf_ba_linear_destroy);
67+
umf_ba_linear_create(pool_size), umf_ba_linear_destroy);
6268

6369
auto poolAlloc = [](int TID, umf_ba_linear_pool_t *pool) {
6470
struct buffer_t {

0 commit comments

Comments
 (0)