Win32 Memory Allocation
-
Hi all, I'm writing my own string library that needs to be super-fast (much faster than CString). I am well on the way but need an efficient way of allocating memory so that I don't get fragmentation when concatening strings etc. I have thought of a way for an allocation routine to do this whereby memory is always allocated as a base 2 block with the minimum being 8 bytes. This will allow the string to grow in size with little fragmentation on reallocation (and indeed less use of realloc). It will of course, always consume slightly more memory than is required to hold the string. All I need is a method for grabbing the highest order bit that describes the length of the string and bit shift it to the left e.g. say the string was "LENGTH" it is 6 chars long so the highest bit is (0100 - since 6 is 0110 in binary) and bit shift it to 1000. I would then malloc 8 bytes which is enough to hold the 6 char string and its 1 char null terminator. However, I'm stuck on finding the quickest method of getting the highest ranking bit. I know I can do it by testing 0100 & 1000, 0100 & 0100 etc. till I find a match, but I was just wondering if there is a better way? Any comments or suggestions on how to make a better string library would also be grateful. Alan. P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too :) "When I left you I was but the learner, now I am the master" - Darth Vader
-
Hi all, I'm writing my own string library that needs to be super-fast (much faster than CString). I am well on the way but need an efficient way of allocating memory so that I don't get fragmentation when concatening strings etc. I have thought of a way for an allocation routine to do this whereby memory is always allocated as a base 2 block with the minimum being 8 bytes. This will allow the string to grow in size with little fragmentation on reallocation (and indeed less use of realloc). It will of course, always consume slightly more memory than is required to hold the string. All I need is a method for grabbing the highest order bit that describes the length of the string and bit shift it to the left e.g. say the string was "LENGTH" it is 6 chars long so the highest bit is (0100 - since 6 is 0110 in binary) and bit shift it to 1000. I would then malloc 8 bytes which is enough to hold the 6 char string and its 1 char null terminator. However, I'm stuck on finding the quickest method of getting the highest ranking bit. I know I can do it by testing 0100 & 1000, 0100 & 0100 etc. till I find a match, but I was just wondering if there is a better way? Any comments or suggestions on how to make a better string library would also be grateful. Alan. P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too :) "When I left you I was but the learner, now I am the master" - Darth Vader
-
Hello Joaquin, its been a while...:). Thanks for the URL, you missed out one directory shift by mistake though it should have been ftp://cap.connx.com/pub/bitcount/LOGS/LOG2.C[^] :)? Many Thanks, Alan. "When I left you I was but the learner, now I am the master" - Darth Vader
-
Hello Joaquin, its been a while...:). Thanks for the URL, you missed out one directory shift by mistake though it should have been ftp://cap.connx.com/pub/bitcount/LOGS/LOG2.C[^] :)? Many Thanks, Alan. "When I left you I was but the learner, now I am the master" - Darth Vader
Ummm. Both URLs point to different files. Yours is based on conversion to
double
, mine on binary search. Try again, I've just tested the URL and is working OK. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Hi all, I'm writing my own string library that needs to be super-fast (much faster than CString). I am well on the way but need an efficient way of allocating memory so that I don't get fragmentation when concatening strings etc. I have thought of a way for an allocation routine to do this whereby memory is always allocated as a base 2 block with the minimum being 8 bytes. This will allow the string to grow in size with little fragmentation on reallocation (and indeed less use of realloc). It will of course, always consume slightly more memory than is required to hold the string. All I need is a method for grabbing the highest order bit that describes the length of the string and bit shift it to the left e.g. say the string was "LENGTH" it is 6 chars long so the highest bit is (0100 - since 6 is 0110 in binary) and bit shift it to 1000. I would then malloc 8 bytes which is enough to hold the 6 char string and its 1 char null terminator. However, I'm stuck on finding the quickest method of getting the highest ranking bit. I know I can do it by testing 0100 & 1000, 0100 & 0100 etc. till I find a match, but I was just wondering if there is a better way? Any comments or suggestions on how to make a better string library would also be grateful. Alan. P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too :) "When I left you I was but the learner, now I am the master" - Darth Vader
P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too This can be done by tweaking a file called
autoexp.dat
living somewhere inside the installation dir of Visual Studio. Check Oskar Wieland's article CStringW and CStringA [^] for an example. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
P.S. Also I notice that CString displays the actual string in the variable pane of the debug window, whereas mine displays {...} and you have to go to the next level to check what the string is, any ideas on how I can get it to display {"LENGTH"} would be brilliant too This can be done by tweaking a file called
autoexp.dat
living somewhere inside the installation dir of Visual Studio. Check Oskar Wieland's article CStringW and CStringA [^] for an example. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloThanks very much Joaquin, this code will cut down my development time immensely! Thankyou. I am looking into the autoexp.dat file thing so hopefully I'll be able to see whats in the string a bit easier too. Many Thanks (again) Alan. "When I left you I was but the learner, now I am the master" - Darth Vader