mimalloc-override.h does not work correctly with namespaces

Đang mở
#1,136 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
c, cpp
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu với mimalloc-override.h và tái hiện lỗi bằng các cờ -include mimalloc-override.h và -lmimalloc, sau đó kiểm tra các lệnh gọi có chỉ định namespace được hiển thị trong llvm/Support/MemAlloc.h. Xác minh hành vi override đối với std::malloc và std::realloc, đồng thời bổ sung coverage cho thấy quá trình biên dịch Clang bị ảnh hưởng đã thành công.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Hello! Recently, I decided to build a project with Clang using mimalloc. I opted for overriding through header files, as I believe this is the most reliable and controllable way to override calls. The flags are quite simple for this:

C*FLAGS="-include mimalloc-override.h"
LDFLAGS="-lmimalloc"

However, Clang failed to compile with errors

/usr/lib/llvm/21/include/llvm/Support/MemAlloc.h:53:18: error: no member named 'mi_realloc' in namespace 'std'; did you mean simply 'mi_realloc'?
   53 |   void *Result = std::realloc(Ptr, Sz);
      |                  ^    ~~~~~~~~~~~~~~~~
/usr/include/mimalloc.h:111:40: note: 'mi_realloc' declared here

It seems that the method of overriding using #define does not account for calls that may use namespaces (or their aliases). For now, one option I see is to add definitions of mi_realloc into the std namespace, creating something like this:

mimalloc-override.h

#include <mimalloc.h>

#define malloc(n)     mi_malloc(n)

#if defined(__cplusplus)
namespace std {
  inline auto mi_malloc(size_t n) { return ::mi_malloc(n); }
}
#endif

out.cpp

#include "mimalloc-override.h"
#include <cstdlib>

int main() {
  auto _ = std::malloc(8);
}

preprocessed_out.cpp

# 2 "./mimalloc-override.h" 2

namespace std {
  inline auto mi_malloc(size_t n) { return ::mi_malloc(n); }
}
# 2 "out.cpp" 2

int main() {
  auto _ = std::mi_malloc(8);
}

Thank you for your time and for the great project! :)

Ngôn ngữ chính
C
Star
13.6k
Fork
1.2k
Merge trung bình
18 giờ 12 phút
Pull request đã merge (30 ngày)
20

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/mimalloc

Tất cả issue của microsoft/mimalloc

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.