Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions conf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
#define USE_MMAP 0
#define HAVE_MUNMAP 0

/*
* On Windows, use VirtualAlloc() to allocate memory and use VirtualFree
* to deallocate it. To be absolutely sure, try to find it in one of the
* two sets of header files.
*/
#define HAVE_DECL_VIRTUALALLOC 0
#define HAVE_DECL_VIRTUALFREE 0
#define HAVE_VIRTUALALLOC_MEMORYAPI_H 0
#define HAVE_VIRTUALALLOC_WINDOWS_H 0

/*
* This is the basic block size in bits. If possible, the configure
* script will set this to be the value returned by the getpagesize()
Expand Down
26 changes: 25 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,31 @@ AC_MSG_NOTICE([important functionality])

AC_CHECK_FUNCS(mmap munmap sbrk)

if test "x$ac_cv_func_mmap" != "xyes" && test "x$ac_cv_func_sbrk" != "xyes"; then
ac_cv_target_win32=no
AC_MSG_CHECKING([for VirtualAlloc in <windows.h> (Windows)])
AC_CHECK_DECLS(
[[VirtualAlloc(LPVOID, SIZE_T, DWORD, DWORD)],
[VirtualFree(LPVOID, SIZE_T, DWORD)]],
[ ac_cv_target_win32=yes; AC_DEFINE(HAVE_VIRTUALALLOC_WINDOWS_H) ],
[
AC_MSG_CHECKING([for VirtualAlloc in <windows.h>, <memoryapi.h> (Windows)])
AC_CHECK_DECLS(
[[VirtualAlloc(LPVOID, SIZE_T, DWORD, DWORD)],
[VirtualFree(LPVOID, SIZE_T, DWORD)]],
[ ac_cv_target_win32=yes; AC_DEFINE(HAVE_VIRTUALALLOC_MEMORYAPI_H) ],
[ ac_cv_target_win32=no ],
[[#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <memoryapi.h>]]
)
],
[[#define WIN32_LEAN_AND_MEAN
#include <windows.h>]]
)

if test "x$ac_cv_func_mmap" != "xyes" && test "x$ac_cv_func_sbrk" != "xyes" && \
test "x$ac_cv_target_win32" != "xyes";
then
AC_MSG_WARN()
AC_MSG_WARN(no mmap nor sbrk function. See INTERNAL_MEMORY_SPACE in settings.dist.)
AC_MSG_WARN()
Expand Down
2 changes: 1 addition & 1 deletion dmalloc_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#define INTER_CHAR 'i'
#define DEFAULT_ITERATIONS 10000
#define MAX_POINTERS 1024
#if HAVE_SBRK == 0 && HAVE_MMAP == 0
#if HAVE_SBRK == 0 && HAVE_MMAP == 0 && (HAVE_DECL_VIRTUALALLOC == 0 || HAVE_DECL_VIRTUALFREE == 0)
/* if we have a small memory area then just take 1/10 of the internal space */
#define MAX_ALLOC (INTERNAL_MEMORY_SPACE / 10)
#else
Expand Down
19 changes: 19 additions & 0 deletions heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@
* heap as well as reporting the current position of the heap.
*/

#include "conf.h"

#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if HAVE_SYS_MMAN_H
# include <sys/mman.h> /* for mmap stuff */
#endif
#if HAVE_VIRTUALALLOC_WINDOWS_H
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#elif HAVE_VIRTUALALLOC_MEMORYAPI_H
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <memoryapi.h>
#endif

#define DMALLOC_DISABLE

Expand Down Expand Up @@ -95,6 +105,9 @@ static void *heap_extend(const int incr)
#else
#if HAVE_SBRK
ret = sbrk(incr);
#elif HAVE_DECL_VIRTUALALLOC && HAVE_DECL_VIRTUALFREE
ret = VirtualAlloc(NULL, incr, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!ret) ret = SBRK_ERROR;
#endif /* if HAVE_SBRK */
#endif /* if not HAVE_MMAP && USE_MMAP */
#endif /* if not INTERNAL_MEMORY_SPACE */
Expand Down Expand Up @@ -149,6 +162,12 @@ static void heap_release(void *addr, const int size)
dmalloc_message("munmap failed to release heap memory %p, size %d",
addr, size);
}
#elif HAVE_DECL_VIRTUALALLOC && HAVE_DECL_VIRTUALFREE
/* NB: Assuming that heap_release is always called with the same size as
** heap_extend. WinAPI requires the second parameter to VirtualFree
** to be 0 to avoid reserving and then abandoning blocks of uncommitted
** address space. */
VirtualFree(addr, 0, MEM_RELEASE);
#else
/* no-op */
#endif /* if not HAVE_MMAP && USE_MMAP */
Expand Down
2 changes: 1 addition & 1 deletion settings.dist
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
* HAVE_MMAP are 0. Please send me email with any problems or
* comments on this feature.
*/
#if HAVE_SBRK == 0 && HAVE_MMAP == 0
#if HAVE_SBRK == 0 && HAVE_MMAP == 0 && (HAVE_DECL_VIRTUALALLOC == 0 || HAVE_DECL_VIRTUALFREE == 0)
#define INTERNAL_MEMORY_SPACE (1024 * 1024)
#endif

Expand Down