Best practice for overriding a library function? [closed]

I have the following situation.
I have a root project which contains a subproject.
This subproject is intended as a library, which you can either build stand-alone or it can be build alongside another project.

In the library I have to allocate memory at several locations.
I wish however, to be able to override this functionality.

For example, in the library I have a call to malloc(ptr).
And I wish to be able to replace these calls by a library consumer (in this case the rootproject).
The rootproject in this case should override/replace the calls in the library without the library being dependent on the rootproject.

For extra context I’m using this MACRO way because it is more performant than using function pointers for example, I am interested in high performance allocations in this case.

My question boils down to this:
What is the best practice for extending libraries like this?

I’ve found several related questions, but I am specifically interested in the best practice for these things; that it’s possible I am already aware of.

What I’ve so far to achieve what I want is the following.

  1. I’ve defined a header with macro’s replacing the allocations like so: HDR_MALLOC(ptr) malloc(ptr).
    Every malloc(ptr) in the library now uses HDR_MALLOC(ptr) instead.
    When the library is built standalone it simply uses this definition.
  2. In the rootproject I’ve also defined a header with the macro HDR_MALLOC(ptr)

Now when building the library this works correctly, the rootproject correctly “overrides” the library’s HDR_MALLOC.
When rootproject uses the library, the library calls the rootproject’s implementation instead of it’s own.

1

In my experience, most libraries seem to reach for a macro-based system of configuration, with the ability for consumers to override by defining said macro themselves. The code pattern tends to be:

#if defined(LIB_CONFIG_HEADER)
  #include LIB_CONFIG_HEADER
#else
  // Include some reasonable defaults
  #include <stdlib.h>
  ...
#endif
 
#if !defined(LIB_ALLOC)
  #define LIB_ALLOC(_size) calloc(1u, _size)
#endif

This has the bonus flexibility of allowing otherwise invalid expressions like

#define LIB_ALLOC(_size) ...
#define LIB_ALLOC_T(_type) ((_type) *)LIB_ALLOC(sizeof(_type))

Since these are handled by the preprocessor, it also tends to be a useful pattern for embedded systems where a full or compliant standard library is not available.

Systems which require a more complex set of injectable behaviours tend to lean towards the function-pointer / vtable approach, which often also includes allocation methods. While this approach does involve some amount of indirection, in practice it is not a large amount of overhead.

There is also a third option involving weak linkage, but I would not recommend this.

Use function pointers, eg.

// lib.h
void libfoo(int one, double two, struct bar *three, void *(*alocfx)(size_t));

and, in the main project, call the library function

#include <stdlib.h>
#include "lib.h"
int main(void) {
    libfoo(1, 3.14159, NULL, malloc);
}

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật