Ben Campbell wrote:
SDL_GetTicks() is justified because there isn’t a standard
cross-platform way to easily get millisecond-resolution timestamps.
But the same applies to time(). It has only second-resolution,
and higher precision functions are nonportable.
To be quite honest, I thought it may possibly be useful to have
all these basical functions covered in SDL so that they always
behave the same way.
Even such basic functions as malloc() have side effects on Win32,
and I believe others have other quirks on various platforms.
How about compiling us a list of all the quirks you know of?
Ok, I’ll try. Generally I feel the main problem is that MS libc is much
less needed for native applications and thus much less supported.
MS libc supports only very small and old set of ANSI functions.
I think it would be much safer to use the Win32 API, because it is used
by all applications, and so the bugs get found and fixed.
Moreover, I think there are some speed/memory penalties when using
the libc (e.g. with threads – when using libc and Win32 Thread
functions, memory leaks occur).
Just for a quick example, when talking about time related functions:
MS libc supports only the old ANSI functions, of which many, namely
gmtime(), localtime(), asctime(), ctime(), are thread unsafe.
There is no portable function for setting the time zone.
eg:
What are the problems with time()? How would you implement SDL_time()?
For Win32, something like
Uint64 SDL_GetCurrentTime()
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
return (Uint64) ft.dwHighDateTime << 32 + (Uint64) ft.dwLowDateTime;
}
You mention win32 malloc() - what problems do you know about? How would
you propose that an SDL_malloc() should work?
The MS libc malloc(0) returns a valid pointer to an empty region,
I think glibc returns NULL in this case.
Also, I’m almost sure malloc() call is less effective than native
Win32 call, which allows heap management etc. Maybe it is not
too serious in this case, but e.g. with file access, it would
be much worse.
The most basic SDL_malloc() would be (for Win32):
void *SDL_malloc(Uint32 size)
{
return HeapAlloc(GetProcessHeap(), 0, size);
}
However, we could use a different heap, or have some debugging
facilities similar to glibc (optional checks for pointer validity,
autodetecting memory leaks etc.).
E.g. a very basic free() with pointer validity test:
int SDL_free(void *mem)
{
if (mem == NULL) return 1;
if (IsBadWritePtr(mem, 1) || !HeapFree(GetProcessHeap(), 0, mem))
{
SDL_SetError(“Attempt to free an invalid memory block”);
return 0;
}
return 1;
}
Have a nice day, and lotsa sunshine!
Yours sincerely,
Jiri "BlueBear" Dluhos--
====================================================================
It is really quite easy to imagine a square yard of multidimensional
space, provided that you have seven brains.
Prof. Abdullah Nightingale
(Walter Moers: 13 1/2 lives of Captain Bluebear)
Jiri “BlueBear” Dluhos
Software Developer, HUMUSOFT s.r.o. (http://www.humusoft.com)
dluhos at humusoft dot com (office)
dluhosj at centrum dot cz (home)