-
Notifications
You must be signed in to change notification settings - Fork 35
Use global base_alloc in proxy lib #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,17 @@ | |
#include "base_alloc.h" | ||
#include "base_alloc_global.h" | ||
#include "base_alloc_internal.h" | ||
#include "utils_common.h" | ||
#include "utils_concurrency.h" | ||
#include "utils_math.h" | ||
|
||
// global base allocator used by all providers and pools | ||
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT; | ||
|
||
// allocation classes need to be powers of 2 | ||
// allocation classes need to be consecutive powers of 2 | ||
#define ALLOCATION_CLASSES \ | ||
{ 16, 32, 64, 128 } | ||
#define NUM_ALLOCATION_CLASSES 4 | ||
{ 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 } | ||
#define NUM_ALLOCATION_CLASSES 10 | ||
|
||
struct base_alloc_t { | ||
size_t ac_sizes[NUM_ALLOCATION_CLASSES]; | ||
|
@@ -82,41 +83,115 @@ static int size_to_idx(size_t size) { | |
return index; | ||
} | ||
|
||
void *umf_ba_global_alloc(size_t size) { | ||
static void *transform_ptr(void *ptr, size_t size, size_t alignment) { | ||
assert(ptr); | ||
|
||
void *user_ptr; | ||
if (alignment <= sizeof(size_t)) { | ||
user_ptr = (void *)((uintptr_t)ptr + sizeof(size_t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment why it is aligned to |
||
} else { | ||
user_ptr = (void *)ALIGN_UP((uintptr_t)ptr + sizeof(size_t), alignment); | ||
} | ||
|
||
size_t ptr_offset_from_original = (uintptr_t)user_ptr - (uintptr_t)ptr; | ||
|
||
size_t *metadata_loc = (size_t *)((char *)user_ptr - sizeof(size_t)); | ||
*metadata_loc = size | (ptr_offset_from_original << 32); | ||
|
||
return user_ptr; | ||
} | ||
|
||
static void *get_original_alloc(void *user_ptr, size_t *total_size, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment what this function does, please. |
||
size_t *usable_size) { | ||
assert(user_ptr); | ||
|
||
size_t *metadata_loc = (size_t *)((char *)user_ptr - sizeof(size_t)); | ||
|
||
size_t stored_size = *metadata_loc & ((1ULL << 32) - 1); | ||
size_t ptr_offset_from_original = *metadata_loc >> 32; | ||
|
||
void *original_ptr = | ||
(void *)((uintptr_t)user_ptr - ptr_offset_from_original); | ||
|
||
if (total_size) { | ||
*total_size = stored_size; | ||
} | ||
|
||
if (usable_size) { | ||
*usable_size = stored_size - ptr_offset_from_original; | ||
} | ||
|
||
return original_ptr; | ||
} | ||
|
||
void *umf_ba_global_aligned_alloc(size_t size, size_t alignment) { | ||
util_init_once(&ba_is_initialized, umf_ba_create_global); | ||
|
||
// for metadata | ||
size += sizeof(size_t); | ||
|
||
if (alignment > sizeof(size_t)) { | ||
size += alignment; | ||
} | ||
|
||
if (size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) { | ||
#ifndef NDEBUG | ||
fprintf(stderr, | ||
"base_alloc: allocation size larger than the biggest " | ||
"allocation class. Falling back to OS memory allocation.\n"); | ||
"base_alloc: allocation size (%zu) larger than the biggest " | ||
"allocation class. Falling back to OS memory allocation.\n", | ||
size); | ||
#endif | ||
return ba_os_alloc(size); | ||
return transform_ptr(ba_os_alloc(size), size, alignment); | ||
} | ||
|
||
int ac_index = size_to_idx(size); | ||
if (!BASE_ALLOC.ac[ac_index]) { | ||
// if creating ac failed, fall back to os allocation | ||
fprintf(stderr, "base_alloc: allocation class not created. Falling " | ||
"back to OS memory allocation.\n"); | ||
return ba_os_alloc(size); | ||
return transform_ptr(ba_os_alloc(size), size, alignment); | ||
} | ||
|
||
return umf_ba_alloc(BASE_ALLOC.ac[ac_index]); | ||
return transform_ptr(umf_ba_alloc(BASE_ALLOC.ac[ac_index]), size, | ||
alignment); | ||
} | ||
|
||
void umf_ba_global_free(void *ptr, size_t size) { | ||
if (size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) { | ||
ba_os_free(ptr, size); | ||
void *umf_ba_global_alloc(size_t size) { | ||
return umf_ba_global_aligned_alloc(size, sizeof(size_t)); | ||
} | ||
|
||
void umf_ba_global_free(void *ptr) { | ||
if (!ptr) { | ||
return; | ||
} | ||
|
||
int ac_index = size_to_idx(size); | ||
size_t total_size; | ||
ptr = get_original_alloc(ptr, &total_size, NULL); | ||
|
||
if (total_size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) { | ||
ba_os_free(ptr, total_size); | ||
return; | ||
} | ||
|
||
int ac_index = size_to_idx(total_size); | ||
if (!BASE_ALLOC.ac[ac_index]) { | ||
// if creating ac failed, memory must have been allocated by os | ||
ba_os_free(ptr, size); | ||
ba_os_free(ptr, total_size); | ||
return; | ||
} | ||
|
||
umf_ba_free(BASE_ALLOC.ac[ac_index], ptr); | ||
} | ||
|
||
size_t umf_ba_global_malloc_usable_size(void *ptr) { | ||
if (!ptr) { | ||
return 0; | ||
} | ||
|
||
size_t usable_size; | ||
get_original_alloc(ptr, NULL, &usable_size); | ||
|
||
assert(usable_size >= sizeof(size_t)); | ||
|
||
return usable_size; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment what this function does, please.