From: Bernhard W. <be...@bl...> - 2005-06-27 22:58:13
|
Hi all Ok, now talking a bit to myself... > Hi all > > To save time I ask the Windows wizards on the list before trying to > figure this out myself;-) > > Today I tried to track down some problems regarding the toolchain within > Windows (trackgen, accc). Now I hit the first time the code which > redefines malloc, realloc, free, and some more (I have not been aware of > that before). Example (shortened): > > tgf.h: > #ifdef WIN32 > #define malloc _tgf_win_malloc > > tgf.cpp: > void *_tgf_win_malloc(size_t size) > { > char * p = (char*)GlobalAlloc(GMEM_FIXED, size + 4 ); > > *(int*)( p ) = size; Ok, looks not too bad for me, but I still do not know why not just to use malloc and co from the library... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalfree.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalalloc.asp > > return p + 4; > } > > So I have some questions regarding this: > - Is it still necessary or is it a 16bit Windows artifact? > - Why is it necessary (I would assume that even if you maintain a > pointer into "dll" data the data is within the process virtual address > space, so it is always at the same address for the same process. Or not, > hmmm... confused)? > - What is this +4 on allocation and -4 on free good for? The +4, -4 is currently to store the size of the block, and the implementation assumes that the handle matches the address. I think that the _tgf_win_realloc function does not implement the usual realloc, look from the man page: realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated mem ory will be uninitialized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. If realloc() fails the original block is left untouched - it is not freed or moved. Or from MS: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_realloc.asp The actual code: void *_tgf_win_realloc(void * memblock, size_t size) { void * p = _tgf_win_malloc(size); if (!p) { _tgf_win_free(memblock); Hmm, frees the wrong block if memory allocation failed, does it? If I get it right it should just free the original here if the given size was 0 "If realloc() fails the original block is left untouched - it is not freed or moved." return NULL; } else { memcpy(p, memblock, min(*(int*)((char*)memblock-4), (int)size) ); Hmm, does not free the original block... return p; } } This stuff looks very suspect to me, any thoughts (for me it simply does not match the spec from the man page)? I will try to switch to the c library functions and see what happens, but I think this code has some reason, but what is it good for? Bye, Bernhard. -- Visit my homepage http://www.berniw.org Official TORCS racing: The TORCS Racing Board, http://www.berniw.org/trb |