www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: TempAlloc review starts now

reply bearophile <bearophileHUGS lycos.com> writes:
Lars T. Kyllingstad:

 Here follows a description of TempAlloc's purpose and functionality.  
 Links to the code and documentation are at the bottom.

I like the contents I am seeing in core.tempalloc. A segmented stack-style allocator is quite useful. In some other situations I have needed a hyerarchical allocator, that allows to allocate from the C/D heap and deallocate whole subtrees at once. Some comments: 1) Regarding the alignedMalloc/alignedFree names: maybe alignedCMalloc/alignedCFree names are better, because they remind both the programmer and the person that reads the code the memory comes from the C heap (I have similar named functions in dlibs1). 2) TempAlloc.slack name: maybe TempAlloc.segmentSlack is better, because it reminds its meaning better. 3) "The memory returned by this function is not scanned for pointers by the garbage collector unless GC.addRange is called." I suggest to add a little in-place example that shows how to do this. 4) Regarding alignedMalloc(size_t size, bool shouldAddRange = false): I suggest to add a compile-time template argument to specify the alignment, that defaults to 16, something like: alignedMalloc(size_t ALIGN=16)(size_t size, bool shouldAddRange = false) This is more future-proof, if future CPUs will need just 8 bytes alignments, of even 32 bytes alignments. 5) Regarding newVoid(): I'd like it to allow to allocate 2D arrays too. 5b) And maybe I'd like to use newVoid() to create a 2D matrix and initialize it all to a fixed value given at runtime, to replace code like: auto M = new int[][](n,m); foreach (row; M) row[] = 10; 6) As an extra function to add to this bunch of functions, I suggest to add a templated function to allocate a 2D matrix carved out of a single zone of memory (from the C or GC heap, or even the stack...). This is not fully safe, but it's handy, because it reduces a little the memory needed to allocate the whole matrix, and the memory of the rows is contigous or almost contigous. The advantage of this function is that the resulting matrix is seen as a standard array of dynamic arrays, usable in many other functions. This needs a corresponding free function. 7) Regarding newStack(): I don't like this name, it doesn' return a stack! I don't know a good name, but I suggest a name that reminds the programmer that this function returns an array. 8) Regarding TempAlloc.malloc/TempAlloc.free: I am not sure, but I'd like those functions to remind the programmer and the person that reads the code that those two functions work on a stack, so they are push-pop pairs. So maybe names like TempAlloc.stackMalloc/TempAlloc.stackFree are better. I am not sure. Bye, bearophile
Jun 06 2011
parent bearophile <bearophileHUGS lycos.com> writes:
 So maybe names like TempAlloc.stackMalloc/TempAlloc.stackFree are better. I am
not sure.

Or pushMalloc/popFree :-) Or pushMem, popMem, etc. Bye, bearophile
Jun 06 2011