//--------------------------------------------------------------------------------- // // Little Color Management System // Copyright (c) 1998-2023 Marti Maria Saguer // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the Software // is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //--------------------------------------------------------------------------------- #include "lcms2_internal.h" #include "core/fxcrt/fx_memory.h" #include "core/fxcrt/fx_system.h" // This function is here to help applications to prevent mixing lcms versions on header and shared objects. int CMSEXPORT cmsGetEncodedCMMversion(void) { … } // I am so tired about incompatibilities on those functions that here are some replacements // that hopefully would be fully portable. // compare two strings ignoring case int CMSEXPORT cmsstrcasecmp(const char* s1, const char* s2) { … } // long int because C99 specifies ftell in such way (7.19.9.2) long int CMSEXPORT cmsfilelength(FILE* f) { … } cmsBool _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin) { … } // Generic allocate void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size) { … } // Generic allocate & zero void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size) { … } // Generic calloc void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size) { … } // Generic reallocate void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size) { … } // Generic free memory void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr) { … } // Generic block duplication void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size) { … } _cmsMemPluginChunkType _cmsMemPluginChunk = …; void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src) { … } void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr) { … } // ******************************************************************************************** // Sub allocation takes care of many pointers of small size. The memory allocated in // this way have be freed at once. Next function allocates a single chunk for linked list // I prefer this method over realloc due to the big impact on xput realloc may have if // memory is being swapped to disk. This approach is safer (although that may not be true on all platforms) static _cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial) { … } // The suballocated is nothing but a pointer to the first element in the list. We also keep // the thread ID in this structure. _cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial) { … } // Get rid of whole linked list void _cmsSubAllocDestroy(_cmsSubAllocator* sub) { … } // Get a pointer to small memory block. void* _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size) { … } // Duplicate in pool void* _cmsSubAllocDup(_cmsSubAllocator* s, const void *ptr, cmsUInt32Number size) { … } // Error logging ****************************************************************** // There is no error handling at all. When a function fails, it returns proper value. // For example, all create functions does return NULL on failure. Other return FALSE // It may be interesting, for the developer, to know why the function is failing. // for that reason, lcms2 does offer a logging function. This function does receive // a ENGLISH string with some clues on what is going wrong. You can show this // info to the end user, or just create some sort of log. // The logging function should NOT terminate the program, as this obviously can leave // resources. It is the programmer's responsibility to check each function return code // to make sure it didn't fail. // Error messages are limited to MAX_ERROR_MESSAGE_LEN #define MAX_ERROR_MESSAGE_LEN … // --------------------------------------------------------------------------------------------------------- // This is our default log error static void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text); // Context0 storage, which is global _cmsLogErrorChunkType _cmsLogErrorChunk = …; // Allocates and inits error logger container for a given context. If src is NULL, only initializes the value // to the default. Otherwise, it duplicates the value. The interface is standard across all context clients void _cmsAllocLogErrorChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src) { … } // The default error logger does nothing. static void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text) { … } // Change log error, context based void CMSEXPORT cmsSetLogErrorHandlerTHR(cmsContext ContextID, cmsLogErrorHandlerFunction Fn) { … } // Change log error, legacy void CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn) { … } // Log an error // ErrorText is a text holding an english description of error. void CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...) { … } // Utility function to print signatures void _cmsTagSignature2String(char String[5], cmsTagSignature sig) { … } //-------------------------------------------------------------------------------------------------- static void* defMtxCreate(cmsContext id) { … } static void defMtxDestroy(cmsContext id, void* mtx) { … } static cmsBool defMtxLock(cmsContext id, void* mtx) { … } static void defMtxUnlock(cmsContext id, void* mtx) { … } // Pointers to memory manager functions in Context0 _cmsMutexPluginChunkType _cmsMutexPluginChunk = …; // Allocate and init mutex container. void _cmsAllocMutexPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src) { … } // Register new ways to transform cmsBool _cmsRegisterMutexPlugin(cmsContext ContextID, cmsPluginBase* Data) { … } // Generic Mutex fns void* CMSEXPORT _cmsCreateMutex(cmsContext ContextID) { … } void CMSEXPORT _cmsDestroyMutex(cmsContext ContextID, void* mtx) { … } cmsBool CMSEXPORT _cmsLockMutex(cmsContext ContextID, void* mtx) { … } void CMSEXPORT _cmsUnlockMutex(cmsContext ContextID, void* mtx) { … } // The global Context0 storage for parallelization plug-in _cmsParallelizationPluginChunkType _cmsParallelizationPluginChunk = …; // Allocate parallelization container. void _cmsAllocParallelizationPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src) { … } // Register parallel processing cmsBool _cmsRegisterParallelizationPlugin(cmsContext ContextID, cmsPluginBase* Data) { … }