2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation of page-related functions for GNUstep
|
1997-01-06 22:34:37 +00:00
|
|
|
Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
Created: May 1996
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1996-05-28 20:53:06 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1996-05-28 20:53:06 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-05-24 08:27:39 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSPage class reference</title>
|
|
|
|
$Date$ $Revision$
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
1996-05-28 20:53:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#if __mach__
|
|
|
|
#include <mach.h>
|
|
|
|
#endif
|
|
|
|
|
2005-10-07 02:06:48 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-19 12:10:11 +00:00
|
|
|
#ifdef __MINGW__
|
2000-06-06 16:50:52 +00:00
|
|
|
#include <malloc.h>
|
2005-02-22 11:22:44 +00:00
|
|
|
static size_t
|
2000-06-06 16:50:52 +00:00
|
|
|
getpagesize(void)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
GetSystemInfo(&info);
|
|
|
|
return info.dwPageSize;
|
|
|
|
}
|
1996-07-15 18:41:44 +00:00
|
|
|
#endif
|
|
|
|
|
1996-10-31 18:34:51 +00:00
|
|
|
#ifdef __SOLARIS__
|
|
|
|
#define getpagesize() sysconf(_SC_PAGESIZE)
|
|
|
|
#endif
|
1998-05-29 15:25:41 +00:00
|
|
|
|
|
|
|
#ifdef __svr4__
|
|
|
|
#define getpagesize() sysconf(_SC_PAGESIZE)
|
|
|
|
#endif
|
1996-10-31 18:34:51 +00:00
|
|
|
|
|
|
|
#if __mach__
|
|
|
|
#define getpagesize vm_page_size
|
|
|
|
#endif
|
|
|
|
|
2003-01-07 18:33:51 +00:00
|
|
|
#ifdef __BEOS__
|
2002-12-29 01:50:31 +00:00
|
|
|
#include <kernel/OS.h>
|
|
|
|
#define getpagesize() B_PAGE_SIZE
|
|
|
|
#endif
|
|
|
|
|
1996-05-28 20:53:06 +00:00
|
|
|
/* Cache the size of a memory page here, so we don't have to make the
|
|
|
|
getpagesize() system call repeatedly. */
|
2008-11-13 10:09:50 +00:00
|
|
|
static NSUInteger ns_page_size = 0;
|
1996-05-28 20:53:06 +00:00
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Return the number of bytes in a memory page.
|
|
|
|
*/
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger
|
1996-05-28 20:53:06 +00:00
|
|
|
NSPageSize (void)
|
|
|
|
{
|
|
|
|
if (!ns_page_size)
|
2008-11-13 10:09:50 +00:00
|
|
|
ns_page_size = getpagesize ();
|
1996-05-28 20:53:06 +00:00
|
|
|
return ns_page_size;
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Return log base 2 of the number of bytes in a memory page.
|
|
|
|
*/
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger
|
1996-05-28 20:53:06 +00:00
|
|
|
NSLogPageSize (void)
|
|
|
|
{
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger tmp_page_size = NSPageSize();
|
|
|
|
NSUInteger log = 0;
|
1996-05-28 20:53:06 +00:00
|
|
|
|
1997-01-06 22:34:37 +00:00
|
|
|
while (tmp_page_size >>= 1)
|
1996-05-28 20:53:06 +00:00
|
|
|
log++;
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Round bytes down to the nearest multiple of the memory page size,
|
|
|
|
* and return it.
|
|
|
|
*/
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger
|
|
|
|
NSRoundDownToMultipleOfPageSize (NSUInteger bytes)
|
1996-05-28 20:53:06 +00:00
|
|
|
{
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger a = NSPageSize();
|
1997-01-06 21:35:19 +00:00
|
|
|
|
|
|
|
return (bytes / a) * a;
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Round bytes up to the nearest multiple of the memory page size,
|
|
|
|
* and return it.
|
|
|
|
*/
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger
|
|
|
|
NSRoundUpToMultipleOfPageSize (NSUInteger bytes)
|
1996-05-28 20:53:06 +00:00
|
|
|
{
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger a = NSPageSize();
|
1997-01-06 21:35:19 +00:00
|
|
|
|
|
|
|
return ((bytes % a) ? ((bytes / a + 1) * a) : bytes);
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
#if __linux__
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Return the number of bytes of real (physical) memory available.
|
|
|
|
*/
|
2008-11-13 10:09:50 +00:00
|
|
|
NSUInteger
|
1996-05-28 20:53:06 +00:00
|
|
|
NSRealMemoryAvailable ()
|
|
|
|
{
|
1998-01-26 14:18:18 +00:00
|
|
|
#if __linux__
|
|
|
|
struct sysinfo info;
|
|
|
|
|
|
|
|
if ((sysinfo(&info)) != 0)
|
|
|
|
return 0;
|
2008-11-13 10:09:50 +00:00
|
|
|
return info.freeram;
|
2010-03-19 12:10:11 +00:00
|
|
|
#elif defined(__MINGW__)
|
2008-11-13 10:09:50 +00:00
|
|
|
MEMORYSTATUSEX memory;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2008-11-13 10:09:50 +00:00
|
|
|
memory.dwLength = sizeof(memory);
|
|
|
|
GlobalMemoryStatusEx(&memory);
|
|
|
|
return memory.ullAvailPhys;
|
2003-01-07 18:33:51 +00:00
|
|
|
#elif defined(__BEOS__)
|
2002-12-31 17:13:30 +00:00
|
|
|
system_info info;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-12-31 17:13:30 +00:00
|
|
|
if (get_system_info(&info) != B_OK)
|
|
|
|
return 0;
|
2008-11-13 10:09:50 +00:00
|
|
|
return (info.max_pages - info.used_pages) * B_PAGE_SIZE;
|
1998-01-26 14:18:18 +00:00
|
|
|
#else
|
1996-05-28 20:53:06 +00:00
|
|
|
fprintf (stderr, "NSRealMemoryAvailable() not implemented.\n");
|
|
|
|
return 0;
|
1998-01-26 14:18:18 +00:00
|
|
|
#endif
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
2004-07-29 15:30:47 +00:00
|
|
|
* Allocate memory for this process and return a pointer to it (or a null
|
2010-06-16 08:45:49 +00:00
|
|
|
* pointer on failure). The allocated memory is page aligned and the
|
|
|
|
* actual size of memory allocated is a multiple of the page size.
|
2002-12-31 19:31:46 +00:00
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
void *
|
2008-11-13 10:09:50 +00:00
|
|
|
NSAllocateMemoryPages (NSUInteger bytes)
|
1996-05-28 20:53:06 +00:00
|
|
|
{
|
2010-06-16 08:45:49 +00:00
|
|
|
NSUInteger size = NSRoundUpToMultipleOfPageSize (bytes);
|
1996-05-28 20:53:06 +00:00
|
|
|
void *where;
|
2010-06-17 19:08:08 +00:00
|
|
|
#if defined(__MINGW__)
|
|
|
|
where = VirtualAlloc(NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
#elif __mach__
|
1996-05-28 20:53:06 +00:00
|
|
|
kern_return_t r;
|
2010-06-16 08:45:49 +00:00
|
|
|
r = vm_allocate (mach_task_self(), &where, (vm_size_t) size, 1);
|
1997-03-03 19:46:52 +00:00
|
|
|
if (r != KERN_SUCCESS)
|
|
|
|
return NULL;
|
1996-05-28 20:53:06 +00:00
|
|
|
return where;
|
2010-06-16 08:45:49 +00:00
|
|
|
#elif HAVE_POSIX_MEMALIGN
|
|
|
|
if (posix_memalign(&where, NSPageSize(), size) != 0)
|
|
|
|
return NULL;
|
1996-05-28 20:53:06 +00:00
|
|
|
#else
|
2010-06-16 08:45:49 +00:00
|
|
|
where = valloc (size);
|
1997-03-03 19:46:52 +00:00
|
|
|
if (where == NULL)
|
|
|
|
return NULL;
|
2010-06-16 08:45:49 +00:00
|
|
|
#endif
|
1997-01-06 21:35:19 +00:00
|
|
|
memset (where, 0, bytes);
|
|
|
|
return where;
|
1996-05-28 20:53:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Deallocate memory which was previously allocated using the
|
2010-06-16 08:45:49 +00:00
|
|
|
* NSAllocateMemoryPages() function.<br />
|
|
|
|
* The bytes argument should be the same as the value passed
|
|
|
|
* to the NSAllocateMemoryPages() function.
|
2002-12-31 19:31:46 +00:00
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
void
|
2008-11-13 10:09:50 +00:00
|
|
|
NSDeallocateMemoryPages (void *ptr, NSUInteger bytes)
|
1996-05-28 20:53:06 +00:00
|
|
|
{
|
2010-06-17 19:08:08 +00:00
|
|
|
#if defined(__MINGW__)
|
|
|
|
VirtualFree(ptr, 0, MEM_RELEASE);
|
|
|
|
#elif __mach__
|
2010-06-16 08:45:49 +00:00
|
|
|
vm_deallocate (mach_task_self (), ptr, NSRoundUpToMultipleOfPageSize (bytes));
|
1996-05-28 20:53:06 +00:00
|
|
|
#else
|
2010-06-16 08:45:49 +00:00
|
|
|
free (ptr);
|
1996-05-28 20:53:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-12-31 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* Perform an efficient large scale copy of data from src to dest.
|
|
|
|
* The value bytes specifies the length of the data copied.
|
|
|
|
*/
|
1996-05-28 20:53:06 +00:00
|
|
|
void
|
2008-11-13 10:09:50 +00:00
|
|
|
NSCopyMemoryPages (const void *src, void *dest, NSUInteger bytes)
|
1996-05-28 20:53:06 +00:00
|
|
|
{
|
|
|
|
#if __mach__
|
|
|
|
kern_return_t r;
|
2002-12-31 19:31:46 +00:00
|
|
|
r = vm_copy (mach_task_self(), src, bytes, dest);
|
1996-05-28 20:53:06 +00:00
|
|
|
NSParameterAssert (r == KERN_SUCCESS);
|
|
|
|
#else
|
2002-12-31 19:31:46 +00:00
|
|
|
memcpy (dest, src, bytes);
|
1996-05-28 20:53:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|