1
0

smalheap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /***
  2. *smalheap.c - small, simple heap manager
  3. *
  4. * Copyright (c) Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *
  9. *******************************************************************************/
  10. #include <stdlib.h>
  11. #include <malloc.h>
  12. #include <windows.h>
  13. #define BYTES_PER_PARA 16
  14. #define DWORDS_PER_PARA 4
  15. #define PARAS_PER_PAGE 256 // tunable value
  16. #define PAGES_PER_GROUP 8 // tunable value
  17. #define GROUPS_PER_REGION 32 // tunable value (max 32)
  18. #define BYTES_PER_PAGE (BYTES_PER_PARA * PARAS_PER_PAGE)
  19. #define BYTES_PER_GROUP (BYTES_PER_PAGE * PAGES_PER_GROUP)
  20. #define BYTES_PER_REGION (BYTES_PER_GROUP * GROUPS_PER_REGION)
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. HANDLE _crtheap;
  25. /*
  26. * Primary heap routines (Initialization, termination, malloc and free).
  27. */
  28. void __cdecl free (
  29. void * pblock
  30. )
  31. {
  32. if ( pblock == NULL )
  33. return;
  34. HeapFree(_crtheap, 0, pblock);
  35. }
  36. int __cdecl _heap_init (
  37. int mtflag
  38. )
  39. {
  40. if ( (_crtheap = HeapCreate( mtflag ? 0 : HEAP_NO_SERIALIZE,
  41. BYTES_PER_PAGE, 0 )) == NULL )
  42. return 0;
  43. return 1;
  44. }
  45. void __cdecl _heap_term (
  46. void
  47. )
  48. {
  49. HeapDestroy( _crtheap );
  50. }
  51. void * __cdecl _nh_malloc (
  52. size_t size,
  53. int nhFlag
  54. )
  55. {
  56. void * retp;
  57. retp = HeapAlloc( _crtheap, 0, size );
  58. /*
  59. * if successful allocation, return pointer to memory
  60. * if new handling turned off altogether, return NULL
  61. */
  62. return retp;
  63. }
  64. void * __cdecl malloc (
  65. size_t size
  66. )
  67. {
  68. return _nh_malloc( size, 0 );
  69. }
  70. /*
  71. * Secondary heap routines.
  72. */
  73. void * __cdecl calloc (
  74. size_t num,
  75. size_t size
  76. )
  77. {
  78. void * retp;
  79. size *= num;
  80. retp = HeapAlloc( _crtheap, HEAP_ZERO_MEMORY, size );
  81. return retp;
  82. /* new handler was successful -- try to allocate again */
  83. }
  84. void * __cdecl _expand (
  85. void * pblock,
  86. size_t newsize
  87. )
  88. {
  89. return HeapReAlloc( _crtheap,
  90. HEAP_REALLOC_IN_PLACE_ONLY,
  91. pblock,
  92. newsize );
  93. }
  94. int __cdecl _heapchk(void)
  95. {
  96. int retcode = _HEAPOK;
  97. if ( !HeapValidate( _crtheap, 0, NULL ) &&
  98. (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) )
  99. retcode = _HEAPBADNODE;
  100. return retcode;
  101. }
  102. int __cdecl _heapmin(void)
  103. {
  104. if ( (HeapCompact( _crtheap, 0 ) == 0) &&
  105. (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) )
  106. return -1;
  107. return 0;
  108. }
  109. size_t __cdecl _msize (
  110. void * pblock
  111. )
  112. {
  113. return (size_t)HeapSize( _crtheap, 0, pblock );
  114. }
  115. void * __cdecl realloc (
  116. void * pblock,
  117. size_t newsize
  118. )
  119. {
  120. void * retp;
  121. /* if pblock is NULL, call malloc */
  122. if ( pblock == (void *) NULL )
  123. return malloc( newsize );
  124. /* if pblock is !NULL and size is 0, call free and return NULL */
  125. if ( newsize == 0 ) {
  126. free( pblock );
  127. return NULL;
  128. }
  129. retp = HeapReAlloc( _crtheap, 0, pblock, newsize );
  130. return retp;
  131. }
  132. #ifdef __cplusplus
  133. }
  134. #endif