-
Notifications
You must be signed in to change notification settings - Fork 260
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
Application-supplied memory allocators #335
Comments
See also https://bugs.php.net/bug.php?id=74808. |
bug isn't public we already have
|
What about thread safety? I've found that using that using a |
what about thread safety are you worried about ? |
I'm worried about changing the allocation methods during application runtime, but I think is much clever to mention that is unwise to change the methods that often. |
The report doesn't really add new info. :)
I think that would be a really bad idea anyway, and should not be allowed (at least by documentation).
|
I was using this struct for my prototypes. typedef struct gdallochelpers
{
void *(*callocf)(size_t nmemb, size_t size);
void *(*mallocf)(size_t size);
void *(*reallocf)(void *ptr, size_t size);
void (*freef)(void *ptr);
} gdAllocHelpers; If a |
An API could be described like this: gdSetMemoryCallocMethod(void *(*callocf)(size_t nmemb, size_t size));
gdSetMemoryMallocMethod(void *(*mallocf)(size_t size));
gdSetMemoryReallocMethod(void *(*reallocf)(void *ptr, size_t size));
gdSetMemoryFreeMethod(void (*freef)(void *ptr));
gdSetMemoryAllocationMethods(gdAllocHelper helper); Error handling may be optional if desired (i.e. returning something else instead of |
i agree switching memory allocators while other threads are actively using gd seems like a terrible idea and not something we can reasonably protect against. imagine:
which allocator should T1 use ? we'd have to pin the allocator information to the image state itself, but even then, you'd be left with T1 accessing the old allocator long in the future which might not be expected. the atomicity of updates for the hooks/methods wouldn't change this problem. we should just document it as "pick an allocator before using gd and leave it at that". having a struct wouldn't help with atomic updates either. doing a memcpy of the struct is not guaranteed ... we'd have to maintain 2 structs and add memory barriers everywhere which sounds like overkill. so the only thing using a struct gains us is having a single API for users to call vs ~5 APIs. the reason i didn't suggest exporting a gdAllocHelpers foo = {
.malloc = mymalloc,
.calloc = mycalloc,
.free = myfree,
};
gdSetMemoryAllocationMethods(&foo);
...
gdSetMemoryMallocMethod(mymalloc);
gdSetMemoryCallocMethod(mycalloc);
gdSetMemoryFreeMethod(myfree); |
The purpose of the struct was more of a holder for the methods, but any other solution could be used instead. The gdSetMemoryAllocationMethods(void *(*callocf)(size_t nmemb, size_t size), void *(*mallocf)(size_t size), void *(*reallocf)(void *ptr, size_t size), void (*freef)(void *ptr)); As of thread safety, all the described methods must be documented to mention that they are not thread-safe and that they should not be called during operations because we cannot guarantee both thread safety (in an easy way) and that there wouldn't be any issues by switching allocators oftentimes. |
wrt ABI compatibility, a func that takes a struct of fields vs a func that takes all the fields directly is the same. if we want to add/remove/update any helpers, then we need to create a new func. actually, a single func-with-many-args is kind of worse than if it just took a struct. at least with a struct we could make the first field a "version" and figure out the layout from there. a func we'd have to create a new symbol, or go the va_args route, and that'd be even more confusing for people. |
if people really really want a single method that takes a (versioned) struct, i won't fight over it more. but a func that takes multiple arguments is out of the question. |
@vapier My only worry is about three functions and users start to use incompatible *alloc and free by mistake, setting them all at once can avoid this, hopefully :) |
@remicollet that is, afair, the last thing that could prevent to use only external GD with PHP thoughts? |
Yes, if this is available, PHP could drop bundled libgd. :) |
IMHO this have to be in small steps
BTW, I have some bad experience with changing memory allocator (can't remember in which ext), which may raise strange issues if the same library is pulled by various projects in the same process, mixing different allocator. But I'm not aware of anything else using libgd (in the PHP stack) |
@remi Collet ***@***.***> yes, me too.
That's why I was suggesting to only being able to do it once in a library
init call. Something like what Freetype or other do, that can happen only
once per process. thoughts?
…On Mon, Sep 13, 2021, 7:17 PM Remi Collet ***@***.***> wrote:
@remicollet <https://github.com/remicollet> that is, afair, the last
thing that could prevent to use only external GD with PHP
IMHO this have to be in small steps
- implement this feature
- sync ext/gd/libgd with upstream libgd so everyone use the same code
(8.2 ?)
- later drop support for bundled library (8.3 ? 8.4 ?)
BTW, I have some bad experience with changing memory allocator (can't
remember in which ext), which may raise strange issues if the same library
is pulled by various projects in the same process, mixing different
allocator. But I'm not aware of anything else using libgd (in the PHP stack)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#335 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACE6KE4BYQ6GUEUC3DXJCLUBXTXFANCNFSM4CS6VR6Q>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
I think we should provide a way for libgd clients to use their own memory allocators. That would be very useful for PHP at least, where there's currently a dichotomy between the bundled and external libgd, because the bundled libgd defines macros to replace gdMalloc() and friends with Zend memory manager functions, but obviously that doesn't work for system libgd, so PHP's memory_limit is not obeyed in the latter case.
I haven't though about the API yet – suggestions are welcome.
The text was updated successfully, but these errors were encountered: