Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
3.9 KiB

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