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:
Richard Frith-MacDonald 2010-06-16 08:45:49 +00:00
parent 350c883bab
commit fcdd57d05f
5 changed files with 24 additions and 13 deletions

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
}