Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ddprof-lib/src/main/cpp/codeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include "codeCache.h"
#include "dwarf_dd.h"
#include "os_dd.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

char *NativeFunc::create(const char *name, short lib_index) {
NativeFunc *f = (NativeFunc *)malloc(sizeof(NativeFunc) + 1 + strlen(name));
size_t size = align_up(sizeof(NativeFunc) + 1 + strlen(name), sizeof(NativeFunc*));
NativeFunc *f = (NativeFunc *)aligned_alloc(sizeof(NativeFunc*), size);
f->_lib_index = lib_index;
f->_mark = 0;
// cppcheck-suppress memleak
Expand Down
4 changes: 3 additions & 1 deletion ddprof-lib/src/main/cpp/codeCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef _CODECACHE_H
#define _CODECACHE_H

#include "utils.h"

#include <jvmti.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -62,7 +64,7 @@ class NativeFunc {

static short libIndex(const char *name) {
NativeFunc* func = from(name);
if (posix_memalign((void**)(&func), sizeof(NativeFunc*), sizeof(NativeFunc)) != 0) {
if (!is_aligned(func, sizeof(func))) {
return -1;
}
return func->_lib_index;
Expand Down
35 changes: 35 additions & 0 deletions ddprof-lib/src/main/cpp/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _UTILS_H
#define _UTILS_H

#include <cassert>
#include <cstdint>
#include <cstddef>

inline bool is_power_of_2(size_t size) {
return size > 0 && (size & (size - 1)) == 0;
}

template <typename T>
inline bool is_aligned(const T* ptr, size_t alignment) noexcept {
assert(is_power_of_2(alignment));
// Convert the pointer to an integer type
auto iptr = reinterpret_cast<uintptr_t>(ptr);

// Check if the integer value is a multiple of the alignment
return (iptr & ~(alignment - 1) == 0);
}

inline size_t align_down(size_t size, size_t alignment) noexcept {
assert(is_power_of_2(alignment));
return size & ~(alignment - 1);
}

inline size_t align_up(size_t size, size_t alignment) noexcept {
assert(is_power_of_2(alignment));
return align_down(size + alignment - 1, alignment);
}




#endif // _UTILS_H
Loading