@@ -67,9 +67,9 @@ extern "C" {
6767 * @{
6868 */
6969#if defined(DMOD_MODULE_NAME )
70- # define Dmod_Malloc (Size ) Dmod_MallocEx(Size, Dmod_GetCurrentModuleName ())
71- # define Dmod_Realloc (Ptr , Size ) Dmod_ReallocEx(Ptr, Size, Dmod_GetCurrentModuleName ())
72- # define Dmod_AlignedMalloc (Size , Alignment ) Dmod_AlignedMallocEx(Size, Alignment, Dmod_GetCurrentModuleName (DMOD_MODULE_NAME))
70+ # define Dmod_Malloc (Size ) Dmod_MallocEx(Size, Dmod_GetCurrentAllocatorName ())
71+ # define Dmod_Realloc (Ptr , Size ) Dmod_ReallocEx(Ptr, Size, Dmod_GetCurrentAllocatorName ())
72+ # define Dmod_AlignedMalloc (Size , Alignment ) Dmod_AlignedMallocEx(Size, Alignment, Dmod_GetCurrentAllocatorName (DMOD_MODULE_NAME))
7373# define Dmod_Free (Ptr ) Dmod_FreeEx(Ptr, false)
7474#else
7575 DMOD_BUILTIN_API (Dmod , 1.0 , void * , _Malloc , ( size_t Size ) );
@@ -199,13 +199,36 @@ DMOD_BUILTIN_API(Dmod, 1.0, size_t, _ReadKernel, ( void* Buffer, size_t Size )
199199# define Dmod_GetCurrentModuleName () Dmod_GetCurrentModuleNameEx(NULL)
200200#endif
201201
202+ /**
203+ * @brief Name used to attribute heap allocations (Dmod_Malloc/Dmod_MallocEx/...) to their owner.
204+ *
205+ * This is deliberately a *separate* identity from Dmod_GetCurrentModuleName(): the module name
206+ * is not unique when the same module is loaded more than once at the same time (e.g. a shell
207+ * spawning another instance of itself in the background) - two independent Dmod_Context_t's
208+ * end up sharing one name. Allocation tracking (dmheap and similar) keys everything off this
209+ * string, including bulk-freeing all of a module's memory on unload (Dmod_FreeModule) - if two
210+ * live instances shared that key, unloading one would free memory the other is still using.
211+ *
212+ * The default (weak) implementation just falls back to the module name, same as
213+ * Dmod_GetCurrentModuleName() - a platform without real process tracking has no way to tell two
214+ * instances apart anyway. A real implementation (see the dmosi glue layer) can return something
215+ * that also incorporates the current process's identity (e.g. name + PID), which is unique per
216+ * spawned instance even when the module name repeats.
217+ */
218+ #ifdef DMOD_MODULE_NAME
219+ # define Dmod_GetCurrentAllocatorName () Dmod_GetCurrentAllocatorNameEx(DMOD_MODULE_NAME)
220+ #else
221+ # define Dmod_GetCurrentAllocatorName () Dmod_GetCurrentAllocatorNameEx(NULL)
222+ #endif
223+
202224DMOD_BUILTIN_API (Dmod , 1.0 , const char * , _GetEnv , ( const char * Name ) );
203225DMOD_BUILTIN_API (Dmod , 1.0 , int , _SetEnv , ( const char * Name , const char * Value , int Overwrite ) );
204226DMOD_BUILTIN_API (Dmod , 1.0 , int , _Unsetenv , ( const char * Name ) );
205227DMOD_BUILTIN_API (Dmod , 1.0 , const char * , _GetNextEnvName , ( const char * Last ) );
206228DMOD_BUILTIN_API (Dmod , 1.0 , int , _EnvCtx_Push , ( void ) );
207229DMOD_BUILTIN_API (Dmod , 1.0 , int , _EnvCtx_Pop , ( void ) );
208230DMOD_BUILTIN_API (Dmod , 1.0 , const char * , _GetCurrentModuleNameEx , ( const char * Default ) );
231+ DMOD_BUILTIN_API (Dmod , 1.0 , const char * , _GetCurrentAllocatorNameEx , ( const char * Default ) );
209232
210233//! @}
211234
0 commit comments