Last active
January 1, 2024 06:08
-
-
Save dlOuOlb/835050aa71cec2367a188fd40f788873 to your computer and use it in GitHub Desktop.
Measuring a maximum memory block size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if !__STDC__ | |
# error "This code is written in standard C90." | |
#else | |
# include <stdio.h> /* printf|size_t */ | |
# include <stdlib.h> /* EXIT_(FAILURE|SUCCESS)|free|realloc|size_t */ | |
static size_t xSize_( void ) | |
{ | |
auto struct { size_t Size; void *Void; } New, Old = { 0U, 0 }; | |
auto size_t Mask = ~Old.Size; | |
Mask^= Mask >> 1U; | |
do Old = 0 == ( New.Void = realloc( Old.Void, New.Size = Old.Size | Mask ) )? Old: New; | |
while( Mask>>= 1U ); | |
switch( Old.Size ) { default: free( Old.Void ); case 0U: return Old.Size; } | |
} | |
extern signed int main( void ) { return 0 <= printf( "%#zx\n", xSize_( ) )? EXIT_SUCCESS: EXIT_FAILURE; } | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment