Memory allocation probe with some rails and meaningful return value.

Format
C
Post date
2017-02-17 14:00
Publication Period
Unlimited
  1. /* Overdone Q&D test of max allocation
  2. // with some rails and meaningful return value
  3. // by Joel Matthew Rees, 17 Feb 2017
  4. //
  5. // Copyright 2017 Joel Matthew Rees
  6. //
  7. // Permission is hereby granted, free of charge, to any person
  8. // obtaining a copy of this software and associated documentation
  9. // files (the "Software"), to deal in the Software without
  10. // restriction, including without limitation the rights to use,
  11. // copy, modify, merge, publish, distribute, sublicense,
  12. // and/or sell copies of the Software, and to permit persons
  13. // to whom the Software is furnished to do so, subject to the
  14. // following conditions:
  15. //
  16. // The above copyright notice and this permission notice
  17. // shall be included in all copies or substantial portions
  18. // of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. // OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. // (MIT template license.)
  30. */
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. int main( int argc, char *argv[] )
  34. {
  35. unsigned long gig_count = 4;
  36. char * block = NULL;
  37. unsigned long long address = (unsigned long long) NULL; /* Protect us from libraries that sniff freed pointers. */
  38. int argx;
  39. int quiet = 0;
  40. for ( argx = 1; argx < argc; ++argx )
  41. { char * marker = argv[ argx ];
  42. if ( ( marker[ 0 ] == '-' ) && ( marker[ 1 ] == 'q' ) )
  43. { quiet = 1;
  44. }
  45. else
  46. { unsigned long count = strtoul( argv[ argx ], &marker, 0 );
  47. if ( marker > argv[ argx ] )
  48. { gig_count = count;
  49. }
  50. else
  51. { if ( !quiet )
  52. {
  53. printf( "usage: %s { size-in-GiB } { -q(uiet) }\n", argv[ 0 ] );
  54. printf( "\tReturns EXIT_FAILURE if malloc fails,\n\t\totherwise EXIT_SUCCESS.\n" );
  55. {
  56. size_t size_t_max = 1;
  57. size_t next;
  58. unsigned long gigs = 0;
  59. while ( ( next = size_t_max << 1 ) > 0 )
  60. { size_t_max = next ;
  61. }
  62. gigs = size_t_max / 0x100000000;
  63. printf( "\tMax size_t is %lu GiB or so.\n", gigs << 1 );
  64. }
  65. }
  66. return EXIT_FAILURE;
  67. }
  68. }
  69. }
  70. address = (unsigned long long) ( block = malloc( (size_t) gig_count * 0x100000000LL ) );
  71. free( block ); /* Just in case we allocated everything available. */
  72. if ( !quiet )
  73. { printf( "Attempt to malloc %lu GiB yields block address 0x%08llx: %s\n",
  74. gig_count, address, ( address == (unsigned long long) NULL ) ? "FAILED" : "SUCCEEDED" );
  75. }
  76. return ( address == (unsigned long long) NULL ) ? EXIT_FAILURE : EXIT_SUCCESS;
  77. }
다운로드 Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text