minor memory management updates/cleanups

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@30760 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2010-06-16 08:45:49 +00:00
parent 0fafb192d2
commit 87f719480f
5 changed files with 24 additions and 13 deletions

View file

@ -388,6 +388,9 @@
/* Define to 1 if you have the <poll.h> header file. */
#undef HAVE_POLL_H
/* Define to 1 if you have the `posix_memalign' function. */
#undef HAVE_POSIX_MEMALIGN
/* Define if system supports the /proc filesystem */
#undef HAVE_PROCFS

View file

@ -87,7 +87,7 @@
NSLog(@"Failed to protect memory as writable: %@", [NSError _last]);
}
#endif
free(buffer);
NSDeallocateMemoryPages(buffer, NSPageSize());
#endif
}
[super dealloc];
@ -95,6 +95,8 @@
- (id) initWithSize: (NSUInteger)_size
{
NSAssert(_size > 0, @"Tried to allocate zero length buffer.");
NSAssert(_size <= NSPageSize(), @"Tried to allocate more than one page.");
#if defined(HAVE_MMAP)
#if defined(HAVE_MPROTECT)
/* We have mprotect, so we create memory as writable and change it to
@ -114,9 +116,7 @@
#elif defined(__MINGW__)
buffer = VirtualAlloc(NULL, _size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
#else
// buffer = malloc(_size);
NSAssert(_size < NSPageSize(), @"Tried to allocate more than one page.");
buffer = valloc(NSPageSize());
buffer = NSAllocateMemoryPages(NSPageSize());
#endif /* HAVE_MMAP */
if (buffer == (void*)0)

View file

@ -156,38 +156,45 @@ NSRealMemoryAvailable ()
/**
* Allocate memory for this process and return a pointer to it (or a null
* pointer on failure).
* pointer on failure). The allocated memory is page aligned and the
* actual size of memory allocated is a multiple of the page size.
*/
void *
NSAllocateMemoryPages (NSUInteger bytes)
{
NSUInteger size = NSRoundUpToMultipleOfPageSize (bytes);
void *where;
#if __mach__
kern_return_t r;
r = vm_allocate (mach_task_self(), &where, (vm_size_t) bytes, 1);
r = vm_allocate (mach_task_self(), &where, (vm_size_t) size, 1);
if (r != KERN_SUCCESS)
return NULL;
return where;
#elif HAVE_POSIX_MEMALIGN
if (posix_memalign(&where, NSPageSize(), size) != 0)
return NULL;
#else
where = objc_valloc (bytes);
where = valloc (size);
if (where == NULL)
return NULL;
#endif
memset (where, 0, bytes);
return where;
#endif
}
/**
* Deallocate memory which was previously allocated using the
* NSAllocateMemoryPages() function.
* NSAllocateMemoryPages() function.<br />
* The bytes argument should be the same as the value passed
* to the NSAllocateMemoryPages() function.
*/
void
NSDeallocateMemoryPages (void *ptr, NSUInteger bytes)
{
#if __mach__
vm_deallocate (mach_task_self (), ptr, bytes);
vm_deallocate (mach_task_self (), ptr, NSRoundUpToMultipleOfPageSize (bytes));
#else
objc_free (ptr);
free (ptr);
#endif
}

3
configure vendored
View file

@ -16168,7 +16168,8 @@ done
# This function needed by NSPage.m
#--------------------------------------------------------------------
for ac_func in valloc
for ac_func in posix_memalign valloc
do
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
{ $as_echo "$as_me:$LINENO: checking for $ac_func" >&5

View file

@ -1840,7 +1840,7 @@ AC_CHECK_HEADERS(getopt.h)
#--------------------------------------------------------------------
# This function needed by NSPage.m
#--------------------------------------------------------------------
AC_CHECK_FUNCS(valloc)
AC_CHECK_FUNCS(posix_memalign valloc)
#--------------------------------------------------------------------
# This function needed by Time.m