-
Notifications
You must be signed in to change notification settings - Fork 27
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
User-defined memory functions #157
Comments
Hello @notcancername, I think anything that adds customizability to concord while also being fully backwards compatible is a win in my book. Not sure if it's in your plans, but to make sure this works well I think we should also implement some custom dynamic memory functions of our own for when concord is built on debug mode (memory tracking purposes). This should give us a good idea if the implementation makes sense.
You might be a little bit, but we'll see how it goes! |
I'm about a day delinquent on this, so, let me compose a long and supposedly eloquent response. I'll do this as a bulleted list:
|
I agree. The system functions installed by
Seems reasonable, I probably won't implement this in this proposal though. It would also require some fairly unclean macro hackery.
I agree.
fair |
The |
tl;dr
user-specified memory functions are hyperbased for a whole host of reasons, we've thoroughly discussed it in the discord, and I will make a PR to implement them Soon™ unless someone objects, in which case we go back to the drawing board.
Status quo
Currently, concord uses the dynamic memory allocation routines provided by the OS. It does not check their return values, even though they can fail. This is probably fine -- not elegant or correct, but perfectly usable.
Proposed change
This backwards-compatible change would introduce a function
ccord_global_init_memory
, which would take as arguments the following five function pointers:void *malloc_fn(size_t length)
, amalloc
equivalent.void *calloc_fn(size_t nmemb, size_t length)
, acalloc
equivalent.void *realloc_fn(void *old_ptr, size_t length)
, arealloc
equivalent.void free_fn(void *ptr)
, afree
equivalent.char *strdup_fn(const char *str)
, astrdup
equivalent.The names are irrelevant, please reply if you wish for them to be changed.
All functions must be thread-safe. They need not be async-signal-safe (Async-signal-safety would be impractical for many implementations).
The functions must behave as follows:
malloc_fn
allocates exactlylength
bytes of memory and returns a pointer to it. It must return a valid pointer or abort execution. The returned pointer must be suitably aligned for any type. Iflength
is0
or exceedsPTRDIFF_MAX
, the behavior is undefined.calloc_fn
allocates memory for exactlylength
items of sizenmemb
and returns a pointer to it. It must return a valid pointer or abort execution. The returned pointer must be suitably aligned for any type. Iflength
ornmemb
are0
, or the value oflength * nmemb
exceedsPTRDIFF_MAX
, or the multiplication overflows, the behavior is undefined.realloc_fn
returns a pointer to a new allocation of lengthlength
for the data inold_ptr
. It must return a valid pointer or abort execution. It need not (but may) return old_ptr. The returned pointer must be suitably aligned for any type. Iflength
is0
or exceedsPTRDIFF_MAX
, the behavior is undefined.free_fn
releases the memory pointed to byptr
. It must succeed or abort execution. Ifptr
is a null pointer, it must return without any operation being performed.strdup_fn
returns a new allocation for the null-terminated string pointed to bystr
. It must return a valid pointer or abort execution. The returned pointer need not be aligned. Ifstr
is a null pointer, or the length of str exceedsPTRDIFF_MAX
, the behavior is undefined.The function
ccord_global_init_memory
would:curl_global_init_mem
with callbacks derived from these function pointers, as specified by libcurl.ccord_global_init
.The implementation of
ccord_global_init
would change as follows:ccord_global_init_memory
with callbacks derived from the system's implementation of these functions.The interface and behavior of
ccord_global_init
would not change. This change is entirely backwards compatible.To prevent decision fatigue and confusion for the library user, a note stating that
ccord_global_init_memory
is reserved for advanced use cases exclusively, and thatccord_global_init
should be used, would be prominently displayed in the documentation.All calls to
malloc
,calloc
,realloc
,free
, andstrdup
would need to be converted to calls to internal functions with identical signatures. No other alterations to the calling code would be required, making porting trivial. These functions would, in debugging builds of concord, assert compliance with the interface specified above, verifying the existing assumption that memory allocation functions cannot fail.Use cases
Additional context
I have sought critique on the discord server. This led to the following reasoning for the choices made:
curl_free
to release memory acquired by libcurl. This is invalid, as curl's memory allocation debugging is implemented on top of the memory allocation functions provided to it.I would happily implement this. I may underestimate the scope of this change, but I do not expect it to be particularly technically challenging, difficult to implement cleanly and elegantly, or labor-intensive.
Thank you for considering this proposal.
The text was updated successfully, but these errors were encountered: