FtM於 2011年12月31日星期*UTC+8下午10時56分53 寫道:
> [xpost comp.lang.c, comp.unix.programmer, comp.os.ms-
> windows.programmer.win32]
> Hello NG,
> I have a very specific question, but first of all I describe you what
> I'm facing as I'm open to alternative general solutions..
> I'm refactoring some old C code that deals with very basilar data
> structures (like lists, priority queues, hashtables and so on) that
> pretends to be very portable. Regarding the memory management, I have
> an internal wrapper that, on *nix and windows systems, maps the malloc/
> calloc/realloc/free functions one-to-one to the default ones, but it's
> possibile to switch to another custom allocator that works pretty much
> as a standard allocator (a big chunk of memory splitted/joined, tought
> for embedded systems).
> I would like to implement my custom allocator for the *nix and windows
> systems too (of course loosing some portability for performance gain),
> and maybe implement something like the C++ allocators to improve the
> memory management of the different algorithms (like rare large
> allocations on vectors / frequent small allocations on lists), but
> I've some questions:
> - In these systems, to have decent performances, I was thinking to
> allocate page-sized (and page-aligned) chunks to have small
> allocations in the same page, but how is it possible to request some
> memory with this characteristics? Will it help the OS memory manager?
> - Does all of this make any sense? 
> Thanks for any suggestion!
> Ciao!
>
> P.S. I don't really have the necessity of a "performance boost", but
> I'm dealing with a general, low-level and already used library, and as
> long as I'm refactoring the code I'd like to do it the better possible
> way
I mean the allocation requests for the heap space can be modeled
as a discrete random process X[n] for n=0,1,2,3... in an application
and so is the free process Y[m] for m=0, 1,2,3 ....
Therefore the amount of memory used can be counted as
for(i=0, s1=0;i<n;i++) s1+=X[i];
for(i=0, s2=0;i<m;i++) s2+=Y[i];
The number s1-s2 is the amount of space used is another random process, too..
The OS part does not know the two random processes better than the programmer
who is the author of the application. Thus a customized heap manager
can beat the OS as proved in the above.